Skip to content

Commit 4172c18

Browse files
committed
Reduce passing of contents
1 parent fdd4143 commit 4172c18

12 files changed

Lines changed: 67 additions & 89 deletions

‎src/main/java/org/javacs/CompileFile.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public class CompileFile {
2323
private final Trees trees;
2424
public final CompilationUnitTree root;
2525

26-
CompileFile(JavaCompilerService parent, URI file, String contents) {
26+
CompileFile(JavaCompilerService parent, URI file) {
2727
this.parent = parent;
2828
this.file = file;
29-
this.contents = contents;
29+
this.contents = FileStore.contents(file);
3030
this.task = CompileFocus.singleFileTask(parent, file, contents);
3131
this.trees = Trees.instance(task);
3232
var profiler = new Profiler();

‎src/main/java/org/javacs/CompileFocus.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public class CompileFocus {
2929
private final CompilationUnitTree root;
3030
private final TreePath path;
3131

32-
CompileFocus(JavaCompilerService parent, URI file, String contents, int line, int character) {
32+
CompileFocus(JavaCompilerService parent, URI file, int line, int character) {
3333
this.parent = parent;
3434
this.file = file;
35-
this.contents = Pruner.prune(file, contents, line, character);
35+
this.contents = Pruner.prune(file, line, character);
3636
this.line = line;
3737
this.character = character;
3838
this.task = singleFileTask(parent, file, this.contents);
@@ -647,7 +647,7 @@ private void completeScopeIdentifiers(String partialName, List<Completion> resul
647647
LOG.info(String.format("...found %d locals", locals.size()));
648648

649649
// Add static imports
650-
var staticImports = staticImports(file, contents, partialName);
650+
var staticImports = staticImports(file, partialName);
651651
for (var m : staticImports) {
652652
result.add(Completion.ofElement(m));
653653
}
@@ -724,7 +724,7 @@ private List<Completion> accessibleClasses(Path file, String partialName, String
724724
return result;
725725
}
726726

727-
private List<Element> staticImports(URI file, String contents, String partialName) {
727+
private List<Element> staticImports(URI file, String partialName) {
728728
var result = new ArrayList<Element>();
729729
for (var i : root.getImports()) {
730730
if (!i.isStatic()) continue;

‎src/main/java/org/javacs/Docs.java‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,7 @@ public ParseFile parse(JavaFileObject file) {
8282
} catch (IOException e) {
8383
throw new RuntimeException(e);
8484
}
85-
String contents;
86-
try {
87-
contents = file.getCharContent(true).toString();
88-
} catch (IOException e) {
89-
throw new RuntimeException(e);
90-
}
91-
return new ParseFile(file.toUri(), contents, task, root);
85+
return new ParseFile(task, root);
9286
}
9387

9488
private static final Pattern HTML_TAG = Pattern.compile("<(\\w+)>");

‎src/main/java/org/javacs/JavaCompilerService.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ public Docs docs() {
7777
return docs;
7878
}
7979

80-
public ParseFile parseFile(URI file, String contents) {
81-
return new ParseFile(this, file, contents);
80+
public ParseFile parseFile(URI file) {
81+
return new ParseFile(this, file);
8282
}
8383

84-
public CompileFocus compileFocus(URI file, String contents, int line, int character) {
85-
return new CompileFocus(this, file, contents, line, character);
84+
public CompileFocus compileFocus(URI file, int line, int character) {
85+
return new CompileFocus(this, file, line, character);
8686
}
8787

88-
public CompileFile compileFile(URI file, String contents) {
89-
return new CompileFile(this, file, contents);
88+
public CompileFile compileFile(URI file) {
89+
return new CompileFile(this, file);
9090
}
9191

9292
public CompileBatch compileBatch(Collection<URI> uris) {
@@ -96,7 +96,6 @@ public CompileBatch compileBatch(Collection<URI> uris) {
9696
public CompileBatch compileBatch(Collection<URI> uris, ReportProgress progress) {
9797
var files = new ArrayList<File>();
9898
for (var p : uris) files.add(new File(p));
99-
// TODO should get current contents of open files from FileStore
10099
var sources = fileManager.getJavaFileObjectsFromFiles(files);
101100
var list = new ArrayList<JavaFileObject>();
102101
for (var s : sources) list.add(s);

‎src/main/java/org/javacs/JavaLanguageServer.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,10 @@ public Optional<CompletionList> completion(TextDocumentPositionParams position)
332332
var started = Instant.now();
333333
var uri = position.textDocument.uri;
334334
if (!FileStore.isJavaFile(uri)) return Optional.empty();
335-
var content = FileStore.contents(uri);
336335
var line = position.position.line + 1;
337336
var column = position.position.character + 1;
338337
// Figure out what kind of completion we want to do
339-
var maybeCtx = compiler.parseFile(uri, content).completionContext(line, column);
338+
var maybeCtx = compiler.parseFile(uri).completionContext(line, column);
340339
// TODO don't complete inside of comments
341340
if (!maybeCtx.isPresent()) {
342341
var items = new ArrayList<CompletionItem>();
@@ -353,7 +352,7 @@ public Optional<CompletionList> completion(TextDocumentPositionParams position)
353352
var ctx = maybeCtx.get();
354353
// TODO CompileFocus should have a "patch" mechanism where we recompile the current file without creating a new
355354
// task
356-
var focus = compiler.compileFocus(uri, content, ctx.line, ctx.character);
355+
var focus = compiler.compileFocus(uri, ctx.line, ctx.character);
357356
// Do a specific type of completion
358357
List<Completion> cs;
359358
boolean isIncomplete;
@@ -624,7 +623,7 @@ private void updateActiveFile(URI uri) {
624623
|| !activeFileCache.file.equals(uri)
625624
|| activeFileCacheVersion != FileStore.version(uri)) {
626625
LOG.info("Recompile active file...");
627-
activeFileCache = compiler.compileFile(uri, FileStore.contents(uri));
626+
activeFileCache = compiler.compileFile(uri);
628627
activeFileCacheVersion = FileStore.version(uri);
629628
}
630629
}
@@ -738,12 +737,11 @@ private SignatureHelp asSignatureHelp(MethodInvocation invoke) {
738737
public Optional<SignatureHelp> signatureHelp(TextDocumentPositionParams position) {
739738
var uri = position.textDocument.uri;
740739
if (!FileStore.isJavaFile(uri)) return Optional.empty();
741-
var content = FileStore.contents(uri);
742740
var line = position.position.line + 1;
743741
var column = position.position.character + 1;
744742
// TODO CompileFocus should have a "patch" mechanism where we recompile the current file without creating a new
745743
// task
746-
var focus = compiler.compileFocus(uri, content, line, column);
744+
var focus = compiler.compileFocus(uri, line, column);
747745
var help = focus.methodInvocation().map(this::asSignatureHelp);
748746
return help;
749747
}
@@ -835,8 +833,7 @@ private List<JavaFileObject> pruneWord(Collection<URI> files, Element el) {
835833
if (name.equals("<init>")) name = el.getEnclosingElement().getSimpleName().toString();
836834
var sources = new ArrayList<JavaFileObject>();
837835
for (var f : files) {
838-
var contents = FileStore.contents(f);
839-
var pruned = Pruner.prune(f, contents, name);
836+
var pruned = Pruner.prune(f, name);
840837
sources.add(new SourceFileObject(f, pruned));
841838
}
842839
return sources;
@@ -858,7 +855,7 @@ private List<JavaFileObject> latestText(Collection<URI> files) {
858855
private void updateCachedParse(URI file) {
859856
if (file.equals(cacheParseFile) && FileStore.version(file) == cacheParseVersion) return;
860857
LOG.info(String.format("Updating cached parse file to %s", file));
861-
cacheParse = compiler.parseFile(file, FileStore.contents(file));
858+
cacheParse = compiler.parseFile(file);
862859
cacheParseFile = file;
863860
cacheParseVersion = FileStore.version(file);
864861
}

‎src/main/java/org/javacs/ParseFile.java‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@
1414
import org.javacs.lsp.*;
1515

1616
public class ParseFile {
17-
private final URI file;
1817
private final String contents;
1918
private final JavacTask task;
2019
private final Trees trees;
2120
private final CompilationUnitTree root;
2221

23-
ParseFile(JavaCompilerService parent, URI file, String contents) {
22+
ParseFile(JavaCompilerService parent, URI file) {
2423
Objects.requireNonNull(parent);
2524
Objects.requireNonNull(file);
26-
Objects.requireNonNull(contents);
2725

28-
this.file = file;
29-
this.contents = contents;
26+
this.contents = FileStore.contents(file);
3027
this.task = CompileFocus.singleFileTask(parent, file, contents);
3128
this.trees = Trees.instance(task);
3229
var profiler = new Profiler();
@@ -39,14 +36,15 @@ public class ParseFile {
3936
profiler.print();
4037
}
4138

42-
ParseFile(URI file, String contents, JavacTask task, CompilationUnitTree root) {
43-
Objects.requireNonNull(file);
44-
Objects.requireNonNull(contents);
39+
ParseFile(JavacTask task, CompilationUnitTree root) {
4540
Objects.requireNonNull(task);
4641
Objects.requireNonNull(root);
4742

48-
this.file = file;
49-
this.contents = contents;
43+
try {
44+
this.contents = root.getSourceFile().getCharContent(true).toString();
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
5048
this.task = task;
5149
this.trees = Trees.instance(task);
5250
this.root = root;
@@ -134,7 +132,10 @@ public Optional<Range> range(TreePath path) {
134132
}
135133

136134
public Optional<CompletionContext> completionContext(int line, int character) {
137-
LOG.info(String.format("Finding completion position near %s(%d,%d)...", file, line, character));
135+
LOG.info(
136+
String.format(
137+
"Finding completion position near %s(%d,%d)...",
138+
root.getSourceFile().toUri(), line, character));
138139

139140
var pos = trees.getSourcePositions();
140141
var lines = root.getLineMap();
@@ -323,7 +324,9 @@ public SourcePositions sourcePositions() {
323324

324325
/** Find and source code associated with a ptr */
325326
public Optional<TreePath> fuzzyFind(Ptr ptr) {
326-
LOG.info(String.format("...find fuzzy match of %s in %s ...", ptr, Parser.fileName(file)));
327+
LOG.info(
328+
String.format(
329+
"...find fuzzy match of %s in %s ...", ptr, Parser.fileName(root.getSourceFile().toUri())));
327330

328331
class FindPtr extends TreePathScanner<Void, Void> {
329332
int bestMatch = Ptr.NOT_MATCHED;
@@ -378,7 +381,6 @@ public DocCommentTree doc(TreePath path) {
378381
return doc;
379382
}
380383

381-
// TODO get rid of this and expose SourcePositions
382384
static Optional<Range> range(JavacTask task, String contents, TreePath path) {
383385
// Find start position
384386
var trees = Trees.instance(task);

‎src/main/java/org/javacs/Pruner.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public Void visitErroneous(ErroneousTree node, Void nothing) {
107107
return buffer.toString();
108108
}
109109

110-
// TODO can get rid of contents now that SourceFileObject references FileStore
111-
static String prune(URI file, String contents, int line, int character) {
110+
static String prune(URI file, int line, int character) {
112111
// Parse file
112+
var contents = FileStore.contents(file);
113113
var task = Parser.parseTask(new SourceFileObject(file, contents));
114114
CompilationUnitTree root;
115115
try {
@@ -125,9 +125,9 @@ static String prune(URI file, String contents, int line, int character) {
125125
return prune(root, pos, buffer, new long[] {cursor});
126126
}
127127

128-
// TODO can get rid of contents now that SourceFileObject references FileStore
129-
static String prune(URI file, String contents, String name) {
128+
static String prune(URI file, String name) {
130129
// Find all occurrences of name in contents
130+
var contents = FileStore.contents(file);
131131
var list = new ArrayList<Long>();
132132
var pattern = Pattern.compile("\\b" + Pattern.quote(name) + "\\b");
133133
var matcher = pattern.matcher(contents);

‎src/test/java/org/javacs/BenchmarkPruner.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ private static List<SourceFileObject> files(boolean prune) {
2424
while (it.hasNext()) {
2525
var file = it.next();
2626
if (!Files.isRegularFile(file)) continue;
27-
var contents = String.join("\n", Files.readAllLines(file));
2827
if (prune) {
29-
contents = Pruner.prune(file.toUri(), contents, "isWord");
28+
var contents = Pruner.prune(file.toUri(), "isWord");
29+
files.add(new SourceFileObject(file, contents));
30+
} else {
31+
files.add(new SourceFileObject(file));
3032
}
31-
files.add(new SourceFileObject(file, contents));
3233
}
3334
return files;
3435
} catch (IOException e) {

‎src/test/java/org/javacs/CodeLensTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public void enumConstants() {
7676
public void signatureMatches() {
7777
var file = "/org/javacs/example/ConstructorRefs.java";
7878
var uri = FindResource.uri(file);
79-
var contents = FindResource.contents(file);
80-
var compile = server.compiler.compileFile(uri, contents);
79+
var compile = server.compiler.compileFile(uri);
8180
var signatureMatches = compile.signatureMatches();
8281

8382
var good = List.of(new Ptr("org.javacs.example/ConstructorRefs#<init>(int)"));

0 commit comments

Comments
 (0)