diff options
author | George Fraser <george@fivetran.com> | 2018-12-31 15:09:44 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-12-31 15:09:44 -0800 |
commit | 8f63f14f980f44af2c5747747320703819ebc402 (patch) | |
tree | f8663c3592f195f59eb944452fb26ffec88a0570 /src | |
parent | 802686046fe1ae412fa3fc559488db5b644db499 (diff) | |
download | java-language-server-8f63f14f980f44af2c5747747320703819ebc402.zip |
Simplify
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/javacs/CompileFile.java | 21 | ||||
-rw-r--r-- | src/main/java/org/javacs/FixImports.java | 17 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 47 | ||||
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerServiceTest.java | 2 |
4 files changed, 39 insertions, 48 deletions
diff --git a/src/main/java/org/javacs/CompileFile.java b/src/main/java/org/javacs/CompileFile.java index 7b8a9f0..c26337d 100644 --- a/src/main/java/org/javacs/CompileFile.java +++ b/src/main/java/org/javacs/CompileFile.java @@ -8,7 +8,10 @@ import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.logging.Logger; @@ -18,11 +21,11 @@ import org.javacs.lsp.*; public class CompileFile { private final JavaCompilerService parent; - final URI file; - final String contents; + public final URI file; + public final String contents; private final JavacTask task; private final Trees trees; - private final CompilationUnitTree root; + public final CompilationUnitTree root; CompileFile(JavaCompilerService parent, URI file, String contents) { this.parent = parent; @@ -44,6 +47,10 @@ public class CompileFile { profiler.print(); } + public SourcePositions sourcePositions() { + return trees.getSourcePositions(); + } + public Optional<Element> element(int line, int character) { LOG.info(String.format("Looking for element under cursor %s(%d,%d)...", file, line, character)); @@ -119,7 +126,7 @@ public class CompileFile { * Figure out what imports this file should have. Star-imports like `import java.util.*` are converted to individual * class imports. Missing imports are inferred by looking at imports in other source files. */ - public FixImports fixImports() { + public List<String> fixImports() { // Check diagnostics for missing imports var unresolved = new HashSet<String>(); for (var d : parent.diags) { @@ -181,7 +188,11 @@ public class CompileFile { } // Add qualified names from fixes qualifiedNames.addAll(fixes.values()); - return new FixImports(root, trees.getSourcePositions(), qualifiedNames); + // Sort in alphabetical order + var sorted = new ArrayList<String>(); + sorted.addAll(qualifiedNames); + Collections.sort(sorted); + return sorted; } public Optional<URI> declaringFile(Element e) { diff --git a/src/main/java/org/javacs/FixImports.java b/src/main/java/org/javacs/FixImports.java deleted file mode 100644 index 751c756..0000000 --- a/src/main/java/org/javacs/FixImports.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.javacs; - -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.util.SourcePositions; -import java.util.Set; - -class FixImports { - final CompilationUnitTree parsed; - final SourcePositions sourcePositions; - final Set<String> fixedImports; - - FixImports(CompilationUnitTree parsed, SourcePositions sourcePositions, Set<String> fixedImports) { - this.parsed = parsed; - this.sourcePositions = sourcePositions; - this.fixedImports = fixedImports; - } -} diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 4048919..30b869a 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -586,8 +586,10 @@ class JavaLanguageServer extends LanguageServer { } else return Optional.empty(); } + // TODO change name private CompileFile hoverCache; + // TODO take only URI and invalidate based on version private void updateHoverCache(URI uri, String contents) { if (hoverCache == null || !hoverCache.file.equals(uri) || !hoverCache.contents.equals(contents)) { LOG.info("File has changed since last hover, recompiling"); @@ -1011,36 +1013,39 @@ class JavaLanguageServer extends LanguageServer { return unresolved; } - private List<TextEdit> fixImports(URI java) { - var contents = contents(java).content; - var fix = compiler.compileFile(java, contents).fixImports(); + @Override + public List<TextEdit> formatting(DocumentFormattingParams params) { + updateHoverCache(params.textDocument.uri, contents(params.textDocument.uri).content); // TODO if imports already match fixed-imports, return empty list // TODO preserve comments and other details of existing imports + var imports = hoverCache.fixImports(); + var pos = hoverCache.sourcePositions(); + var lines = hoverCache.root.getLineMap(); var edits = new ArrayList<TextEdit>(); // Delete all existing imports - for (var i : fix.parsed.getImports()) { + for (var i : hoverCache.root.getImports()) { if (!i.isStatic()) { - var offset = fix.sourcePositions.getStartPosition(fix.parsed, i); - var line = (int) fix.parsed.getLineMap().getLineNumber(offset) - 1; + var offset = pos.getStartPosition(hoverCache.root, i); + var line = (int) lines.getLineNumber(offset) - 1; var delete = new TextEdit(new Range(new Position(line, 0), new Position(line + 1, 0)), ""); edits.add(delete); } } - if (fix.fixedImports.isEmpty()) return edits; + if (imports.isEmpty()) return edits; // Find a place to insert the new imports long insertLine = -1; var insertText = new StringBuilder(); // If there are imports, use the start of the first import as the insert position - for (var i : fix.parsed.getImports()) { + for (var i : hoverCache.root.getImports()) { if (!i.isStatic() && insertLine == -1) { - long offset = fix.sourcePositions.getStartPosition(fix.parsed, i); - insertLine = fix.parsed.getLineMap().getLineNumber(offset) - 1; + long offset = pos.getStartPosition(hoverCache.root, i); + insertLine = lines.getLineNumber(offset) - 1; } } // If there are no imports, insert after the package declaration - if (insertLine == -1 && fix.parsed.getPackageName() != null) { - long offset = fix.sourcePositions.getEndPosition(fix.parsed, fix.parsed.getPackageName()); - insertLine = fix.parsed.getLineMap().getLineNumber(offset); + if (insertLine == -1 && hoverCache.root.getPackageName() != null) { + long offset = pos.getEndPosition(hoverCache.root, hoverCache.root.getPackageName()); + insertLine = lines.getLineNumber(offset); insertText.append("\n"); } // If there are no imports and no package, insert at the top of the file @@ -1048,22 +1053,14 @@ class JavaLanguageServer extends LanguageServer { insertLine = 0; } // Insert each import - fix.fixedImports - .stream() - .sorted() - .forEach( - i -> { - insertText.append("import ").append(i).append(";\n"); - }); + for (var i : imports) { + insertText.append("import ").append(i).append(";\n"); + } var insertPosition = new Position((int) insertLine, 0); var insert = new TextEdit(new Range(insertPosition, insertPosition), insertText.toString()); edits.add(insert); - return edits; - } - @Override - public List<TextEdit> formatting(DocumentFormattingParams params) { - return fixImports(params.textDocument.uri); + return edits; } @Override diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index b0f80e1..69d032b 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -312,7 +312,7 @@ public class JavaCompilerServiceTest { public void fixImports() { var uri = resourceUri("MissingImport.java"); var contents = contents("MissingImport.java"); - var qualifiedNames = compiler.compileFile(uri, contents).fixImports().fixedImports; + var qualifiedNames = compiler.compileFile(uri, contents).fixImports(); assertThat(qualifiedNames, hasItem("java.util.List")); } |