rust_analyzer/
mem_docs.rs1use std::mem;
4
5use rustc_hash::FxHashMap;
6use vfs::VfsPath;
7
8#[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 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#[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}