From f6f68bed522a0e391cfea4fe108cce1968c43ccf Mon Sep 17 00:00:00 2001 From: Dan Timofte Date: Sat, 5 Oct 2024 11:27:57 +0300 Subject: [PATCH 1/2] resolve and show documentation for field and group --- .../QuickFixPsiReferenceProvider.java | 4 +- .../clickableviews/QuickfixPsiReference.java | 10 ++--- .../QuickfixSpecPsiReferenceContributor.java | 8 +++- .../quant/quickfixspec/common/PsiUtils.java | 44 +++++++++++++++---- .../QuickfixComponentDocumentationTarget.kt | 23 +++++----- ...fixComponentDocumentationTargetProvider.kt | 16 ++++--- 6 files changed, 72 insertions(+), 33 deletions(-) diff --git a/src/main/java/ac/quant/quickfixspec/clickableviews/QuickFixPsiReferenceProvider.java b/src/main/java/ac/quant/quickfixspec/clickableviews/QuickFixPsiReferenceProvider.java index 2f6ec80..9ac4408 100644 --- a/src/main/java/ac/quant/quickfixspec/clickableviews/QuickFixPsiReferenceProvider.java +++ b/src/main/java/ac/quant/quickfixspec/clickableviews/QuickFixPsiReferenceProvider.java @@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; -import static ac.quant.quickfixspec.common.PsiUtils.isComponentsDeclaration; +import static ac.quant.quickfixspec.common.PsiUtils.isTagDeclaration; @Slf4j public class QuickFixPsiReferenceProvider extends PsiReferenceProvider { @@ -23,7 +23,7 @@ public class QuickFixPsiReferenceProvider extends PsiReferenceProvider { XmlAttributeValue attributeValue = (XmlAttributeValue) element; - if (isComponentsDeclaration(attributeValue)) { + if (isTagDeclaration(attributeValue)) { return PsiReference.EMPTY_ARRAY; } diff --git a/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixPsiReference.java b/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixPsiReference.java index e5c4052..4ae9a4d 100644 --- a/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixPsiReference.java +++ b/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixPsiReference.java @@ -10,18 +10,18 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; - -import static ac.quant.quickfixspec.common.PsiUtils.findComponent; -import static ac.quant.quickfixspec.common.PsiUtils.getRootTag; +import static ac.quant.quickfixspec.common.PsiUtils.*; @Slf4j public class QuickfixPsiReference implements PsiReference { private final XmlAttributeValue attributeValue; private final XmlTag rootTag; + private final String parentTagName; public QuickfixPsiReference(final XmlAttributeValue attributeValue) { this.attributeValue = attributeValue; + this.parentTagName = ((XmlTag) attributeValue.getParent().getParent()).getName(); this.rootTag = getRootTag(attributeValue); } @@ -37,8 +37,8 @@ public QuickfixPsiReference(final XmlAttributeValue attributeValue) { @Override public @Nullable PsiElement resolve() { - String componentName = attributeValue.getValue(); - return findComponent(rootTag, componentName); + String tagName = attributeValue.getValue(); + return findDefinition(tagName, parentTagName, rootTag); } @Override diff --git a/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixSpecPsiReferenceContributor.java b/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixSpecPsiReferenceContributor.java index 481cc5d..2ecfcc5 100644 --- a/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixSpecPsiReferenceContributor.java +++ b/src/main/java/ac/quant/quickfixspec/clickableviews/QuickfixSpecPsiReferenceContributor.java @@ -8,16 +8,20 @@ import com.intellij.psi.xml.XmlAttributeValue; import org.jetbrains.annotations.NotNull; import lombok.extern.slf4j.Slf4j; +import static ac.quant.quickfixspec.common.PsiUtils.NAME_ATTRIBUTE; +import static ac.quant.quickfixspec.common.PsiUtils.TAGS_WITH_DEFINITION; @Slf4j public class QuickfixSpecPsiReferenceContributor extends PsiReferenceContributor { + @Override public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { + PsiElementPattern.Capture psiElementCapture = PlatformPatterns.psiElement(XmlAttributeValue.class) - .withParent(XmlPatterns.xmlAttribute().withName("name")) - .withSuperParent(2, XmlPatterns.xmlTag().withName("component")); + .withParent(XmlPatterns.xmlAttribute().withName(NAME_ATTRIBUTE)) + .withSuperParent(2, XmlPatterns.xmlTag().withName(TAGS_WITH_DEFINITION)); registrar.registerReferenceProvider( psiElementCapture, diff --git a/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java b/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java index 704f903..ae865f8 100644 --- a/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java +++ b/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java @@ -1,5 +1,6 @@ package ac.quant.quickfixspec.common; +import com.github.weisj.jsvg.S; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.xml.XmlAttribute; @@ -7,8 +8,25 @@ import com.intellij.psi.xml.XmlTag; import org.jetbrains.annotations.Nullable; +import java.util.Map; + public class PsiUtils { + public final static String NAME_ATTRIBUTE = "name"; + + public static final Map DEFINITION_GROUP_NAME = Map.of( + "component", "components", + "group", "fields", + "field", "fields" + ); + + public static final Map DEFINITION_TAG_NAME = Map.of( + "component", "component", + "group", "field", + "field", "field" + ); + public final static String[] TAGS_WITH_DEFINITION = new String[]{"component", "group", "field"}; + // Helper method to navigate to the root tag public static @Nullable XmlTag getRootTag(PsiElement element) { @@ -22,14 +40,14 @@ public class PsiUtils { return null; } - public static @Nullable XmlTag findComponent(final XmlTag rootTag, final String componentName) { + public static @Nullable XmlTag findDefinition(final String tagName , final String parentTagName, final XmlTag rootTag) { - XmlTag[] componentsTags = rootTag.findSubTags("components"); + XmlTag[] tags = rootTag.findSubTags(DEFINITION_GROUP_NAME.get(parentTagName)); - for (XmlTag componentsTag : componentsTags) { - for (XmlTag componentTag : componentsTag.findSubTags("component")) { - String nameAttr = componentTag.getAttributeValue("name"); - if (componentName.equals(nameAttr)) { + for (XmlTag tag : tags) { + for (XmlTag componentTag : tag.findSubTags(DEFINITION_TAG_NAME.get(parentTagName))) { + String nameAttr = componentTag.getAttributeValue(NAME_ATTRIBUTE); + if (tagName.equals(nameAttr)) { return componentTag; } } @@ -37,10 +55,20 @@ public class PsiUtils { return null; } - public static boolean isComponentsDeclaration(XmlAttributeValue valueElement) { + // answers the question : + // is the attribute value is inside one of the 2 tags that are used for declaration + // + // + public static boolean isTagDeclaration(XmlAttributeValue valueElement) { XmlAttribute parentAttribute = (XmlAttribute) valueElement.getParent(); XmlTag xmlTag = parentAttribute.getParent(); XmlTag parentTag = xmlTag.getParentTag(); - return parentTag != null && "components".equals(parentTag.getName()); + if (parentTag == null) { + return false; + } + + final String parentTagName = parentTag.getName(); + + return DEFINITION_GROUP_NAME.containsValue(parentTagName); } } diff --git a/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTarget.kt b/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTarget.kt index f472103..f148b91 100644 --- a/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTarget.kt +++ b/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTarget.kt @@ -1,7 +1,7 @@ package ac.quant.quickfixspec.documentation -import ac.quant.quickfixspec.common.PsiUtils.findComponent +import ac.quant.quickfixspec.common.PsiUtils.findDefinition import com.intellij.model.Pointer import com.intellij.platform.backend.documentation.DocumentationResult import com.intellij.platform.backend.documentation.DocumentationTarget @@ -10,7 +10,8 @@ import com.intellij.psi.xml.XmlTag @Suppress("UnstableApiUsage") class QuickfixComponentDocumentationTarget( - private val componentName: String, + private val tagName: String, + private val parentTagName: String, private val rootTag: XmlTag? ) : DocumentationTarget { @@ -20,7 +21,7 @@ class QuickfixComponentDocumentationTarget( } override fun computePresentation(): TargetPresentation { - return TargetPresentation.builder(componentName).presentation() + return TargetPresentation.builder(tagName).presentation() } override fun computeDocumentationHint(): String { @@ -29,21 +30,21 @@ class QuickfixComponentDocumentationTarget( } override fun computeDocumentation(): DocumentationResult { - val componentDetails = getComponentDetails(componentName) + val componentDetails = getTagDetails(tagName) return DocumentationResult.documentation(componentDetails) } - private fun getComponentDetails(componentName: String): String { - val component: XmlTag? = findComponent(rootTag, componentName) - component ?: return "" - return getDisplayText(component) + private fun getTagDetails(tagName: String): String { + val tag: XmlTag? = findDefinition(tagName, parentTagName, rootTag) + tag ?: return "" + return getDisplayText(tag) } - private fun getDisplayText(component: XmlTag): String { - val text = component.children.joinToString("") { it.text } + private fun getDisplayText(tag: XmlTag): String { + val text = tag.children.joinToString("") { it.text } val xmlEscapedText = xmlEscaped(text) val indentationRemoved = indentationRemoved(xmlEscapedText) - val displayText = "

${component.name}

$indentationRemoved
" + val displayText = "

${tag.name}

$indentationRemoved
" return displayText } diff --git a/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTargetProvider.kt b/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTargetProvider.kt index d22e589..81fdf34 100644 --- a/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTargetProvider.kt +++ b/src/main/kotlin/ac/quant/quickfixspec/documentation/QuickfixComponentDocumentationTargetProvider.kt @@ -1,5 +1,8 @@ package ac.quant.quickfixspec.documentation +import ac.quant.quickfixspec.common.PsiUtils.NAME_ATTRIBUTE +import ac.quant.quickfixspec.common.PsiUtils.TAGS_WITH_DEFINITION +import ac.quant.quickfixspec.common.PsiUtils.DEFINITION_GROUP_NAME import ac.quant.quickfixspec.common.PsiUtils.getRootTag import com.intellij.platform.backend.documentation.DocumentationTarget import com.intellij.platform.backend.documentation.DocumentationTargetProvider @@ -22,24 +25,27 @@ class QuickfixComponentDocumentationTargetProvider : DocumentationTargetProvider rootTag = getRootTag(attribute) } - if ("name" != attribute.name) { + if (NAME_ATTRIBUTE != attribute.name) { return emptyList() } val tag = attribute.parent as? XmlTag ?: return emptyList() - if ("component" != tag.name) { + if (tag.name !in TAGS_WITH_DEFINITION) { return emptyList() } val tagParent = tag.parent as? XmlTag ?: return emptyList() - if ("components" == tagParent.name) { + + // skip definition when tagParent.name is "components" or "fields" + + if (tagParent.name in DEFINITION_GROUP_NAME.values) { return emptyList() } - val componentName = attribute.value ?: return emptyList() - return listOf(QuickfixComponentDocumentationTarget(componentName, rootTag)) + val attrNameValue = attribute.value ?: return emptyList() + return listOf(QuickfixComponentDocumentationTarget(attrNameValue, tag.name, rootTag)) } From 0320948e5223e44bdfde1885f2b31357481d7d57 Mon Sep 17 00:00:00 2001 From: Dan Timofte Date: Sat, 5 Oct 2024 11:37:42 +0300 Subject: [PATCH 2/2] remove unused import --- src/main/java/ac/quant/quickfixspec/common/PsiUtils.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java b/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java index ae865f8..a50c8a4 100644 --- a/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java +++ b/src/main/java/ac/quant/quickfixspec/common/PsiUtils.java @@ -1,6 +1,5 @@ package ac.quant.quickfixspec.common; -import com.github.weisj.jsvg.S; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.xml.XmlAttribute; @@ -69,6 +68,10 @@ public static boolean isTagDeclaration(XmlAttributeValue valueElement) { final String parentTagName = parentTag.getName(); - return DEFINITION_GROUP_NAME.containsValue(parentTagName); + if (DEFINITION_GROUP_NAME.containsValue(parentTagName)) { + return true; + } + + return false; } }