From 0ae46a975cd32301ee01df4eafb23c9db52f1198 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Sun, 2 Jan 2022 16:25:42 +1100 Subject: [PATCH] fix(lsp): properly generate data URLs for completion items (#13246) --- cli/lsp/registries.rs | 42 ++++++++++++++++++- .../registries/deno-import-intellisense.json | 1 + 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index 912a6a9ded..9b68b8850c 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -152,6 +152,31 @@ fn get_data( .map(|specifier| json!({ "documentation": specifier })) } +/// Generate a data value for a completion item that will instruct the client to +/// resolve the completion item to obtain further information, in this case, the +/// details/documentation endpoint for the item if it exists in the registry +/// configuration when there is a match result that should be interpolated +fn get_data_with_match( + registry: &RegistryConfiguration, + base: &ModuleSpecifier, + tokens: &[Token], + match_result: &MatchResult, + variable: &Key, + value: &str, +) -> Option { + let url = registry.get_documentation_url_for_key(variable)?; + get_endpoint_with_match( + variable, + url, + base, + tokens, + match_result, + Some(value), + ) + .ok() + .map(|specifier| json!({ "documentation": specifier })) +} + /// Convert a single variable templated string into a fully qualified URL which /// can be fetched to provide additional data. fn get_endpoint( @@ -667,8 +692,14 @@ impl ModuleRegistry { let sort_text = Some(format!("{:0>10}", idx + 1)); let preselect = get_preselect(item.clone(), preselect.clone()); - let data = - get_data(registry, &specifier, &key, &item); + let data = get_data_with_match( + registry, + &specifier, + &tokens, + &match_result, + &key, + &item, + ); completions.insert( item, lsp::CompletionItem { @@ -1282,6 +1313,12 @@ mod tests { assert!(completions.is_some()); let completions = completions.unwrap().items; assert_eq!(completions.len(), 3); + 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, @@ -1292,6 +1329,7 @@ mod tests { character: 53, }, }; + let completions = module_registry .get_completions("http://localhost:4545/x/a@v1.0.0/", 33, &range, |_| { false diff --git a/cli/tests/testdata/lsp/registries/deno-import-intellisense.json b/cli/tests/testdata/lsp/registries/deno-import-intellisense.json index cdd71a7cf5..f2784d2f69 100644 --- a/cli/tests/testdata/lsp/registries/deno-import-intellisense.json +++ b/cli/tests/testdata/lsp/registries/deno-import-intellisense.json @@ -11,6 +11,7 @@ }, { "key": "version", + "documentation": "/lsp/registries/doc_${module}_${{version}}.json", "url": "/lsp/registries/${module}_versions.json" }, {