Skip to content

Commit e1e79cf

Browse files
committed
Take label offets client capability into account
1 parent 6da22ed commit e1e79cf

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct ClientCapsConfig {
127127
pub resolve_code_action: bool,
128128
pub hover_actions: bool,
129129
pub status_notification: bool,
130+
pub signature_help_label_offsets: bool,
130131
}
131132

132133
impl Config {
@@ -302,6 +303,15 @@ impl Config {
302303
{
303304
self.client_caps.code_action_literals = value;
304305
}
306+
if let Some(value) = doc_caps
307+
.signature_help
308+
.as_ref()
309+
.and_then(|it| it.signature_information.as_ref())
310+
.and_then(|it| it.parameter_information.as_ref())
311+
.and_then(|it| it.label_offset_support)
312+
{
313+
self.client_caps.signature_help_label_offsets = value;
314+
}
305315

306316
self.completion.allow_snippets(false);
307317
if let Some(completion) = &doc_caps.completion {

crates/rust-analyzer/src/handlers.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,11 @@ pub(crate) fn handle_signature_help(
557557
None => return Ok(None),
558558
};
559559
let concise = !snap.config.call_info_full;
560-
let res = to_proto::signature_help(call_info, concise);
560+
let res = to_proto::signature_help(
561+
call_info,
562+
concise,
563+
snap.config.client_caps.signature_help_label_offsets,
564+
);
561565
Ok(Some(res))
562566
}
563567

crates/rust-analyzer/src/to_proto.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,58 @@ pub(crate) fn completion_item(
219219
res
220220
}
221221

222-
pub(crate) fn signature_help(call_info: CallInfo, concise: bool) -> lsp_types::SignatureHelp {
223-
let parameters = call_info
224-
.parameter_labels()
225-
.map(|label| lsp_types::ParameterInformation {
226-
label: lsp_types::ParameterLabel::Simple(label.to_string()),
227-
documentation: None,
228-
})
229-
.collect();
222+
pub(crate) fn signature_help(
223+
call_info: CallInfo,
224+
concise: bool,
225+
label_offsets: bool,
226+
) -> lsp_types::SignatureHelp {
227+
let (label, parameters) = match (concise, label_offsets) {
228+
(_, false) => {
229+
let params = call_info
230+
.parameter_labels()
231+
.map(|label| lsp_types::ParameterInformation {
232+
label: lsp_types::ParameterLabel::Simple(label.to_string()),
233+
documentation: None,
234+
})
235+
.collect::<Vec<_>>();
236+
let label =
237+
if concise { call_info.parameter_labels().join(", ") } else { call_info.signature };
238+
(label, params)
239+
}
240+
(false, true) => {
241+
let params = call_info
242+
.parameter_ranges()
243+
.iter()
244+
.map(|it| [u32::from(it.start()).into(), u32::from(it.end()).into()])
245+
.map(|label_offsets| lsp_types::ParameterInformation {
246+
label: lsp_types::ParameterLabel::LabelOffsets(label_offsets),
247+
documentation: None,
248+
})
249+
.collect::<Vec<_>>();
250+
(call_info.signature, params)
251+
}
252+
(true, true) => {
253+
let mut params = Vec::new();
254+
let mut label = String::new();
255+
let mut first = true;
256+
for param in call_info.parameter_labels() {
257+
if !first {
258+
label.push_str(", ");
259+
}
260+
first = false;
261+
let start = label.len() as u64;
262+
label.push_str(param);
263+
let end = label.len() as u64;
264+
params.push(lsp_types::ParameterInformation {
265+
label: lsp_types::ParameterLabel::LabelOffsets([start, end]),
266+
documentation: None,
267+
});
268+
}
269+
270+
(label, params)
271+
}
272+
};
230273

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

0 commit comments

Comments
 (0)