aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkjeremy <[email protected]>2020-07-24 14:01:48 +0100
committerkjeremy <[email protected]>2020-07-24 14:02:57 +0100
commit48da2d4c16a05bf0559c864d2b3f1b832d1fec85 (patch)
tree83d2512711ead30e30e2ca93aaf57a3a577676c3
parent0e5095d3cac11d4b569c6e1594bd07937556c812 (diff)
Add DocumentData to represent in-memory document with LSP info
-rw-r--r--crates/rust-analyzer/src/document.rs16
-rw-r--r--crates/rust-analyzer/src/global_state.rs7
-rw-r--r--crates/rust-analyzer/src/lib.rs1
-rw-r--r--crates/rust-analyzer/src/main_loop.rs9
4 files changed, 26 insertions, 7 deletions
diff --git a/crates/rust-analyzer/src/document.rs b/crates/rust-analyzer/src/document.rs
new file mode 100644
index 000000000..43219e633
--- /dev/null
+++ b/crates/rust-analyzer/src/document.rs
@@ -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)]
8pub(crate) struct DocumentData {
9 pub version: Option<i64>,
10}
11
12impl DocumentData {
13 pub fn new(version: i64) -> Self {
14 DocumentData { version: Some(version) }
15 }
16}
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 80937dbc4..b2d65a6d1 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -17,6 +17,7 @@ use rustc_hash::FxHashMap;
17use crate::{ 17use crate::{
18 config::Config, 18 config::Config,
19 diagnostics::{CheckFixes, DiagnosticCollection}, 19 diagnostics::{CheckFixes, DiagnosticCollection},
20 document::DocumentData,
20 from_proto, 21 from_proto,
21 line_endings::LineEndings, 22 line_endings::LineEndings,
22 main_loop::Task, 23 main_loop::Task,
@@ -69,7 +70,7 @@ pub(crate) struct GlobalState {
69 pub(crate) config: Config, 70 pub(crate) config: Config,
70 pub(crate) analysis_host: AnalysisHost, 71 pub(crate) analysis_host: AnalysisHost,
71 pub(crate) diagnostics: DiagnosticCollection, 72 pub(crate) diagnostics: DiagnosticCollection,
72 pub(crate) mem_docs: FxHashMap<VfsPath, Option<i64>>, 73 pub(crate) mem_docs: FxHashMap<VfsPath, DocumentData>,
73 pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>, 74 pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
74 pub(crate) status: Status, 75 pub(crate) status: Status,
75 pub(crate) source_root_config: SourceRootConfig, 76 pub(crate) source_root_config: SourceRootConfig,
@@ -84,7 +85,7 @@ pub(crate) struct GlobalStateSnapshot {
84 pub(crate) analysis: Analysis, 85 pub(crate) analysis: Analysis,
85 pub(crate) check_fixes: CheckFixes, 86 pub(crate) check_fixes: CheckFixes,
86 pub(crate) latest_requests: Arc<RwLock<LatestRequests>>, 87 pub(crate) latest_requests: Arc<RwLock<LatestRequests>>,
87 mem_docs: FxHashMap<VfsPath, Option<i64>>, 88 mem_docs: FxHashMap<VfsPath, DocumentData>,
88 vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>, 89 vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
89 pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>, 90 pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
90} 91}
@@ -259,7 +260,7 @@ impl GlobalStateSnapshot {
259 260
260 pub(crate) fn url_file_version(&self, url: &Url) -> Option<i64> { 261 pub(crate) fn url_file_version(&self, url: &Url) -> Option<i64> {
261 let path = from_proto::vfs_path(&url).ok()?; 262 let path = from_proto::vfs_path(&url).ok()?;
262 self.mem_docs.get(&path).copied()? 263 self.mem_docs.get(&path)?.version
263 } 264 }
264 265
265 pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url { 266 pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url {
diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs
index 369830973..c4284556e 100644
--- a/crates/rust-analyzer/src/lib.rs
+++ b/crates/rust-analyzer/src/lib.rs
@@ -33,6 +33,7 @@ mod line_endings;
33mod request_metrics; 33mod request_metrics;
34mod lsp_utils; 34mod lsp_utils;
35mod thread_pool; 35mod thread_pool;
36mod document;
36pub mod lsp_ext; 37pub mod lsp_ext;
37pub mod config; 38pub mod config;
38 39
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index e95d4157c..51626fcd5 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -15,6 +15,7 @@ use ra_prof::profile;
15use crate::{ 15use crate::{
16 config::Config, 16 config::Config,
17 dispatch::{NotificationDispatcher, RequestDispatcher}, 17 dispatch::{NotificationDispatcher, RequestDispatcher},
18 document::DocumentData,
18 from_proto, 19 from_proto,
19 global_state::{file_id_to_url, url_to_file_id, GlobalState, Status}, 20 global_state::{file_id_to_url, url_to_file_id, GlobalState, Status},
20 handlers, lsp_ext, 21 handlers, lsp_ext,
@@ -311,7 +312,7 @@ impl GlobalState {
311 let url = file_id_to_url(&self.vfs.read().0, file_id); 312 let url = file_id_to_url(&self.vfs.read().0, file_id);
312 let diagnostics = self.diagnostics.diagnostics_for(file_id).cloned().collect(); 313 let diagnostics = self.diagnostics.diagnostics_for(file_id).cloned().collect();
313 let version = from_proto::vfs_path(&url) 314 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)
315 .unwrap_or_default(); 316 .unwrap_or_default();
316 317
317 self.send_notification::<lsp_types::notification::PublishDiagnostics>( 318 self.send_notification::<lsp_types::notification::PublishDiagnostics>(
@@ -406,7 +407,7 @@ impl GlobalState {
406 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) { 407 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
407 if this 408 if this
408 .mem_docs 409 .mem_docs
409 .insert(path.clone(), Some(params.text_document.version)) 410 .insert(path.clone(), DocumentData::new(params.text_document.version))
410 .is_some() 411 .is_some()
411 { 412 {
412 log::error!("duplicate DidOpenTextDocument: {}", path) 413 log::error!("duplicate DidOpenTextDocument: {}", path)
@@ -428,7 +429,7 @@ impl GlobalState {
428 429
429 // The version passed in DidChangeTextDocument is the version after all edits are applied 430 // The version passed in DidChangeTextDocument is the version after all edits are applied
430 // so we should apply it before the vfs is notified. 431 // so we should apply it before the vfs is notified.
431 *doc = params.text_document.version; 432 doc.version = params.text_document.version;
432 433
433 vfs.set_file_contents(path.clone(), Some(text.into_bytes())); 434 vfs.set_file_contents(path.clone(), Some(text.into_bytes()));
434 } 435 }
@@ -438,7 +439,7 @@ impl GlobalState {
438 let mut version = None; 439 let mut version = None;
439 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) { 440 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
440 match this.mem_docs.remove(&path) { 441 match this.mem_docs.remove(&path) {
441 Some(entry) => version = entry, 442 Some(doc) => version = doc.version,
442 None => log::error!("orphan DidCloseTextDocument: {}", path), 443 None => log::error!("orphan DidCloseTextDocument: {}", path),
443 } 444 }
444 445