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 /src/main | |
parent | 727bd8341afc689c842385a234647b8f7b22de21 (diff) | |
download | java-language-server-07a4b8b44b492d18cd759022395eb3a593f42f75.zip |
Reduce passing of contents
Diffstat (limited to 'src/main')
-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 |
7 files changed, 38 insertions, 46 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); |