diff --git a/CHANGELOG.md b/CHANGELOG.md index 7973f648f118deaf4ceef40b4681fe54bffa8f76..c4ebc514a47627bfd20d41a7651ff95d7f0f1cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - syntax highlighter and color settings - two file types for jrag and jadd - embedded java for attribute equation blocks +- Annotations for JastAdd usages in Java. + - configurable highlighters for attribute and api usages - Dark mode icon which is a bit less colourful. ### Changed diff --git a/src/main/java/org/jastadd/tooling/java/AstNodeAnnotationBasedHighlighter.java b/src/main/java/org/jastadd/tooling/java/AstNodeAnnotationBasedHighlighter.java deleted file mode 100644 index d52ff7f61a521f2f5277ed39ed1a29efb00458be..0000000000000000000000000000000000000000 --- a/src/main/java/org/jastadd/tooling/java/AstNodeAnnotationBasedHighlighter.java +++ /dev/null @@ -1,56 +0,0 @@ -package org.jastadd.tooling.java; - -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.psi.PsiElement; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiMethodCallExpression; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.Objects; - - -public class AstNodeAnnotationBasedHighlighter implements Annotator { - - @Override - public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) { - - if (!(element instanceof PsiMethodCallExpression)) { - return; - } - PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) element; - - PsiMethod value = psiMethodCallExpression.resolveMethod(); - if (value == null) { - return; - } - - // TODO check if ASTNodeAnnotation can be renamed - try { - if (Arrays.stream(value.getAnnotations()).noneMatch(x -> Objects.requireNonNull(x.getQualifiedName()).contains("ASTNodeAnnotation.Attribute"))) { - return; - } - } catch (NullPointerException e) { - return; - } - - PsiElement referenceElement = psiMethodCallExpression.getMethodExpression().getReferenceNameElement(); - if (referenceElement == null) { - return; - } - - // highlight - // TODO add more info in tooltip - holder.newAnnotation(HighlightSeverity.INFORMATION, "JastAdd Attribute") - .range(referenceElement.getTextRange()) - .highlightType(ProblemHighlightType.INFORMATION) - .textAttributes(JavaSyntaxHighlighter.ATTRIBUTE_CALL) - .tooltip("<b>JastAdd Attribute</b><br/>" + element.getContainingFile().getVirtualFile().getPath() + ":" + GeneratedCodeUtil.getLine(element)) - .create(); - } - - -} diff --git a/src/main/java/org/jastadd/tooling/java/AstNodeJavaDocBasedHighlighter.java b/src/main/java/org/jastadd/tooling/java/AstNodeJavaDocBasedHighlighter.java deleted file mode 100644 index 1ea64a6c713531313bf62455e16fb80989d85080..0000000000000000000000000000000000000000 --- a/src/main/java/org/jastadd/tooling/java/AstNodeJavaDocBasedHighlighter.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.jastadd.tooling.java; - -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.openapi.editor.colors.TextAttributesKey; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiMethodCallExpression; -import com.intellij.psi.javadoc.PsiDocComment; -import com.intellij.psi.javadoc.PsiDocTag; -import com.intellij.psi.javadoc.PsiDocTagValue; -import org.jetbrains.annotations.NotNull; - - -public class AstNodeJavaDocBasedHighlighter implements Annotator { - - @Override - public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) { - - if (!(element instanceof PsiMethodCallExpression)) { - return; - } - PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) element; - - PsiMethod targetMethod = psiMethodCallExpression.resolveMethod(); - if (targetMethod == null) { - return; - } - - PsiDocComment docComment = targetMethod.getDocComment(); - if (docComment == null) { - return; - } - - PsiDocTag apiLevelTag = docComment.findTagByName("apilevel"); - if (apiLevelTag == null) { - return; - } - - PsiDocTagValue value = apiLevelTag.getValueElement(); - if (value == null) { - return; - } - - TextAttributesKey highlightKey; - String description; - - switch (value.getText()) { - case "high": - highlightKey = JavaSyntaxHighlighter.HIGHLEVEL_API_USE; - description = "JastAdd high-level API use"; - break; - case "low": - highlightKey = JavaSyntaxHighlighter.LOWLEVEL_API_USE; - description = "JastAdd low-level API use"; - break; - case "internal": - highlightKey = JavaSyntaxHighlighter.INTERNAL_API_USE; - description = "JastAdd internal API use"; - break; - default: - // ignore error case - return; - } - - - PsiElement referenceElement = psiMethodCallExpression.getMethodExpression().getReferenceNameElement(); - if (referenceElement == null) { - return; - } - - // highlight - // TODO add more info in tooltip - holder.newAnnotation(HighlightSeverity.INFORMATION, description) - .range(referenceElement.getTextRange()) - .highlightType(ProblemHighlightType.INFORMATION) - .textAttributes(highlightKey) - .tooltip("<b>" + description + "</b><br/>" + element.getContainingFile().getVirtualFile().getPath() + ":" + GeneratedCodeUtil.getLine(element)) - .create(); - } - -} diff --git a/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java b/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java new file mode 100644 index 0000000000000000000000000000000000000000..a44e96e007c19befc0fb473b4ff7528a29c08884 --- /dev/null +++ b/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java @@ -0,0 +1,108 @@ +package org.jastadd.tooling.java; + +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.openapi.editor.colors.TextAttributesKey; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiMethodCallExpression; +import com.intellij.psi.javadoc.PsiDocComment; +import com.intellij.psi.javadoc.PsiDocTag; +import com.intellij.psi.javadoc.PsiDocTagValue; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Collectors; + + +public class JavaMethodHighlighter implements Annotator { + + @Override + public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) { + + if (!(element instanceof PsiMethodCallExpression)) { + return; + } + PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) element; + + PsiMethod targetMethod = psiMethodCallExpression.resolveMethod(); + if (targetMethod == null) { + return; + } + + PsiElement referenceElement = psiMethodCallExpression.getMethodExpression().getReferenceNameElement(); + if (referenceElement == null) { + return; + } + + // for anything interesting, there is a DocComment with a declaredat tag + PsiDocComment docComment = targetMethod.getDocComment(); + if (docComment == null) { + return; + } + + PsiDocTag declaredAtTag = docComment.findTagByName("declaredat"); + if (declaredAtTag == null) { + return; + } + + String declaredAtString = Arrays.stream(declaredAtTag.getDataElements()).map(PsiElement::getText).collect(Collectors.joining()); + + // case 1: there is an AstNodeAnnotation + // TODO check if ASTNodeAnnotation can be renamed + if (Arrays.stream(targetMethod.getAnnotations()).anyMatch(x -> Objects.requireNonNull(x.getQualifiedName()).contains("ASTNodeAnnotation.Attribute"))) { + // TODO add more info in tooltip + holder.newAnnotation(HighlightSeverity.INFORMATION, "JastAdd attribute") + .range(referenceElement.getTextRange()) + .highlightType(ProblemHighlightType.INFORMATION) + .textAttributes(JavaSyntaxHighlighter.ATTRIBUTE_CALL) + .tooltip("<b>JastAdd Attribute</b><br/><a href=\"#navigation/" + targetMethod.getContainingFile().getVirtualFile().getPath() + ":" + targetMethod.getTextRange().getStartOffset() + "\">" + targetMethod.getContainingFile().getVirtualFile().getName() + ":" + GeneratedCodeUtil.getLine(targetMethod) + "</a>") // + .create(); + } else { + // case 2: use the JastAdd DocComment + TextAttributesKey highlightKey; + String description; + + PsiDocTag apiLevelTag = docComment.findTagByName("apilevel"); + if (apiLevelTag == null) { + highlightKey = JavaSyntaxHighlighter.INTERTYPE_DECL_USE; + description = "JastAdd inter-type declaration use"; + } else { + PsiDocTagValue value = apiLevelTag.getValueElement(); + if (value == null) { + return; + } + switch (value.getText()) { + case "high": // the first value ends at the hyphen + highlightKey = JavaSyntaxHighlighter.HIGHLEVEL_API_USE; + description = "JastAdd high-level API use"; + break; + case "low": // the first value ends at the hyphen + highlightKey = JavaSyntaxHighlighter.LOWLEVEL_API_USE; + description = "JastAdd low-level API use"; + break; + case "internal": + highlightKey = JavaSyntaxHighlighter.INTERNAL_API_USE; + description = "JastAdd internal API use"; + break; + default: + // ignore error case + return; + } + } + + com.intellij.codeInsight.hint.NavigationLinkHandler h; + + // TODO add more info in tooltip + holder.newAnnotation(HighlightSeverity.INFORMATION, description) + .range(referenceElement.getTextRange()) + .highlightType(ProblemHighlightType.INFORMATION) + .textAttributes(highlightKey) + .tooltip("<b>" + description + "</b><br/><a href=\"#navigation/" + targetMethod.getContainingFile().getVirtualFile().getPath() + ":" + targetMethod.getTextRange().getStartOffset() + "\">" + targetMethod.getContainingFile().getVirtualFile().getName() + ":" + GeneratedCodeUtil.getLine(targetMethod) + "</a>") // + .create(); + } + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index a3cea52acb61ef2cc9b857830a6334f049d65bcb..2f239442ac09562e90ff54d47b73ee116233db4e 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -74,8 +74,7 @@ <colorSettingsPage implementation="org.jastadd.tooling.aspect.AspectColorSettingsPage"/> - <annotator language="JAVA" implementationClass="org.jastadd.tooling.java.AstNodeAnnotationBasedHighlighter"/> - <annotator language="JAVA" implementationClass="org.jastadd.tooling.java.AstNodeJavaDocBasedHighlighter"/> + <annotator language="JAVA" implementationClass="org.jastadd.tooling.java.JavaMethodHighlighter"/> <colorSettingsPage implementation="org.jastadd.tooling.java.JavaColorSettingsPage"/> </extensions>