diff options
author | George Fraser <george@fivetran.com> | 2019-01-08 20:05:17 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2019-01-08 20:05:17 -0800 |
commit | 07a4b8b44b492d18cd759022395eb3a593f42f75 (patch) | |
tree | f8d9eb874d686ed5961791a8e0c1c0e6b55b35da | |
parent | 727bd8341afc689c842385a234647b8f7b22de21 (diff) | |
download | java-language-server-07a4b8b44b492d18cd759022395eb3a593f42f75.zip |
Reduce passing of contents
-rw-r--r-- | src/main/java/org/javacs/CompileFile.java | 4 | ||||
-rw-r--r-- | src/main/java/org/javacs/CompileFocus.java | 8 | ||||
-rw-r--r-- | src/main/java/org/javacs/Docs.java | 8 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaCompilerService.java | 13 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 15 | ||||
-rw-r--r-- | src/main/java/org/javacs/ParseFile.java | 28 | ||||
-rw-r--r-- | src/main/java/org/javacs/Pruner.java | 8 | ||||
-rw-r--r-- | src/test/java/org/javacs/BenchmarkPruner.java | 7 | ||||
-rw-r--r-- | src/test/java/org/javacs/CodeLensTest.java | 3 | ||||
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerServiceTest.java | 49 | ||||
-rw-r--r-- | src/test/java/org/javacs/PrunerTest.java | 10 | ||||
-rw-r--r-- | src/test/java/org/javacs/PtrTest.java | 3 |
12 files changed, 67 insertions, 89 deletions
diff --git a/src/main/java/org/javacs/CompileFile.java b/src/main/java/org/javacs/CompileFile.java index 3ae5313..aa38001 100644 --- a/src/main/java/org/javacs/CompileFile.java +++ b/src/main/java/org/javacs/CompileFile.java @@ -23,10 +23,10 @@ public class CompileFile { private final Trees trees; public final CompilationUnitTree root; - CompileFile(JavaCompilerService parent, URI file, String contents) { + CompileFile(JavaCompilerService parent, URI file) { this.parent = parent; this.file = file; - this.contents = contents; + this.contents = FileStore.contents(file); this.task = CompileFocus.singleFileTask(parent, file, contents); this.trees = Trees.instance(task); var profiler = new Profiler(); diff --git a/src/main/java/org/javacs/CompileFocus.java b/src/main/java/org/javacs/CompileFocus.java index 882cbf1..d7634e8 100644 --- a/src/main/java/org/javacs/CompileFocus.java +++ b/src/main/java/org/javacs/CompileFocus.java @@ -29,10 +29,10 @@ public class CompileFocus { private final CompilationUnitTree root; private final TreePath path; - CompileFocus(JavaCompilerService parent, URI file, String contents, int line, int character) { + CompileFocus(JavaCompilerService parent, URI file, int line, int character) { this.parent = parent; this.file = file; - this.contents = Pruner.prune(file, contents, line, character); + this.contents = Pruner.prune(file, line, character); this.line = line; this.character = character; this.task = singleFileTask(parent, file, this.contents); @@ -647,7 +647,7 @@ public class CompileFocus { LOG.info(String.format("...found %d locals", locals.size())); // Add static imports - var staticImports = staticImports(file, contents, partialName); + var staticImports = staticImports(file, partialName); for (var m : staticImports) { result.add(Completion.ofElement(m)); } @@ -724,7 +724,7 @@ public class CompileFocus { return result; } - private List<Element> staticImports(URI file, String contents, String partialName) { + private List<Element> staticImports(URI file, String partialName) { var result = new ArrayList<Element>(); for (var i : root.getImports()) { if (!i.isStatic()) continue; diff --git a/src/main/java/org/javacs/Docs.java b/src/main/java/org/javacs/Docs.java index b25c0f7..fa9181a 100644 --- a/src/main/java/org/javacs/Docs.java +++ b/src/main/java/org/javacs/Docs.java @@ -82,13 +82,7 @@ public class Docs { } catch (IOException e) { throw new RuntimeException(e); } - String contents; - try { - contents = file.getCharContent(true).toString(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return new ParseFile(file.toUri(), contents, task, root); + return new ParseFile(task, root); } private static final Pattern HTML_TAG = Pattern.compile("<(\\w+)>"); diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index 757722f..8379922 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -77,16 +77,16 @@ public class JavaCompilerService { return docs; } - public ParseFile parseFile(URI file, String contents) { - return new ParseFile(this, file, contents); + public ParseFile parseFile(URI file) { + return new ParseFile(this, file); } - public CompileFocus compileFocus(URI file, String contents, int line, int character) { - return new CompileFocus(this, file, contents, line, character); + public CompileFocus compileFocus(URI file, int line, int character) { + return new CompileFocus(this, file, line, character); } - public CompileFile compileFile(URI file, String contents) { - return new CompileFile(this, file, contents); + public CompileFile compileFile(URI file) { + return new CompileFile(this, file); } public CompileBatch compileBatch(Collection<URI> uris) { @@ -96,7 +96,6 @@ public class JavaCompilerService { public CompileBatch compileBatch(Collection<URI> uris, ReportProgress progress) { var files = new ArrayList<File>(); for (var p : uris) files.add(new File(p)); - // TODO should get current contents of open files from FileStore var sources = fileManager.getJavaFileObjectsFromFiles(files); var list = new ArrayList<JavaFileObject>(); for (var s : sources) list.add(s); diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 175e4ce..65d95dc 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -332,11 +332,10 @@ class JavaLanguageServer extends LanguageServer { var started = Instant.now(); var uri = position.textDocument.uri; if (!FileStore.isJavaFile(uri)) return Optional.empty(); - var content = FileStore.contents(uri); var line = position.position.line + 1; var column = position.position.character + 1; // Figure out what kind of completion we want to do - var maybeCtx = compiler.parseFile(uri, content).completionContext(line, column); + var maybeCtx = compiler.parseFile(uri).completionContext(line, column); // TODO don't complete inside of comments if (!maybeCtx.isPresent()) { var items = new ArrayList<CompletionItem>(); @@ -353,7 +352,7 @@ class JavaLanguageServer extends LanguageServer { var ctx = maybeCtx.get(); // TODO CompileFocus should have a "patch" mechanism where we recompile the current file without creating a new // task - var focus = compiler.compileFocus(uri, content, ctx.line, ctx.character); + var focus = compiler.compileFocus(uri, ctx.line, ctx.character); // Do a specific type of completion List<Completion> cs; boolean isIncomplete; @@ -624,7 +623,7 @@ class JavaLanguageServer extends LanguageServer { || !activeFileCache.file.equals(uri) || activeFileCacheVersion != FileStore.version(uri)) { LOG.info("Recompile active file..."); - activeFileCache = compiler.compileFile(uri, FileStore.contents(uri)); + activeFileCache = compiler.compileFile(uri); activeFileCacheVersion = FileStore.version(uri); } } @@ -738,12 +737,11 @@ class JavaLanguageServer extends LanguageServer { public Optional<SignatureHelp> signatureHelp(TextDocumentPositionParams position) { var uri = position.textDocument.uri; if (!FileStore.isJavaFile(uri)) return Optional.empty(); - var content = FileStore.contents(uri); var line = position.position.line + 1; var column = position.position.character + 1; // TODO CompileFocus should have a "patch" mechanism where we recompile the current file without creating a new // task - var focus = compiler.compileFocus(uri, content, line, column); + var focus = compiler.compileFocus(uri, line, column); var help = focus.methodInvocation().map(this::asSignatureHelp); return help; } @@ -835,8 +833,7 @@ class JavaLanguageServer extends LanguageServer { if (name.equals("<init>")) name = el.getEnclosingElement().getSimpleName().toString(); var sources = new ArrayList<JavaFileObject>(); for (var f : files) { - var contents = FileStore.contents(f); - var pruned = Pruner.prune(f, contents, name); + var pruned = Pruner.prune(f, name); sources.add(new SourceFileObject(f, pruned)); } return sources; @@ -858,7 +855,7 @@ class JavaLanguageServer extends LanguageServer { private void updateCachedParse(URI file) { if (file.equals(cacheParseFile) && FileStore.version(file) == cacheParseVersion) return; LOG.info(String.format("Updating cached parse file to %s", file)); - cacheParse = compiler.parseFile(file, FileStore.contents(file)); + cacheParse = compiler.parseFile(file); cacheParseFile = file; cacheParseVersion = FileStore.version(file); } diff --git a/src/main/java/org/javacs/ParseFile.java b/src/main/java/org/javacs/ParseFile.java index 0015416..b37a3b6 100644 --- a/src/main/java/org/javacs/ParseFile.java +++ b/src/main/java/org/javacs/ParseFile.java @@ -14,19 +14,16 @@ import javax.lang.model.element.*; import org.javacs.lsp.*; public class ParseFile { - private final URI file; private final String contents; private final JavacTask task; private final Trees trees; private final CompilationUnitTree root; - ParseFile(JavaCompilerService parent, URI file, String contents) { + ParseFile(JavaCompilerService parent, URI file) { Objects.requireNonNull(parent); Objects.requireNonNull(file); - Objects.requireNonNull(contents); - this.file = file; - this.contents = contents; + this.contents = FileStore.contents(file); this.task = CompileFocus.singleFileTask(parent, file, contents); this.trees = Trees.instance(task); var profiler = new Profiler(); @@ -39,14 +36,15 @@ public class ParseFile { profiler.print(); } - ParseFile(URI file, String contents, JavacTask task, CompilationUnitTree root) { - Objects.requireNonNull(file); - Objects.requireNonNull(contents); + ParseFile(JavacTask task, CompilationUnitTree root) { Objects.requireNonNull(task); Objects.requireNonNull(root); - this.file = file; - this.contents = contents; + try { + this.contents = root.getSourceFile().getCharContent(true).toString(); + } catch (IOException e) { + throw new RuntimeException(e); + } this.task = task; this.trees = Trees.instance(task); this.root = root; @@ -134,7 +132,10 @@ public class ParseFile { } public Optional<CompletionContext> completionContext(int line, int character) { - LOG.info(String.format("Finding completion position near %s(%d,%d)...", file, line, character)); + LOG.info( + String.format( + "Finding completion position near %s(%d,%d)...", + root.getSourceFile().toUri(), line, character)); var pos = trees.getSourcePositions(); var lines = root.getLineMap(); @@ -323,7 +324,9 @@ public class ParseFile { /** Find and source code associated with a ptr */ public Optional<TreePath> fuzzyFind(Ptr ptr) { - LOG.info(String.format("...find fuzzy match of %s in %s ...", ptr, Parser.fileName(file))); + LOG.info( + String.format( + "...find fuzzy match of %s in %s ...", ptr, Parser.fileName(root.getSourceFile().toUri()))); class FindPtr extends TreePathScanner<Void, Void> { int bestMatch = Ptr.NOT_MATCHED; @@ -378,7 +381,6 @@ public class ParseFile { return doc; } - // TODO get rid of this and expose SourcePositions static Optional<Range> range(JavacTask task, String contents, TreePath path) { // Find start position var trees = Trees.instance(task); diff --git a/src/main/java/org/javacs/Pruner.java b/src/main/java/org/javacs/Pruner.java index bda547d..390bf32 100644 --- a/src/main/java/org/javacs/Pruner.java +++ b/src/main/java/org/javacs/Pruner.java @@ -107,9 +107,9 @@ class Pruner { return buffer.toString(); } - // TODO can get rid of contents now that SourceFileObject references FileStore - static String prune(URI file, String contents, int line, int character) { + static String prune(URI file, int line, int character) { // Parse file + var contents = FileStore.contents(file); var task = Parser.parseTask(new SourceFileObject(file, contents)); CompilationUnitTree root; try { @@ -125,9 +125,9 @@ class Pruner { return prune(root, pos, buffer, new long[] {cursor}); } - // TODO can get rid of contents now that SourceFileObject references FileStore - static String prune(URI file, String contents, String name) { + static String prune(URI file, String name) { // Find all occurrences of name in contents + var contents = FileStore.contents(file); var list = new ArrayList<Long>(); var pattern = Pattern.compile("\\b" + Pattern.quote(name) + "\\b"); var matcher = pattern.matcher(contents); diff --git a/src/test/java/org/javacs/BenchmarkPruner.java b/src/test/java/org/javacs/BenchmarkPruner.java index 9ad432a..56922a5 100644 --- a/src/test/java/org/javacs/BenchmarkPruner.java +++ b/src/test/java/org/javacs/BenchmarkPruner.java @@ -24,11 +24,12 @@ public class BenchmarkPruner { while (it.hasNext()) { var file = it.next(); if (!Files.isRegularFile(file)) continue; - var contents = String.join("\n", Files.readAllLines(file)); if (prune) { - contents = Pruner.prune(file.toUri(), contents, "isWord"); + var contents = Pruner.prune(file.toUri(), "isWord"); + files.add(new SourceFileObject(file, contents)); + } else { + files.add(new SourceFileObject(file)); } - files.add(new SourceFileObject(file, contents)); } return files; } catch (IOException e) { diff --git a/src/test/java/org/javacs/CodeLensTest.java b/src/test/java/org/javacs/CodeLensTest.java index 61b8095..48f8803 100644 --- a/src/test/java/org/javacs/CodeLensTest.java +++ b/src/test/java/org/javacs/CodeLensTest.java @@ -76,8 +76,7 @@ public class CodeLensTest { public void signatureMatches() { var file = "/org/javacs/example/ConstructorRefs.java"; var uri = FindResource.uri(file); - var contents = FindResource.contents(file); - var compile = server.compiler.compileFile(uri, contents); + var compile = server.compiler.compileFile(uri); var signatureMatches = compile.signatureMatches(); var good = List.of(new Ptr("org.javacs.example/ConstructorRefs#<init>(int)")); diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index dde1569..9b6c2ee 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -53,8 +53,7 @@ public class JavaCompilerServiceTest { @Test public void element() { var uri = resourceUri("HelloWorld.java"); - var contents = contents("HelloWorld.java"); - var found = compiler.compileFocus(uri, contents, 3, 24).element(); + var found = compiler.compileFocus(uri, 3, 24).element(); assertThat(found.getSimpleName(), hasToString(containsString("println"))); } @@ -62,8 +61,7 @@ public class JavaCompilerServiceTest { @Test public void elementWithError() { var uri = resourceUri("CompleteMembers.java"); - var contents = contents("CompleteMembers.java"); - var found = compiler.compileFocus(uri, contents, 3, 12).element(); + var found = compiler.compileFocus(uri, 3, 12).element(); assertThat(found, notNullValue()); } @@ -87,8 +85,7 @@ public class JavaCompilerServiceTest { @Test public void identifiers() { var uri = resourceUri("CompleteIdentifiers.java"); - var contents = contents("CompleteIdentifiers.java"); - var focus = compiler.compileFocus(uri, contents, 13, 21); + var focus = compiler.compileFocus(uri, 13, 21); var found = focus.scopeMembers("complete"); var names = elementNames(found); assertThat(names, hasItem("completeLocal")); @@ -105,8 +102,7 @@ public class JavaCompilerServiceTest { @Test public void identifiersInMiddle() { var uri = resourceUri("CompleteInMiddle.java"); - var contents = contents("CompleteInMiddle.java"); - var focus = compiler.compileFocus(uri, contents, 13, 21); + var focus = compiler.compileFocus(uri, 13, 21); var found = focus.scopeMembers("complete"); var names = elementNames(found); assertThat(names, hasItem("completeLocal")); @@ -123,9 +119,8 @@ public class JavaCompilerServiceTest { @Test public void completeIdentifiers() { var uri = resourceUri("CompleteIdentifiers.java"); - var contents = contents("CompleteIdentifiers.java"); - var ctx = compiler.parseFile(uri, contents).completionContext(13, 21).get(); - var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character); + var ctx = compiler.parseFile(uri).completionContext(13, 21).get(); + var focus = compiler.compileFocus(uri, ctx.line, ctx.character); var found = focus.completeIdentifiers(ctx.inClass, ctx.inMethod, ctx.partialName); var names = completionNames(found); assertThat(names, hasItem("completeLocal")); @@ -142,8 +137,7 @@ public class JavaCompilerServiceTest { @Test public void members() { var uri = resourceUri("CompleteMembers.java"); - var contents = contents("CompleteMembers.java"); - var focus = compiler.compileFocus(uri, contents, 3, 14); + var focus = compiler.compileFocus(uri, 3, 14); var found = focus.completeMembers(false); var names = completionNames(found); assertThat(names, hasItem("subMethod")); @@ -154,9 +148,8 @@ public class JavaCompilerServiceTest { @Test public void completeMembers() { var uri = resourceUri("CompleteMembers.java"); - var contents = contents("CompleteMembers.java"); - var ctx = compiler.parseFile(uri, contents).completionContext(3, 15).get(); - var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character); + var ctx = compiler.parseFile(uri).completionContext(3, 15).get(); + var focus = compiler.compileFocus(uri, ctx.line, ctx.character); var found = focus.completeMembers(false); var names = completionNames(found); assertThat(names, hasItem("subMethod")); @@ -167,9 +160,8 @@ public class JavaCompilerServiceTest { @Test public void completeExpression() { var uri = resourceUri("CompleteExpression.java"); - var contents = contents("CompleteExpression.java"); - var ctx = compiler.parseFile(uri, contents).completionContext(3, 37).get(); - var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character); + var ctx = compiler.parseFile(uri).completionContext(3, 37).get(); + var focus = compiler.compileFocus(uri, ctx.line, ctx.character); var found = focus.completeMembers(false); var names = completionNames(found); assertThat(names, hasItem("instanceMethod")); @@ -180,9 +172,8 @@ public class JavaCompilerServiceTest { @Test public void completeClass() { var uri = resourceUri("CompleteClass.java"); - var contents = contents("CompleteClass.java"); - var ctx = compiler.parseFile(uri, contents).completionContext(3, 23).get(); - var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character); + var ctx = compiler.parseFile(uri).completionContext(3, 23).get(); + var focus = compiler.compileFocus(uri, ctx.line, ctx.character); var found = focus.completeMembers(false); var names = completionNames(found); assertThat(names, hasItems("staticMethod", "staticField")); @@ -194,9 +185,8 @@ public class JavaCompilerServiceTest { @Test public void completeImports() { var uri = resourceUri("CompleteImports.java"); - var contents = contents("CompleteImports.java"); - var ctx = compiler.parseFile(uri, contents).completionContext(1, 18).get(); - var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character); + var ctx = compiler.parseFile(uri).completionContext(1, 18).get(); + var focus = compiler.compileFocus(uri, ctx.line, ctx.character); var found = focus.completeMembers(false); var names = completionNames(found); assertThat(names, hasItem("List")); @@ -206,8 +196,7 @@ public class JavaCompilerServiceTest { @Test public void overloads() { var uri = resourceUri("Overloads.java"); - var contents = contents("Overloads.java"); - var found = compiler.compileFocus(uri, contents, 3, 15).methodInvocation().get(); + var found = compiler.compileFocus(uri, 3, 15).methodInvocation().get(); var strings = found.overloads.stream().map(Object::toString).collect(Collectors.toList()); assertThat(strings, hasItem(containsString("print(int)"))); @@ -259,8 +248,7 @@ public class JavaCompilerServiceTest { @Test public void localDoc() { var uri = resourceUri("LocalMethodDoc.java"); - var contents = contents("LocalMethodDoc.java"); - var method = compiler.compileFocus(uri, contents, 3, 21).methodInvocation().get().activeMethod.get(); + var method = compiler.compileFocus(uri, 3, 21).methodInvocation().get().activeMethod.get(); var ptr = new Ptr(method); var file = compiler.docs().find(ptr).get(); var parse = compiler.docs().parse(file); @@ -272,8 +260,7 @@ public class JavaCompilerServiceTest { @Test public void fixImports() { var uri = resourceUri("MissingImport.java"); - var contents = contents("MissingImport.java"); - var qualifiedNames = compiler.compileFile(uri, contents).fixImports(); + var qualifiedNames = compiler.compileFile(uri).fixImports(); assertThat(qualifiedNames, hasItem("java.util.List")); } diff --git a/src/test/java/org/javacs/PrunerTest.java b/src/test/java/org/javacs/PrunerTest.java index 9d649c2..3b6f017 100644 --- a/src/test/java/org/javacs/PrunerTest.java +++ b/src/test/java/org/javacs/PrunerTest.java @@ -10,35 +10,35 @@ public class PrunerTest { @Test public void pruneMethods() { - var actual = Pruner.prune(resourceUri("PruneMethods.java"), contents("PruneMethods.java"), 6, 19); + var actual = Pruner.prune(resourceUri("PruneMethods.java"), 6, 19); var expected = contents("PruneMethods_erased.java"); assertThat(actual, equalToIgnoringWhiteSpace(expected)); } @Test public void pruneToEndOfBlock() { - var actual = Pruner.prune(resourceUri("PruneToEndOfBlock.java"), contents("PruneToEndOfBlock.java"), 4, 18); + var actual = Pruner.prune(resourceUri("PruneToEndOfBlock.java"), 4, 18); var expected = contents("PruneToEndOfBlock_erased.java"); assertThat(actual, equalToIgnoringWhiteSpace(expected)); } @Test public void pruneMiddle() { - var actual = Pruner.prune(resourceUri("PruneMiddle.java"), contents("PruneMiddle.java"), 4, 12); + var actual = Pruner.prune(resourceUri("PruneMiddle.java"), 4, 12); var expected = contents("PruneMiddle_erased.java"); assertThat(actual, equalToIgnoringWhiteSpace(expected)); } @Test public void pruneDot() { - var actual = Pruner.prune(resourceUri("PruneDot.java"), contents("PruneDot.java"), 3, 11); + var actual = Pruner.prune(resourceUri("PruneDot.java"), 3, 11); var expected = contents("PruneDot_erased.java"); assertThat(actual, equalToIgnoringWhiteSpace(expected)); } @Test public void pruneWords() { - var actual = Pruner.prune(resourceUri("PruneWords.java"), contents("PruneWords.java"), "word"); + var actual = Pruner.prune(resourceUri("PruneWords.java"), "word"); var expected = contents("PruneWords_erased.java"); assertThat(actual, equalToIgnoringWhiteSpace(expected)); } diff --git a/src/test/java/org/javacs/PtrTest.java b/src/test/java/org/javacs/PtrTest.java index 8dcf110..379df59 100644 --- a/src/test/java/org/javacs/PtrTest.java +++ b/src/test/java/org/javacs/PtrTest.java @@ -11,8 +11,7 @@ public class PtrTest { static JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer(); static String file = "/org/javacs/example/Ptrs.java"; static URI uri = FindResource.uri(file); - static String contents = FindResource.contents(file); - static CompileFile compile = server.compiler.compileFile(uri, contents); + static CompileFile compile = server.compiler.compileFile(uri); @Test public void classPtr() { |