rust_analyzer/
mem_docs.rs

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