]> git.lizzy.rs Git - rust.git/blob - 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
1 //! In-memory document information.
2
3 use std::mem;
4
5 use rustc_hash::FxHashMap;
6 use vfs::VfsPath;
7
8 /// Holds the set of in-memory documents.
9 ///
10 /// For these document, there true contents is maintained by the client. It
11 /// might be different from what's on disk.
12 #[derive(Default, Clone)]
13 pub(crate) struct MemDocs {
14     mem_docs: FxHashMap<VfsPath, DocumentData>,
15     added_or_removed: bool,
16 }
17
18 impl MemDocs {
19     pub(crate) fn contains(&self, path: &VfsPath) -> bool {
20         self.mem_docs.contains_key(path)
21     }
22     pub(crate) fn insert(&mut self, path: VfsPath, data: DocumentData) -> Result<(), ()> {
23         self.added_or_removed = true;
24         match self.mem_docs.insert(path, data) {
25             Some(_) => Err(()),
26             None => Ok(()),
27         }
28     }
29     pub(crate) fn remove(&mut self, path: &VfsPath) -> Result<(), ()> {
30         self.added_or_removed = true;
31         match self.mem_docs.remove(path) {
32             Some(_) => Ok(()),
33             None => Err(()),
34         }
35     }
36     pub(crate) fn get(&self, path: &VfsPath) -> Option<&DocumentData> {
37         self.mem_docs.get(path)
38     }
39     pub(crate) fn get_mut(&mut self, path: &VfsPath) -> Option<&mut DocumentData> {
40         // NB: don't set `self.added_or_removed` here, as that purposefully only
41         // tracks changes to the key set.
42         self.mem_docs.get_mut(path)
43     }
44     pub(crate) fn iter(&self) -> impl Iterator<Item = &VfsPath> {
45         self.mem_docs.keys()
46     }
47     pub(crate) fn take_changes(&mut self) -> bool {
48         mem::replace(&mut self.added_or_removed, false)
49     }
50 }
51
52 /// Information about a document that the Language Client
53 /// knows about.
54 /// Its lifetime is driven by the textDocument/didOpen and textDocument/didClose
55 /// client notifications.
56 #[derive(Debug, Clone)]
57 pub(crate) struct DocumentData {
58     pub(crate) version: i32,
59 }
60
61 impl DocumentData {
62     pub(crate) fn new(version: i32) -> Self {
63         DocumentData { version }
64     }
65 }