Skip to content

Commit

Permalink
refactor(lsp): store project version on documents (#21892)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Jan 11, 2024
1 parent 6861411 commit d8f86c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
28 changes: 23 additions & 5 deletions cli/lsp/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ pub struct Documents {
redirect_resolver: Arc<RedirectResolver>,
/// If --unstable-sloppy-imports is enabled.
unstable_sloppy_imports: bool,
project_version: usize,
}

impl Documents {
Expand Down Expand Up @@ -924,6 +925,7 @@ impl Documents {
has_injected_types_node_package: false,
redirect_resolver: Arc::new(RedirectResolver::new(cache)),
unstable_sloppy_imports: false,
project_version: 0,
}
}

Expand All @@ -935,6 +937,14 @@ impl Documents {
.flat_map(|value| value.get_type().or_else(|| value.get_code()))
}

pub fn project_version(&self) -> String {
self.project_version.to_string()
}

pub fn increment_project_version(&mut self) {
self.project_version += 1;
}

/// "Open" a document from the perspective of the editor, meaning that
/// requests for information from the document will come from the in-memory
/// representation received from the language server client, versus reading
Expand All @@ -957,10 +967,13 @@ impl Documents {
resolver,
npm_resolver,
);
let mut file_system_docs = self.file_system_docs.lock();
file_system_docs.docs.remove(&specifier);
file_system_docs.dirty = true;
{
let mut file_system_docs = self.file_system_docs.lock();
file_system_docs.docs.remove(&specifier);
file_system_docs.dirty = true;
}
self.open_docs.insert(specifier, document.clone());
self.increment_project_version();
self.dirty = true;
document
}
Expand Down Expand Up @@ -995,6 +1008,7 @@ impl Documents {
self.get_npm_resolver(),
)?;
self.open_docs.insert(doc.specifier().clone(), doc.clone());
self.increment_project_version();
Ok(doc)
}

