diff options
author | George Fraser <george@fivetran.com> | 2017-05-06 11:01:48 -0400 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2017-05-06 11:01:48 -0400 |
commit | cc9325869823d2f8e32bfee79346e5441359739d (patch) | |
tree | 71ecf3d5f090dc8eaa9dcd6d090f558b95a9eaf0 | |
parent | add83cbaf4d3079c92eaecc139559a902df7ff3f (diff) | |
download | java-language-server-cc9325869823d2f8e32bfee79346e5441359739d.zip |
Pull rather than push javadocs
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 9 | ||||
-rw-r--r-- | src/main/java/org/javacs/Javadocs.java | 68 |
2 files changed, 35 insertions, 42 deletions
diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index e5ea13b..0610656 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -307,11 +307,6 @@ class JavaLanguageServer implements LanguageServer { public void didSave(DidSaveTextDocumentParams params) { // Re-lint all active documents doLint(activeDocuments.keySet()); - - // Re-index javadocs of saved document - URI uri = URI.create(params.getTextDocument().getUri()); - - activeContent(uri).ifPresent(content -> doJavadoc(new StringFileObject(content, uri))); } }; } @@ -368,10 +363,6 @@ class JavaLanguageServer implements LanguageServer { publishDiagnostics(paths, errors); } - private void doJavadoc(JavaFileObject source) { - docs().update(source); - } - /** * Text of file, if it is in the active set */ diff --git a/src/main/java/org/javacs/Javadocs.java b/src/main/java/org/javacs/Javadocs.java index 0f98f43..d9799de 100644 --- a/src/main/java/org/javacs/Javadocs.java +++ b/src/main/java/org/javacs/Javadocs.java @@ -9,6 +9,7 @@ import com.sun.source.util.JavacTask; import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javadoc.api.JavadocTool; +import java.time.Instant; import org.eclipse.lsp4j.CompletionItem; import javax.lang.model.element.Element; @@ -49,7 +50,17 @@ public class Javadocs { /** * All the classes we have indexed so far */ - private final Map<String, RootDoc> topLevelClasses = new ConcurrentHashMap<>(); + private final Map<String, IndexedDoc> topLevelClasses = new ConcurrentHashMap<>(); + + private static class IndexedDoc { + final RootDoc doc; + final Instant updated; + + IndexedDoc(RootDoc doc, Instant updated) { + this.doc = doc; + this.updated = updated; + } + } private final JavacTask task; @@ -229,53 +240,44 @@ public class Javadocs { return Optional.ofNullable(index.classNamed(className)); } - void update(JavaFileObject source) { - LOG.info("Update javadocs for " + source.toUri()); - - DocumentationTool.DocumentationTask task = new JavadocTool().getTask( - null, - emptyFileManager, - Javadocs::onDiagnostic, - Javadocs.class, - ImmutableList.of("-private"), - ImmutableList.of(source) - ); - - task.call(); + /** + * Get or compute the javadoc for `className` + */ + RootDoc index(String className) { + if (needsUpdate(className)) + topLevelClasses.put(className, doIndex(className)); - getSneakyReturn().ifPresent(root -> updateCache(root, source)); + return topLevelClasses.get(className).doc; } - private void updateCache(RootDoc root, JavaFileObject source) { - for (ClassDoc each : root.classes()) { - if (source.isNameCompatible(each.simpleTypeName(), JavaFileObject.Kind.SOURCE)) { - topLevelClasses.put(each.qualifiedName(), root); + private boolean needsUpdate(String className) { + if (!topLevelClasses.containsKey(className)) + return true; - return; - } - } - } + IndexedDoc indexed = topLevelClasses.get(className); + + try { + JavaFileObject fromDisk = actualFileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, className, JavaFileObject.Kind.SOURCE); + Instant modified = Instant.ofEpochMilli(fromDisk.getLastModified()); - /** - * Get or compute the javadoc for `className` - */ - RootDoc index(String className) { - return topLevelClasses.computeIfAbsent(className, this::doIndex); + return indexed.updated.isBefore(modified); + } catch (IOException e) { + throw new RuntimeException(); + } } /** * Read all the Javadoc for `className` */ - private RootDoc doIndex(String className) { + private IndexedDoc doIndex(String className) { try { JavaFileObject fromDisk = actualFileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, className, JavaFileObject.Kind.SOURCE); if (fromDisk == null) { LOG.warning("No source file for " + className); - return EmptyRootDoc.INSTANCE; + return new IndexedDoc(EmptyRootDoc.INSTANCE, Instant.EPOCH); } - JavaFileObject source = activeFile(fromDisk); LOG.info("Found " + source.toUri() + " for " + className); @@ -290,11 +292,11 @@ public class Javadocs { ); task.call(); + + return new IndexedDoc(getSneakyReturn().orElse(EmptyRootDoc.INSTANCE), Instant.ofEpochMilli(fromDisk.getLastModified())); } catch (IOException e) { throw new RuntimeException(); } - - return getSneakyReturn().orElse(EmptyRootDoc.INSTANCE); } private JavaFileObject activeFile(JavaFileObject file) { |