From a6a2bd74b2621e1789898511498a5e2032a3b5d4 Mon Sep 17 00:00:00 2001 From: George Fraser Date: Tue, 2 Apr 2019 00:08:11 -0700 Subject: Cleanup --- src/main/java/org/javacs/JavaLanguageServer.java | 55 ++++++++++---------- src/main/java/org/javacs/Parser.java | 66 ------------------------ 2 files changed, 26 insertions(+), 95 deletions(-) diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index f6d8d8b..26db0a1 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -616,20 +616,18 @@ class JavaLanguageServer extends LanguageServer { return Optional.of(md); } - private CompileBatch activeFileCache; - private URI activeFileCacheFile = URI.create("file:///NONE"); - private int activeFileCacheVersion = -1; + private CompileBatch cacheCompile; + private URI cacheCompileFile = URI.create("file:///NONE"); + private int cacheCompileVersion = -1; // TODO take only URI and invalidate based on version private void updateActiveFile(URI uri) { - if (activeFileCache == null - || !activeFileCacheFile.equals(uri) - || activeFileCacheVersion != FileStore.version(uri)) { + if (cacheCompile == null || !cacheCompileFile.equals(uri) || cacheCompileVersion != FileStore.version(uri)) { LOG.info("Recompile active file..."); - if (activeFileCache != null) activeFileCache.close(); - activeFileCache = compiler.compileFile(uri); - activeFileCacheFile = uri; - activeFileCacheVersion = FileStore.version(uri); + if (cacheCompile != null) cacheCompile.close(); + cacheCompile = compiler.compileFile(uri); + cacheCompileFile = uri; + cacheCompileVersion = FileStore.version(uri); } } @@ -643,7 +641,7 @@ class JavaLanguageServer extends LanguageServer { // Find element undeer cursor var line = position.position.line + 1; var column = position.position.character + 1; - var el = activeFileCache.element(uri, line, column); + var el = cacheCompile.element(uri, line, column); if (!el.isPresent()) return Optional.empty(); var result = new ArrayList(); @@ -764,7 +762,7 @@ class JavaLanguageServer extends LanguageServer { // Compile from-file and identify element under cursor LOG.info(String.format("Go-to-def at %s:%d...", fromUri, fromLine)); updateActiveFile(fromUri); - var toEl = activeFileCache.element(fromUri, fromLine, fromColumn); + var toEl = cacheCompile.element(fromUri, fromLine, fromColumn); if (!toEl.isPresent()) { LOG.info(String.format("...no element at cursor")); return Optional.empty(); @@ -805,7 +803,7 @@ class JavaLanguageServer extends LanguageServer { // Compile from-file and identify element under cursor LOG.warning(String.format("Looking for references to %s(%d,%d)...", toUri.getPath(), toLine, toColumn)); updateActiveFile(toUri); - var toEl = activeFileCache.element(toUri, toLine, toColumn); + var toEl = cacheCompile.element(toUri, toLine, toColumn); if (!toEl.isPresent()) { LOG.warning("...no element under cursor"); return Optional.empty(); @@ -1057,7 +1055,7 @@ class JavaLanguageServer extends LanguageServer { updateActiveFile(toUri); // Find the element we want to count references to - var toEl = activeFileCache.element(toUri, toLine, toColumn); + var toEl = cacheCompile.element(toUri, toLine, toColumn); if (!toEl.isPresent()) { LOG.warning("...no element at code lens"); return -1; @@ -1065,7 +1063,7 @@ class JavaLanguageServer extends LanguageServer { var toPtr = new Ptr(toEl.get()); // Find the signature of the target file - var declarations = activeFileCache.declarations(toUri); + var declarations = cacheCompile.declarations(toUri); var signature = new HashSet(); for (var el : declarations) { signature.add(new Ptr(el)); @@ -1096,7 +1094,7 @@ class JavaLanguageServer extends LanguageServer { } // Always update active file - var activeIndex = activeFileCache.index(toUri, declarations); + var activeIndex = cacheCompile.index(toUri, declarations); var count = activeIndex.count(toPtr); // Count up references out of index @@ -1182,14 +1180,14 @@ class JavaLanguageServer extends LanguageServer { private List fixImports() { // TODO if imports already match fixed-imports, return empty list // TODO preserve comments and other details of existing imports - var imports = activeFileCache.fixImports(activeFileCacheFile); - var pos = activeFileCache.sourcePositions(); - var lines = activeFileCache.lineMap(activeFileCacheFile); + var imports = cacheCompile.fixImports(cacheCompileFile); + var pos = cacheCompile.sourcePositions(); + var lines = cacheCompile.lineMap(cacheCompileFile); var edits = new ArrayList(); // Delete all existing imports - for (var i : activeFileCache.imports(activeFileCacheFile)) { + for (var i : cacheCompile.imports(cacheCompileFile)) { if (!i.isStatic()) { - var offset = pos.getStartPosition(activeFileCache.root(activeFileCacheFile), i); + var offset = pos.getStartPosition(cacheCompile.root(cacheCompileFile), 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); @@ -1200,18 +1198,17 @@ class JavaLanguageServer extends LanguageServer { 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 : activeFileCache.imports(activeFileCacheFile)) { + for (var i : cacheCompile.imports(cacheCompileFile)) { if (!i.isStatic() && insertLine == -1) { - long offset = pos.getStartPosition(activeFileCache.root(activeFileCacheFile), i); + long offset = pos.getStartPosition(cacheCompile.root(cacheCompileFile), i); insertLine = lines.getLineNumber(offset) - 1; } } // If there are no imports, insert after the package declaration - if (insertLine == -1 && activeFileCache.root(activeFileCacheFile).getPackageName() != null) { + if (insertLine == -1 && cacheCompile.root(cacheCompileFile).getPackageName() != null) { long offset = pos.getEndPosition( - activeFileCache.root(activeFileCacheFile), - activeFileCache.root(activeFileCacheFile).getPackageName()); + cacheCompile.root(cacheCompileFile), cacheCompile.root(cacheCompileFile).getPackageName()); insertLine = lines.getLineNumber(offset); insertText.append("\n"); } @@ -1232,9 +1229,9 @@ class JavaLanguageServer extends LanguageServer { private List addOverrides() { var edits = new ArrayList(); - var methods = activeFileCache.needsOverrideAnnotation(activeFileCacheFile); - var pos = activeFileCache.sourcePositions(); - var lines = activeFileCache.lineMap(activeFileCacheFile); + var methods = cacheCompile.needsOverrideAnnotation(cacheCompileFile); + var pos = cacheCompile.sourcePositions(); + var lines = cacheCompile.lineMap(cacheCompileFile); for (var t : methods) { var methodStart = pos.getStartPosition(t.getCompilationUnit(), t.getLeaf()); var insertLine = lines.getLineNumber(methodStart); diff --git a/src/main/java/org/javacs/Parser.java b/src/main/java/org/javacs/Parser.java index b6d07c1..8c5852d 100644 --- a/src/main/java/org/javacs/Parser.java +++ b/src/main/java/org/javacs/Parser.java @@ -136,21 +136,6 @@ class Parser { } } - static boolean containsText(Path java, String query) { - var search = new StringSearch(query); - try (var channel = FileChannel.open(java)) { - // Read up to 1 MB of data from file - var limit = Math.min((int) channel.size(), SEARCH_BUFFER.capacity()); - SEARCH_BUFFER.position(0); - SEARCH_BUFFER.limit(limit); - channel.read(SEARCH_BUFFER); - SEARCH_BUFFER.position(0); - return search.next(SEARCH_BUFFER) != -1; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - static boolean containsWord(Path java, String query) { var search = new StringSearch(query); try (var channel = FileChannel.open(java)) { @@ -166,21 +151,6 @@ class Parser { } } - static boolean containsPattern(Path java, Pattern pattern) { - try (var channel = FileChannel.open(java)) { - // Read up to 1 MB of data from file - var limit = Math.min((int) channel.size(), SEARCH_BUFFER.capacity()); - SEARCH_BUFFER.position(0); - SEARCH_BUFFER.limit(limit); - channel.read(SEARCH_BUFFER); - SEARCH_BUFFER.position(0); - var chars = Charset.forName("UTF-8").decode(SEARCH_BUFFER); - return pattern.matcher(chars).find(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - static List findSymbolsMatching(CompilationUnitTree parse, String query) { class Find extends TreePathScanner { List found = new ArrayList<>(); @@ -273,40 +243,6 @@ class Parser { return ""; } - static Set importsPackages(Path file) { - var importStatic = Pattern.compile("^import +static +(.+);"); - var importAny = Pattern.compile("^import +(.+);"); - var startOfClass = Pattern.compile("^[\\w ]*class +\\w+"); - var pkgs = new HashSet(); - 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()) { - var id = matchImportStatic.group(1); - var pkg = new StringJoiner("."); - for (var part : id.split("\\.")) { - var firstChar = part.charAt(0); - if (Character.isUpperCase(firstChar) || firstChar == '*') break; - pkg.add(part); - } - pkgs.add(pkg.toString()); - continue; - } - var matchImportAny = importAny.matcher(line); - if (matchImportAny.matches()) { - var id = matchImportAny.group(1); - var lastDot = id.lastIndexOf("."); - if (lastDot != -1) id = id.substring(0, lastDot); - pkgs.add(id); - } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return pkgs; - } - /** Find all already-imported symbols in all .java files in workspace */ static ExistingImports existingImports(Collection allJavaFiles) { var classes = new HashSet(); @@ -380,7 +316,6 @@ class Parser { return leaf.toString(); } - // TODO does this really belong in Parser? private static Optional resolveSymbol(String unresolved, ExistingImports imports, Set classPath) { // Try to disambiguate by looking for exact matches // For example, Foo is exactly matched by `import com.bar.Foo` @@ -433,7 +368,6 @@ class Parser { return candidates.stream().filter(c -> c.startsWith("java.")).sorted(order).findFirst(); } - // TODO does this really belong in Parser? static Map resolveSymbols( Set unresolvedSymbols, ExistingImports imports, Set classPath) { var result = new HashMap(); -- cgit v1.2.3