diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2022-01-02 16:25:42 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-02 16:25:42 +1100 |
commit | 0ae46a975cd32301ee01df4eafb23c9db52f1198 (patch) | |
tree | 2f7e66b6ca3bc94b23fe07f75fbf36582dfa753a /cli/lsp/registries.rs | |
parent | 003534e8836043892499bac86424e059fc14c2f1 (diff) |
fix(lsp): properly generate data URLs for completion items (#13246)
Diffstat (limited to 'cli/lsp/registries.rs')
-rw-r--r-- | cli/lsp/registries.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index 912a6a9de..9b68b8850 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<Value> { + 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 |