diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-08-02 16:57:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-02 16:57:25 -0400 |
commit | 480894e5c8f9532a4c42477cdf5c058cb8e9e1e3 (patch) | |
tree | 7241c8864439b60c97f2140ccdef17ed07a5dbcf /cli/lsp/urls.rs | |
parent | e8d03119a0599ef8d811f8fa33ad1a580adf5511 (diff) |
feat(unstable/lsp): support navigating to deno_modules folder (#20030)
Closes #20015
Closes https://github.com/denoland/vscode_deno/issues/850 (only for
deno_modules, but I don't think this will be possible for the global
cache)
Diffstat (limited to 'cli/lsp/urls.rs')
-rw-r--r-- | cli/lsp/urls.rs | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/cli/lsp/urls.rs b/cli/lsp/urls.rs index ee9684f64..8b16292e9 100644 --- a/cli/lsp/urls.rs +++ b/cli/lsp/urls.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::cache::LocalLspHttpCache; use crate::file_fetcher::map_content_type; use data_url::DataUrl; @@ -12,6 +13,7 @@ use deno_core::url::Url; use deno_core::ModuleSpecifier; use once_cell::sync::Lazy; use std::collections::HashMap; +use std::sync::Arc; /// Used in situations where a default URL needs to be used where otherwise a /// panic is undesired. @@ -119,17 +121,31 @@ pub enum LspUrlKind { /// A bi-directional map of URLs sent to the LSP client and internal module /// specifiers. We need to map internal specifiers into `deno:` schema URLs /// to allow the Deno language server to manage these as virtual documents. -#[derive(Debug, Default)] -pub struct LspUrlMap(Mutex<LspUrlMapInner>); +#[derive(Debug, Default, Clone)] +pub struct LspUrlMap { + local_http_cache: Option<Arc<LocalLspHttpCache>>, + inner: Arc<Mutex<LspUrlMapInner>>, +} impl LspUrlMap { + pub fn set_cache(&mut self, http_cache: Option<Arc<LocalLspHttpCache>>) { + self.local_http_cache = http_cache; + } + /// Normalize a specifier that is used internally within Deno (or tsc) to a /// URL that can be handled as a "virtual" document by an LSP client. pub fn normalize_specifier( &self, specifier: &ModuleSpecifier, ) -> Result<LspClientUrl, AnyError> { - let mut inner = self.0.lock(); + if let Some(cache) = &self.local_http_cache { + if matches!(specifier.scheme(), "http" | "https") { + if let Some(file_url) = cache.get_file_url(specifier) { + return Ok(LspClientUrl(file_url)); + } + } + } + let mut inner = self.inner.lock(); if let Some(url) = inner.get_url(specifier).cloned() { Ok(url) } else { @@ -183,7 +199,16 @@ impl LspUrlMap { /// so we need to force it to in the mapping and nee to explicitly state whether /// this is a file or directory url. pub fn normalize_url(&self, url: &Url, kind: LspUrlKind) -> ModuleSpecifier { - let mut inner = self.0.lock(); + if let Some(cache) = &self.local_http_cache { + if url.scheme() == "file" { + if let Ok(path) = url.to_file_path() { + if let Some(remote_url) = cache.get_remote_url(&path) { + return remote_url; + } + } + } + } + let mut inner = self.inner.lock(); if let Some(specifier) = inner.get_specifier(url).cloned() { specifier } else { |