Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve completions properly #18212

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,14 @@ impl LanguageServer {
snippet_support: Some(true),
resolve_support: Some(CompletionItemCapabilityResolveSupport {
properties: vec![
"documentation".to_string(),
"additionalTextEdits".to_string(),
"command".to_string(),
"detail".to_string(),
"documentation".to_string(),
"filterText".to_string(),
"labelDetails".to_string(),
"tags".to_string(),
"textEdit".to_string(),
],
}),
insert_replace_support: Some(true),
Expand Down
37 changes: 27 additions & 10 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,10 +1615,6 @@ impl LspStore {
let (server_id, completion) = {
let completions_guard = completions.read();
let completion = &completions_guard[completion_index];
if completion.documentation.is_some() {
continue;
}

did_resolve = true;
let server_id = completion.server_id;
let completion = completion.lsp_completion.clone();
Expand All @@ -1643,10 +1639,6 @@ impl LspStore {
let (server_id, completion) = {
let completions_guard = completions.read();
let completion = &completions_guard[completion_index];
if completion.documentation.is_some() {
continue;
}

let server_id = completion.server_id;
let completion = completion.lsp_completion.clone();

Expand Down Expand Up @@ -1743,6 +1735,10 @@ impl LspStore {
completion.lsp_completion.insert_text_format = completion_item.insert_text_format;
}
}

let mut completions = completions.write();
let completion = &mut completions[completion_index];
completion.lsp_completion = completion_item;
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -1771,6 +1767,10 @@ impl LspStore {
else {
return;
};
let Some(lsp_completion) = serde_json::from_slice(&response.lsp_completion).log_err()
else {
return;
};

let documentation = if response.documentation.is_empty() {
Documentation::Undocumented
Expand All @@ -1787,6 +1787,7 @@ impl LspStore {
let mut completions = completions.write();
let completion = &mut completions[completion_index];
completion.documentation = Some(documentation);
completion.lsp_completion = lsp_completion;

let old_range = response
.old_start
Expand Down Expand Up @@ -4192,17 +4193,32 @@ impl LspStore {
let lsp_completion = serde_json::from_slice(&envelope.payload.lsp_completion)?;

let completion = this
.read_with(&cx, |this, _| {
.read_with(&cx, |this, cx| {
let id = LanguageServerId(envelope.payload.language_server_id as usize);
let Some(server) = this.language_server_for_id(id) else {
return Err(anyhow!("No language server {id}"));
};

Ok(server.request::<lsp::request::ResolveCompletionItem>(lsp_completion))
Ok(cx.background_executor().spawn(async move {
let can_resolve = server
.capabilities()
.completion_provider
.as_ref()
.and_then(|options| options.resolve_provider)
.unwrap_or(false);
if can_resolve {
server
.request::<lsp::request::ResolveCompletionItem>(lsp_completion)
.await
} else {
anyhow::Ok(lsp_completion)
}
}))
})??
.await?;

let mut documentation_is_markdown = false;
let lsp_completion = serde_json::to_string(&completion)?.into_bytes();
let documentation = match completion.documentation {
Some(lsp::Documentation::String(text)) => text,

Expand Down Expand Up @@ -4244,6 +4260,7 @@ impl LspStore {
old_start,
old_end,
new_text,
lsp_completion,
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/proto/proto/zed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ message ResolveCompletionDocumentationResponse {
Anchor old_start = 3;
Anchor old_end = 4;
string new_text = 5;
bytes lsp_completion = 6;
}

message ResolveInlayHint {
Expand Down
Loading