2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-02-17 23:37:05 -05:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-02-17 23:37:05 -05:00
|
|
|
use deno_core::error::AnyError;
|
2022-01-19 11:38:40 -05:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2021-04-06 07:45:53 -04:00
|
|
|
use deno_core::url::Position;
|
2021-02-17 23:37:05 -05:00
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2024-08-23 20:21:21 -04:00
|
|
|
use lsp_types::Uri;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2021-02-17 23:37:05 -05:00
|
|
|
use std::collections::HashMap;
|
2024-08-23 20:21:21 -04:00
|
|
|
use std::str::FromStr;
|
2023-08-02 16:57:25 -04:00
|
|
|
use std::sync::Arc;
|
2021-02-17 23:37:05 -05:00
|
|
|
|
2024-06-17 16:54:23 -04:00
|
|
|
use super::cache::LspCache;
|
2024-08-28 00:15:48 -04:00
|
|
|
use super::logging::lsp_warn;
|
2024-06-17 16:54:23 -04:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
/// Used in situations where a default URL needs to be used where otherwise a
|
|
|
|
/// panic is undesired.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub static INVALID_SPECIFIER: Lazy<ModuleSpecifier> =
|
2023-02-06 16:49:49 -05:00
|
|
|
Lazy::new(|| ModuleSpecifier::parse("deno://invalid").unwrap());
|
2021-07-25 01:33:42 -04:00
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
/// Used in situations where a default URL needs to be used where otherwise a
|
|
|
|
/// panic is undesired.
|
|
|
|
pub static INVALID_URI: Lazy<Uri> =
|
|
|
|
Lazy::new(|| Uri::from_str("deno://invalid").unwrap());
|
|
|
|
|
2021-04-06 07:45:53 -04:00
|
|
|
/// Matches the `encodeURIComponent()` encoding from JavaScript, which matches
|
|
|
|
/// the component percent encoding set.
|
|
|
|
///
|
2021-09-05 10:22:45 -04:00
|
|
|
/// See: <https://url.spec.whatwg.org/#component-percent-encode-set>
|
2021-04-06 07:45:53 -04:00
|
|
|
const COMPONENT: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
|
|
|
|
.add(b' ')
|
|
|
|
.add(b'"')
|
|
|
|
.add(b'#')
|
|
|
|
.add(b'<')
|
|
|
|
.add(b'>')
|
|
|
|
.add(b'?')
|
|
|
|
.add(b'`')
|
|
|
|
.add(b'{')
|
|
|
|
.add(b'}')
|
|
|
|
.add(b'/')
|
2021-02-17 23:37:05 -05:00
|
|
|
.add(b':')
|
|
|
|
.add(b';')
|
|
|
|
.add(b'=')
|
|
|
|
.add(b'@')
|
|
|
|
.add(b'[')
|
|
|
|
.add(b'\\')
|
|
|
|
.add(b']')
|
|
|
|
.add(b'^')
|
2021-04-06 07:45:53 -04:00
|
|
|
.add(b'|')
|
|
|
|
.add(b'$')
|
2023-10-10 14:32:22 -04:00
|
|
|
.add(b'%')
|
2021-04-06 07:45:53 -04:00
|
|
|
.add(b'&')
|
|
|
|
.add(b'+')
|
|
|
|
.add(b',');
|
2021-02-17 23:37:05 -05:00
|
|
|
|
2024-09-11 06:11:39 -04:00
|
|
|
/// Characters that may be left unencoded in a `Url` path but not valid in a
|
|
|
|
/// `Uri` path.
|
|
|
|
const URL_TO_URI_PATH: &percent_encoding::AsciiSet =
|
|
|
|
&percent_encoding::CONTROLS
|
|
|
|
.add(b'[')
|
|
|
|
.add(b']')
|
|
|
|
.add(b'^')
|
|
|
|
.add(b'|');
|
|
|
|
|
|
|
|
/// Characters that may be left unencoded in a `Url` query but not valid in a
|
|
|
|
/// `Uri` query.
|
|
|
|
const URL_TO_URI_QUERY: &percent_encoding::AsciiSet =
|
|
|
|
&URL_TO_URI_PATH.add(b'\\').add(b'`').add(b'{').add(b'}');
|
|
|
|
|
|
|
|
/// Characters that may be left unencoded in a `Url` fragment but not valid in
|
|
|
|
/// a `Uri` fragment.
|
|
|
|
const URL_TO_URI_FRAGMENT: &percent_encoding::AsciiSet =
|
|
|
|
&URL_TO_URI_PATH.add(b'#').add(b'\\').add(b'{').add(b'}');
|
|
|
|
|
2021-02-17 23:37:05 -05:00
|
|
|
fn hash_data_specifier(specifier: &ModuleSpecifier) -> String {
|
|
|
|
let mut file_name_str = specifier.path().to_string();
|
|
|
|
if let Some(query) = specifier.query() {
|
|
|
|
file_name_str.push('?');
|
|
|
|
file_name_str.push_str(query);
|
|
|
|
}
|
2022-11-28 17:28:54 -05:00
|
|
|
crate::util::checksum::gen(&[file_name_str.as_bytes()])
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
fn to_deno_uri(specifier: &Url) -> String {
|
2023-10-10 14:32:22 -04:00
|
|
|
let mut string = String::with_capacity(specifier.as_str().len() + 6);
|
|
|
|
string.push_str("deno:/");
|
|
|
|
string.push_str(specifier.scheme());
|
|
|
|
for p in specifier[Position::BeforeHost..].split('/') {
|
|
|
|
string.push('/');
|
|
|
|
string.push_str(
|
|
|
|
&percent_encoding::utf8_percent_encode(p, COMPONENT).to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_deno_url(url: &Url) -> Option<Url> {
|
|
|
|
if url.scheme() != "deno" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut segments = url.path_segments()?;
|
|
|
|
let mut string = String::with_capacity(url.as_str().len());
|
|
|
|
string.push_str(segments.next()?);
|
|
|
|
string.push_str("://");
|
|
|
|
string.push_str(
|
|
|
|
&percent_encoding::percent_decode(segments.next()?.as_bytes())
|
|
|
|
.decode_utf8()
|
|
|
|
.ok()?,
|
|
|
|
);
|
|
|
|
for segment in segments {
|
|
|
|
string.push('/');
|
|
|
|
string.push_str(
|
|
|
|
&percent_encoding::percent_decode(segment.as_bytes())
|
|
|
|
.decode_utf8()
|
|
|
|
.ok()?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Url::parse(&string).ok()
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:38:40 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct LspUrlMapInner {
|
2024-08-28 00:15:48 -04:00
|
|
|
specifier_to_uri: HashMap<ModuleSpecifier, Uri>,
|
|
|
|
uri_to_specifier: HashMap<Uri, ModuleSpecifier>,
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 11:38:40 -05:00
|
|
|
impl LspUrlMapInner {
|
2024-08-28 00:15:48 -04:00
|
|
|
fn put(&mut self, specifier: ModuleSpecifier, uri: Uri) {
|
|
|
|
self.uri_to_specifier.insert(uri.clone(), specifier.clone());
|
|
|
|
self.specifier_to_uri.insert(specifier, uri);
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
fn get_uri(&self, specifier: &ModuleSpecifier) -> Option<&Uri> {
|
|
|
|
self.specifier_to_uri.get(specifier)
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
fn get_specifier(&self, uri: &Uri) -> Option<&ModuleSpecifier> {
|
|
|
|
self.uri_to_specifier.get(uri)
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
2022-01-19 11:38:40 -05:00
|
|
|
}
|
2021-02-17 23:37:05 -05:00
|
|
|
|
2024-09-11 06:11:39 -04:00
|
|
|
pub fn uri_parse_unencoded(s: &str) -> Result<Uri, AnyError> {
|
|
|
|
url_to_uri(&Url::parse(s)?)
|
|
|
|
}
|
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
pub fn url_to_uri(url: &Url) -> Result<Uri, AnyError> {
|
2024-09-11 06:11:39 -04:00
|
|
|
let components = deno_core::url::quirks::internal_components(url);
|
|
|
|
let mut input = String::with_capacity(url.as_str().len());
|
|
|
|
input.push_str(&url.as_str()[..components.path_start as usize]);
|
|
|
|
input.push_str(
|
|
|
|
&percent_encoding::utf8_percent_encode(url.path(), URL_TO_URI_PATH)
|
|
|
|
.to_string(),
|
|
|
|
);
|
|
|
|
if let Some(query) = url.query() {
|
|
|
|
input.push('?');
|
|
|
|
input.push_str(
|
|
|
|
&percent_encoding::utf8_percent_encode(query, URL_TO_URI_QUERY)
|
|
|
|
.to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if let Some(fragment) = url.fragment() {
|
|
|
|
input.push('#');
|
|
|
|
input.push_str(
|
|
|
|
&percent_encoding::utf8_percent_encode(fragment, URL_TO_URI_FRAGMENT)
|
|
|
|
.to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(Uri::from_str(&input).inspect_err(|err| {
|
2024-08-28 00:15:48 -04:00
|
|
|
lsp_warn!("Could not convert URL \"{url}\" to URI: {err}")
|
|
|
|
})?)
|
2024-08-23 20:21:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn uri_to_url(uri: &Uri) -> Url {
|
|
|
|
Url::parse(uri.as_str()).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum LspUrlKind {
|
|
|
|
File,
|
|
|
|
Folder,
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:38:40 -05:00
|
|
|
/// A bi-directional map of URLs sent to the LSP client and internal module
|
2023-03-15 10:34:23 -04:00
|
|
|
/// specifiers. We need to map internal specifiers into `deno:` schema URLs
|
2022-01-19 11:38:40 -05:00
|
|
|
/// to allow the Deno language server to manage these as virtual documents.
|
2023-08-02 16:57:25 -04:00
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct LspUrlMap {
|
2024-06-17 16:54:23 -04:00
|
|
|
cache: LspCache,
|
2023-08-02 16:57:25 -04:00
|
|
|
inner: Arc<Mutex<LspUrlMapInner>>,
|
|
|
|
}
|
2022-01-19 11:38:40 -05:00
|
|
|
|
|
|
|
impl LspUrlMap {
|
2024-06-17 16:54:23 -04:00
|
|
|
pub fn set_cache(&mut self, cache: &LspCache) {
|
|
|
|
self.cache = cache.clone();
|
2023-08-02 16:57:25 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 23:37:05 -05:00
|
|
|
/// 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.
|
2024-08-28 00:15:48 -04:00
|
|
|
pub fn specifier_to_uri(
|
2022-01-19 11:38:40 -05:00
|
|
|
&self,
|
2021-02-17 23:37:05 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-08-28 00:15:48 -04:00
|
|
|
) -> Result<Uri, AnyError> {
|
2024-06-17 16:54:23 -04:00
|
|
|
if let Some(file_url) =
|
|
|
|
self.cache.vendored_specifier(specifier, file_referrer)
|
|
|
|
{
|
2024-08-28 00:15:48 -04:00
|
|
|
return url_to_uri(&file_url);
|
2023-08-02 16:57:25 -04:00
|
|
|
}
|
|
|
|
let mut inner = self.inner.lock();
|
2024-08-28 00:15:48 -04:00
|
|
|
if let Some(uri) = inner.get_uri(specifier).cloned() {
|
|
|
|
Ok(uri)
|
2021-02-17 23:37:05 -05:00
|
|
|
} else {
|
2024-08-28 00:15:48 -04:00
|
|
|
let uri = if specifier.scheme() == "file" {
|
|
|
|
url_to_uri(specifier)?
|
2021-02-17 23:37:05 -05:00
|
|
|
} else {
|
2024-08-28 00:15:48 -04:00
|
|
|
let uri_str = if specifier.scheme() == "asset" {
|
2023-02-06 16:49:49 -05:00
|
|
|
format!("deno:/asset{}", specifier.path())
|
2021-10-28 19:56:01 -04:00
|
|
|
} else if specifier.scheme() == "data" {
|
2024-01-31 22:15:22 -05:00
|
|
|
let data_url = deno_graph::source::RawDataUrl::parse(specifier)?;
|
|
|
|
let media_type = data_url.media_type();
|
2021-02-17 23:37:05 -05:00
|
|
|
let extension = if media_type == MediaType::Unknown {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
media_type.as_ts_extension()
|
|
|
|
};
|
|
|
|
format!(
|
2023-02-06 16:49:49 -05:00
|
|
|
"deno:/{}/data_url{}",
|
2021-02-17 23:37:05 -05:00
|
|
|
hash_data_specifier(specifier),
|
|
|
|
extension
|
|
|
|
)
|
|
|
|
} else {
|
2024-08-28 00:15:48 -04:00
|
|
|
to_deno_uri(specifier)
|
2021-02-17 23:37:05 -05:00
|
|
|
};
|
2024-09-11 06:11:39 -04:00
|
|
|
let uri = uri_parse_unencoded(&uri_str)?;
|
2024-08-28 00:15:48 -04:00
|
|
|
inner.put(specifier.clone(), uri.clone());
|
|
|
|
uri
|
2021-02-17 23:37:05 -05:00
|
|
|
};
|
2024-08-28 00:15:48 -04:00
|
|
|
Ok(uri)
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 16:49:49 -05:00
|
|
|
/// Normalize URLs from the client, where "virtual" `deno:///` URLs are
|
2021-04-08 19:36:32 -04:00
|
|
|
/// converted into proper module specifiers, as well as handle situations
|
|
|
|
/// where the client encodes a file URL differently than Rust does by default
|
|
|
|
/// causing issues with string matching of URLs.
|
2023-03-15 10:34:23 -04:00
|
|
|
///
|
|
|
|
/// Note: Sometimes the url provided by the client may not have a trailing slash,
|
|
|
|
/// so we need to force it to in the mapping and nee to explicitly state whether
|
|
|
|
/// this is a file or directory url.
|
2024-08-28 00:15:48 -04:00
|
|
|
pub fn uri_to_specifier(
|
|
|
|
&self,
|
|
|
|
uri: &Uri,
|
|
|
|
kind: LspUrlKind,
|
|
|
|
) -> ModuleSpecifier {
|
2024-08-23 20:21:21 -04:00
|
|
|
let url = uri_to_url(uri);
|
|
|
|
if let Some(remote_url) = self.cache.unvendored_specifier(&url) {
|
2024-06-17 16:54:23 -04:00
|
|
|
return remote_url;
|
2023-08-02 16:57:25 -04:00
|
|
|
}
|
|
|
|
let mut inner = self.inner.lock();
|
2024-08-28 00:15:48 -04:00
|
|
|
if let Some(specifier) = inner.get_specifier(uri).cloned() {
|
2023-10-10 14:32:22 -04:00
|
|
|
return specifier;
|
|
|
|
}
|
|
|
|
let mut specifier = None;
|
|
|
|
if url.scheme() == "file" {
|
|
|
|
if let Ok(path) = url.to_file_path() {
|
|
|
|
specifier = Some(match kind {
|
|
|
|
LspUrlKind::Folder => Url::from_directory_path(path).unwrap(),
|
|
|
|
LspUrlKind::File => Url::from_file_path(path).unwrap(),
|
|
|
|
});
|
|
|
|
}
|
2024-08-23 20:21:21 -04:00
|
|
|
} else if let Some(s) = file_like_to_file_specifier(&url) {
|
2023-11-24 17:35:33 -05:00
|
|
|
specifier = Some(s);
|
2024-08-23 20:21:21 -04:00
|
|
|
} else if let Some(s) = from_deno_url(&url) {
|
2023-10-10 14:32:22 -04:00
|
|
|
specifier = Some(s);
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
2023-10-10 14:32:22 -04:00
|
|
|
let specifier = specifier.unwrap_or_else(|| url.clone());
|
2024-08-28 00:15:48 -04:00
|
|
|
inner.put(specifier.clone(), uri.clone());
|
2023-10-10 14:32:22 -04:00
|
|
|
specifier
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 17:35:33 -05:00
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:37:05 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use deno_core::resolve_url;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_data_specifier() {
|
|
|
|
let fixture = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap();
|
|
|
|
let actual = hash_data_specifier(&fixture);
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
"c21c7fc382b2b0553dc0864aa81a3acacfb7b3d1285ab5ae76da6abec213fb37"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lsp_url_map() {
|
2022-01-19 11:38:40 -05:00
|
|
|
let map = LspUrlMap::default();
|
2021-02-17 23:37:05 -05:00
|
|
|
let fixture = resolve_url("https://deno.land/x/pkg@1.0.0/mod.ts").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_uri = map
|
|
|
|
.specifier_to_uri(&fixture, None)
|
2021-02-17 23:37:05 -05:00
|
|
|
.expect("could not handle specifier");
|
2024-08-28 00:15:48 -04:00
|
|
|
assert_eq!(
|
|
|
|
actual_uri.as_str(),
|
|
|
|
"deno:/https/deno.land/x/pkg%401.0.0/mod.ts"
|
|
|
|
);
|
|
|
|
let actual_specifier = map.uri_to_specifier(&actual_uri, LspUrlKind::File);
|
2021-02-17 23:37:05 -05:00
|
|
|
assert_eq!(actual_specifier, fixture);
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:32:22 -04:00
|
|
|
#[test]
|
|
|
|
fn test_lsp_url_reverse() {
|
|
|
|
let map = LspUrlMap::default();
|
|
|
|
let fixture =
|
2024-08-23 20:21:21 -04:00
|
|
|
Uri::from_str("deno:/https/deno.land/x/pkg%401.0.0/mod.ts").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_specifier = map.uri_to_specifier(&fixture, LspUrlKind::File);
|
2023-10-10 14:32:22 -04:00
|
|
|
let expected_specifier =
|
|
|
|
Url::parse("https://deno.land/x/pkg@1.0.0/mod.ts").unwrap();
|
|
|
|
assert_eq!(&actual_specifier, &expected_specifier);
|
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_uri = map.specifier_to_uri(&actual_specifier, None).unwrap();
|
|
|
|
assert_eq!(actual_uri, fixture);
|
2023-10-10 14:32:22 -04:00
|
|
|
}
|
|
|
|
|
2021-04-06 07:45:53 -04:00
|
|
|
#[test]
|
|
|
|
fn test_lsp_url_map_complex_encoding() {
|
|
|
|
// Test fix for #9741 - not properly encoding certain URLs
|
2022-01-19 11:38:40 -05:00
|
|
|
let map = LspUrlMap::default();
|
2021-04-06 07:45:53 -04:00
|
|
|
let fixture = resolve_url("https://cdn.skypack.dev/-/postcss@v8.2.9-E4SktPp9c0AtxrJHp8iV/dist=es2020,mode=types/lib/postcss.d.ts").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_uri = map
|
|
|
|
.specifier_to_uri(&fixture, None)
|
2021-04-06 07:45:53 -04:00
|
|
|
.expect("could not handle specifier");
|
2024-08-28 00:15:48 -04:00
|
|
|
assert_eq!(actual_uri.as_str(), "deno:/https/cdn.skypack.dev/-/postcss%40v8.2.9-E4SktPp9c0AtxrJHp8iV/dist%3Des2020%2Cmode%3Dtypes/lib/postcss.d.ts");
|
|
|
|
let actual_specifier = map.uri_to_specifier(&actual_uri, LspUrlKind::File);
|
2021-04-06 07:45:53 -04:00
|
|
|
assert_eq!(actual_specifier, fixture);
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:37:05 -05:00
|
|
|
#[test]
|
|
|
|
fn test_lsp_url_map_data() {
|
2022-01-19 11:38:40 -05:00
|
|
|
let map = LspUrlMap::default();
|
2021-02-17 23:37:05 -05:00
|
|
|
let fixture = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_uri = map
|
|
|
|
.specifier_to_uri(&fixture, None)
|
2021-02-17 23:37:05 -05:00
|
|
|
.expect("could not handle specifier");
|
2023-02-06 16:49:49 -05:00
|
|
|
let expected_url = Url::parse("deno:/c21c7fc382b2b0553dc0864aa81a3acacfb7b3d1285ab5ae76da6abec213fb37/data_url.ts").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
assert_eq!(&uri_to_url(&actual_uri), &expected_url);
|
2021-02-17 23:37:05 -05:00
|
|
|
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_specifier = map.uri_to_specifier(&actual_uri, LspUrlKind::File);
|
2021-02-17 23:37:05 -05:00
|
|
|
assert_eq!(actual_specifier, fixture);
|
|
|
|
}
|
2021-04-08 19:36:32 -04:00
|
|
|
|
2023-10-06 09:46:43 -04:00
|
|
|
#[test]
|
|
|
|
fn test_lsp_url_map_host_with_port() {
|
|
|
|
let map = LspUrlMap::default();
|
|
|
|
let fixture = resolve_url("http://localhost:8000/mod.ts").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual_uri = map
|
|
|
|
.specifier_to_uri(&fixture, None)
|
2023-10-06 09:46:43 -04:00
|
|
|
.expect("could not handle specifier");
|
2024-08-28 00:15:48 -04:00
|
|
|
assert_eq!(actual_uri.as_str(), "deno:/http/localhost%3A8000/mod.ts");
|
|
|
|
let actual_specifier = map.uri_to_specifier(&actual_uri, LspUrlKind::File);
|
2023-10-06 09:46:43 -04:00
|
|
|
assert_eq!(actual_specifier, fixture);
|
|
|
|
}
|
|
|
|
|
2021-04-08 19:36:32 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_windows_path() {
|
|
|
|
let map = LspUrlMap::default();
|
2024-08-23 20:21:21 -04:00
|
|
|
let fixture = Uri::from_str(
|
2021-04-08 19:36:32 -04:00
|
|
|
"file:///c%3A/Users/deno/Desktop/file%20with%20spaces%20in%20name.txt",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual = map.uri_to_specifier(&fixture, LspUrlKind::File);
|
2021-04-08 19:36:32 -04:00
|
|
|
let expected =
|
|
|
|
Url::parse("file:///C:/Users/deno/Desktop/file with spaces in name.txt")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_percent_encoded_path() {
|
|
|
|
let map = LspUrlMap::default();
|
2024-08-23 20:21:21 -04:00
|
|
|
let fixture = Uri::from_str(
|
2021-04-08 19:36:32 -04:00
|
|
|
"file:///Users/deno/Desktop/file%20with%20spaces%20in%20name.txt",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual = map.uri_to_specifier(&fixture, LspUrlKind::File);
|
2021-04-08 19:36:32 -04:00
|
|
|
let expected =
|
|
|
|
Url::parse("file:///Users/deno/Desktop/file with spaces in name.txt")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
2023-04-16 16:02:48 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_deno_status() {
|
|
|
|
let map = LspUrlMap::default();
|
2024-08-23 20:21:21 -04:00
|
|
|
let fixture = Uri::from_str("deno:/status.md").unwrap();
|
2024-08-28 00:15:48 -04:00
|
|
|
let actual = map.uri_to_specifier(&fixture, LspUrlKind::File);
|
2024-08-23 20:21:21 -04:00
|
|
|
assert_eq!(actual.as_str(), fixture.as_str());
|
2023-04-16 16:02:48 -04:00
|
|
|
}
|
2023-11-24 17:35:33 -05:00
|
|
|
|
|
|
|
#[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()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-02-17 23:37:05 -05:00
|
|
|
}
|