Skip to content

Commit a09a00a

Browse files
bors[bot]kjeremy
andauthored
Merge #5520
5520: Add DocumentData to represent in-memory document with LSP info r=matklad a=kjeremy At the moment this only holds document version information but in the near-future it will hold other things like semantic token delta info. Co-authored-by: kjeremy <[email protected]>
2 parents 18172eb + 48da2d4 commit a09a00a

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! In-memory document information.
2+
3+
/// Information about a document that the Language Client
4+
// knows about.
5+
// Its lifetime is driven by the textDocument/didOpen and textDocument/didClose
6+
// client notifications.
7+
#[derive(Debug, Clone)]
8+
pub(crate) struct DocumentData {
9+
pub version: Option<i64>,
10+
}
11+
12+
impl DocumentData {
13+
pub fn new(version: i64) -> Self {
14+
DocumentData { version: Some(version) }
15+
}
16+
}

crates/rust-analyzer/src/global_state.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_hash::FxHashMap;
1717
use crate::{
1818
config::Config,
1919
diagnostics::{CheckFixes, DiagnosticCollection},
20+
document::DocumentData,
2021
from_proto,
2122
line_endings::LineEndings,
2223
main_loop::Task,
@@ -69,7 +70,7 @@ pub(crate) struct GlobalState {
6970
pub(crate) config: Config,
7071
pub(crate) analysis_host: AnalysisHost,
7172
pub(crate) diagnostics: DiagnosticCollection,
72-
pub(crate) mem_docs: FxHashMap<VfsPath, Option<i64>>,
73+
pub(crate) mem_docs: FxHashMap<VfsPath, DocumentData>,
7374
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
7475
pub(crate) status: Status,
7576
pub(crate) source_root_config: SourceRootConfig,
@@ -84,7 +85,7 @@ pub(crate) struct GlobalStateSnapshot {
8485
pub(crate) analysis: Analysis,
8586
pub(crate) check_fixes: CheckFixes,
8687
pub(crate) latest_requests: Arc<RwLock<LatestRequests>>,
87-
mem_docs: FxHashMap<VfsPath, Option<i64>>,
88+
mem_docs: FxHashMap<VfsPath, DocumentData>,
8889
vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
8990
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
9091
}
@@ -259,7 +260,7 @@ impl GlobalStateSnapshot {
259260

260261
pub(crate) fn url_file_version(&self, url: &Url) -> Option<i64> {
261262
let path = from_proto::vfs_path(&url).ok()?;
262-
self.mem_docs.get(&path).copied()?
263+
self.mem_docs.get(&path)?.version
263264
}
264265

265266
pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url {

crates/rust-analyzer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod line_endings;
3333
mod request_metrics;
3434
mod lsp_utils;
3535
mod thread_pool;
36+
mod document;
3637
pub mod lsp_ext;
3738
pub mod config;
3839

crates/rust-analyzer/src/main_loop.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ra_prof::profile;
1515
use crate::{
1616
config::Config,
1717
dispatch::{NotificationDispatcher, RequestDispatcher},
18+
document::DocumentData,
1819
from_proto,
1920
global_state::{file_id_to_url, url_to_file_id, GlobalState, Status},
2021
handlers, lsp_ext,
@@ -311,7 +312,7 @@ impl GlobalState {
311312
let url = file_id_to_url(&self.vfs.read().0, file_id);
312313
let diagnostics = self.diagnostics.diagnostics_for(file_id).cloned().collect();
313314
let version = from_proto::vfs_path(&url)
314-
.map(|path| self.mem_docs.get(&path).copied().flatten())
315+
.map(|path| self.mem_docs.get(&path)?.version)
315316
.unwrap_or_default();
316317

317318
self.send_notification::<lsp_types::notification::PublishDiagnostics>(
@@ -406,7 +407,7 @@ impl GlobalState {
406407
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
407408
if this
408409
.mem_docs
409-
.insert(path.clone(), Some(params.text_document.version))
410+
.insert(path.clone(), DocumentData::new(params.text_document.version))
410411
.is_some()
411412
{
412413
log::error!("duplicate DidOpenTextDocument: {}", path)
@@ -428,7 +429,7 @@ impl GlobalState {
428429

429430
// The version passed in DidChangeTextDocument is the version after all edits are applied
430431
// so we should apply it before the vfs is notified.
431-
*doc = params.text_document.version;
432+
doc.version = params.text_document.version;
432433

433434
vfs.set_file_contents(path.clone(), Some(text.into_bytes()));
434435
}
@@ -438,7 +439,7 @@ impl GlobalState {
438439
let mut version = None;
439440
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
440441
match this.mem_docs.remove(&path) {
441-
Some(entry) => version = entry,
442+
Some(doc) => version = doc.version,
442443
None => log::error!("orphan DidCloseTextDocument: {}", path),
443444
}
444445

0 commit comments

Comments
 (0)