Skip to content

Commit

Permalink
Take label offets client capability into account
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jul 16, 2020
1 parent 6da22ed commit e1e79cf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
10 changes: 10 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct ClientCapsConfig {
pub resolve_code_action: bool,
pub hover_actions: bool,
pub status_notification: bool,
pub signature_help_label_offsets: bool,
}

impl Config {
Expand Down Expand Up @@ -302,6 +303,15 @@ impl Config {
{
self.client_caps.code_action_literals = value;
}
if let Some(value) = doc_caps
.signature_help
.as_ref()
.and_then(|it| it.signature_information.as_ref())
.and_then(|it| it.parameter_information.as_ref())
.and_then(|it| it.label_offset_support)
{
self.client_caps.signature_help_label_offsets = value;
}

self.completion.allow_snippets(false);
if let Some(completion) = &doc_caps.completion {
Expand Down
6 changes: 5 additions & 1 deletion crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,11 @@ pub(crate) fn handle_signature_help(
None => return Ok(None),
};
let concise = !snap.config.call_info_full;
let res = to_proto::signature_help(call_info, concise);
let res = to_proto::signature_help(
call_info,
concise,
snap.config.client_caps.signature_help_label_offsets,
);
Ok(Some(res))
}

Expand Down
60 changes: 51 additions & 9 deletions crates/rust-analyzer/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,58 @@ pub(crate) fn completion_item(
res
}

pub(crate) fn signature_help(call_info: CallInfo, concise: bool) -> lsp_types::SignatureHelp {
let parameters = call_info
.parameter_labels()
.map(|label| lsp_types::ParameterInformation {
label: lsp_types::ParameterLabel::Simple(label.to_string()),
documentation: None,
})
.collect();
pub(crate) fn signature_help(
call_info: CallInfo,
concise: bool,
label_offsets: bool,
) -> lsp_types::SignatureHelp {
let (label, parameters) = match (concise, label_offsets) {
(_, false) => {
let params = call_info
.parameter_labels()
.map(|label| lsp_types::ParameterInformation {
label: lsp_types::ParameterLabel::Simple(label.to_string()),
documentation: None,
})
.collect::<Vec<_>>();
let label =
if concise { call_info.parameter_labels().join(", ") } else { call_info.signature };
(label, params)
}
(false, true) => {
let params = call_info
.parameter_ranges()
.iter()
.map(|it| [u32::from(it.start()).into(), u32::from(it.end()).into()])
.map(|label_offsets| lsp_types::ParameterInformation {
label: lsp_types::ParameterLabel::LabelOffsets(label_offsets),
documentation: None,
})
.collect::<Vec<_>>();
(call_info.signature, params)
}
(true, true) => {
let mut params = Vec::new();
let mut label = String::new();
let mut first = true;
for param in call_info.parameter_labels() {
if !first {
label.push_str(", ");
}
first = false;
let start = label.len() as u64;
label.push_str(param);
let end = label.len() as u64;
params.push(lsp_types::ParameterInformation {
label: lsp_types::ParameterLabel::LabelOffsets([start, end]),
documentation: None,
});
}

(label, params)
}
};

let label = if concise { call_info.parameter_labels().join(", ") } else { call_info.signature };
let documentation = call_info.doc.map(|doc| {
lsp_types::Documentation::MarkupContent(lsp_types::MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
Expand Down

0 comments on commit e1e79cf

Please sign in to comment.