Skip to content
Snippets Groups Projects
Commit 0ad60e4e authored by Johannes Mey's avatar Johannes Mey
Browse files

show reference to implementation for attributes

parent ab0d846d
No related tags found
No related merge requests found
Pipeline #16794 passed
package org.jastadd.tooling.aspect;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.lang.jvm.annotation.JvmAnnotationConstantValue;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.java.stubs.index.JavaMethodNameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.jastadd.tooling.aspect.psi.JastAddAspectAttributeName;
import org.jastadd.tooling.java.JavaSyntaxHighlighter;
import org.jetbrains.annotations.NotNull;
import static org.jastadd.tooling.java.GeneratedCodeUtil.getNavigationUrlFromFileLocation;
public class ImplementationAnnotator implements Annotator {
@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
if (!(element instanceof JastAddAspectAttributeName nameElement)) {
return;
}
for (var method : JavaMethodNameIndex.getInstance().get(nameElement.getText(), element.getProject(), GlobalSearchScope.allScope(element.getProject()))) {
for (var annotation : method.getAnnotations()) {
String fqn = annotation.getQualifiedName();
if (fqn != null && fqn.endsWith("ASTNodeAnnotation.Source")) {
for (var attribute : annotation.getAttributes()) {
if (attribute.getAttributeName().equals("declaredAt")) {
JvmAnnotationConstantValue value = (JvmAnnotationConstantValue) attribute.getAttributeValue();
if (value != null) {
Object originalReference = value.getConstantValue();
String fileName = element.getContainingFile().getVirtualFile().getPath();
int lineNumber = StringUtil.offsetToLineNumber(element.getContainingFile().getText(), element.getTextOffset()) + 1; // lines start with 0 in JetBrains
String reference = fileName + ":" + lineNumber;
if (originalReference != null && reference.equals(originalReference.toString())) {
String methodFileName = method.getContainingFile().getVirtualFile().getPath();
int methodLineNumber = StringUtil.offsetToLineNumber(method.getContainingFile().getText(), method.getTextOffset()) + 1; // lines start with 0 in JetBrains
String methodNavigationUrl = getNavigationUrlFromFileLocation(methodFileName + ":" + methodLineNumber);
holder.newAnnotation(HighlightSeverity.INFORMATION, "JastAdd definition")
.range(element.getTextRange())
.highlightType(ProblemHighlightType.INFORMATION)
.textAttributes(JavaSyntaxHighlighter.IMPLEMENTED_ATTRIBUTE_DEFINITION)
.tooltip(methodNavigationUrl)
.create();
}
}
}
}
}
}
}
}
}
......@@ -23,6 +23,8 @@ public class JavaSyntaxHighlighter extends SyntaxHighlighterBase {
createTextAttributesKey("INTERNAL_API_USE", DefaultLanguageHighlighterColors.HIGHLIGHTED_REFERENCE);
public static final TextAttributesKey ATTRIBUTE_CALL =
createTextAttributesKey("ATTRIBUTE_CALL", DefaultLanguageHighlighterColors.HIGHLIGHTED_REFERENCE);
public static final TextAttributesKey IMPLEMENTED_ATTRIBUTE_DEFINITION =
createTextAttributesKey("IMPLEMENTED_ATTRIBUTE_DEFINITION", DefaultLanguageHighlighterColors.HIGHLIGHTED_REFERENCE);
public static final TextAttributesKey INTERTYPE_DECL_USE =
createTextAttributesKey("INTERTYPE_DECL_USE", DefaultLanguageHighlighterColors.HIGHLIGHTED_REFERENCE);
......
......@@ -92,6 +92,8 @@
<lang.foldingBuilder language="JastAddAspect" implementationClass="org.jastadd.tooling.aspect.AspectFoldingBuilder"/>
<lang.psiStructureViewFactory language="JastAddAspect" implementationClass="org.jastadd.tooling.aspect.AspectStructureViewFactory"/>
<annotator language="JastAddAspect" implementationClass="org.jastadd.tooling.aspect.ImplementationAnnotator"/>
</extensions>
<actions>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment