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

improved highlighting

parent 1b4cbdb5
No related branches found
No related tags found
No related merge requests found
Pipeline #11833 passed
......@@ -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
......
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();
}
}
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();
}
}
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();
}
}
}
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment