Skip to content

Commit

Permalink
#1085 | Added reference resolution and code completion for fields ref…
Browse files Browse the repository at this point in the history
…erenced by `template-bean`
  • Loading branch information
mlytvyn authored Apr 11, 2024
1 parent 6a874cb commit dd49095
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- Added `Spring EL` language injection into the `y:preview`:`urlQualifier` attribute [#1036](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1036)
- Added `Spring EL` language injection into the `y:preview`:`description` tag body [#1037](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1037)
- Added reference resolution and code completion for nested references [#1038](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1038)
- Added reference resolution and code completion for fields referenced by `template-bean` [#1085](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1085)

### `Module Dependencies Diagram` enhancements
- Added possibility to display extension details within the Module node [#1041](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1041)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.search.GlobalSearchScope

class JavaClassReference : PsiReferenceBase<PsiElement>, HighlightedReference {
open class JavaClassReference : PsiReferenceBase<PsiElement>, HighlightedReference {

private val className: String
protected val className: String

constructor(element: PsiElement, className: String) : super(element) {
this.className = className
Expand All @@ -40,12 +40,14 @@ class JavaClassReference : PsiReferenceBase<PsiElement>, HighlightedReference {
this.className = className
}

protected open fun evalClassName() = className

override fun calculateDefaultRangeInElement(): TextRange =
if (element.textLength == 0) super.calculateDefaultRangeInElement()
else TextRange.from(1, element.textLength - HybrisConstants.QUOTE_LENGTH)

override fun getVariants() = JavaPsiFacade.getInstance(element.project)
.findClass(className, GlobalSearchScope.allScope(element.project))
.findClass(evalClassName(), GlobalSearchScope.allScope(element.project))
?.let { psiClass ->
val fields = psiClass.allFields

Expand All @@ -66,7 +68,7 @@ class JavaClassReference : PsiReferenceBase<PsiElement>, HighlightedReference {
override fun resolve(): PsiElement? {
val project = element.project
return JavaPsiFacade.getInstance(project)
.findClass(className, GlobalSearchScope.allScope(project))
.findClass(evalClassName(), GlobalSearchScope.allScope(project))
?.let { psiClass ->
val field = psiClass.findFieldByName(value, true)
return@let if (psiClass.isRecord) field
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of "SAP Commerce Developers Toolset" plugin for IntelliJ IDEA.
* 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
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.intellij.idea.plugin.hybris.java.psi.reference

import com.intellij.idea.plugin.hybris.system.type.spring.TSSpringHelper
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement

class SpringBeanJavaClassReference : JavaClassReference {

constructor(element: PsiElement, className: String) : super(element, className)
constructor(element: PsiElement, textRange: TextRange, className: String) : super(element, textRange, className)

override fun evalClassName() = TSSpringHelper.resolveBeanClass(element, className)
?.qualifiedName
?: className

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ object CngPsiHelper {
?.find { it.localName == "initialize" && it.getAttributeValue("property") == newItemName }

// ignore code completion and reference resolution for template-bean properties, corresponding Inspection will suggest conversion into plain type
if (initializeProperty?.getAttributeValue("template-bean") != null) return null
val templateBean = initializeProperty?.getAttributeValue("template-bean")
if (templateBean != null) return newItemName to "SPRING_BEAN_$templateBean"

val newItemType = initializeProperty?.getAttributeValue("type")
?: return newItemName to resolveContextType(element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.intellij.idea.plugin.hybris.system.cockpitng.psi.provider

import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.java.psi.reference.JavaClassReference
import com.intellij.idea.plugin.hybris.java.psi.reference.SpringBeanJavaClassReference
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.CngPsiHelper
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.reference.CngFlowTSItemAttributeReference
import com.intellij.openapi.application.ApplicationManager
Expand All @@ -37,10 +38,14 @@ class CngFlowPropertyListPropertyQualifierReferenceProvider : PsiReferenceProvid
val type = CngPsiHelper.resolveContextTypeForNewItemInWizardFlow(element)
?: return emptyArray()

return if (type.contains(".")
&& type != HybrisConstants.COCKPIT_NG_INITIALIZE_CONTEXT_TYPE
) arrayOf(JavaClassReference(element, type))
else arrayOf(CngFlowTSItemAttributeReference(element))
val reference = if (type.startsWith("SPRING_BEAN_"))
SpringBeanJavaClassReference(element, type.replace("SPRING_BEAN_", ""))
else if (type.contains(".") && type != HybrisConstants.COCKPIT_NG_INITIALIZE_CONTEXT_TYPE)
JavaClassReference(element, type)
else
CngFlowTSItemAttributeReference(element)

return arrayOf(reference)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.intellij.idea.plugin.hybris.system.cockpitng.psi.provider

import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.java.psi.reference.JavaClassReference
import com.intellij.idea.plugin.hybris.java.psi.reference.SpringBeanJavaClassReference
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.CngPsiHelper
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.reference.CngFlowTSItemAttributeReference
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.reference.CngInitializePropertyReference
Expand Down Expand Up @@ -46,10 +47,13 @@ class CngFlowPropertyQualifierReferenceProvider : PsiReferenceProvider() {
val qualifier = it[1]
val attrReference: PsiReference? = CngPsiHelper.resolveContextTypeForNewItemInWizardFlow(element)
?.let { type ->
if (type.contains(".")
&& type != HybrisConstants.COCKPIT_NG_INITIALIZE_CONTEXT_TYPE
) JavaClassReference(element, TextRange.from(initializeProperty.length + 2, qualifier.length), type)
else CngFlowTSItemAttributeReference(element, TextRange.from(initializeProperty.length + 2, qualifier.length))
val textRange = TextRange.from(initializeProperty.length + 2, qualifier.length)
if (type.startsWith("SPRING_BEAN_"))
SpringBeanJavaClassReference(element, textRange, type.replace("SPRING_BEAN_", ""))
else if (type.contains(".") && type != HybrisConstants.COCKPIT_NG_INITIALIZE_CONTEXT_TYPE)
JavaClassReference(element, textRange, type)
else
CngFlowTSItemAttributeReference(element, textRange)
}

listOfNotNull(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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) 2019-2024 EPAM Systems <[email protected]> and contributors
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -26,10 +26,12 @@ import com.intellij.psi.*
class SpringReference(
element: PsiElement,
val name: String,
private val textRange: TextRange? = null
) : PsiReferenceBase<PsiElement>(element, true), PsiPolyVariantReference {

override fun calculateDefaultRangeInElement() = if (element.text.startsWith("\"") || element.text.startsWith("'")) TextRange.from(1, element.textLength - HybrisConstants.QUOTE_LENGTH)
else TextRange.from(0, element.textLength)
override fun calculateDefaultRangeInElement() = textRange
?: if (element.text.startsWith("\"") || element.text.startsWith("'")) TextRange.from(1, element.textLength - HybrisConstants.QUOTE_LENGTH)
else TextRange.from(0, element.textLength)

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> = TSSpringHelper.resolveBeanClass(element, name)
?.let { PsiElementResolveResult.createResults(it) }
Expand Down

0 comments on commit dd49095

Please sign in to comment.