1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(lsp): include scoped import map keys in completions (#25047)

This commit is contained in:
Nayeem Rahman 2024-08-20 19:38:47 +01:00 committed by GitHub
parent a7c8bb1596
commit acba2cd48c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 20 deletions

View file

@ -249,7 +249,7 @@ pub async fn get_import_completions(
.collect(); .collect();
let mut is_incomplete = false; let mut is_incomplete = false;
if let Some(import_map) = maybe_import_map { if let Some(import_map) = maybe_import_map {
items.extend(get_base_import_map_completions(import_map)); items.extend(get_base_import_map_completions(import_map, specifier));
} }
if let Some(origin_items) = if let Some(origin_items) =
module_registries.get_origin_completions(&text, &range) module_registries.get_origin_completions(&text, &range)
@ -268,20 +268,20 @@ pub async fn get_import_completions(
/// map as completion items. /// map as completion items.
fn get_base_import_map_completions( fn get_base_import_map_completions(
import_map: &ImportMap, import_map: &ImportMap,
referrer: &ModuleSpecifier,
) -> Vec<lsp::CompletionItem> { ) -> Vec<lsp::CompletionItem> {
import_map import_map
.imports() .entries_for_referrer(referrer)
.keys() .map(|entry| {
.map(|key| {
// for some strange reason, keys that start with `/` get stored in the // for some strange reason, keys that start with `/` get stored in the
// import map as `file:///`, and so when we pull the keys out, we need to // import map as `file:///`, and so when we pull the keys out, we need to
// change the behavior // change the behavior
let mut label = if key.starts_with("file://") { let mut label = if entry.key.starts_with("file://") {
FILE_PROTO_RE.replace(key, "").to_string() FILE_PROTO_RE.replace(entry.key, "").to_string()
} else { } else {
key.to_string() entry.key.to_string()
}; };
let kind = if key.ends_with('/') { let kind = if entry.key.ends_with('/') {
label.pop(); label.pop();
Some(lsp::CompletionItemKind::FOLDER) Some(lsp::CompletionItemKind::FOLDER)
} else { } else {

View file

@ -1346,23 +1346,27 @@ fn lsp_import_map_import_completions() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
"import-map.json", "deno.json",
r#"{ json!({
"imports": { "imports": {
"/~/": "./lib/", "/~/": "./lib/",
"/#/": "./src/", "/#/": "./src/",
"fs": "https://example.com/fs/index.js", "fs": "https://example.com/fs/index.js",
"std/": "https://example.com/std@0.123.0/" "std/": "https://example.com/std@0.123.0/",
} },
}"#, "scopes": {
"file:///": {
"file": "./file.ts",
},
},
})
.to_string(),
); );
temp_dir.create_dir_all("lib"); temp_dir.create_dir_all("lib");
temp_dir.write("lib/b.ts", r#"export const b = "b";"#); temp_dir.write("lib/b.ts", r#"export const b = "b";"#);
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize_default();
builder.set_import_map("import-map.json");
});
let uri = temp_dir.uri().join("a.ts").unwrap(); let uri = temp_dir.uri().join("a.ts").unwrap();
@ -1403,6 +1407,13 @@ fn lsp_import_map_import_completions() {
"insertText": "..", "insertText": "..",
"commitCharacters": ["\"", "'"], "commitCharacters": ["\"", "'"],
}, { }, {
"label": "file",
"kind": 17,
"detail": "(import map)",
"sortText": "file",
"insertText": "file",
"commitCharacters": ["\"", "'"],
}, {
"label": "std", "label": "std",
"kind": 19, "kind": 19,
"detail": "(import map)", "detail": "(import map)",