diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index 36e7b093c8..a9dc843f4c 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -85,13 +85,14 @@ pub async fn generate_lint_diagnostics( tokio::task::spawn_blocking(move || { let mut diagnostic_list = Vec::new(); - let documents = state_snapshot.documents.lock().unwrap(); - for specifier in documents.open_specifiers() { - let version = documents.version(specifier); + for specifier in state_snapshot.documents.open_specifiers() { + let version = state_snapshot.documents.version(specifier); let current_version = diagnostic_collection.get_version(specifier); if version != current_version { let media_type = MediaType::from(specifier); - if let Ok(Some(source_code)) = documents.content(specifier) { + if let Ok(Some(source_code)) = + state_snapshot.documents.content(specifier) + { if let Ok(references) = get_lint_references(specifier, &media_type, &source_code) { @@ -239,14 +240,11 @@ pub async fn generate_ts_diagnostics( ) -> Result { let mut diagnostics = Vec::new(); let mut specifiers = Vec::new(); - { - let documents = state_snapshot.documents.lock().unwrap(); - for specifier in documents.open_specifiers() { - let version = documents.version(specifier); - let current_version = diagnostic_collection.get_version(specifier); - if version != current_version { - specifiers.push(specifier.clone()); - } + for specifier in state_snapshot.documents.open_specifiers() { + let version = state_snapshot.documents.version(specifier); + let current_version = diagnostic_collection.get_version(specifier); + if version != current_version { + specifiers.push(specifier.clone()); } } if !specifiers.is_empty() { @@ -255,8 +253,7 @@ pub async fn generate_ts_diagnostics( let ts_diagnostic_map: TsDiagnostics = serde_json::from_value(res)?; for (specifier_str, ts_diagnostics) in ts_diagnostic_map.iter() { let specifier = ModuleSpecifier::resolve_url(specifier_str)?; - let version = - state_snapshot.documents.lock().unwrap().version(&specifier); + let version = state_snapshot.documents.version(&specifier); diagnostics.push(( specifier, version, @@ -279,13 +276,12 @@ pub async fn generate_dependency_diagnostics( } else { return Err(custom_error("Deadlock", "deadlock locking sources")); }; - let documents = state_snapshot.documents.lock().unwrap(); - for specifier in documents.open_specifiers() { - let version = documents.version(specifier); + for specifier in state_snapshot.documents.open_specifiers() { + let version = state_snapshot.documents.version(specifier); let current_version = diagnostic_collection.get_version(specifier); if version != current_version { let mut diagnostic_list = Vec::new(); - if let Some(dependencies) = documents.dependencies(specifier) { + if let Some(dependencies) = state_snapshot.documents.dependencies(specifier) { for (_, dependency) in dependencies.iter() { if let (Some(code), Some(range)) = ( &dependency.maybe_code, @@ -306,7 +302,7 @@ pub async fn generate_dependency_diagnostics( }) } ResolvedDependency::Resolved(specifier) => { - if !(documents.contains(&specifier) || sources.contains(&specifier)) { + if !(state_snapshot.documents.contains(&specifier) || sources.contains(&specifier)) { let is_local = specifier.as_url().scheme() == "file"; diagnostic_list.push(lsp_types::Diagnostic { range: *range, diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 316aabf911..2ccbb6284c 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -58,7 +58,7 @@ pub struct LanguageServer { #[derive(Debug, Clone, Default)] pub struct StateSnapshot { pub assets: Arc>>>, - pub documents: Arc>, + pub documents: DocumentCache, pub sources: Arc>, } @@ -289,7 +289,7 @@ impl LanguageServer { pub fn snapshot(&self) -> StateSnapshot { StateSnapshot { assets: self.assets.clone(), - documents: self.documents.clone(), + documents: self.documents.lock().unwrap().clone(), sources: self.sources.clone(), } } diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 575476e402..8c7c4189cc 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -822,13 +822,7 @@ fn cache_snapshot( .contains_key(&(specifier.clone().into(), version.clone().into())) { let s = ModuleSpecifier::resolve_url(&specifier)?; - let content = state - .state_snapshot - .documents - .lock() - .unwrap() - .content(&s)? - .unwrap(); + let content = state.state_snapshot.documents.content(&s)?.unwrap(); state .snapshots .insert((specifier.into(), version.into()), content); @@ -913,13 +907,7 @@ fn get_change_range(state: &mut State, args: Value) -> Result { fn get_length(state: &mut State, args: Value) -> Result { let v: SourceSnapshotArgs = serde_json::from_value(args)?; let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; - if state - .state_snapshot - .documents - .lock() - .unwrap() - .contains(&specifier) - { + if state.state_snapshot.documents.contains(&specifier) { cache_snapshot(state, v.specifier.clone(), v.version.clone())?; let content = state .snapshots @@ -944,13 +932,7 @@ struct GetTextArgs { fn get_text(state: &mut State, args: Value) -> Result { let v: GetTextArgs = serde_json::from_value(args)?; let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; - let content = if state - .state_snapshot - .documents - .lock() - .unwrap() - .contains(&specifier) - { + let content = if state.state_snapshot.documents.contains(&specifier) { cache_snapshot(state, v.specifier.clone(), v.version.clone())?; state .snapshots @@ -974,9 +956,10 @@ fn resolve(state: &mut State, args: Value) -> Result { return Err(custom_error("Deadlock", "deadlock locking sources")); }; - let documents = state.state_snapshot.documents.lock().unwrap(); - if documents.contains(&referrer) { - if let Some(dependencies) = documents.dependencies(&referrer) { + if state.state_snapshot.documents.contains(&referrer) { + if let Some(dependencies) = + state.state_snapshot.documents.dependencies(&referrer) + { for specifier in &v.specifiers { if specifier.starts_with("asset:///") { resolved.push(Some(( @@ -995,7 +978,7 @@ fn resolve(state: &mut State, args: Value) -> Result { if let ResolvedDependency::Resolved(resolved_specifier) = resolved_import { - if documents.contains(&resolved_specifier) + if state.state_snapshot.documents.contains(&resolved_specifier) || sources.contains(&resolved_specifier) { let media_type = if let Some(media_type) = @@ -1050,9 +1033,7 @@ fn respond(state: &mut State, args: Value) -> Result { } fn script_names(state: &mut State, _args: Value) -> Result { - let documents = state.state_snapshot.documents.lock().unwrap(); - let script_names = documents.open_specifiers(); - Ok(json!(script_names)) + Ok(json!(state.state_snapshot.documents.open_specifiers())) } #[derive(Debug, Deserialize)] @@ -1064,13 +1045,7 @@ struct ScriptVersionArgs { fn script_version(state: &mut State, args: Value) -> Result { let v: ScriptVersionArgs = serde_json::from_value(args)?; let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; - if let Some(version) = state - .state_snapshot - .documents - .lock() - .unwrap() - .version(&specifier) - { + if let Some(version) = state.state_snapshot.documents.version(&specifier) { return Ok(json!(version.to_string())); } else { let mut sources = state.state_snapshot.sources.lock().unwrap(); @@ -1336,8 +1311,6 @@ pub fn request( mod tests { use super::*; use crate::lsp::documents::DocumentCache; - use std::sync::Arc; - use std::sync::Mutex; fn mock_state_snapshot(sources: Vec<(&str, &str, i32)>) -> StateSnapshot { let mut documents = DocumentCache::default(); @@ -1348,7 +1321,7 @@ mod tests { } StateSnapshot { assets: Default::default(), - documents: Arc::new(Mutex::new(documents)), + documents, sources: Default::default(), } }