diff --git a/cli/lsp/path_to_regex.rs b/cli/lsp/path_to_regex.rs index 16db881940..ef782c4db7 100644 --- a/cli/lsp/path_to_regex.rs +++ b/cli/lsp/path_to_regex.rs @@ -249,7 +249,11 @@ impl StringOrVec { } } - pub fn to_string(&self, maybe_key: Option<&Key>) -> String { + pub fn to_string( + &self, + maybe_key: Option<&Key>, + omit_initial_prefix: bool, + ) -> String { match self { Self::String(s) => s.clone(), Self::Vec(v) => { @@ -262,8 +266,12 @@ impl StringOrVec { ("/".to_string(), "".to_string()) }; let mut s = String::new(); - for segment in v { - s.push_str(&format!("{}{}{}", prefix, segment, suffix)); + for (i, segment) in v.iter().enumerate() { + if omit_initial_prefix && i == 0 { + s.push_str(&format!("{}{}", segment, suffix)); + } else { + s.push_str(&format!("{}{}{}", prefix, segment, suffix)); + } } s } diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index acb6a401a0..93333dd928 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -112,7 +112,7 @@ fn get_completor_type( if let StringOrNumber::String(name) = &k.name { let value = match_result .get(name) - .map(|s| s.to_string(Some(k))) + .map(|s| s.to_string(Some(k), false)) .unwrap_or_default(); len += value.chars().count(); if offset <= len { @@ -213,11 +213,12 @@ fn get_endpoint_with_match( Token::Key(k) if k.name == *key => Some(k), _ => None, }); - url = url.replace(&format!("${{{}}}", name), &value.to_string(maybe_key)); + url = url + .replace(&format!("${{{}}}", name), &value.to_string(maybe_key, true)); url = url.replace( &format!("${{{{{}}}}}", name), &percent_encoding::percent_encode( - value.to_string(maybe_key).as_bytes(), + value.to_string(maybe_key, true).as_bytes(), COMPONENT, ) .to_string(), @@ -1212,15 +1213,7 @@ mod tests { .await; assert!(completions.is_some()); let completions = completions.unwrap().items; - assert_eq!(completions.len(), 1); - assert_eq!(completions[0].label, "/x"); - assert_eq!( - completions[0].text_edit, - Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit { - range, - new_text: "http://localhost:4545/x".to_string() - })) - ); + assert_eq!(completions.len(), 3); let range = lsp::Range { start: lsp::Position { line: 0, @@ -1236,15 +1229,7 @@ mod tests { .await; assert!(completions.is_some()); let completions = completions.unwrap().items; - assert_eq!(completions.len(), 1); - assert_eq!(completions[0].label, "/x"); - assert_eq!( - completions[0].text_edit, - Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit { - range, - new_text: "http://localhost:4545/x".to_string() - })) - ); + assert_eq!(completions.len(), 3); let range = lsp::Range { start: lsp::Position { line: 0, @@ -1328,6 +1313,30 @@ mod tests { "documentation": format!("http://localhost:4545/lsp/registries/doc_a_{}.json", completions[0].label), })) ); + + let range = lsp::Range { + start: lsp::Position { + line: 0, + character: 20, + }, + end: lsp::Position { + line: 0, + character: 49, + }, + }; + let completions = module_registry + .get_completions("http://localhost:4545/x/a@v1.", 29, &range, |_| false) + .await; + assert!(completions.is_some()); + let completions = completions.unwrap().items; + assert_eq!(completions.len(), 2); + assert_eq!( + completions[0].data, + Some(json!({ + "documentation": format!("http://localhost:4545/lsp/registries/doc_a_{}.json", completions[0].label), + })) + ); + let range = lsp::Range { start: lsp::Position { line: 0, @@ -1338,7 +1347,6 @@ mod tests { character: 53, }, }; - let completions = module_registry .get_completions("http://localhost:4545/x/a@v1.0.0/", 33, &range, |_| { false @@ -1353,6 +1361,53 @@ mod tests { assert_eq!(completions[1].detail, Some("(path)".to_string())); assert_eq!(completions[0].kind, Some(lsp::CompletionItemKind::FILE)); assert!(completions[1].command.is_some()); + + let range = lsp::Range { + start: lsp::Position { + line: 0, + character: 20, + }, + end: lsp::Position { + line: 0, + character: 54, + }, + }; + let completions = module_registry + .get_completions("http://localhost:4545/x/a@v1.0.0/b", 34, &range, |_| { + false + }) + .await; + assert!(completions.is_some()); + let completions = completions.unwrap().items; + assert_eq!(completions.len(), 1); + assert_eq!(completions[0].detail, Some("(path)".to_string())); + assert_eq!(completions[0].kind, Some(lsp::CompletionItemKind::FILE)); + assert!(completions[0].command.is_some()); + + let range = lsp::Range { + start: lsp::Position { + line: 0, + character: 20, + }, + end: lsp::Position { + line: 0, + character: 55, + }, + }; + let completions = module_registry + .get_completions( + "http://localhost:4545/x/a@v1.0.0/b/", + 35, + &range, + |_| false, + ) + .await; + assert!(completions.is_some()); + let completions = completions.unwrap().items; + assert_eq!(completions.len(), 1); + assert_eq!(completions[0].detail, Some("(path)".to_string())); + assert_eq!(completions[0].kind, Some(lsp::CompletionItemKind::FILE)); + assert!(completions[0].command.is_some()); } #[tokio::test] diff --git a/cli/tests/testdata/lsp/registries/a_latest.json b/cli/tests/testdata/lsp/registries/a_latest_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/a_latest.json rename to cli/tests/testdata/lsp/registries/a_latest_.json diff --git a/cli/tests/testdata/lsp/registries/a_v1.0.0.json b/cli/tests/testdata/lsp/registries/a_v1.0.0_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/a_v1.0.0.json rename to cli/tests/testdata/lsp/registries/a_v1.0.0_.json diff --git a/cli/tests/testdata/lsp/registries/a_v1.0.0_b.json b/cli/tests/testdata/lsp/registries/a_v1.0.0_b.json new file mode 100644 index 0000000000..20ec4ad906 --- /dev/null +++ b/cli/tests/testdata/lsp/registries/a_v1.0.0_b.json @@ -0,0 +1,3 @@ +[ + "b/c.ts" +] diff --git a/cli/tests/testdata/lsp/registries/a_v1.0.1.json b/cli/tests/testdata/lsp/registries/a_v1.0.1_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/a_v1.0.1.json rename to cli/tests/testdata/lsp/registries/a_v1.0.1_.json diff --git a/cli/tests/testdata/lsp/registries/a_v2.0.0.json b/cli/tests/testdata/lsp/registries/a_v2.0.0_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/a_v2.0.0.json rename to cli/tests/testdata/lsp/registries/a_v2.0.0_.json diff --git a/cli/tests/testdata/lsp/registries/a_versions.json b/cli/tests/testdata/lsp/registries/a_versions_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/a_versions.json rename to cli/tests/testdata/lsp/registries/a_versions_.json diff --git a/cli/tests/testdata/lsp/registries/a_versions_v1..json b/cli/tests/testdata/lsp/registries/a_versions_v1..json new file mode 100644 index 0000000000..1d8a865c10 --- /dev/null +++ b/cli/tests/testdata/lsp/registries/a_versions_v1..json @@ -0,0 +1,4 @@ +[ + "v1.0.0", + "v1.0.1" +] diff --git a/cli/tests/testdata/lsp/registries/b_latest.json b/cli/tests/testdata/lsp/registries/b_latest_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/b_latest.json rename to cli/tests/testdata/lsp/registries/b_latest_.json diff --git a/cli/tests/testdata/lsp/registries/b_v0.0.1.json b/cli/tests/testdata/lsp/registries/b_v0.0.1_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/b_v0.0.1.json rename to cli/tests/testdata/lsp/registries/b_v0.0.1_.json diff --git a/cli/tests/testdata/lsp/registries/b_v0.0.2.json b/cli/tests/testdata/lsp/registries/b_v0.0.2_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/b_v0.0.2.json rename to cli/tests/testdata/lsp/registries/b_v0.0.2_.json diff --git a/cli/tests/testdata/lsp/registries/b_v0.0.3.json b/cli/tests/testdata/lsp/registries/b_v0.0.3_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/b_v0.0.3.json rename to cli/tests/testdata/lsp/registries/b_v0.0.3_.json diff --git a/cli/tests/testdata/lsp/registries/b_versions.json b/cli/tests/testdata/lsp/registries/b_versions_.json similarity index 100% rename from cli/tests/testdata/lsp/registries/b_versions.json rename to cli/tests/testdata/lsp/registries/b_versions_.json diff --git a/cli/tests/testdata/lsp/registries/deno-import-intellisense.json b/cli/tests/testdata/lsp/registries/deno-import-intellisense.json index f2784d2f69..7fe514dc09 100644 --- a/cli/tests/testdata/lsp/registries/deno-import-intellisense.json +++ b/cli/tests/testdata/lsp/registries/deno-import-intellisense.json @@ -12,11 +12,11 @@ { "key": "version", "documentation": "/lsp/registries/doc_${module}_${{version}}.json", - "url": "/lsp/registries/${module}_versions.json" + "url": "/lsp/registries/${module}_versions_${{version}}.json" }, { "key": "path", - "url": "/lsp/registries/${module}_${{version}}.json" + "url": "/lsp/registries/${module}_${{version}}_${path}.json" } ] }, @@ -30,7 +30,29 @@ }, { "key": "path", - "url": "/lsp/registries/${module}_latest.json" + "url": "/lsp/registries/${module}_latest_${path}.json" + } + ] + }, + { + "schema": "/std@:version?/:path*", + "variables": [ + { + "key": "version", + "url": "/lsp/registries/std_${{version}}.json" + }, + { + "key": "path", + "url": "/lsp/registries/std_${{version}}_${path}.json" + } + ] + }, + { + "schema": "/std/:path*", + "variables": [ + { + "key": "path", + "url": "/lsp/registries/std_latest_${path}.json" } ] }