Skip to content

Commit 615abb3

Browse files
committed
Improve completion label details display
1 parent cccc7ca commit 615abb3

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

crates/ide-completion/src/item.rs

Lines changed: 17 additions & 4 deletions
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/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 1 addition & 1 deletion
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)