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