diff options
Diffstat (limited to 'cli/lsp/documents.rs')
-rw-r--r-- | cli/lsp/documents.rs | 130 |
1 files changed, 53 insertions, 77 deletions
diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index 42c67c45d..6c7f8433f 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -1,6 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use super::cache::calculate_fs_version; +use super::cache::LspCache; use super::cache::LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY; use super::config::Config; use super::resolver::LspResolver; @@ -10,7 +11,6 @@ use super::text::LineIndex; use super::tsc; use super::tsc::AssetDocument; -use crate::cache::HttpCache; use crate::graph_util::CliJsrUrlProvider; use crate::lsp::logging::lsp_warn; use deno_graph::source::Resolver; @@ -287,7 +287,7 @@ impl Document { maybe_headers: Option<HashMap<String, String>>, resolver: Arc<LspResolver>, config: Arc<Config>, - cache: &Arc<dyn HttpCache>, + cache: &Arc<LspCache>, ) -> Arc<Self> { let text_info = SourceTextInfo::new(content); let media_type = resolve_media_type( @@ -507,7 +507,7 @@ impl Document { })) } - pub fn closed(&self, cache: &Arc<dyn HttpCache>) -> Arc<Self> { + pub fn closed(&self, cache: &Arc<LspCache>) -> Arc<Self> { Arc::new(Self { config: self.config.clone(), specifier: self.specifier.clone(), @@ -528,7 +528,7 @@ impl Document { }) } - pub fn saved(&self, cache: &Arc<dyn HttpCache>) -> Arc<Self> { + pub fn saved(&self, cache: &Arc<LspCache>) -> Arc<Self> { Arc::new(Self { config: self.config.clone(), specifier: self.specifier.clone(), @@ -565,6 +565,10 @@ impl Document { self.line_index.clone() } + pub fn maybe_headers(&self) -> Option<&HashMap<String, String>> { + self.maybe_headers.as_ref() + } + fn maybe_fs_version(&self) -> Option<&str> { self.maybe_fs_version.as_deref() } @@ -712,7 +716,7 @@ impl FileSystemDocuments { specifier: &ModuleSpecifier, resolver: &Arc<LspResolver>, config: &Arc<Config>, - cache: &Arc<dyn HttpCache>, + cache: &Arc<LspCache>, ) -> Option<Arc<Document>> { let new_fs_version = calculate_fs_version(cache, specifier); let old_doc = self.docs.get(specifier).map(|v| v.value().clone()); @@ -742,7 +746,7 @@ impl FileSystemDocuments { specifier: &ModuleSpecifier, resolver: &Arc<LspResolver>, config: &Arc<Config>, - cache: &Arc<dyn HttpCache>, + cache: &Arc<LspCache>, ) -> Option<Arc<Document>> { let doc = if specifier.scheme() == "file" { let path = specifier_to_file_path(specifier).ok()?; @@ -775,11 +779,12 @@ impl FileSystemDocuments { cache, ) } else { - let cache_key = cache.cache_item_key(specifier).ok()?; - let bytes = cache + let http_cache = cache.root_vendor_or_global(); + let cache_key = http_cache.cache_item_key(specifier).ok()?; + let bytes = http_cache .read_file_bytes(&cache_key, None, LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY) .ok()??; - let specifier_headers = cache.read_headers(&cache_key).ok()??; + let specifier_headers = http_cache.read_headers(&cache_key).ok()??; let (_, maybe_charset) = deno_graph::source::resolve_media_type_and_charset_from_headers( specifier, @@ -832,10 +837,10 @@ pub enum DocumentsFilter { OpenDiagnosable, } -#[derive(Debug, Clone)] +#[derive(Debug, Default, Clone)] pub struct Documents { /// The DENO_DIR that the documents looks for non-file based modules. - cache: Arc<dyn HttpCache>, + cache: Arc<LspCache>, config: Arc<Config>, /// A flag that indicates that stated data is potentially invalid and needs to /// be recalculated before being considered valid. @@ -855,19 +860,6 @@ pub struct Documents { } impl Documents { - pub fn new(cache: Arc<dyn HttpCache>) -> Self { - Self { - cache: cache.clone(), - config: Default::default(), - dirty: true, - open_docs: HashMap::default(), - file_system_docs: Default::default(), - resolver: Default::default(), - npm_specifier_reqs: Default::default(), - has_injected_types_node_package: false, - } - } - /// "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 @@ -1019,7 +1011,7 @@ impl Documents { .map(|p| p.is_file()) .unwrap_or(false); } - if self.cache.contains(&specifier) { + if self.cache.root_vendor_or_global().contains(&specifier) { return true; } } @@ -1179,11 +1171,11 @@ impl Documents { &mut self, config: &Config, resolver: &Arc<LspResolver>, - cache: Arc<dyn HttpCache>, + cache: &LspCache, workspace_files: &BTreeSet<ModuleSpecifier>, ) { self.config = Arc::new(config.clone()); - self.cache = cache; + self.cache = Arc::new(cache.clone()); self.resolver = resolver.clone(); { let fs_docs = &self.file_system_docs; @@ -1461,31 +1453,29 @@ fn analyze_module( #[cfg(test)] mod tests { - use crate::cache::GlobalHttpCache; - use crate::cache::RealDenoCacheEnv; - use super::*; + use crate::lsp::cache::LspCache; use deno_config::ConfigFile; use deno_core::serde_json; use deno_core::serde_json::json; use pretty_assertions::assert_eq; - use test_util::PathRef; use test_util::TempDir; - fn setup(temp_dir: &TempDir) -> (Documents, PathRef, Arc<dyn HttpCache>) { - let location = temp_dir.path().join("deps"); - let cache = Arc::new(GlobalHttpCache::new( - location.to_path_buf(), - RealDenoCacheEnv, - )); - let documents = Documents::new(cache.clone()); - (documents, location, cache) + async fn setup() -> (Documents, LspCache, TempDir) { + let temp_dir = TempDir::new(); + let cache = LspCache::new(Some(temp_dir.uri())); + let config = Config::default(); + let resolver = LspResolver::default() + .with_new_config(&config, &cache, None) + .await; + let mut documents = Documents::default(); + documents.update_config(&config, &resolver, &cache, &Default::default()); + (documents, cache, temp_dir) } - #[test] - fn test_documents_open_close() { - let temp_dir = TempDir::new(); - let (mut documents, _, _) = setup(&temp_dir); + #[tokio::test] + async fn test_documents_open_close() { + let (mut documents, _, _) = setup().await; let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap(); let content = r#"import * as b from "./b.ts"; console.log(b); @@ -1508,10 +1498,9 @@ console.log(b); assert!(document.maybe_lsp_version().is_none()); } - #[test] - fn test_documents_change() { - let temp_dir = TempDir::new(); - let (mut documents, _, _) = setup(&temp_dir); + #[tokio::test] + async fn test_documents_change() { + let (mut documents, _, _) = setup().await; let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap(); let content = r#"import * as b from "./b.ts"; console.log(b); @@ -1550,15 +1539,13 @@ console.log(b, "hello deno"); ); } - #[test] - fn test_documents_ensure_no_duplicates() { + #[tokio::test] + async fn test_documents_ensure_no_duplicates() { // it should never happen that a user of this API causes this to happen, // but we'll guard against it anyway - let temp_dir = TempDir::new(); - let (mut documents, documents_path, _) = setup(&temp_dir); - let file_path = documents_path.join("file.ts"); - let file_specifier = ModuleSpecifier::from_file_path(&file_path).unwrap(); - documents_path.create_dir_all(); + let (mut documents, _, temp_dir) = setup().await; + let file_path = temp_dir.path().join("file.ts"); + let file_specifier = temp_dir.uri().join("file.ts").unwrap(); file_path.write(""); // open the document @@ -1582,27 +1569,21 @@ console.log(b, "hello deno"); async fn test_documents_refresh_dependencies_config_change() { // it should never happen that a user of this API causes this to happen, // but we'll guard against it anyway - let temp_dir = TempDir::new(); - let (mut documents, documents_path, cache) = setup(&temp_dir); - fs::create_dir_all(&documents_path).unwrap(); + let (mut documents, cache, temp_dir) = setup().await; - let file1_path = documents_path.join("file1.ts"); - let file1_specifier = ModuleSpecifier::from_file_path(&file1_path).unwrap(); + let file1_path = temp_dir.path().join("file1.ts"); + let file1_specifier = temp_dir.uri().join("file1.ts").unwrap(); fs::write(&file1_path, "").unwrap(); - let file2_path = documents_path.join("file2.ts"); - let file2_specifier = ModuleSpecifier::from_file_path(&file2_path).unwrap(); + let file2_path = temp_dir.path().join("file2.ts"); + let file2_specifier = temp_dir.uri().join("file2.ts").unwrap(); fs::write(&file2_path, "").unwrap(); - let file3_path = documents_path.join("file3.ts"); - let file3_specifier = ModuleSpecifier::from_file_path(&file3_path).unwrap(); + let file3_path = temp_dir.path().join("file3.ts"); + let file3_specifier = temp_dir.uri().join("file3.ts").unwrap(); fs::write(&file3_path, "").unwrap(); - let mut config = - Config::new_with_roots(vec![ModuleSpecifier::from_directory_path( - &documents_path, - ) - .unwrap()]); + let mut config = Config::new_with_roots([temp_dir.uri()]); let workspace_settings = serde_json::from_str(r#"{ "enable": true }"#).unwrap(); config.set_workspace_settings(workspace_settings, vec![]); @@ -1632,14 +1613,9 @@ console.log(b, "hello deno"); .await; let resolver = LspResolver::default() - .with_new_config(&config, cache.clone(), None, None) + .with_new_config(&config, &cache, None) .await; - documents.update_config( - &config, - &resolver, - cache.clone(), - &workspace_files, - ); + documents.update_config(&config, &resolver, &cache, &workspace_files); // open the document let document = documents.open( @@ -1681,9 +1657,9 @@ console.log(b, "hello deno"); .await; let resolver = LspResolver::default() - .with_new_config(&config, cache.clone(), None, None) + .with_new_config(&config, &cache, None) .await; - documents.update_config(&config, &resolver, cache, &workspace_files); + documents.update_config(&config, &resolver, &cache, &workspace_files); // check the document's dependencies let document = documents.get(&file1_specifier).unwrap(); |