lsp: Implement textDocument/signatureHelp
for ProjectClientState::Local
environment (#12909)
Closes https://github.com/zed-industries/zed/issues/5155 Closes https://github.com/zed-industries/zed/issues/4879 # Purpose There was no way to know what to put in function signatures or struct fields other than hovering at the moment. Therefore, it was necessary to implement LSP's `textDocument/signatureHelp`. I tried my best to match the surrounding coding style, but since this is my first contribution, I believe there are various aspects that may be lacking. I would greatly appreciate your code review. # Description When the window is displayed, the current argument or field at the cursor's position is automatically bolded. If the cursor moves and there is nothing to display, the window closes automatically. To minimize changes and reduce the burden of review and debugging, the SignatureHelp feature is implemented only when `is_local` is `true`. Some `unimplemented!()` macros are embedded, but rest assured that they are not called in this implementation. # How to try it out Press `cmd + i` (MacOS), `ctrl + i` (Linux). # Enable auto signature help (2 ways) ### Add `"auto_signature_help": true` to `settings.json` <img width="426" alt="image" src="https://github.com/zed-industries/zed/assets/55743826/61310c39-47f9-4586-94b0-ae519dc3b37c"> Or ### Press `Auto Signature Help`. (Default `false`) <img width="226" alt="image" src="https://github.com/zed-industries/zed/assets/55743826/34155215-1eb5-4621-b09b-55df2f1ab6a8"> # Disable to show signature help after completion ### Add `"show_signature_help_after_completion": false` to `settings.json` <img width="438" alt="image" src="https://github.com/zed-industries/zed/assets/55743826/5e5bacac-62e0-4921-9243-17e1e72d5eb6"> # Movie https://github.com/zed-industries/zed/assets/55743826/77c12d51-b0a5-415d-8901-f93ef92098e7 # Screen Shot <img width="628" alt="image" src="https://github.com/zed-industries/zed/assets/55743826/3ebcf4b6-2b94-4dea-97f9-ac4f33e0291e"> <img width="637" alt="image" src="https://github.com/zed-industries/zed/assets/55743826/6dc3eb4d-beee-460b-8dbe-d6eec6379b76"> Release Notes: - Show function signature popovers ([4879](https://github.com/zed-industries/zed/issues/4879), [5155](https://github.com/zed-industries/zed/issues/5155)) --------- Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
parent
6a11184ea3
commit
291d64c803
19 changed files with 1994 additions and 11 deletions
|
@ -709,6 +709,7 @@ impl Project {
|
|||
client.add_model_request_handler(Self::handle_task_context_for_location);
|
||||
client.add_model_request_handler(Self::handle_task_templates);
|
||||
client.add_model_request_handler(Self::handle_lsp_command::<LinkedEditingRange>);
|
||||
client.add_model_request_handler(Self::handle_signature_help);
|
||||
}
|
||||
|
||||
pub fn local(
|
||||
|
@ -5778,6 +5779,63 @@ impl Project {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn signature_help<T: ToPointUtf16>(
|
||||
&self,
|
||||
buffer: &Model<Buffer>,
|
||||
position: T,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Vec<SignatureHelp>> {
|
||||
let position = position.to_point_utf16(buffer.read(cx));
|
||||
if self.is_local() {
|
||||
let all_actions_task = self.request_multiple_lsp_locally(
|
||||
buffer,
|
||||
Some(position),
|
||||
|server_capabilities| server_capabilities.signature_help_provider.is_some(),
|
||||
GetSignatureHelp { position },
|
||||
cx,
|
||||
);
|
||||
cx.spawn(|_, _| async move {
|
||||
all_actions_task
|
||||
.await
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|help| !help.markdown.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
} else if let Some(project_id) = self.remote_id() {
|
||||
let position_anchor = buffer
|
||||
.read(cx)
|
||||
.anchor_at(buffer.read(cx).point_utf16_to_offset(position), Bias::Right);
|
||||
let request = self.client.request(proto::GetSignatureHelp {
|
||||
project_id,
|
||||
position: Some(serialize_anchor(&position_anchor)),
|
||||
buffer_id: buffer.read(cx).remote_id().to_proto(),
|
||||
version: serialize_version(&buffer.read(cx).version()),
|
||||
});
|
||||
let buffer = buffer.clone();
|
||||
cx.spawn(move |project, cx| async move {
|
||||
let Some(response) = request.await.log_err() else {
|
||||
return Vec::new();
|
||||
};
|
||||
let Some(project) = project.upgrade() else {
|
||||
return Vec::new();
|
||||
};
|
||||
GetSignatureHelp::response_from_proto(
|
||||
GetSignatureHelp { position },
|
||||
response,
|
||||
project,
|
||||
buffer,
|
||||
cx,
|
||||
)
|
||||
.await
|
||||
.log_err()
|
||||
.unwrap_or_default()
|
||||
})
|
||||
} else {
|
||||
Task::ready(Vec::new())
|
||||
}
|
||||
}
|
||||
|
||||
fn hover_impl(
|
||||
&self,
|
||||
buffer: &Model<Buffer>,
|
||||
|
@ -9851,6 +9909,43 @@ impl Project {
|
|||
Ok(proto::TaskTemplatesResponse { templates })
|
||||
}
|
||||
|
||||
async fn handle_signature_help(
|
||||
project: Model<Self>,
|
||||
envelope: TypedEnvelope<proto::GetSignatureHelp>,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> Result<proto::GetSignatureHelpResponse> {
|
||||
let sender_id = envelope.original_sender_id()?;
|
||||
let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
|
||||
let buffer = project.update(&mut cx, |project, _| {
|
||||
project
|
||||
.opened_buffers
|
||||
.get(&buffer_id)
|
||||
.and_then(|buffer| buffer.upgrade())
|
||||
.with_context(|| format!("unknown buffer id {}", envelope.payload.buffer_id))
|
||||
})??;
|
||||
let response = GetSignatureHelp::from_proto(
|
||||
envelope.payload.clone(),
|
||||
project.clone(),
|
||||
buffer.clone(),
|
||||
cx.clone(),
|
||||
)
|
||||
.await?;
|
||||
let help_response = project
|
||||
.update(&mut cx, |project, cx| {
|
||||
project.signature_help(&buffer, response.position, cx)
|
||||
})?
|
||||
.await;
|
||||
project.update(&mut cx, |project, cx| {
|
||||
GetSignatureHelp::response_to_proto(
|
||||
help_response,
|
||||
project,
|
||||
sender_id,
|
||||
&buffer.read(cx).version(),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async fn try_resolve_code_action(
|
||||
lang_server: &LanguageServer,
|
||||
action: &mut CodeAction,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue