diff options
author | George Fraser <george@fivetran.com> | 2019-01-06 12:53:17 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2019-01-06 12:53:17 -0800 |
commit | ae08ea7010b47a4210d30ae9e6e192c5d8a01a5e (patch) | |
tree | 74b09ebec02e8d9ddd1eb63d2e3db83ac753058c /src | |
parent | 7cee5e9b47fd88a089044067993e68cd8c740a63 (diff) | |
download | java-language-server-ae08ea7010b47a4210d30ae9e6e192c5d8a01a5e.zip |
Cache modified in FileStore
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/javacs/Cache.java | 9 | ||||
-rw-r--r-- | src/main/java/org/javacs/FileStore.java | 34 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 3 |
3 files changed, 38 insertions, 8 deletions
diff --git a/src/main/java/org/javacs/Cache.java b/src/main/java/org/javacs/Cache.java index 139d1e1..f08270f 100644 --- a/src/main/java/org/javacs/Cache.java +++ b/src/main/java/org/javacs/Cache.java @@ -1,7 +1,5 @@ package org.javacs; -import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.time.Instant; import java.util.HashMap; @@ -50,12 +48,7 @@ class Cache<K, V> { // If key was loaded before file was last modified, it needs to be reloaded var value = map.get(key); - Instant modified; - try { - modified = Files.getLastModifiedTime(file).toInstant(); - } catch (IOException e) { - throw new RuntimeException(e); - } + var modified = FileStore.modified(file); return value.created.isBefore(modified); } diff --git a/src/main/java/org/javacs/FileStore.java b/src/main/java/org/javacs/FileStore.java index 8f60e25..f3648d6 100644 --- a/src/main/java/org/javacs/FileStore.java +++ b/src/main/java/org/javacs/FileStore.java @@ -6,7 +6,9 @@ import java.io.StringReader; import java.io.StringWriter; import java.net.URI; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -20,6 +22,38 @@ import org.javacs.lsp.TextDocumentContentChangeEvent; class FileStore { private static final Map<URI, VersionedContent> activeDocuments = new HashMap<>(); + private static final Map<Path, Instant> modified = new HashMap<>(); + + static Instant modified(Path file) { + // If we've never checked before, look up modified time on disk + if (!modified.containsKey(file)) { + readModifiedFromDisk(file); + } + + // Look up modified time from cache + return modified.get(file); + } + + static void externalCreate(Path file) { + readModifiedFromDisk(file); + } + + static void externalChange(Path file) { + readModifiedFromDisk(file); + } + + static void externalDelete(Path file) { + modified.remove(file); + } + + private static void readModifiedFromDisk(Path file) { + try { + var time = Files.getLastModifiedTime(file).toInstant(); + modified.put(file, time); + } catch (IOException e) { + throw new RuntimeException(e); + } + } static boolean isJavaFile(URI uri) { return uri.getScheme().equals("file") && uri.getPath().endsWith(".java"); diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 32d4bdf..2a6ee1b 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -268,12 +268,15 @@ class JavaLanguageServer extends LanguageServer { var file = Paths.get(c.uri); switch (c.type) { case FileChangeType.Created: + FileStore.externalCreate(file); created.add(file); break; case FileChangeType.Changed: + FileStore.externalChange(file); if (sourcePath.update(file)) changed = true; break; case FileChangeType.Deleted: + FileStore.externalDelete(file); deleted.add(file); break; } |