Skip to content

Commit

Permalink
#516 | Improved performance of the Item attribute resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mlytvyn authored Jul 16, 2023
1 parent b02f0f6 commit 4e435d5
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 65 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
- Improved language injection into Java files [#515](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/515)

### Features
- Added possibility to skip non-existing source directories during project import [#511](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/511)
- Added possibility to skip non-existing source directories during project import [#511](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/511)- Added possibility to skip non-existing source directories during project import [#511](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/511)
- Improved performance of the Item attribute resolution [#516](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/511)- Added possibility to skip non-existing source directories during project import [#511](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/516)

### Fixes
- Fixed deadlock when ItemType name equals to its extends [#513](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/513)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import com.intellij.psi.PsiClass
class DynamicAttributeDeclarativeInlayActionHandler : InlayActionHandler {

override fun handleClick(editor: Editor, payload: InlayActionPayload) {
val target = (payload as? PsiPointerInlayActionPayload)?.pointer?.element as? PsiClass
target
(payload as? PsiPointerInlayActionPayload)
?.pointer
?.element
?.let { it as? PsiClass }
?.let {
invokeLater { it.navigate(true) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class DynamicAttributeDeclarativeInlayHintsCollector : SharedBypassCollector {
.map { it.value }
.firstOrNull { it != null } ?: return

val attribute = meta.allAttributes
.filter { it.persistence.type == PersistenceType.DYNAMIC }
.firstOrNull { it.name == qualifier } ?: return
val attribute = meta.allAttributes[qualifier]
?.takeIf { it.persistence.type == PersistenceType.DYNAMIC }
?: return

val identifier = element.methodExpression.lastChild

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ object ModelEnumerationChildrenRendererInfoProvider {
val computedConstantValue = it.computeConstantValue() ?: return@mapNotNull null
if (computedConstantValue !is String) return@mapNotNull null

meta.allAttributes
.find { attr -> attr.name == computedConstantValue }
meta.allAttributes[computedConstantValue]
?.let { attribute -> return@mapNotNull createChildInfo(attribute, computedConstantValue, it.name, metaAccess, debuggerUtils) }
meta.allRelationEnds
.filter { relation -> relation.isNavigable }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class FxSYColumnReference(owner: FlexibleSearchYColumnName) : PsiReferenceBase.P
return getSuitablePrefixes() + getColumns(type)
}
if ((hasColumnAlias && hasTableAlias)
|| (!hasColumnAlias && (!hasTableAlias || canFallback))) {
|| (!hasColumnAlias && (!hasTableAlias || canFallback))
) {
return getColumns(type)
}

Expand Down Expand Up @@ -183,9 +184,10 @@ class FxSYColumnReference(owner: FlexibleSearchYColumnName) : PsiReferenceBase.P
private fun tryResolveByItemType(type: String, refName: String, metaService: TSMetaModelAccess): Array<ResolveResult>? =
metaService.findMetaItemByName(type)
?.let { meta ->
val attributes = meta.allAttributes
.filter { refName.equals(it.name, true) }
.map { AttributeResolveResult(it) }
val attributes = meta.allAttributes[refName]
?.let { AttributeResolveResult(it) }
?.let { listOf(it) }
?: emptyList()

val relations = meta.allRelationEnds
.filter { refName.equals(it.name, true) }
Expand All @@ -208,8 +210,7 @@ class FxSYColumnReference(owner: FlexibleSearchYColumnName) : PsiReferenceBase.P

private fun tryResolveByEnumType(type: String, refName: String, metaService: TSMetaModelAccess): Array<ResolveResult>? = metaService.findMetaEnumByName(type)
?.let { metaService.findMetaItemByName(HybrisConstants.TS_TYPE_ENUMERATION_VALUE) }
?.allAttributes
?.firstOrNull { refName.equals(it.name, true) }
?.let { it.allAttributes[refName] }
?.let { arrayOf(AttributeResolveResult(it)) }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ class ImpexFunctionTSAttributeReference(owner: ImpexParameter) : TSReferenceBase
?.let { meta ->
when (meta) {
is TSGlobalMetaEnum -> metaService.findMetaItemByName(HybrisConstants.TS_TYPE_ENUMERATION_VALUE)
?.allAttributes
?.find { attr -> attr.name.equals(featureName, true) }
?.let { it.allAttributes[featureName] }
?.let { attr -> AttributeResolveResult(attr) }

is TSGlobalMetaItem -> meta.allAttributes
.find { attr -> attr.name.equals(featureName, true) }
is TSGlobalMetaItem -> meta.allAttributes[featureName]
?.let { attr -> AttributeResolveResult(attr) }
?: meta.allRelationEnds
.find { relationEnd -> relationEnd.name.equals(featureName, true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ internal class ImpexTSAttributeReference(owner: ImpexAnyHeaderParameterName) : T
?.text
?.let { metaService.findMetaEnumByName(it) }
?.let { metaService.findMetaItemByName(HybrisConstants.TS_TYPE_ENUMERATION_VALUE) }
?.allAttributes
?.firstOrNull { refName.equals(it.name, true) }
?.let { it.allAttributes[refName] }
?.let { AttributeResolveResult(it) }

private fun tryResolveForItemType(
Expand All @@ -84,8 +83,7 @@ internal class ImpexTSAttributeReference(owner: ImpexAnyHeaderParameterName) : T
): ResolveResult? = itemTypeCode
?.let { metaModelService.findMetaItemByName(it) }
?.let { meta ->
meta.allAttributes
.find { it.name.equals(featureName, true) }
meta.allAttributes[featureName]
?.let { AttributeResolveResult(it) }
?: meta.allRelationEnds
.find { it.name.equals(featureName, true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ class ImpexUserRightsTSAttributeReference(owner: ImpexUserRightsAttributeValue)
?.let { meta ->
when (meta) {
is TSGlobalMetaEnum -> metaModelAccess.findMetaItemByName(HybrisConstants.TS_TYPE_ENUMERATION_VALUE)
?.allAttributes
?.find { attr -> attr.name.equals(featureName, true) }
?.let { it.allAttributes[featureName] }
?.let { attr -> AttributeResolveResult(attr) }

is TSGlobalMetaItem -> resolve(meta, featureName)
Expand Down Expand Up @@ -104,8 +103,7 @@ class ImpexUserRightsTSAttributeReference(owner: ImpexUserRightsAttributeValue)
)
}

private fun resolve(meta: TSGlobalMetaItem, featureName: String) = meta.allAttributes
.find { attr -> attr.name.equals(featureName, true) }
private fun resolve(meta: TSGlobalMetaItem, featureName: String) = meta.allAttributes[featureName]
?.let { attr -> AttributeResolveResult(attr) }
?: meta.allRelationEnds
.find { relationEnd -> relationEnd.name.equals(featureName, true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package com.intellij.idea.plugin.hybris.polyglotQuery.psi.reference

import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.flexibleSearch.FxSUtils
import com.intellij.idea.plugin.hybris.flexibleSearch.codeInsight.lookup.FxSLookupElementFactory
import com.intellij.idea.plugin.hybris.flexibleSearch.completion.FlexibleSearchCompletionContributor
import com.intellij.idea.plugin.hybris.flexibleSearch.FxSUtils
import com.intellij.idea.plugin.hybris.polyglotQuery.psi.PolyglotQueryAttributeKeyName
import com.intellij.idea.plugin.hybris.psi.util.PsiUtils
import com.intellij.idea.plugin.hybris.system.type.codeInsight.completion.TSCompletionService
Expand Down Expand Up @@ -99,9 +99,10 @@ class PolyglotQueryAttributeKeyNameReference(owner: PolyglotQueryAttributeKeyNam
private fun tryResolveByItemType(type: String, refName: String, metaService: TSMetaModelAccess): Array<ResolveResult>? =
metaService.findMetaItemByName(type)
?.let { meta ->
val attributes = meta.allAttributes
.filter { refName.equals(it.name, true) }
.map { AttributeResolveResult(it) }
val attributes = meta.allAttributes[refName]
?.let { AttributeResolveResult(it) }
?.let { listOf(it) }
?: emptyList()

val relations = meta.allRelationEnds
.filter { refName.equals(it.name, true) }
Expand All @@ -112,8 +113,7 @@ class PolyglotQueryAttributeKeyNameReference(owner: PolyglotQueryAttributeKeyNam

private fun tryResolveByEnumType(type: String, refName: String, metaService: TSMetaModelAccess): Array<ResolveResult>? = metaService.findMetaEnumByName(type)
?.let { metaService.findMetaItemByName(HybrisConstants.TS_TYPE_ENUMERATION_VALUE) }
?.allAttributes
?.firstOrNull { refName.equals(it.name, true) }
?.let { it.allAttributes[refName] }
?.let { arrayOf(AttributeResolveResult(it)) }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class DefaultTSCompletionService(private val project: Project) : TSCompletionSer

private fun getCompletionsForEnum(metaModelAccess: TSMetaModelAccess) = metaModelAccess.findMetaItemByName(HybrisConstants.TS_TYPE_ENUMERATION_VALUE)
?.allAttributes
?.values
?.map { TSLookupElementFactory.build(it) }
?: emptyList()

Expand All @@ -151,7 +152,7 @@ class DefaultTSCompletionService(private val project: Project) : TSCompletionSer
private fun getCompletions(metaItem: TSGlobalMetaItem) = getCompletions(metaItem, emptySet())

private fun getCompletions(metaItem: TSGlobalMetaItem, excludeNames: Set<String>): List<LookupElementBuilder> {
val attributes = metaItem.allAttributes
val attributes = metaItem.allAttributes.values
.mapNotNull { mapAttributeToLookup(excludeNames, it) }
val relationEnds = metaItem.allRelationEnds
.mapNotNull { TSLookupElementFactory.build(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class AttributeDeclarationCompletionProvider : CompletionProvider<Compl
val meta = TSMetaModelAccess.getInstance(project).findMetaItemByName(type)
meta
?.allAttributes
?.values
?.map { TSLookupElementFactory.build(it) }
?.forEach { resultCaseInsensitive.addElement(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class AbstractModelAttributeLineMarkerProvider<T : PsiElement> : Abstra
protected open fun getPsiElementItemLineMarkerInfo(
meta: TSGlobalMetaItem, name: String, nameIdentifier: PsiIdentifier
) = with(getAttributeElements(meta, name)) {
if (this.isEmpty()) {
if (this == null) {
val groupedRelElements = getRelations(meta, name)
return@with getRelationMarkers(groupedRelElements, RelationEnd.SOURCE, HybrisIcons.TS_RELATION_SOURCE, nameIdentifier)
?: getRelationMarkers(groupedRelElements, RelationEnd.TARGET, HybrisIcons.TS_RELATION_TARGET, nameIdentifier)
Expand All @@ -63,13 +63,12 @@ abstract class AbstractModelAttributeLineMarkerProvider<T : PsiElement> : Abstra
}
}

open fun getAttributeElements(meta: TSGlobalMetaItem, name: String) = meta.allAttributes
.filter { it.name == name }
.flatMap { it.declarations }
.map { it.domAnchor }
.mapNotNull { it.retrieveDomElement() }
.map { it.qualifier }
.mapNotNull { it.xmlAttributeValue }
open fun getAttributeElements(meta: TSGlobalMetaItem, name: String) = meta.allAttributes[name]
?.declarations
?.map { it.domAnchor }
?.mapNotNull { it.retrieveDomElement() }
?.map { it.qualifier }
?.mapNotNull { it.xmlAttributeValue }

open fun getRelations(meta: TSGlobalMetaItem, name: String) = meta.allRelationEnds
.filter { it.qualifier == name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Copyright (C) 2019 EPAM Systems <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
Expand Down Expand Up @@ -42,9 +42,9 @@ public TSMetaItemServiceImpl(@NotNull final Project project) {
@Override
public List<? extends TSGlobalMetaItem.TSGlobalMetaItemAttribute> findAttributesByName(final TSGlobalMetaItem meta, final String name, final boolean includeInherited) {
return includeInherited
? meta.getAllAttributes().stream()
.filter(attribute -> attribute.getName().equalsIgnoreCase(name))
.collect(Collectors.toList())
? Optional.ofNullable(meta.getAllAttributes().get(name))
.map(Collections::singletonList)
.orElseGet(Collections::emptyList)
: Optional.ofNullable(meta.getAttributes().get(name))
.map(Collections::singletonList)
.orElseGet(Collections::emptyList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TSMetaModelMergerImpl(val myProject: Project) : TSMetaModelMerger {
.forEach { (it as? TSGlobalMetaItemSelfMerge<*, *>)?.postMerge(this) }

getMetaType<TSGlobalMetaItem>(TSMetaType.META_ITEM).values
.flatMap { it.allAttributes }
.flatMap { it.allAttributes.values }
.filter { it.type != null }
.forEach { it.flattenType = TSMetaHelper.flattenType(it.type!!, allTypes) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ interface TSGlobalMetaItem : TSMetaItem, TSGlobalMetaClassifier<ItemType>, TSTyp
override val attributes: Map<String, TSGlobalMetaItemAttribute>
override val indexes: Map<String, TSGlobalMetaItemIndex>
val relationEnds: List<TSMetaRelation.TSMetaRelationElement>
val allAttributes: List<TSGlobalMetaItemAttribute>
val allAttributes: Map<String, TSGlobalMetaItemAttribute>
val allIndexes: List<TSGlobalMetaItemIndex>
val allCustomProperties: List<TSMetaCustomProperty>
val allRelationEnds: List<TSMetaRelation.TSMetaRelationElement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ internal class TSGlobalMetaItemImpl(localMeta: TSMetaItem) : TSGlobalMetaItemSel
override val indexes = CaseInsensitiveConcurrentHashMap<String, TSGlobalMetaItem.TSGlobalMetaItemIndex>()
override val relationEnds = LinkedList<TSMetaRelation.TSMetaRelationElement>()

override val allAttributes = LinkedList<TSGlobalMetaItem.TSGlobalMetaItemAttribute>()
override val allAttributes = CaseInsensitiveConcurrentHashMap<String, TSGlobalMetaItem.TSGlobalMetaItemAttribute>()
override val allIndexes = LinkedList<TSGlobalMetaItem.TSGlobalMetaItemIndex>()
override val allCustomProperties = LinkedList<TSMetaCustomProperty>()
override val allRelationEnds = LinkedList<TSMetaRelation.TSMetaRelationElement>()
Expand Down Expand Up @@ -161,7 +161,8 @@ internal class TSGlobalMetaItemImpl(localMeta: TSMetaItem) : TSGlobalMetaItemSel
val combinedRelationEnds = TSMetaHelper.getAllRelationEnds(globalMetaModel, this, extends)

allExtends.addAll(extends)
allAttributes.addAll(attributes.values + extends.flatMap { it.attributes.values })
allAttributes.putAll(attributes)
extends.forEach { allAttributes.putAll(it.attributes) }
allCustomProperties.addAll(customProperties.values + extends.flatMap { it.customProperties.values })
allIndexes.addAll(indexes.values + extends.flatMap { it.indexes.values })
allRelationEnds.addAll(combinedRelationEnds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ private ModelsUtils() {
public static boolean isItemModelFile(final PsiClass psiClass) {
final var psiFile = psiClass.getContainingFile();

if (psiFile.getText().contains("Generated model class for type")) {
return true;
}

final VirtualFile virtualFile = psiFile.getVirtualFile() != null
? psiFile.getVirtualFile()
: psiFile.getOriginalFile().getVirtualFile();

if (virtualFile == null || virtualFile.getExtension() == null) {
return false;
}
if (virtualFile == null) return false;

return virtualFile.getExtension().equals("class")
final var extension = virtualFile.getExtension();
if (extension == null) return false;

if (extension.equals("java")
&& virtualFile.getPath().contains(HybrisConstants.PL_BOOTSTRAP_GEN_SRC_PATH)
&& shouldProcessItemType(psiClass)
) return true;


return extension.equals("class")
&& virtualFile.getPath().contains(HybrisConstants.JAR_MODELS)
&& shouldProcessItemType(psiClass);
}
Expand All @@ -68,15 +71,17 @@ private static boolean shouldProcessItemType(final PsiClass psiClass) {

public static boolean isEnumFile(final PsiClass psiClass) {
final var psiFile = psiClass.getContainingFile();
final var virtualFile = psiFile.getVirtualFile();

if (psiFile.getText().contains("Generated enum")) {
return true;
}
if (virtualFile == null) return false;

final VirtualFile virtualFile = psiFile.getVirtualFile();
final var extension = virtualFile.getExtension();
if (extension == null) return false;

if (virtualFile == null) return false;
if (virtualFile.getExtension() == null) return false;
if (extension.equals("java")
&& virtualFile.getPath().contains(HybrisConstants.PL_BOOTSTRAP_GEN_SRC_PATH)
&& shouldProcessEnum(psiClass)
) return true;

return virtualFile.getExtension().equals("class")
&& virtualFile.getPath().contains(HybrisConstants.JAR_MODELS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TSMetaItemAttributesTable private constructor(myProject: Project) : Abstra
)

override fun select(item: TSMetaItemAttribute) = selectRowWithValue(item.name, COLUMN_QUALIFIER)
override fun getItems(owner: TSGlobalMetaItem): MutableList<TSMetaItemAttribute> = owner.allAttributes
override fun getItems(owner: TSGlobalMetaItem): MutableList<TSMetaItemAttribute> = owner.allAttributes.values
.sortedWith(
compareBy(
{ !it.isCustom },
Expand Down

0 comments on commit 4e435d5

Please sign in to comment.