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

working links in jastadd tooltips

parent 1b59b50a
No related branches found
No related tags found
No related merge requests found
package org.jastadd.tooling.java;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Paths;
public class GeneratedCodeUtil {
/**
* Get the line of the PSI element within its document
......@@ -19,4 +24,35 @@ public class GeneratedCodeUtil {
}
return line;
}
public static String getNavigationUrlFromFileLocation(String jastAddFileLocation) {
int pos = jastAddFileLocation.lastIndexOf(':');
if (pos <= 0 || pos == jastAddFileLocation.length() - 1) {
return jastAddFileLocation;
}
String path = jastAddFileLocation.substring(0, pos);
System.out.println(path);
VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
int line;
try {
line = Integer.parseInt(jastAddFileLocation.substring(pos + 1));
} catch (NumberFormatException e) {
line = 0;
}
if (vFile == null) {
return Paths.get(path).getFileName() + (line == 0 ? "" : ":" + line);
}
System.out.println(line);
Document document = FileDocumentManager.getInstance().getDocument(vFile);
if (document == null) {
return vFile.getName() + (line == 0 ? "" : ":" + line);
}
int offset = document.getLineStartOffset(line - 1); // lines start with 0 in jetbrains
return "<a href=\"#navigation/" + path + ":" + offset + "\">" + vFile.getName() + ":" + line + "</a>";
}
}
......@@ -17,6 +17,8 @@ import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.jastadd.tooling.java.GeneratedCodeUtil.getNavigationUrlFromFileLocation;
public class JavaMethodHighlighter implements Annotator {
......@@ -59,7 +61,7 @@ public class JavaMethodHighlighter implements Annotator {
.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>") //
.tooltip("<b>JastAdd Attribute</b><br/>Definition: " + getNavigationUrlFromFileLocation(declaredAtString))
.create();
} else {
// case 2: use the JastAdd DocComment
......@@ -69,7 +71,7 @@ public class JavaMethodHighlighter implements Annotator {
PsiDocTag apiLevelTag = docComment.findTagByName("apilevel");
if (apiLevelTag == null) {
highlightKey = JavaSyntaxHighlighter.INTERTYPE_DECL_USE;
description = "JastAdd inter-type declaration use";
description = "JastAdd inter-type declaration";
} else {
PsiDocTagValue value = apiLevelTag.getValueElement();
if (value == null) {
......@@ -78,15 +80,15 @@ public class JavaMethodHighlighter implements Annotator {
switch (value.getText()) {
case "high": // the first value ends at the hyphen
highlightKey = JavaSyntaxHighlighter.HIGHLEVEL_API_USE;
description = "JastAdd high-level API use";
description = "JastAdd high-level API";
break;
case "low": // the first value ends at the hyphen
highlightKey = JavaSyntaxHighlighter.LOWLEVEL_API_USE;
description = "JastAdd low-level API use";
description = "JastAdd low-level API";
break;
case "internal":
highlightKey = JavaSyntaxHighlighter.INTERNAL_API_USE;
description = "JastAdd internal API use";
description = "JastAdd internal API";
break;
default:
// ignore error case
......@@ -94,14 +96,12 @@ public class JavaMethodHighlighter implements Annotator {
}
}
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>") //
.tooltip("<b>" + description + "</b><br/>Definition: " + getNavigationUrlFromFileLocation(declaredAtString))
.create();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment