diff options
author | George Fraser <george@fivetran.com> | 2019-01-06 13:47:26 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2019-01-06 13:47:26 -0800 |
commit | 840a3dd93cd0724d52cd8c12fec174fd98c143d5 (patch) | |
tree | 83b07ca06ab6283032f2925e7828ce2d0418b37e /src | |
parent | 7aca5297246443a948b47c8219f6c95394ddaa1f (diff) | |
download | java-language-server-840a3dd93cd0724d52cd8c12fec174fd98c143d5.zip |
Fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/javacs/FileStore.java | 12 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaCompilerService.java | 19 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 20 | ||||
-rw-r--r-- | src/main/java/org/javacs/SourcePath.java | 7 |
4 files changed, 34 insertions, 24 deletions
diff --git a/src/main/java/org/javacs/FileStore.java b/src/main/java/org/javacs/FileStore.java index 96b82a2..25fdbe8 100644 --- a/src/main/java/org/javacs/FileStore.java +++ b/src/main/java/org/javacs/FileStore.java @@ -55,21 +55,17 @@ class FileStore { } } - static boolean isJavaFile(URI uri) { - return uri.getScheme().equals("file") && uri.getPath().endsWith(".java"); - } - static void open(DidOpenTextDocumentParams params) { var document = params.textDocument; var uri = document.uri; - if (!isJavaFile(uri)) return; + if (!SourcePath.isJavaFile(uri)) return; activeDocuments.put(uri, new VersionedContent(document.text, document.version)); } static void change(DidChangeTextDocumentParams params) { var document = params.textDocument; var uri = document.uri; - if (isJavaFile(uri)) { + if (SourcePath.isJavaFile(uri)) { var existing = activeDocuments.get(uri); var newText = existing.content; @@ -87,7 +83,7 @@ class FileStore { static void close(DidCloseTextDocumentParams params) { var document = params.textDocument; var uri = document.uri; - if (isJavaFile(uri)) { + if (SourcePath.isJavaFile(uri)) { // Remove from source cache activeDocuments.remove(uri); } @@ -103,7 +99,7 @@ class FileStore { } static String contents(URI file) { - if (!isJavaFile(file)) { + if (!SourcePath.isJavaFile(file)) { LOG.warning("Ignoring non-java file " + file); return ""; } diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index 19b5562..0a6a136 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -141,7 +141,7 @@ public class JavaCompilerService { // Construct list of sources var files = new ArrayList<File>(); for (var uri : uris) { - if (FileStore.isJavaFile(uri)) { + if (SourcePath.isJavaFile(uri)) { files.add(new File(uri)); ; } @@ -335,7 +335,7 @@ public class JavaCompilerService { return files; } // If the declaring file isn't a normal file, for example if it's in src.zip - if (!FileStore.isJavaFile(toFile.get())) { + if (!SourcePath.isJavaFile(toFile.get())) { LOG.info(String.format("...%s is not on the source path", toFile.get())); return files; } @@ -430,14 +430,23 @@ public class JavaCompilerService { var isPublic = e.getModifiers().contains(Modifier.PUBLIC); if (!isPublic) { for (var root : sourcePath) { + // Create directory where this package would live, if this package is in this part of the source path var absDir = root.resolve(packagePath); + // If package isn't in this part of the source path, keep looking + if (!Files.exists(absDir)) continue; + // List dirs + Iterable<Path> list; try { - var foundFile = - Files.list(absDir).filter(f -> containsTopLevelDeclaration(f, className)).findFirst(); - if (foundFile.isPresent()) return foundFile.map(Path::toUri); + list = Files.list(absDir)::iterator; } catch (IOException err) { throw new RuntimeException(err); } + // Check each .java file in the package for package-private class declarations + for (var f : list) { + if (SourcePath.isJavaFile(f) && containsTopLevelDeclaration(f, className)) { + return Optional.of(f.toUri()); + } + } } } return Optional.empty(); diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 33a44e5..d9f2105 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -264,7 +264,7 @@ class JavaLanguageServer extends LanguageServer { var created = new HashSet<Path>(); var deleted = new HashSet<Path>(); for (var c : params.changes) { - if (!FileStore.isJavaFile(c.uri)) continue; + if (!SourcePath.isJavaFile(c.uri)) continue; var file = Paths.get(c.uri); switch (c.type) { case FileChangeType.Created: @@ -342,7 +342,7 @@ class JavaLanguageServer extends LanguageServer { // TODO reuse previous compilation when changes are small var started = Instant.now(); var uri = position.textDocument.uri; - if (!FileStore.isJavaFile(uri)) return Optional.empty(); + if (!SourcePath.isJavaFile(uri)) return Optional.empty(); var content = FileStore.contents(uri); var line = position.position.line + 1; var column = position.position.character + 1; @@ -644,7 +644,7 @@ class JavaLanguageServer extends LanguageServer { public Optional<Hover> hover(TextDocumentPositionParams position) { // Compile entire file if it's changed since last hover var uri = position.textDocument.uri; - if (!FileStore.isJavaFile(uri)) return Optional.empty(); + if (!SourcePath.isJavaFile(uri)) return Optional.empty(); updateActiveFile(uri); // Find element undeer cursor @@ -748,7 +748,7 @@ class JavaLanguageServer extends LanguageServer { @Override public Optional<SignatureHelp> signatureHelp(TextDocumentPositionParams position) { var uri = position.textDocument.uri; - if (!FileStore.isJavaFile(uri)) return Optional.empty(); + if (!SourcePath.isJavaFile(uri)) return Optional.empty(); var content = FileStore.contents(uri); var line = position.position.line + 1; var column = position.position.character + 1; @@ -762,7 +762,7 @@ class JavaLanguageServer extends LanguageServer { @Override public Optional<List<Location>> gotoDefinition(TextDocumentPositionParams position) { var fromUri = position.textDocument.uri; - if (!FileStore.isJavaFile(fromUri)) return Optional.empty(); + if (!SourcePath.isJavaFile(fromUri)) return Optional.empty(); var fromLine = position.position.line + 1; var fromColumn = position.position.character + 1; @@ -803,7 +803,7 @@ class JavaLanguageServer extends LanguageServer { @Override public Optional<List<Location>> findReferences(ReferenceParams position) { var toUri = position.textDocument.uri; - if (!FileStore.isJavaFile(toUri)) return Optional.empty(); + if (!SourcePath.isJavaFile(toUri)) return Optional.empty(); var toLine = position.position.line + 1; var toColumn = position.position.character + 1; @@ -876,7 +876,7 @@ class JavaLanguageServer extends LanguageServer { @Override public List<SymbolInformation> documentSymbol(DocumentSymbolParams params) { var uri = params.textDocument.uri; - if (!FileStore.isJavaFile(uri)) return List.of(); + if (!SourcePath.isJavaFile(uri)) return List.of(); updateCachedParse(uri); var paths = cacheParse.documentSymbols(); var infos = new ArrayList<SymbolInformation>(); @@ -955,7 +955,7 @@ class JavaLanguageServer extends LanguageServer { public List<CodeLens> codeLens(CodeLensParams params) { // TODO just create a blank code lens on every method, then resolve it async var uri = params.textDocument.uri; - if (!FileStore.isJavaFile(uri)) return List.of(); + if (!SourcePath.isJavaFile(uri)) return List.of(); updateCachedParse(uri); var declarations = cacheParse.declarations(); var result = new ArrayList<CodeLens>(); @@ -1308,7 +1308,7 @@ class JavaLanguageServer extends LanguageServer { public void didCloseTextDocument(DidCloseTextDocumentParams params) { FileStore.close(params); - if (FileStore.isJavaFile(params.textDocument.uri)) { + if (SourcePath.isJavaFile(params.textDocument.uri)) { // Clear diagnostics publishDiagnostics(List.of(params.textDocument.uri), List.of()); } @@ -1316,7 +1316,7 @@ class JavaLanguageServer extends LanguageServer { @Override public void didSaveTextDocument(DidSaveTextDocumentParams params) { - if (FileStore.isJavaFile(params.textDocument.uri)) { + if (SourcePath.isJavaFile(params.textDocument.uri)) { // Re-lint all active documents reportErrors(FileStore.activeDocuments()); } diff --git a/src/main/java/org/javacs/SourcePath.java b/src/main/java/org/javacs/SourcePath.java index 491b87d..60133bc 100644 --- a/src/main/java/org/javacs/SourcePath.java +++ b/src/main/java/org/javacs/SourcePath.java @@ -2,6 +2,7 @@ package org.javacs; import java.io.File; import java.io.IOException; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -93,11 +94,15 @@ class SourcePath { return false; } - private static boolean isJavaFile(Path file) { + static boolean isJavaFile(Path file) { var name = file.getFileName().toString(); return name.endsWith(".java") && !name.equals("module-info.java"); } + static boolean isJavaFile(URI uri) { + return uri.getScheme().equals("file") && isJavaFile(Paths.get(uri)); + } + private static Set<Path> allJavaFilesInDirs(Set<Path> dirs) { var all = new HashSet<Path>(); for (var dir : dirs) { |