diff options
author | George Fraser <george@fivetran.com> | 2018-06-21 17:06:46 -0700 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-06-21 17:06:46 -0700 |
commit | 683d91259b10c898bd570cba0d3bad15101548b8 (patch) | |
tree | 66ea045ba8ff04a18fd8a11e5e9ec53d06619ea1 | |
parent | 1716586fad1372aaeadaab358c320b2dc4e32b77 (diff) | |
download | java-language-server-683d91259b10c898bd570cba0d3bad15101548b8.zip |
Active method and parameter
-rw-r--r-- | src/main/java/org/javacs/JavaCompilerService.java | 26 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaTextDocumentService.java | 54 | ||||
-rw-r--r-- | src/main/java/org/javacs/MethodInvocation.java | 26 | ||||
-rw-r--r-- | src/test/java/org/javacs/CompletionsBase.java | 13 | ||||
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerServiceTest.java | 7 | ||||
-rw-r--r-- | src/test/java/org/javacs/SymbolUnderCursorTest.java | 1 |
6 files changed, 93 insertions, 34 deletions
diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index bc009f6..f813c91 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -519,7 +519,7 @@ public class JavaCompilerService { } /** Find all overloads for the smallest method call that includes the cursor */ - public List<ExecutableElement> overloads(URI file, String contents, int line, int character) { + public Optional<MethodInvocation> methodInvocation(URI file, String contents, int line, int character) { recompile(file, contents, line, character); Trees trees = Trees.instance(cache.task); @@ -535,10 +535,30 @@ public class JavaCompilerService { results.add((ExecutableElement) m); } } - return results; + int activeParameter = invoke.getArguments().indexOf(start.getLeaf()); + Optional<ExecutableElement> activeMethod = + method instanceof ExecutableElement + ? Optional.of((ExecutableElement) method) + : Optional.empty(); + return Optional.of(new MethodInvocation(invoke, activeMethod, activeParameter, results)); + } else if (path.getLeaf() instanceof NewClassTree) { + NewClassTree invoke = (NewClassTree) path.getLeaf(); + Element method = trees.getElement(path); + List<ExecutableElement> results = new ArrayList<>(); + for (Element m : method.getEnclosingElement().getEnclosedElements()) { + if (m.getKind() == ElementKind.CONSTRUCTOR) { + results.add((ExecutableElement) m); + } + } + int activeParameter = invoke.getArguments().indexOf(start.getLeaf()); + Optional<ExecutableElement> activeMethod = + method instanceof ExecutableElement + ? Optional.of((ExecutableElement) method) + : Optional.empty(); + return Optional.of(new MethodInvocation(invoke, activeMethod, activeParameter, results)); } } - return Collections.emptyList(); + return Optional.empty(); } /** Find the smallest element that includes the cursor */ diff --git a/src/main/java/org/javacs/JavaTextDocumentService.java b/src/main/java/org/javacs/JavaTextDocumentService.java index 5e5579d..254a1d9 100644 --- a/src/main/java/org/javacs/JavaTextDocumentService.java +++ b/src/main/java/org/javacs/JavaTextDocumentService.java @@ -71,32 +71,46 @@ class JavaTextDocumentService implements TextDocumentService { } else return CompletableFuture.completedFuture(new Hover(Collections.emptyList())); } + private SignatureInformation asSignatureInformation(ExecutableElement e) { + SignatureInformation i = new SignatureInformation(); + List<ParameterInformation> ps = new ArrayList<>(); + StringJoiner args = new StringJoiner(", "); + for (VariableElement v : e.getParameters()) { + ParameterInformation p = new ParameterInformation(); + String label = v.getSimpleName().toString(); + // TODO use type when name is not available + args.add(label); + p.setLabel(label); + ps.add(p); + } + String name = e.getSimpleName().toString(); + if (name.equals("<init>")) name = e.getEnclosingElement().getSimpleName().toString(); + i.setLabel(name + "(" + args + ")"); + i.setParameters(ps); + return i; + } + + private SignatureHelp asSignatureHelp(MethodInvocation invoke) { + List<SignatureInformation> sigs = new ArrayList<>(); + for (ExecutableElement e : invoke.overloads) { + sigs.add(asSignatureInformation(e)); + } + int activeSig = invoke.activeMethod.map(invoke.overloads::indexOf).orElse(0); + return new SignatureHelp(sigs, activeSig, invoke.activeParameter); + } + @Override public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams position) { URI uri = URI.create(position.getTextDocument().getUri()); String content = contents(uri).content; int line = position.getPosition().getLine() + 1; int column = position.getPosition().getCharacter() + 1; - List<SignatureInformation> result = new ArrayList<>(); - for (ExecutableElement e : server.compiler.overloads(uri, content, line, column)) { - SignatureInformation i = new SignatureInformation(); - List<ParameterInformation> ps = new ArrayList<>(); - StringJoiner args = new StringJoiner(", "); - for (VariableElement v : e.getParameters()) { - ParameterInformation p = new ParameterInformation(); - String label = v.getSimpleName().toString(); - // TODO use type when name is not available - args.add(label); - p.setLabel(label); - ps.add(p); - } - i.setLabel(e.getSimpleName().toString() + "(" + args + ")"); - i.setParameters(ps); - result.add(i); - } - int activeSig = 0; // TODO - int activeParam = 0; // TODO - return CompletableFuture.completedFuture(new SignatureHelp(result, activeSig, activeParam)); + SignatureHelp help = + server.compiler + .methodInvocation(uri, content, line, column) + .map(this::asSignatureHelp) + .orElse(new SignatureHelp()); + return CompletableFuture.completedFuture(help); } private Location location(TreePath p) { diff --git a/src/main/java/org/javacs/MethodInvocation.java b/src/main/java/org/javacs/MethodInvocation.java new file mode 100644 index 0000000..d5c3a4e --- /dev/null +++ b/src/main/java/org/javacs/MethodInvocation.java @@ -0,0 +1,26 @@ +package org.javacs; + +import com.sun.source.tree.ExpressionTree; +import java.util.List; +import java.util.Optional; +import javax.lang.model.element.ExecutableElement; + +public class MethodInvocation { + /** MethodInvocationTree or NewClassTree */ + public final ExpressionTree tree; + + public final Optional<ExecutableElement> activeMethod; + public final int activeParameter; + public final List<ExecutableElement> overloads; + + public MethodInvocation( + ExpressionTree tree, + Optional<ExecutableElement> activeMethod, + int activeParameter, + List<ExecutableElement> overloads) { + this.tree = tree; + this.activeMethod = activeMethod; + this.activeParameter = activeParameter; + this.overloads = overloads; + } +} diff --git a/src/test/java/org/javacs/CompletionsBase.java b/src/test/java/org/javacs/CompletionsBase.java index 7e2629d..e981224 100644 --- a/src/test/java/org/javacs/CompletionsBase.java +++ b/src/test/java/org/javacs/CompletionsBase.java @@ -10,10 +10,7 @@ import java.util.concurrent.ExecutionException; import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.eclipse.lsp4j.CompletionItem; -import org.eclipse.lsp4j.Position; -import org.eclipse.lsp4j.TextDocumentIdentifier; -import org.eclipse.lsp4j.TextDocumentPositionParams; +import org.eclipse.lsp4j.*; public class CompletionsBase { protected static final Logger LOG = Logger.getLogger("main"); @@ -72,7 +69,8 @@ public class CompletionsBase { return items.stream() .flatMap( i -> { - if (i.getDocumentation() != null) return Stream.of(i.getDocumentation().trim()); + if (i.getDocumentation() != null) + return Stream.of(i.getDocumentation().getRight().getValue().trim()); else return Stream.empty(); }) .collect(Collectors.toSet()); @@ -82,9 +80,8 @@ public class CompletionsBase { protected List<? extends CompletionItem> items(String file, int row, int column) { URI uri = FindResource.uri(file); - TextDocumentPositionParams position = - new TextDocumentPositionParams( - new TextDocumentIdentifier(uri.toString()), new Position(row - 1, column - 1)); + CompletionParams position = + new CompletionParams(new TextDocumentIdentifier(uri.toString()), new Position(row - 1, column - 1)); try { return server.getTextDocumentService().completion(position).get().getRight().getItems(); diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index 66dac36..09e96ee 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -188,9 +188,10 @@ public class JavaCompilerServiceTest { @Test public void overloads() { - List<ExecutableElement> found = - compiler.overloads(URI.create("/Overloads.java"), contents("/Overloads.java"), 3, 15); + MethodInvocation found = + compiler.methodInvocation(URI.create("/Overloads.java"), contents("/Overloads.java"), 3, 15).get(); - assertThat(found, containsInAnyOrder(hasToString("print(int)"), hasToString("print(java.lang.String)"))); + assertThat( + found.overloads, containsInAnyOrder(hasToString("print(int)"), hasToString("print(java.lang.String)"))); } } diff --git a/src/test/java/org/javacs/SymbolUnderCursorTest.java b/src/test/java/org/javacs/SymbolUnderCursorTest.java index 1578fee..f01a5c1 100644 --- a/src/test/java/org/javacs/SymbolUnderCursorTest.java +++ b/src/test/java/org/javacs/SymbolUnderCursorTest.java @@ -95,6 +95,7 @@ public class SymbolUnderCursorTest { .hover(pos) .get() .getContents() + .getLeft() .forEach(hover -> result.add(hover.getRight().getValue())); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); |