Skip to content

Commit 8733728

Browse files
committed
Auto merge of #15956 - ahlinc:label-detail, r=lnicola
Improve completion label details display This PR improves completion label details display by separating trait and alias info from the `label` LSP field to an additional existing `label_detail` field. Changes look like the following: ### Before ![Screenshot from 2023-11-24 12-03-08](https://github.com/rust-lang/rust-analyzer/assets/14666676/74066f0d-f1ac-4e99-8be5-c5141d513d23) ### After ![Screenshot from 2023-11-24 12-21-57](https://github.com/rust-lang/rust-analyzer/assets/14666676/45fca112-4612-40a3-81b9-07ff12de0962) _All existing tests are passed without any changes in test themselves logic._
2 parents cccc7ca + f0adf8c commit 8733728

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

crates/ide-completion/src/item.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ use crate::{
2626
pub struct CompletionItem {
2727
/// Label in the completion pop up which identifies completion.
2828
pub label: SmolStr,
29+
/// Addition label details in the completion pop up that are
30+
/// displayed and aligned on the right side after the label.
31+
pub label_detail: Option<SmolStr>,
32+
2933
/// Range of identifier that is being completed.
3034
///
3135
/// It should be used primarily for UI, but we also use this to convert
@@ -425,13 +429,14 @@ impl Builder {
425429
pub(crate) fn build(self, db: &RootDatabase) -> CompletionItem {
426430
let _p = profile::span("item::Builder::build");
427431

428-
let mut label = self.label;
432+
let label = self.label;
433+
let mut label_detail = None;
429434
let mut lookup = self.lookup.unwrap_or_else(|| label.clone());
430435
let insert_text = self.insert_text.unwrap_or_else(|| label.to_string());
431436

432437
if !self.doc_aliases.is_empty() {
433438
let doc_aliases = self.doc_aliases.iter().join(", ");
434-
label = SmolStr::from(format!("{label} (alias {doc_aliases})"));
439+
label_detail.replace(SmolStr::from(format!(" (alias {doc_aliases})")));
435440
let lookup_doc_aliases = self
436441
.doc_aliases
437442
.iter()
@@ -454,10 +459,17 @@ impl Builder {
454459
if let [import_edit] = &*self.imports_to_add {
455460
// snippets can have multiple imports, but normal completions only have up to one
456461
if let Some(original_path) = import_edit.original_path.as_ref() {
457-
label = SmolStr::from(format!("{label} (use {})", original_path.display(db)));
462+
label_detail.replace(SmolStr::from(format!(
463+
"{} (use {})",
464+
label_detail.as_deref().unwrap_or_default(),
465+
original_path.display(db)
466+
)));
458467
}
459468
} else if let Some(trait_name) = self.trait_name {
460-
label = SmolStr::from(format!("{label} (as {trait_name})"));
469+
label_detail.replace(SmolStr::from(format!(
470+
"{} (as {trait_name})",
471+
label_detail.as_deref().unwrap_or_default(),
472+
)));
461473
}
462474

463475
let text_edit = match self.text_edit {
@@ -479,6 +491,7 @@ impl Builder {
479491
CompletionItem {
480492
source_range: self.source_range,
481493
label,
494+
label_detail,
482495
text_edit,
483496
is_snippet: self.is_snippet,
484497
detail: self.detail,

crates/ide-completion/src/render.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,11 @@ mod tests {
557557

558558
let tag = it.kind.tag();
559559
let relevance = display_relevance(it.relevance);
560-
items.push(format!("{tag} {} {relevance}\n", it.label));
560+
items.push(format!(
561+
"{tag} {}{} {relevance}\n",
562+
it.label,
563+
it.label_detail.clone().unwrap_or_default(),
564+
));
561565

562566
if let Some((label, _indel, relevance)) = it.ref_match() {
563567
let relevance = display_relevance(relevance);

crates/ide-completion/src/tests.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,29 @@ fn render_completion_list(completions: Vec<CompletionItem>) -> String {
150150
fn monospace_width(s: &str) -> usize {
151151
s.chars().count()
152152
}
153-
let label_width =
154-
completions.iter().map(|it| monospace_width(&it.label)).max().unwrap_or_default().min(22);
153+
let label_width = completions
154+
.iter()
155+
.map(|it| {
156+
monospace_width(&it.label)
157+
+ monospace_width(it.label_detail.as_deref().unwrap_or_default())
158+
})
159+
.max()
160+
.unwrap_or_default()
161+
.min(22);
155162
completions
156163
.into_iter()
157164
.map(|it| {
158165
let tag = it.kind.tag();
159166
let var_name = format!("{tag} {}", it.label);
160167
let mut buf = var_name;
168+
if let Some(ref label_detail) = it.label_detail {
169+
format_to!(buf, "{label_detail}");
170+
}
161171
if let Some(detail) = it.detail {
162-
let width = label_width.saturating_sub(monospace_width(&it.label));
172+
let width = label_width.saturating_sub(
173+
monospace_width(&it.label)
174+
+ monospace_width(&it.label_detail.unwrap_or_default()),
175+
);
163176
format_to!(buf, "{:width$} {}", "", detail, width = width);
164177
}
165178
if it.deprecated {

crates/rust-analyzer/src/lsp/to_proto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn completion_item(
301301

302302
if config.completion_label_details_support() {
303303
lsp_item.label_details = Some(lsp_types::CompletionItemLabelDetails {
304-
detail: None,
304+
detail: item.label_detail.as_ref().map(ToString::to_string),
305305
description: lsp_item.detail.clone(),
306306
});
307307
}

0 commit comments

Comments
 (0)