Expand All @@ -1016,8 +1030,11 @@ impl Documents {
/// information about the document is required.
pub fn close(&mut self, specifier: &ModuleSpecifier) -> Result<(), AnyError> {
if let Some(document) = self.open_docs.remove(specifier) {
let mut file_system_docs = self.file_system_docs.lock();
file_system_docs.docs.insert(specifier.clone(), document);
{
let mut file_system_docs = self.file_system_docs.lock();
file_system_docs.docs.insert(specifier.clone(), document);
}
self.increment_project_version();
self.dirty = true;
}
Ok(())
Expand Down Expand Up @@ -1422,6 +1439,7 @@ impl Documents {
);
self.resolver_config_hash = new_resolver_config_hash;

self.increment_project_version();
self.dirty = true;
self.calculate_dependents_if_dirty();
}
Expand Down
9 changes: 2 additions & 7 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ impl LspNpmConfigHash {
#[derive(Debug, Clone)]
pub struct LanguageServer(Arc<tokio::sync::RwLock<Inner>>, CancellationToken);

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct StateNpmSnapshot {
pub node_resolver: Arc<NodeResolver>,
pub npm_resolver: Arc<dyn CliNpmResolver>,
}

/// Snapshot of the state used by TSC.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct StateSnapshot {
pub assets: AssetsSnapshot,
pub cache_metadata: cache::CacheMetadata,
Expand Down Expand Up @@ -1427,7 +1427,6 @@ impl Inner {
self
.diagnostics_server
.invalidate(&self.documents.dependents(&specifier));
self.ts_server.increment_project_version();
self.send_diagnostics_update();
self.send_testing_update();
}
Expand Down Expand Up @@ -1472,7 +1471,6 @@ impl Inner {
let mut specifiers = self.documents.dependents(&specifier);
specifiers.push(specifier.clone());
self.diagnostics_server.invalidate(&specifiers);
self.ts_server.increment_project_version();
self.send_diagnostics_update();
self.send_testing_update();
}
Expand Down Expand Up @@ -1525,7 +1523,6 @@ impl Inner {
self.refresh_documents_config().await;

self.diagnostics_server.invalidate_all();
self.ts_server.increment_project_version();
self.send_diagnostics_update();
self.send_testing_update();
}
Expand Down Expand Up @@ -3388,7 +3385,6 @@ impl tower_lsp::LanguageServer for LanguageServer {
inner.refresh_npm_specifiers().await;
let specifiers = inner.documents.dependents(&specifier);
inner.diagnostics_server.invalidate(&specifiers);
inner.ts_server.increment_project_version();
inner.send_diagnostics_update();
inner.send_testing_update();
}
Expand Down Expand Up @@ -3479,7 +3475,6 @@ impl tower_lsp::LanguageServer for LanguageServer {
let mut ls = self.0.write().await;
ls.refresh_documents_config().await;
ls.diagnostics_server.invalidate_all();
ls.ts_server.increment_project_version();
ls.send_diagnostics_update();
}
performance.measure(mark);
Expand Down
35 changes: 10 additions & 25 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ use std::net::SocketAddr;
use std::ops::Range;
use std::path::Path;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::thread;
use text_size::TextRange;
Expand Down Expand Up @@ -222,7 +220,6 @@ pub struct TsServer {
sender: mpsc::UnboundedSender<Request>,
receiver: Mutex<Option<mpsc::UnboundedReceiver<Request>>>,
specifier_map: Arc<TscSpecifierMap>,
project_version: Arc<AtomicUsize>,
inspector_server: Mutex<Option<Arc<InspectorServer>>>,
}

Expand All @@ -234,7 +231,6 @@ impl std::fmt::Debug for TsServer {
.field("sender", &self.sender)
.field("receiver", &self.receiver)
.field("specifier_map", &self.specifier_map)
.field("project_version", &self.project_version)
.field("inspector_server", &self.inspector_server.lock().is_some())
.finish()
}
Expand All @@ -249,7 +245,6 @@ impl TsServer {
sender: tx,
receiver: Mutex::new(Some(request_rx)),
specifier_map: Arc::new(TscSpecifierMap::new()),
project_version: Arc::new(AtomicUsize::new(1)),
inspector_server: Mutex::new(None),
}
}
Expand All @@ -272,14 +267,12 @@ impl TsServer {
let performance = self.performance.clone();
let cache = self.cache.clone();
let specifier_map = self.specifier_map.clone();
let project_version = self.project_version.clone();
let _join_handle = thread::spawn(move || {
run_tsc_thread(
receiver,
performance.clone(),
cache.clone(),
specifier_map.clone(),
project_version,
maybe_inspector_server,
)
});
Expand Down Expand Up @@ -361,10 +354,6 @@ impl TsServer {
self.request(snapshot, req).await
}

pub fn increment_project_version(&self) {
self.project_version.fetch_add(1, Ordering::Relaxed);
}

pub async fn get_supported_code_fixes(
&self,
snapshot: Arc<StateSnapshot>,
Expand Down Expand Up @@ -3841,7 +3830,6 @@ struct State {
response: Option<Response>,
state_snapshot: Arc<StateSnapshot>,
specifier_map: Arc<TscSpecifierMap>,
project_version: Arc<AtomicUsize>,
token: CancellationToken,
}

Expand All @@ -3850,15 +3838,13 @@ impl State {
state_snapshot: Arc<StateSnapshot>,
specifier_map: Arc<TscSpecifierMap>,
performance: Arc<Performance>,
project_version: Arc<AtomicUsize>,
) -> Self {
Self {
last_id: 1,
performance,
response: None,
state_snapshot,
specifier_map,
project_version,
token: Default::default(),
}
}
Expand Down Expand Up @@ -4077,7 +4063,7 @@ fn op_script_version(
fn op_project_version(state: &mut OpState) -> String {
let state = state.borrow_mut::<State>();
let mark = state.performance.mark("tsc.op.op_project_version");
let r = state.project_version.load(Ordering::Relaxed).to_string();
let r = state.state_snapshot.documents.project_version();
state.performance.measure(mark);
r
}
Expand All @@ -4087,20 +4073,14 @@ fn run_tsc_thread(
performance: Arc<Performance>,
cache: Arc<dyn HttpCache>,
specifier_map: Arc<TscSpecifierMap>,
project_version: Arc<AtomicUsize>,
maybe_inspector_server: Option<Arc<InspectorServer>>,
) {
let has_inspector_server = maybe_inspector_server.is_some();
// Create and setup a JsRuntime based on a snapshot. It is expected that the
// supplied snapshot is an isolate that contains the TypeScript language
// server.
let mut tsc_runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![deno_tsc::init_ops(
performance,
cache,
specifier_map,
project_version,
)],
extensions: vec![deno_tsc::init_ops(performance, cache, specifier_map)],
startup_snapshot: Some(tsc::compiler_snapshot()),
inspector: maybe_inspector_server.is_some(),
..Default::default()
Expand Down Expand Up @@ -4173,7 +4153,6 @@ deno_core::extension!(deno_tsc,
performance: Arc<Performance>,
cache: Arc<dyn HttpCache>,
specifier_map: Arc<TscSpecifierMap>,
project_version: Arc<AtomicUsize>,
},
state = |state, options| {
state.put(State::new(
Expand All @@ -4187,7 +4166,6 @@ deno_core::extension!(deno_tsc,
}),
options.specifier_map,
options.performance,
options.project_version,
));
},
);
Expand Down Expand Up @@ -5115,7 +5093,14 @@ mod tests {
b"export const b = \"b\";\n\nexport const a = \"b\";\n",
)
.unwrap();
ts_server.increment_project_version();
let snapshot = {
let mut documents = snapshot.documents.clone();
documents.increment_project_version();
Arc::new(StateSnapshot {
documents,
..snapshot.as_ref().clone()
})
};
let specifier = resolve_url("file:///a.ts").unwrap();
let diagnostics = ts_server
.get_diagnostics(snapshot.clone(), vec![specifier], Default::default())
Expand Down

0 comments on commit d8f86c8

Please sign in to comment.