diff options
-rw-r--r-- | src/main/java/org/javacs/CompileBatch.java | 12 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 26 | ||||
-rw-r--r-- | src/main/java/org/javacs/lsp/LanguageServer.java | 4 | ||||
-rw-r--r-- | src/test/java/org/javacs/FindReferencesTest.java | 2 | ||||
-rw-r--r-- | src/test/java/org/javacs/GotoTest.java | 2 |
5 files changed, 24 insertions, 22 deletions
diff --git a/src/main/java/org/javacs/CompileBatch.java b/src/main/java/org/javacs/CompileBatch.java index 6d71e8e..96ab36e 100644 --- a/src/main/java/org/javacs/CompileBatch.java +++ b/src/main/java/org/javacs/CompileBatch.java @@ -99,12 +99,12 @@ public class CompileBatch { throw new RuntimeException("File " + uri + " isn't in batch " + roots); } - public List<TreePath> definitions(Element el) { + public Optional<List<TreePath>> definitions(Element el) { LOG.info(String.format("Search for definitions of `%s` in %d files...", el, roots.size())); if (el.asType().getKind() == TypeKind.ERROR) { LOG.info(String.format("...`%s` is an error type, giving up", el.asType())); - return List.of(); + return Optional.empty(); } var refs = new ArrayList<TreePath>(); @@ -172,15 +172,15 @@ public class CompileBatch { for (var r : roots) { finder.scan(r, null); } - return refs; + return Optional.of(refs); } - public List<TreePath> references(Element to) { + public Optional<List<TreePath>> references(Element to) { LOG.info(String.format("Search for references to `%s` in %d files...", to, roots.size())); if (to.asType().getKind() == TypeKind.ERROR) { LOG.info(String.format("...`%s` is an error type, giving up", to.asType())); - return List.of(); + return Optional.empty(); } var refs = new ArrayList<TreePath>(); @@ -254,7 +254,7 @@ public class CompileBatch { for (var r : roots) { finder.scan(r, null); } - return refs; + return Optional.of(refs); } public Map<URI, Index> countReferences() { diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 3a5bc0c..4932cc1 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -756,9 +756,9 @@ class JavaLanguageServer extends LanguageServer { } @Override - public List<Location> gotoDefinition(TextDocumentPositionParams position) { + public Optional<List<Location>> gotoDefinition(TextDocumentPositionParams position) { var fromUri = position.textDocument.uri; - if (!isJavaFile(fromUri)) return List.of(); + if (!isJavaFile(fromUri)) return Optional.empty(); var fromLine = position.position.line + 1; var fromColumn = position.position.character + 1; var fromContent = contents(fromUri).content; @@ -769,12 +769,12 @@ class JavaLanguageServer extends LanguageServer { var toEl = hoverCache.element(fromLine, fromColumn); if (!toEl.isPresent()) { LOG.info(String.format("...no element at cursor")); - return List.of(); + return Optional.empty(); } // Compile all files that *might* contain definitions of fromEl var toFiles = compiler.potentialDefinitions(toEl.get()); - if (toFiles.isEmpty()) return List.of(); + if (toFiles.isEmpty()) return Optional.of(List.of()); var batch = compiler.compileBatch(latestText(toFiles)); // Find fromEl again, so that we have an Element from the current batch @@ -782,8 +782,9 @@ class JavaLanguageServer extends LanguageServer { // Find all definitions of fromElAgain var toTreePaths = batch.definitions(fromElAgain); + if (toTreePaths.isEmpty()) return Optional.empty(); var result = new ArrayList<Location>(); - for (var path : toTreePaths) { + for (var path : toTreePaths.get()) { var toUri = path.getCompilationUnit().getSourceFile().toUri(); var toRange = batch.range(path); if (!toRange.isPresent()) { @@ -793,7 +794,7 @@ class JavaLanguageServer extends LanguageServer { var from = new Location(toUri, toRange.get()); result.add(from); } - return result; + return Optional.of(result); } class Progress implements ReportProgress, AutoCloseable { @@ -825,9 +826,9 @@ class JavaLanguageServer extends LanguageServer { } @Override - public List<Location> findReferences(ReferenceParams position) { + public Optional<List<Location>> findReferences(ReferenceParams position) { var toUri = position.textDocument.uri; - if (!isJavaFile(toUri)) return List.of(); + if (!isJavaFile(toUri)) return Optional.empty(); var toLine = position.position.line + 1; var toColumn = position.position.character + 1; var toContent = contents(toUri).content; @@ -838,12 +839,12 @@ class JavaLanguageServer extends LanguageServer { var toEl = hoverCache.element(toLine, toColumn); if (!toEl.isPresent()) { LOG.warning("...no element under cursor"); - return List.of(); + return Optional.empty(); } // Compile all files that *might* contain references to toEl var fromFiles = compiler.potentialReferences(toEl.get()); - if (fromFiles.isEmpty()) return List.of(); + if (fromFiles.isEmpty()) return Optional.of(List.of()); var batch = compiler.compileBatch(latestText(fromFiles)); // Find toEl again, so that we have an Element from the current batch @@ -852,8 +853,9 @@ class JavaLanguageServer extends LanguageServer { // Find all references to toElAgain // TODO this should references to supers of toEl var fromTreePaths = batch.references(toElAgain); + if (fromTreePaths.isEmpty()) return Optional.empty(); var result = new ArrayList<Location>(); - for (var path : fromTreePaths) { + for (var path : fromTreePaths.get()) { var fromUri = path.getCompilationUnit().getSourceFile().toUri(); var fromRange = batch.range(path); if (!fromRange.isPresent()) { @@ -863,7 +865,7 @@ class JavaLanguageServer extends LanguageServer { var from = new Location(fromUri, fromRange.get()); result.add(from); } - return result; + return Optional.of(result); } private List<JavaFileObject> latestText(List<URI> files) { diff --git a/src/main/java/org/javacs/lsp/LanguageServer.java b/src/main/java/org/javacs/lsp/LanguageServer.java index 1c3f1bb..169973e 100644 --- a/src/main/java/org/javacs/lsp/LanguageServer.java +++ b/src/main/java/org/javacs/lsp/LanguageServer.java @@ -64,11 +64,11 @@ public class LanguageServer { throw new RuntimeException("Unimplemented"); } - public List<Location> gotoDefinition(TextDocumentPositionParams params) { + public Optional<List<Location>> gotoDefinition(TextDocumentPositionParams params) { throw new RuntimeException("Unimplemented"); } - public List<Location> findReferences(ReferenceParams params) { + public Optional<List<Location>> findReferences(ReferenceParams params) { throw new RuntimeException("Unimplemented"); } diff --git a/src/test/java/org/javacs/FindReferencesTest.java b/src/test/java/org/javacs/FindReferencesTest.java index e728cc9..bbb14cf 100644 --- a/src/test/java/org/javacs/FindReferencesTest.java +++ b/src/test/java/org/javacs/FindReferencesTest.java @@ -21,7 +21,7 @@ public class FindReferencesTest { params.textDocument = new TextDocumentIdentifier(uri); params.position = new Position(row - 1, column - 1); - var locations = server.findReferences(params); + var locations = server.findReferences(params).orElse(List.of()); var strings = new ArrayList<String>(); for (var l : locations) { var fileName = Parser.fileName(l.uri); diff --git a/src/test/java/org/javacs/GotoTest.java b/src/test/java/org/javacs/GotoTest.java index e92565c..564071e 100644 --- a/src/test/java/org/javacs/GotoTest.java +++ b/src/test/java/org/javacs/GotoTest.java @@ -171,7 +171,7 @@ public class GotoTest { p.textDocument = document; p.position = position; - var locations = server.gotoDefinition(p); + var locations = server.gotoDefinition(p).orElse(List.of()); var strings = new ArrayList<String>(); for (var l : locations) { var fileName = Paths.get(l.uri).getFileName(); |