diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index 7e0705e99d..ab2d8000c7 100644 --- a/cli/lsp/completions.rs +++ b/cli/lsp/completions.rs @@ -249,7 +249,7 @@ pub async fn get_import_completions( .collect(); let mut is_incomplete = false; 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) = module_registries.get_origin_completions(&text, &range) @@ -268,20 +268,20 @@ pub async fn get_import_completions( /// map as completion items. fn get_base_import_map_completions( import_map: &ImportMap, + referrer: &ModuleSpecifier, ) -> Vec { import_map - .imports() - .keys() - .map(|key| { + .entries_for_referrer(referrer) + .map(|entry| { // 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 // change the behavior - let mut label = if key.starts_with("file://") { - FILE_PROTO_RE.replace(key, "").to_string() + let mut label = if entry.key.starts_with("file://") { + FILE_PROTO_RE.replace(entry.key, "").to_string() } else { - key.to_string() + entry.key.to_string() }; - let kind = if key.ends_with('/') { + let kind = if entry.key.ends_with('/') { label.pop(); Some(lsp::CompletionItemKind::FOLDER) } else { diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 3d20efbba0..cacd05d9fb 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -1346,23 +1346,27 @@ fn lsp_import_map_import_completions() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); temp_dir.write( - "import-map.json", - r#"{ - "imports": { - "/~/": "./lib/", - "/#/": "./src/", - "fs": "https://example.com/fs/index.js", - "std/": "https://example.com/std@0.123.0/" - } -}"#, + "deno.json", + json!({ + "imports": { + "/~/": "./lib/", + "/#/": "./src/", + "fs": "https://example.com/fs/index.js", + "std/": "https://example.com/std@0.123.0/", + }, + "scopes": { + "file:///": { + "file": "./file.ts", + }, + }, + }) + .to_string(), ); temp_dir.create_dir_all("lib"); temp_dir.write("lib/b.ts", r#"export const b = "b";"#); let mut client = context.new_lsp_command().build(); - client.initialize(|builder| { - builder.set_import_map("import-map.json"); - }); + client.initialize_default(); let uri = temp_dir.uri().join("a.ts").unwrap(); @@ -1403,6 +1407,13 @@ fn lsp_import_map_import_completions() { "insertText": "..", "commitCharacters": ["\"", "'"], }, { + "label": "file", + "kind": 17, + "detail": "(import map)", + "sortText": "file", + "insertText": "file", + "commitCharacters": ["\"", "'"], + }, { "label": "std", "kind": 19, "detail": "(import map)",