diff options
Diffstat (limited to 'src')
5 files changed, 66 insertions, 16 deletions
diff --git a/src/main/java/org/javacs/CompileFile.java b/src/main/java/org/javacs/CompileFile.java index c99c2b8..7d7802c 100644 --- a/src/main/java/org/javacs/CompileFile.java +++ b/src/main/java/org/javacs/CompileFile.java @@ -282,7 +282,7 @@ public class CompileFile { return i -> { // For each pointer, check if it refers to something in this file that no longer exists for (var ptr : i) { - if (thisClasses.contains(ptr.qualifiedClassName()) && find(ptr).isEmpty()) { + if (thisClasses.contains(ptr.qualifiedClassName()) && !find(ptr).isPresent()) { LOG.info( String.format("`%s` refers to signature that no longer exists in %s", ptr, file.getPath())); return false; diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index c9ebb30..2218b8c 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -1180,7 +1180,7 @@ class JavaLanguageServer extends LanguageServer { private String countReferencesTitle(URI uri, int line, int character) { updateHoverCache(uri, contents(uri).content); var el = hoverCache.element(line, character); - if (el.isEmpty()) { + if (!el.isPresent()) { LOG.warning(String.format("No element to resolve code lens at %s(%d,%d)", uri.getPath(), line, character)); return "? references"; } diff --git a/src/main/java/org/javacs/ParseFile.java b/src/main/java/org/javacs/ParseFile.java index 45f1443..40ed816 100644 --- a/src/main/java/org/javacs/ParseFile.java +++ b/src/main/java/org/javacs/ParseFile.java @@ -359,23 +359,56 @@ public class ParseFile { end = start + path.getLeaf().toString().length(); } - // If this is a class or method declaration, we need to refine the range - if (path.getLeaf() instanceof ClassTree || path.getLeaf() instanceof MethodTree || path.getLeaf() instanceof VariableTree) { - // Figure out what name to search for - var className = JavaCompilerService.className(path); - var memberName = JavaCompilerService.memberName(path); - String searchFor; - if (!memberName.isPresent()) searchFor = className; - else if (memberName.get().equals("<init>")) searchFor = className; - else searchFor = memberName.get(); - - // Search text for searchFor - start = contents.indexOf(searchFor, start); - end = start + searchFor.length(); + if (path.getLeaf() instanceof ClassTree) { + var cls = (ClassTree) path.getLeaf(); + + // If class has annotations, skip over them + if (!cls.getModifiers().getAnnotations().isEmpty()) + start = (int) pos.getEndPosition(root, cls.getModifiers()); + + // Find position of class name + var name = cls.getSimpleName().toString(); + start = contents.indexOf(name, start); + if (start == -1) { + LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf())); + return Optional.empty(); + } + end = start + name.length(); + } + if (path.getLeaf() instanceof MethodTree) { + var method = (MethodTree) path.getLeaf(); + + // If method has annotations, skip over them + if (!method.getModifiers().getAnnotations().isEmpty()) + start = (int) pos.getEndPosition(root, method.getModifiers()); + + // Find position of method name + var name = method.getName().toString(); + if (name.equals("<init>")) { + name = JavaCompilerService.className(path); + } + start = contents.indexOf(name, start); + if (start == -1) { + LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf())); + return Optional.empty(); + } + end = start + name.length(); + } + if (path.getLeaf() instanceof VariableTree) { + var field = (VariableTree) path.getLeaf(); + + // If field has annotations, skip over them + if (!field.getModifiers().getAnnotations().isEmpty()) + start = (int) pos.getEndPosition(root, field.getModifiers()); + + // Find position of method name + var name = field.getName().toString(); + start = contents.indexOf(name, start); if (start == -1) { - LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", searchFor, path.getLeaf())); + LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf())); return Optional.empty(); } + end = start + name.length(); } var startLine = (int) lines.getLineNumber(start); var startCol = (int) lines.getColumnNumber(start); diff --git a/src/test/java/org/javacs/CodeLensTest.java b/src/test/java/org/javacs/CodeLensTest.java index 893c605..963929c 100644 --- a/src/test/java/org/javacs/CodeLensTest.java +++ b/src/test/java/org/javacs/CodeLensTest.java @@ -86,4 +86,12 @@ public class CodeLensTest { var bad = List.of(new Ptr("org.javacs.example/ConstructorRefs#<init>(int,int)")); assertFalse(signatureMatches.test(bad)); } + + @Test + public void placement() { + var lenses = lenses("/org/javacs/example/CodeLensPlacement.java"); + var titles = titles(lenses); + assertThat(titles, hasItem("4:0 references")); + assertThat(titles, hasItem("6:0 references")); + } } diff --git a/src/test/test-project/workspace/src/org/javacs/example/CodeLensPlacement.java b/src/test/test-project/workspace/src/org/javacs/example/CodeLensPlacement.java new file mode 100644 index 0000000..d58bd67 --- /dev/null +++ b/src/test/test-project/workspace/src/org/javacs/example/CodeLensPlacement.java @@ -0,0 +1,9 @@ +package org.javacs.example; + +@Deprecated(since = "The presence of the word CodeLensPlacement screws up positioning") +public class CodeLensPlacement { + @Deprecated(since = "The presence of the word main screws up positioning") + void main() { + + } +}
\ No newline at end of file |