diff options
author | George Fraser <george@fivetran.com> | 2019-01-06 13:21:44 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2019-01-06 13:21:44 -0800 |
commit | d2942ecf8b33af9bcf02e585f669b0c85c05e77b (patch) | |
tree | 9755ff777884a6210565290f59519972f220ccbb /src/main | |
parent | ae08ea7010b47a4210d30ae9e6e192c5d8a01a5e (diff) | |
download | java-language-server-d2942ecf8b33af9bcf02e585f669b0c85c05e77b.zip |
Read from FileStore
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/javacs/CompileBatch.java | 11 | ||||
-rw-r--r-- | src/main/java/org/javacs/Docs.java | 2 | ||||
-rw-r--r-- | src/main/java/org/javacs/FileManagerWrapper.java | 2 | ||||
-rw-r--r-- | src/main/java/org/javacs/FileStore.java | 27 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaCompilerService.java | 7 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 4 | ||||
-rw-r--r-- | src/main/java/org/javacs/Parser.java | 35 |
7 files changed, 44 insertions, 44 deletions
diff --git a/src/main/java/org/javacs/CompileBatch.java b/src/main/java/org/javacs/CompileBatch.java index d6b9bda..47d66fa 100644 --- a/src/main/java/org/javacs/CompileBatch.java +++ b/src/main/java/org/javacs/CompileBatch.java @@ -4,11 +4,8 @@ import com.sun.source.tree.*; import com.sun.source.util.*; import java.io.IOException; import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.*; import java.util.logging.Logger; -import java.util.stream.Collectors; import javax.lang.model.element.*; import javax.lang.model.type.TypeKind; import javax.lang.model.util.*; @@ -218,13 +215,7 @@ public class CompileBatch { public Optional<Range> range(TreePath path) { var uri = path.getCompilationUnit().getSourceFile().toUri(); - var file = Paths.get(uri); - String contents; - try { - contents = Files.readAllLines(file).stream().collect(Collectors.joining("\n")); - } catch (IOException e) { - throw new RuntimeException(e); - } + var contents = FileStore.contents(uri); return ParseFile.range(task, contents, path); } diff --git a/src/main/java/org/javacs/Docs.java b/src/main/java/org/javacs/Docs.java index 43b535d..10a0c95 100644 --- a/src/main/java/org/javacs/Docs.java +++ b/src/main/java/org/javacs/Docs.java @@ -45,6 +45,7 @@ public class Docs { // Find the file el was declared in var className = ptr.qualifiedClassName(); try { + // TODO should get current contents of open files from FileStore var fromSourcePath = fileManager.getJavaFileForInput( StandardLocation.SOURCE_PATH, className, JavaFileObject.Kind.SOURCE); @@ -55,6 +56,7 @@ public class Docs { for (var module : Classes.JDK_MODULES) { var moduleLocation = fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, module); if (moduleLocation == null) continue; + // TODO should get current contents of open files from FileStore var fromModuleSourcePath = fileManager.getJavaFileForInput(moduleLocation, className, JavaFileObject.Kind.SOURCE); if (fromModuleSourcePath != null) { diff --git a/src/main/java/org/javacs/FileManagerWrapper.java b/src/main/java/org/javacs/FileManagerWrapper.java index 8b765c0..43e9f41 100644 --- a/src/main/java/org/javacs/FileManagerWrapper.java +++ b/src/main/java/org/javacs/FileManagerWrapper.java @@ -20,6 +20,8 @@ import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.StandardLocation; +// TODO instead of forwarding to delegate, implement a from-scratch JavaFileManager that knows how to get open text from +// FileStore class FileManagerWrapper implements StandardJavaFileManager { private final StandardJavaFileManager delegate; diff --git a/src/main/java/org/javacs/FileStore.java b/src/main/java/org/javacs/FileStore.java index f3648d6..96b82a2 100644 --- a/src/main/java/org/javacs/FileStore.java +++ b/src/main/java/org/javacs/FileStore.java @@ -97,21 +97,30 @@ class FileStore { return activeDocuments.keySet(); } - static int version(URI openFile) { - if (!activeDocuments.containsKey(openFile)) return -1; - return activeDocuments.get(openFile).version; + static int version(URI file) { + if (!activeDocuments.containsKey(file)) return -1; + return activeDocuments.get(file).version; } - static String contents(URI openFile) { - if (!isJavaFile(openFile)) { - LOG.warning("Ignoring non-java file " + openFile); + static String contents(URI file) { + if (!isJavaFile(file)) { + LOG.warning("Ignoring non-java file " + file); return ""; } - if (activeDocuments.containsKey(openFile)) { - return activeDocuments.get(openFile).content; + if (activeDocuments.containsKey(file)) { + return activeDocuments.get(file).content; } try { - return Files.readAllLines(Paths.get(openFile)).stream().collect(Collectors.joining("\n")); + // TODO I think there is a faster path here + return Files.readAllLines(Paths.get(file)).stream().collect(Collectors.joining("\n")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + static BufferedReader lines(Path file) { + try { + return Files.newBufferedReader(file); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index e1ad2b8..b46fac5 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -124,6 +124,7 @@ 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); @@ -140,6 +141,7 @@ public class JavaCompilerService { // Construct list of sources 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); // Create task @@ -438,9 +440,8 @@ public class JavaCompilerService { private boolean containsTopLevelDeclaration(Path file, String simpleClassName) { var find = Pattern.compile("\\b(class|interface|enum) +" + simpleClassName + "\\b"); - try (var lines = Files.newBufferedReader(file)) { - var line = lines.readLine(); - while (line != null) { + try (var lines = FileStore.lines(file)) { + for (var line = lines.readLine(); line != null; line = lines.readLine()) { if (find.matcher(line).find()) return true; line = lines.readLine(); } diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 2a6ee1b..33a44e5 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -362,6 +362,8 @@ class JavaLanguageServer extends LanguageServer { } // Compile again, focusing on a region that depends on what type of completion we want to do 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); // Do a specific type of completion List<Completion> cs; @@ -750,6 +752,8 @@ class JavaLanguageServer extends LanguageServer { 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 help = focus.methodInvocation().map(this::asSignatureHelp); return help; diff --git a/src/main/java/org/javacs/Parser.java b/src/main/java/org/javacs/Parser.java index 62e7d10..b70b963 100644 --- a/src/main/java/org/javacs/Parser.java +++ b/src/main/java/org/javacs/Parser.java @@ -7,7 +7,6 @@ import java.net.URI; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; -import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import java.util.logging.Logger; @@ -38,6 +37,7 @@ class Parser { } static JavacTask parseTask(Path source) { + // TODO should get current contents of open files from FileStore JavaFileObject file = fileManager.getJavaFileObjectsFromFiles(Collections.singleton(source.toFile())).iterator().next(); return parseTask(file); @@ -239,11 +239,8 @@ class Parser { var importStar = Pattern.compile("^import +" + toPackage + "\\.\\*;"); var importStatic = Pattern.compile("^import +static +" + toPackage + "\\." + toClass); var startOfClass = Pattern.compile("^[\\w ]*class +\\w+"); - // TODO this needs to use open text if available - try (var read = Files.newBufferedReader(file)) { - while (true) { - var line = read.readLine(); - if (line == null) return false; + try (var lines = FileStore.lines(file)) { + for (var line = lines.readLine(); line != null; line = lines.readLine()) { if (startOfClass.matcher(line).find()) return false; if (samePackage.matcher(line).find()) return true; if (importClass.matcher(line).find()) return true; @@ -254,14 +251,14 @@ class Parser { } catch (IOException e) { throw new RuntimeException(e); } + return false; } static String packageName(Path file) { var packagePattern = Pattern.compile("^package +(.*);"); var startOfClass = Pattern.compile("^[\\w ]*class +\\w+"); - // TODO this needs to use open text if available - try (var read = Files.newBufferedReader(file)) { - for (var line = read.readLine(); line != null; line = read.readLine()) { + try (var lines = FileStore.lines(file)) { + for (var line = lines.readLine(); line != null; line = lines.readLine()) { if (startOfClass.matcher(line).find()) return ""; var matchPackage = packagePattern.matcher(line); if (matchPackage.matches()) { @@ -269,21 +266,19 @@ class Parser { return id; } } - return ""; } catch (IOException e) { throw new RuntimeException(e); } + return ""; } static Set<String> importsPackages(Path file) { var importStatic = Pattern.compile("^import +static +(.+);"); var importAny = Pattern.compile("^import +(.+);"); var startOfClass = Pattern.compile("^[\\w ]*class +\\w+"); - // TODO this needs to use open text if available - try (var read = Files.newBufferedReader(file)) { - var pkgs = new HashSet<String>(); - - for (var line = read.readLine(); line != null; line = read.readLine()) { + var pkgs = new HashSet<String>(); + try (var lines = FileStore.lines(file)) { + for (var line = lines.readLine(); line != null; line = lines.readLine()) { if (startOfClass.matcher(line).find()) break; var matchImportStatic = importStatic.matcher(line); if (matchImportStatic.matches()) { @@ -305,11 +300,10 @@ class Parser { pkgs.add(id); } } - - return pkgs; } catch (IOException e) { throw new RuntimeException(e); } + return pkgs; } /** Find all already-imported symbols in all .java files in workspace */ @@ -320,11 +314,8 @@ class Parser { var importStar = Pattern.compile("^import +([\\w\\.]+)\\.\\*;"); var importSimple = Pattern.compile("^import +(\\w+);"); for (var path : allJavaFiles) { - try (var lines = Files.newBufferedReader(path)) { - while (true) { - var line = lines.readLine(); - // If we reach the end of the file, stop looking for imports - if (line == null) break; + try (var lines = FileStore.lines(path)) { + for (var line = lines.readLine(); line != null; line = lines.readLine()) { // If we reach a class declaration, stop looking for imports // TODO This could be a little more specific if (line.contains("class")) break; |