2021-07-26 12:16:47 -05:00
|
|
|
//! In-memory document information.
|
|
|
|
|
2021-07-26 13:18:22 -05:00
|
|
|
use std::mem;
|
|
|
|
|
2021-07-26 12:16:47 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use vfs::VfsPath;
|
|
|
|
|
|
|
|
/// Holds the set of in-memory documents.
|
|
|
|
///
|
|
|
|
/// For these document, there true contents is maintained by the client. It
|
|
|
|
/// might be different from what's on disk.
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub(crate) struct MemDocs {
|
|
|
|
mem_docs: FxHashMap<VfsPath, DocumentData>,
|
2021-07-26 13:18:22 -05:00
|
|
|
added_or_removed: bool,
|
2021-07-26 12:16:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemDocs {
|
|
|
|
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<(), ()> {
|
2021-07-26 13:18:22 -05:00
|
|
|
self.added_or_removed = true;
|
2021-07-26 12:16:47 -05:00
|
|
|
match self.mem_docs.insert(path, data) {
|
|
|
|
Some(_) => Err(()),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub(crate) fn remove(&mut self, path: &VfsPath) -> Result<(), ()> {
|
2021-07-26 13:18:22 -05:00
|
|
|
self.added_or_removed = true;
|
2021-07-26 12:16:47 -05:00
|
|
|
match self.mem_docs.remove(path) {
|
|
|
|
Some(_) => Ok(()),
|
|
|
|
None => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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> {
|
2021-07-26 13:18:22 -05:00
|
|
|
// NB: don't set `self.added_or_removed` here, as that purposefully only
|
|
|
|
// tracks changes to the key set.
|
2021-07-26 12:16:47 -05:00
|
|
|
self.mem_docs.get_mut(path)
|
|
|
|
}
|
|
|
|
pub(crate) fn iter(&self) -> impl Iterator<Item = &VfsPath> {
|
|
|
|
self.mem_docs.keys()
|
|
|
|
}
|
2021-07-26 13:18:22 -05:00
|
|
|
pub(crate) fn take_changes(&mut self) -> bool {
|
|
|
|
mem::replace(&mut self.added_or_removed, false)
|
|
|
|
}
|
2021-07-26 12:16:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about a document that the Language Client
|
|
|
|
/// knows about.
|
|
|
|
/// Its lifetime is driven by the textDocument/didOpen and textDocument/didClose
|
|
|
|
/// client notifications.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct DocumentData {
|
|
|
|
pub(crate) version: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentData {
|
|
|
|
pub(crate) fn new(version: i32) -> Self {
|
|
|
|
DocumentData { version }
|
|
|
|
}
|
|
|
|
}
|