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:
tomoikey 2024-07-11 19:38:33 +09:00 committed by GitHub
parent 6a11184ea3
commit 291d64c803
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 1994 additions and 11 deletions

View file

@ -1,3 +1,5 @@
mod signature_help;
use crate::{
CodeAction, CoreCompletion, DocumentHighlight, Hover, HoverBlock, HoverBlockKind, InlayHint,
InlayHintLabel, InlayHintLabelPart, InlayHintLabelPartTooltip, InlayHintTooltip, Location,
@ -6,10 +8,12 @@ use crate::{
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::proto::{self, PeerId};
use clock::Global;
use futures::future;
use gpui::{AppContext, AsyncAppContext, Model};
use gpui::{AppContext, AsyncAppContext, FontWeight, Model};
use language::{
language_settings::{language_settings, InlayHintKind},
markdown::{MarkdownHighlight, MarkdownHighlightStyle},
point_from_lsp, point_to_lsp,
proto::{deserialize_anchor, deserialize_version, serialize_anchor, serialize_version},
range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, BufferSnapshot, CachedLspAdapter, CharKind,
@ -23,6 +27,10 @@ use lsp::{
use std::{cmp::Reverse, ops::Range, path::Path, sync::Arc};
use text::{BufferId, LineEnding};
pub use signature_help::{
SignatureHelp, SIGNATURE_HELP_HIGHLIGHT_CURRENT, SIGNATURE_HELP_HIGHLIGHT_OVERLOAD,
};
pub fn lsp_formatting_options(tab_size: u32) -> lsp::FormattingOptions {
lsp::FormattingOptions {
tab_size,
@ -121,6 +129,11 @@ pub(crate) struct GetDocumentHighlights {
pub position: PointUtf16,
}
#[derive(Clone)]
pub(crate) struct GetSignatureHelp {
pub position: PointUtf16,
}
#[derive(Clone)]
pub(crate) struct GetHover {
pub position: PointUtf16,
@ -1225,6 +1238,164 @@ impl LspCommand for GetDocumentHighlights {
}
}
#[async_trait(?Send)]
impl LspCommand for GetSignatureHelp {
type Response = Vec<SignatureHelp>;
type LspRequest = lsp::SignatureHelpRequest;
type ProtoRequest = proto::GetSignatureHelp;
fn check_capabilities(&self, capabilities: &ServerCapabilities) -> bool {
capabilities.signature_help_provider.is_some()
}
fn to_lsp(
&self,
path: &Path,
_: &Buffer,
_: &Arc<LanguageServer>,
_cx: &AppContext,
) -> lsp::SignatureHelpParams {
let url_result = lsp::Url::from_file_path(path);
if url_result.is_err() {
log::error!("an invalid file path has been specified");
}
lsp::SignatureHelpParams {
text_document_position_params: lsp::TextDocumentPositionParams {
text_document: lsp::TextDocumentIdentifier {
uri: url_result.expect("invalid file path"),
},
position: point_to_lsp(self.position),
},
context: None,
work_done_progress_params: Default::default(),
}
}
async fn response_from_lsp(
self,
message: Option<lsp::SignatureHelp>,
_: Model<Project>,
buffer: Model<Buffer>,
_: LanguageServerId,
mut cx: AsyncAppContext,
) -> Result<Self::Response> {
let language = buffer.update(&mut cx, |buffer, _| buffer.language().cloned())?;
Ok(message
.into_iter()
.filter_map(|message| SignatureHelp::new(message, language.clone()))
.collect())
}
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest {
let offset = buffer.point_utf16_to_offset(self.position);
proto::GetSignatureHelp {
project_id,
buffer_id: buffer.remote_id().to_proto(),
position: Some(serialize_anchor(&buffer.anchor_after(offset))),
version: serialize_version(&buffer.version()),
}
}
async fn from_proto(
payload: Self::ProtoRequest,
_: Model<Project>,
buffer: Model<Buffer>,
mut cx: AsyncAppContext,
) -> Result<Self> {
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&payload.version))
})?
.await
.with_context(|| format!("waiting for version for buffer {}", buffer.entity_id()))?;
let buffer_snapshot = buffer.update(&mut cx, |buffer, _| buffer.snapshot())?;
Ok(Self {
position: payload
.position
.and_then(deserialize_anchor)
.context("invalid position")?
.to_point_utf16(&buffer_snapshot),
})
}
fn response_to_proto(
response: Self::Response,
_: &mut Project,
_: PeerId,
_: &Global,
_: &mut AppContext,
) -> proto::GetSignatureHelpResponse {
proto::GetSignatureHelpResponse {
entries: response
.into_iter()
.map(|signature_help| proto::SignatureHelp {
rendered_text: signature_help.markdown,
highlights: signature_help
.highlights
.into_iter()
.filter_map(|(range, highlight)| {
let MarkdownHighlight::Style(highlight) = highlight else {
return None;
};
Some(proto::HighlightedRange {
range: Some(proto::Range {
start: range.start as u64,
end: range.end as u64,
}),
highlight: Some(proto::MarkdownHighlight {
italic: highlight.italic,
underline: highlight.underline,
strikethrough: highlight.strikethrough,
weight: highlight.weight.0,
}),
})
})
.collect(),
})
.collect(),
}
}
async fn response_from_proto(
self,
response: proto::GetSignatureHelpResponse,
_: Model<Project>,
_: Model<Buffer>,
_: AsyncAppContext,
) -> Result<Self::Response> {
Ok(response
.entries
.into_iter()
.map(|proto_entry| SignatureHelp {
markdown: proto_entry.rendered_text,
highlights: proto_entry
.highlights
.into_iter()
.filter_map(|highlight| {
let proto_highlight = highlight.highlight?;
let range = highlight.range?;
Some((
range.start as usize..range.end as usize,
MarkdownHighlight::Style(MarkdownHighlightStyle {
italic: proto_highlight.italic,
underline: proto_highlight.underline,
strikethrough: proto_highlight.strikethrough,
weight: FontWeight(proto_highlight.weight),
}),
))
})
.collect(),
})
.collect())
}
fn buffer_id_from_proto(message: &Self::ProtoRequest) -> Result<BufferId> {
BufferId::new(message.buffer_id)
}
}
#[async_trait(?Send)]
impl LspCommand for GetHover {
type Response = Option<Hover>;