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();
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<lsp::CompletionItem> {
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 {

View file

@ -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#"{
"deno.json",
json!({
"imports": {
"/~/": "./lib/",
"/#/": "./src/",
"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.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();
@ -1402,6 +1406,13 @@ fn lsp_import_map_import_completions() {
"sortText": "1",
"insertText": "..",
"commitCharacters": ["\"", "'"],
}, {
"label": "file",
"kind": 17,
"detail": "(import map)",
"sortText": "file",
"insertText": "file",
"commitCharacters": ["\"", "'"],
}, {
"label": "std",
"kind": 19,