Skip to content

Commit 4106792

Browse files
committed
internal: prepare to track changes to mem_docs
1 parent f0db648 commit 4106792

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

crates/rust-analyzer/src/document.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/rust-analyzer/src/global_state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{sync::Arc, time::Instant};
88
use crossbeam_channel::{unbounded, Receiver, Sender};
99
use flycheck::FlycheckHandle;
1010
use ide::{Analysis, AnalysisHost, Cancellable, Change, FileId};
11-
use ide_db::base_db::{CrateId, VfsPath};
11+
use ide_db::base_db::CrateId;
1212
use lsp_types::{SemanticTokens, Url};
1313
use parking_lot::{Mutex, RwLock};
1414
use project_model::{
@@ -20,11 +20,11 @@ use vfs::AnchoredPathBuf;
2020
use crate::{
2121
config::Config,
2222
diagnostics::{CheckFixes, DiagnosticCollection},
23-
document::DocumentData,
2423
from_proto,
2524
line_index::{LineEndings, LineIndex},
2625
lsp_ext,
2726
main_loop::Task,
27+
mem_docs::MemDocs,
2828
op_queue::OpQueue,
2929
reload::SourceRootConfig,
3030
request_metrics::{LatestRequests, RequestMetrics},
@@ -57,7 +57,7 @@ pub(crate) struct GlobalState {
5757
pub(crate) config: Arc<Config>,
5858
pub(crate) analysis_host: AnalysisHost,
5959
pub(crate) diagnostics: DiagnosticCollection,
60-
pub(crate) mem_docs: FxHashMap<VfsPath, DocumentData>,
60+
pub(crate) mem_docs: MemDocs,
6161
pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
6262
pub(crate) shutdown_requested: bool,
6363
pub(crate) last_reported_status: Option<lsp_ext::ServerStatusParams>,
@@ -115,7 +115,7 @@ pub(crate) struct GlobalStateSnapshot {
115115
pub(crate) analysis: Analysis,
116116
pub(crate) check_fixes: CheckFixes,
117117
pub(crate) latest_requests: Arc<RwLock<LatestRequests>>,
118-
mem_docs: FxHashMap<VfsPath, DocumentData>,
118+
mem_docs: MemDocs,
119119
pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
120120
vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
121121
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
@@ -147,7 +147,7 @@ impl GlobalState {
147147
config: Arc::new(config.clone()),
148148
analysis_host,
149149
diagnostics: Default::default(),
150-
mem_docs: FxHashMap::default(),
150+
mem_docs: MemDocs::default(),
151151
semantic_tokens_cache: Arc::new(Default::default()),
152152
shutdown_requested: false,
153153
last_reported_status: None,

crates/rust-analyzer/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mod line_index;
3333
mod request_metrics;
3434
mod lsp_utils;
3535
mod thread_pool;
36-
mod document;
36+
mod mem_docs;
3737
mod diff;
3838
mod op_queue;
3939
pub mod lsp_ext;

crates/rust-analyzer/src/main_loop.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use vfs::ChangeKind;
1717
use crate::{
1818
config::Config,
1919
dispatch::{NotificationDispatcher, RequestDispatcher},
20-
document::DocumentData,
2120
from_proto,
2221
global_state::{file_id_to_url, url_to_file_id, GlobalState},
2322
handlers, lsp_ext,
2423
lsp_utils::{apply_document_changes, is_cancelled, notification_is, Progress},
24+
mem_docs::DocumentData,
2525
reload::{BuildDataProgress, ProjectWorkspaceProgress},
2626
Result,
2727
};
@@ -305,7 +305,7 @@ impl GlobalState {
305305
let vfs = &mut self.vfs.write().0;
306306
for (path, contents) in files {
307307
let path = VfsPath::from(path);
308-
if !self.mem_docs.contains_key(&path) {
308+
if !self.mem_docs.contains(&path) {
309309
vfs.set_file_contents(path, contents);
310310
}
311311
}
@@ -582,7 +582,7 @@ impl GlobalState {
582582
if this
583583
.mem_docs
584584
.insert(path.clone(), DocumentData::new(params.text_document.version))
585-
.is_some()
585+
.is_err()
586586
{
587587
log::error!("duplicate DidOpenTextDocument: {}", path)
588588
}
@@ -628,7 +628,7 @@ impl GlobalState {
628628
})?
629629
.on::<lsp_types::notification::DidCloseTextDocument>(|this, params| {
630630
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
631-
if this.mem_docs.remove(&path).is_none() {
631+
if this.mem_docs.remove(&path).is_err() {
632632
log::error!("orphan DidCloseTextDocument: {}", path);
633633
}
634634

@@ -719,7 +719,7 @@ impl GlobalState {
719719
fn maybe_update_diagnostics(&mut self) {
720720
let subscriptions = self
721721
.mem_docs
722-
.keys()
722+
.iter()
723723
.map(|path| self.vfs.read().0.file_id(path).unwrap())
724724
.collect::<Vec<_>>();
725725

crates/rust-analyzer/src/mem_docs.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

crates/rust-analyzer/src/reload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl GlobalState {
403403
let mut load = |path: &AbsPath| {
404404
let _p = profile::span("GlobalState::load");
405405
let vfs_path = vfs::VfsPath::from(path.to_path_buf());
406-
if !mem_docs.contains_key(&vfs_path) {
406+
if !mem_docs.contains(&vfs_path) {
407407
let contents = loader.handle.load_sync(path);
408408
vfs.set_file_contents(vfs_path.clone(), contents);
409409
}

0 commit comments

Comments
 (0)