diff --git a/src/main/java/org/jastadd/tooling/java/GeneratedCodeUtil.java b/src/main/java/org/jastadd/tooling/java/GeneratedCodeUtil.java index 88d50093e23731a81ea2af183efa7373a5225cb8..3c8e9915bb2c878475447a9ec7b10ffaf1a8bc6e 100644 --- a/src/main/java/org/jastadd/tooling/java/GeneratedCodeUtil.java +++ b/src/main/java/org/jastadd/tooling/java/GeneratedCodeUtil.java @@ -1,9 +1,14 @@ 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>"; + } } diff --git a/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java b/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java index a44e96e007c19befc0fb473b4ff7528a29c08884..ea6fd5fb1d00a793f606590ff161d4e3520ae8ec 100644 --- a/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java +++ b/src/main/java/org/jastadd/tooling/java/JavaMethodHighlighter.java @@ -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(); } }