]> git.lizzy.rs Git - rust.git/blobdiff - crates/rust-analyzer/src/mem_docs.rs
fix: correctly update diagnostics when files are opened and closed
[rust.git] / crates / rust-analyzer / src / mem_docs.rs
index 8989d7d9e44e10eef3154317361aef9a1065978c..f86a0f66ad8d6b6de7b0339ca7fdf06a882d907d 100644 (file)
@@ -1,5 +1,7 @@
 //! In-memory document information.
 
+use std::mem;
+
 use rustc_hash::FxHashMap;
 use vfs::VfsPath;
 
@@ -10,6 +12,7 @@
 #[derive(Default, Clone)]
 pub(crate) struct MemDocs {
     mem_docs: FxHashMap<VfsPath, DocumentData>,
+    added_or_removed: bool,
 }
 
 impl MemDocs {
@@ -17,12 +20,14 @@ pub(crate) fn contains(&self, path: &VfsPath) -> bool {
         self.mem_docs.contains_key(path)
     }
     pub(crate) fn insert(&mut self, path: VfsPath, data: DocumentData) -> Result<(), ()> {
+        self.added_or_removed = true;
         match self.mem_docs.insert(path, data) {
             Some(_) => Err(()),
             None => Ok(()),
         }
     }
     pub(crate) fn remove(&mut self, path: &VfsPath) -> Result<(), ()> {
+        self.added_or_removed = true;
         match self.mem_docs.remove(path) {
             Some(_) => Ok(()),
             None => Err(()),
@@ -32,12 +37,16 @@ pub(crate) fn get(&self, path: &VfsPath) -> Option<&DocumentData> {
         self.mem_docs.get(path)
     }
     pub(crate) fn get_mut(&mut self, path: &VfsPath) -> Option<&mut DocumentData> {
+        // NB: don't set `self.added_or_removed` here, as that purposefully only
+        // tracks changes to the key set.
         self.mem_docs.get_mut(path)
     }
-
     pub(crate) fn iter(&self) -> impl Iterator<Item = &VfsPath> {
         self.mem_docs.keys()
     }
+    pub(crate) fn take_changes(&mut self) -> bool {
+        mem::replace(&mut self.added_or_removed, false)
+    }
 }
 
 /// Information about a document that the Language Client