diff options
author | George Fraser <george@fivetran.com> | 2018-12-31 16:20:03 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-12-31 16:20:03 -0800 |
commit | 7c990c76519faba1ba234391ed3714aef8f1357b (patch) | |
tree | cc05db5154fc00d405805b79328155f1dab3e3f9 /src | |
parent | 25c67bb8f300f8af13b7312933dec157e67e678c (diff) | |
download | java-language-server-7c990c76519faba1ba234391ed3714aef8f1357b.zip |
Disambiguate overrides
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/javacs/CompileBatch.java | 9 | ||||
-rw-r--r-- | src/main/java/org/javacs/CompileFile.java | 4 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 22 | ||||
-rw-r--r-- | src/main/java/org/javacs/Ptr.java | 36 |
4 files changed, 21 insertions, 50 deletions
diff --git a/src/main/java/org/javacs/CompileBatch.java b/src/main/java/org/javacs/CompileBatch.java index 9b35eb5..96319d9 100644 --- a/src/main/java/org/javacs/CompileBatch.java +++ b/src/main/java/org/javacs/CompileBatch.java @@ -224,7 +224,7 @@ public class CompileBatch { // TODO remember if the file contains errors, and keep re-indexing it var refs = new ArrayList<Ptr>(); class IndexFile extends TreePathScanner<Void, Void> { - Optional<TreePath> ref(TreePath from) { + Optional<Element> ref(TreePath from) { var root = from.getCompilationUnit(); var lines = root.getLineMap(); var to = trees.getElement(from); @@ -238,12 +238,7 @@ public class CompileBatch { return Optional.empty(); } // TODO skip anything not on source path - var result = trees.getPath(to); - if (result == null) { - // LOG.warning(String.format("Element `%s` has no TreePath", to)); - return Optional.empty(); - } - return Optional.of(result); + return Optional.of(to); } void check(TreePath from) { diff --git a/src/main/java/org/javacs/CompileFile.java b/src/main/java/org/javacs/CompileFile.java index 9ed833c..712cce8 100644 --- a/src/main/java/org/javacs/CompileFile.java +++ b/src/main/java/org/javacs/CompileFile.java @@ -86,7 +86,9 @@ public class CompileFile { /** Check if the declaration at the current path is the same symbol as `e` */ boolean sameSymbol() { - return new Ptr(getCurrentPath()).equals(target); + var path = getCurrentPath(); + var el = trees.getElement(path); + return new Ptr(el).equals(target); } void check() { diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 7bfa3a5..3ace4c1 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -958,7 +958,6 @@ class JavaLanguageServer extends LanguageServer { data.add(uri.toString()); data.add(line); data.add(character); - data.add(new Ptr(d).toString()); var lens = new CodeLens(range.get(), null, data); result.add(lens); } @@ -990,12 +989,17 @@ class JavaLanguageServer extends LanguageServer { var command = data.get(0).getAsString(); assert command.equals("java.command.findReferences"); var uriString = data.get(1).getAsString(); - var line = data.get(2).getAsInt(); - var character = data.get(3).getAsInt(); - var ptrString = data.get(4).getAsString(); - // Parse data var uri = URI.create(uriString); - var ptr = new Ptr(ptrString); + var line = data.get(2).getAsInt() + 1; + var character = data.get(3).getAsInt() + 1; + // Find the element being referenced + updateHoverCache(uri, contents(uri).content); + var el = hoverCache.element(line, character); + if (el.isEmpty()) { + LOG.warning(String.format("No element to resolve code lens at %s(%d,%d)", uri.getPath(), line, character)); + return unresolved; + } + var ptr = new Ptr(el.get()); // Update cache if necessary updateCacheCountReferences(uri); // Read reference count from cache @@ -1006,8 +1010,8 @@ class JavaLanguageServer extends LanguageServer { else title = String.format("%d references", count); var arguments = new JsonArray(); arguments.add(uri.toString()); - arguments.add(line); - arguments.add(character); + arguments.add(line - 1); + arguments.add(character - 1); unresolved.command = new Command(title, command, arguments); return unresolved; @@ -1084,7 +1088,7 @@ class JavaLanguageServer extends LanguageServer { for (var i = 0; i < indent; i++) insertText.append(' '); insertText.append("@Override"); insertText.append('\n'); - var insertPosition = new Position((int) insertLine, 0); + var insertPosition = new Position((int) insertLine - 1, 0); var insert = new TextEdit(new Range(insertPosition, insertPosition), insertText.toString()); edits.add(insert); } diff --git a/src/main/java/org/javacs/Ptr.java b/src/main/java/org/javacs/Ptr.java index 5f2cbaf..d0bf349 100644 --- a/src/main/java/org/javacs/Ptr.java +++ b/src/main/java/org/javacs/Ptr.java @@ -1,9 +1,5 @@ package org.javacs; -import com.sun.source.tree.ClassTree; -import com.sun.source.tree.MethodTree; -import com.sun.source.tree.VariableTree; -import com.sun.source.util.TreePath; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -15,10 +11,7 @@ import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; -/** - * Ptr is a reference to a named element, that can be serialized into a String. Ptr considers all overloads to be the - * same element. - */ +/** Ptr is a reference to a named element, that can be serialized into a String. */ public class Ptr { private final String path; @@ -26,30 +19,6 @@ public class Ptr { this.path = path; } - public Ptr(TreePath path) { - var packageName = path.getCompilationUnit().getPackageName(); - var rev = new ArrayList<CharSequence>(); - while (path != null) { - var part = path.getLeaf(); - if (part instanceof ClassTree) { - var cls = (ClassTree) part; - rev.add(cls.getSimpleName()); - } else if (part instanceof MethodTree) { - var method = (MethodTree) part; - // TODO overloads - rev.add(method.getName()); - } else if (part instanceof VariableTree) { - var variable = (VariableTree) part; - rev.add(variable.getName()); - } - path = path.getParentPath(); - } - if (packageName != null) rev.add(packageName.toString()); - var name = reverseAndJoin(rev, "."); - if (!name.matches("(\\w+\\.)*(\\w+|<init>)")) LOG.warning(String.format("`%s` doesn't look like a name", name)); - this.path = name; - } - public Ptr(Element e) { var rev = new ArrayList<CharSequence>(); while (e != null) { @@ -62,7 +31,7 @@ public class Ptr { } else if (e instanceof ExecutableElement) { var method = (ExecutableElement) e; // TODO overloads - rev.add(method.getSimpleName()); + rev.add(method.toString()); } else if (e instanceof VariableElement) { var field = (VariableElement) e; rev.add(field.getSimpleName()); @@ -94,6 +63,7 @@ public class Ptr { return Objects.hash(path); } + @Override public String toString() { return path; } |