diff options
| author | David Sherret <dsherret@users.noreply.github.com> | 2022-10-20 13:23:21 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-20 13:23:21 -0400 |
| commit | da906de18437318527ae368932c2c8663db44f76 (patch) | |
| tree | ca1e55b8a8380b452bb89ce3537f87980fa2c0da /cli/lsp | |
| parent | a48d05fac779e53e92fe0e8b5668adf120f319e4 (diff) | |
fix(lsp): allow caching deps in non-saved files (#16353)
Diffstat (limited to 'cli/lsp')
| -rw-r--r-- | cli/lsp/documents.rs | 28 | ||||
| -rw-r--r-- | cli/lsp/language_server.rs | 20 |
2 files changed, 44 insertions, 4 deletions
diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index e395b565d..61db899e5 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -21,6 +21,7 @@ use deno_ast::ParsedSource; use deno_ast::SourceTextInfo; use deno_core::error::custom_error; use deno_core::error::AnyError; +use deno_core::futures::future; use deno_core::parking_lot::Mutex; use deno_core::url; use deno_core::ModuleSpecifier; @@ -1111,6 +1112,33 @@ impl Documents { } } +/// Loader that will look at the open documents. +pub struct DocumentsDenoGraphLoader<'a> { + pub inner_loader: &'a mut dyn deno_graph::source::Loader, + pub open_docs: &'a HashMap<ModuleSpecifier, Document>, +} + +impl<'a> deno_graph::source::Loader for DocumentsDenoGraphLoader<'a> { + fn load( + &mut self, + specifier: &ModuleSpecifier, + is_dynamic: bool, + ) -> deno_graph::source::LoadFuture { + if specifier.scheme() == "file" { + if let Some(doc) = self.open_docs.get(specifier) { + return Box::pin(future::ready(Ok(Some( + deno_graph::source::LoadResponse::Module { + content: doc.content(), + specifier: doc.specifier().clone(), + maybe_headers: None, + }, + )))); + } + } + self.inner_loader.load(specifier, is_dynamic) + } +} + /// The default parser from `deno_graph` does not include the configuration /// options we require for the lsp. #[derive(Debug, Default)] diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 9141f7288..27d69127c 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -14,6 +14,7 @@ use import_map::ImportMap; use log::error; use log::warn; use serde_json::from_value; +use std::collections::HashMap; use std::env; use std::fmt::Write as _; use std::path::PathBuf; @@ -2825,9 +2826,19 @@ impl Inner { async fn create_graph_for_caching( cli_options: CliOptions, roots: Vec<(ModuleSpecifier, ModuleKind)>, + open_docs: Vec<Document>, ) -> Result<(), AnyError> { + let open_docs = open_docs + .into_iter() + .map(|d| (d.specifier().clone(), d)) + .collect::<HashMap<_, _>>(); let ps = ProcState::from_options(Arc::new(cli_options)).await?; - let graph = ps.create_graph(roots).await?; + let mut inner_loader = ps.create_graph_loader(); + let mut loader = crate::lsp::documents::DocumentsDenoGraphLoader { + inner_loader: &mut inner_loader, + open_docs: &open_docs, + }; + let graph = ps.create_graph_with_loader(roots, &mut loader).await?; graph_valid(&graph, true, false)?; Ok(()) } @@ -2867,10 +2878,11 @@ impl Inner { // todo(dsherret): why is running this on a new thread necessary? It does // a compile error otherwise. + let open_docs = self.documents.documents(true, true); let handle = tokio::task::spawn_blocking(|| { - run_local( - async move { create_graph_for_caching(cli_options, roots).await }, - ) + run_local(async move { + create_graph_for_caching(cli_options, roots, open_docs).await + }) }); if let Err(err) = handle.await.unwrap() { self.client.show_message(MessageType::WARNING, err).await; |
