diff options
| author | David Sherret <dsherret@users.noreply.github.com> | 2023-11-24 17:35:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-24 17:35:33 -0500 |
| commit | 60b5d32d902deb46f640cbdb0d2d4caf47a437c4 (patch) | |
| tree | d3d315d5a1c34d7f350f22c083c3d67410ad18e8 /cli/lsp/urls.rs | |
| parent | 6f02fa1abf6bd42975b75f1777dcde748ee662af (diff) | |
fix(lsp): handle byonm specifiers in jupyter notebooks (#21332)
Part of https://github.com/denoland/deno/issues/21308
Diffstat (limited to 'cli/lsp/urls.rs')
| -rw-r--r-- | cli/lsp/urls.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/cli/lsp/urls.rs b/cli/lsp/urls.rs index c3bd381d4..6a5e5f0d3 100644 --- a/cli/lsp/urls.rs +++ b/cli/lsp/urls.rs @@ -247,6 +247,8 @@ impl LspUrlMap { LspUrlKind::File => Url::from_file_path(path).unwrap(), }); } + } else if let Some(s) = file_like_to_file_specifier(url) { + specifier = Some(s); } else if let Some(s) = from_deno_url(url) { specifier = Some(s); } @@ -256,6 +258,30 @@ impl LspUrlMap { } } +/// Convert a e.g. `deno-notebook-cell:` specifier to a `file:` specifier. +/// ```rust +/// assert_eq!( +/// file_like_to_file_specifier( +/// &Url::parse("deno-notebook-cell:/path/to/file.ipynb#abc").unwrap(), +/// ), +/// Some(Url::parse("file:///path/to/file.ipynb.ts?scheme=deno-notebook-cell#abc").unwrap()), +/// ); +fn file_like_to_file_specifier(specifier: &Url) -> Option<Url> { + if matches!(specifier.scheme(), "untitled" | "deno-notebook-cell") { + if let Ok(mut s) = ModuleSpecifier::parse(&format!( + "file://{}", + &specifier.as_str()[deno_core::url::quirks::internal_components(specifier) + .host_end as usize..], + )) { + s.query_pairs_mut() + .append_pair("scheme", specifier.scheme()); + s.set_path(&format!("{}.ts", s.path())); + return Some(s); + } + } + None +} + #[cfg(test)] mod tests { use super::*; @@ -389,4 +415,28 @@ mod tests { let actual = map.normalize_url(&fixture, LspUrlKind::File); assert_eq!(actual, fixture); } + + #[test] + fn test_file_like_to_file_specifier() { + assert_eq!( + file_like_to_file_specifier( + &Url::parse("deno-notebook-cell:/path/to/file.ipynb#abc").unwrap(), + ), + Some( + Url::parse( + "file:///path/to/file.ipynb.ts?scheme=deno-notebook-cell#abc" + ) + .unwrap() + ), + ); + assert_eq!( + file_like_to_file_specifier( + &Url::parse("untitled:/path/to/file.ipynb#123").unwrap(), + ), + Some( + Url::parse("file:///path/to/file.ipynb.ts?scheme=untitled#123") + .unwrap() + ), + ); + } } |
