Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImpEx: Cache header abbreviation reference #1149

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

### `ImpEx` enhancements
- Cache document id reference [#1148](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1148)
- Cache header abbreviation reference [#1149](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1149)

### `OCC` enhancements
- Cache bean property reference [#1138](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1138)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA.
* This file is part of "SAP Commerce Developers Toolset" plugin for IntelliJ IDEA.
* Copyright (C) 2014-2016 Alexander Bartash <[email protected]>
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors
* Copyright (C) 2019-2024 EPAM Systems <[email protected]> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -24,38 +24,55 @@ import com.intellij.idea.plugin.hybris.impex.psi.ImpexAnyHeaderParameterName
import com.intellij.idea.plugin.hybris.impex.rename.manipulator.ImpexMacrosManipulator
import com.intellij.idea.plugin.hybris.properties.PropertyService
import com.intellij.idea.plugin.hybris.psi.util.PsiUtils
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementResolveResult
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.ResolveResult
import com.intellij.psi.util.*

class ImpExHeaderAbbreviationReference(owner: ImpexAnyHeaderParameterName) : PsiReferenceBase.Poly<PsiElement?>(owner, false) {

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> =
findHeaderAbbreviation(element.project, value.removeSuffix(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED))
?.psiElement
?.let { PsiElementResolveResult.createResults(it) }
?.let { PsiUtils.getValidResults(it) }
?: emptyArray()
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> = CachedValuesManager.getManager(element.project)
.getParameterizedCachedValue(element, CACHE_KEY, provider, false, this)
.let { PsiUtils.getValidResults(it) }

override fun calculateDefaultRangeInElement() = TextRange.from(0, element.textLength)

private fun findHeaderAbbreviation(project: Project, currentText: String) = PropertyService.getInstance(project)
?.findAutoCompleteProperties(HybrisConstants.PROPERTY_IMPEX_HEADER_REPLACEMENT)
?.firstOrNull { property ->
property.value
?.split("...")
?.takeIf { it.size == 2 }
?.firstOrNull()
?.trim()
?.replace("\\\\", "\\")
?.toRegex()
?.let { currentText.matches(it) }
?: false
}

override fun handleElementRename(newElementName: String) = ImpexMacrosManipulator().handleContentChange(element, rangeInElement, newElementName)

companion object {
val CACHE_KEY = Key.create<ParameterizedCachedValue<Array<ResolveResult>, ImpExHeaderAbbreviationReference>>("HYBRIS_IMPEXHEADERABBREVIATIONREFERENCE")

private val provider = ParameterizedCachedValueProvider<Array<ResolveResult>, ImpExHeaderAbbreviationReference> { ref ->
val element = ref.element
val value = ref.value
val currentText = value.removeSuffix(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED)

val result = PropertyService.getInstance(element.project)
?.findAutoCompleteProperties(HybrisConstants.PROPERTY_IMPEX_HEADER_REPLACEMENT)
?.firstOrNull { property ->
property.value
?.split("...")
?.takeIf { it.size == 2 }
?.firstOrNull()
?.trim()
?.replace("\\\\", "\\")
?.toRegex()
?.let { currentText.matches(it) }
?: false
}
?.psiElement
?.let { PsiElementResolveResult.createResults(it) }
?.let { PsiUtils.getValidResults(it) }
?: emptyArray()

CachedValueProvider.Result.create(
result,
PsiModificationTracker.MODIFICATION_COUNT
)
}
}

}
Loading