Add label_for_completion to extension API (#10175)

This PR adds the ability for extensions to implement
`label_for_completion` to customize completions coming back from the
language server.

We've used the Gleam extension as a motivating example, adding
`label_for_completion` support to it.

Release Notes:

- N/A

---------

Co-authored-by: Max <max@zed.dev>
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Marshall Bowers 2024-04-04 13:56:04 -04:00 committed by GitHub
parent 0f1c2e6f2b
commit d306b531c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 1124 additions and 252 deletions

View file

@ -619,6 +619,53 @@ async fn test_extension_store_with_gleam_extension(cx: &mut TestAppContext) {
]
);
// The extension creates custom labels for completion items.
fake_server.handle_request::<lsp::request::Completion, _, _>(|_, _| async move {
Ok(Some(lsp::CompletionResponse::Array(vec![
lsp::CompletionItem {
label: "foo".into(),
kind: Some(lsp::CompletionItemKind::FUNCTION),
detail: Some("fn() -> Result(Nil, Error)".into()),
..Default::default()
},
lsp::CompletionItem {
label: "bar.baz".into(),
kind: Some(lsp::CompletionItemKind::FUNCTION),
detail: Some("fn(List(a)) -> a".into()),
..Default::default()
},
lsp::CompletionItem {
label: "Quux".into(),
kind: Some(lsp::CompletionItemKind::CONSTRUCTOR),
detail: Some("fn(String) -> T".into()),
..Default::default()
},
lsp::CompletionItem {
label: "my_string".into(),
kind: Some(lsp::CompletionItemKind::CONSTANT),
detail: Some("String".into()),
..Default::default()
},
])))
});
let completion_labels = project
.update(cx, |project, cx| project.completions(&buffer, 0, cx))
.await
.unwrap()
.into_iter()
.map(|c| c.label.text)
.collect::<Vec<_>>();
assert_eq!(
completion_labels,
[
"foo: fn() -> Result(Nil, Error)".to_string(),
"bar.baz: fn(List(a)) -> a".to_string(),
"Quux: fn(String) -> T".to_string(),
"my_string: String".to_string(),
]
);
// Simulate a new version of the language server being released
language_server_version.lock().version = "v2.0.0".into();
language_server_version.lock().binary_contents = "the-new-binary-contents".into();