[ty] Render `import <...>` in completions when "label details" isn't supported

This fixes a bug where the `import module` part of a completion for
unimported candidates would be missing. This makes it especially
confusing because the user can't tell where the symbol is coming from,
and there is no hint that an `import` statement will be inserted.

Previously, we were using [`CompletionItemLabelDetails`] to render the
`import module` part of the suggestion. But this is only supported in
clients that support version 3.17 (or newer) of the LSP specification.
It turns out that this support isn't widespread yet. In particular,
Heliex doesn't seem to support "label details."

To fix this, we take a [cue from rust-analyzer][rust-analyzer-details].
We detect if the client supports "label details," and if so, use it.
Otherwise, we push the `import module` text into the completion label
itself.

Fixes https://github.com/astral-sh/ruff/pull/20439#issuecomment-3313689568

[`CompletionItemLabelDetails`]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItemLabelDetails
[rust-analyzer-details]: 5d905576d4/crates/rust-analyzer/src/lsp/to_proto.rs (L391-L404)
This commit is contained in:
Andrew Gallant 2025-10-28 11:23:15 -04:00 committed by Andrew Gallant
parent 349061117c
commit 196a68e4c8
2 changed files with 36 additions and 6 deletions

View File

@ -36,6 +36,7 @@ bitflags::bitflags! {
const DIAGNOSTIC_DYNAMIC_REGISTRATION = 1 << 14; const DIAGNOSTIC_DYNAMIC_REGISTRATION = 1 << 14;
const WORKSPACE_CONFIGURATION = 1 << 15; const WORKSPACE_CONFIGURATION = 1 << 15;
const RENAME_DYNAMIC_REGISTRATION = 1 << 16; const RENAME_DYNAMIC_REGISTRATION = 1 << 16;
const COMPLETION_ITEM_LABEL_DETAILS_SUPPORT = 1 << 17;
} }
} }
@ -158,6 +159,11 @@ impl ResolvedClientCapabilities {
self.contains(Self::RENAME_DYNAMIC_REGISTRATION) self.contains(Self::RENAME_DYNAMIC_REGISTRATION)
} }
/// Returns `true` if the client supports "label details" in completion items.
pub(crate) const fn supports_completion_item_label_details(self) -> bool {
self.contains(Self::COMPLETION_ITEM_LABEL_DETAILS_SUPPORT)
}
pub(super) fn new(client_capabilities: &ClientCapabilities) -> Self { pub(super) fn new(client_capabilities: &ClientCapabilities) -> Self {
let mut flags = Self::empty(); let mut flags = Self::empty();
@ -314,6 +320,15 @@ impl ResolvedClientCapabilities {
flags |= Self::WORK_DONE_PROGRESS; flags |= Self::WORK_DONE_PROGRESS;
} }
if text_document
.and_then(|text_document| text_document.completion.as_ref())
.and_then(|completion| completion.completion_item.as_ref())
.and_then(|completion_item| completion_item.label_details_support)
.unwrap_or_default()
{
flags |= Self::COMPLETION_ITEM_LABEL_DETAILS_SUPPORT;
}
flags flags
} }
} }

View File

@ -81,15 +81,30 @@ impl BackgroundDocumentRequestHandler for CompletionRequestHandler {
new_text: edit.content().map(ToString::to_string).unwrap_or_default(), new_text: edit.content().map(ToString::to_string).unwrap_or_default(),
} }
}); });
let name = comp.name.to_string();
let import_suffix = comp.module_name.map(|name| format!(" (import {name})"));
let (label, label_details) = if snapshot
.resolved_client_capabilities()
.supports_completion_item_label_details()
{
let label_details = CompletionItemLabelDetails {
detail: import_suffix,
description: type_display.clone(),
};
(name, Some(label_details))
} else {
let label = import_suffix
.map(|suffix| format!("{name}{suffix}"))
.unwrap_or_else(|| name);
(label, None)
};
CompletionItem { CompletionItem {
label: comp.name.into(), label,
kind, kind,
sort_text: Some(format!("{i:-max_index_len$}")), sort_text: Some(format!("{i:-max_index_len$}")),
detail: type_display.clone(), detail: type_display,
label_details: Some(CompletionItemLabelDetails { label_details,
detail: comp.module_name.map(|name| format!(" (import {name})")),
description: type_display,
}),
insert_text: comp.insert.map(String::from), insert_text: comp.insert.map(String::from),
additional_text_edits: import_edit.map(|edit| vec![edit]), additional_text_edits: import_edit.map(|edit| vec![edit]),
documentation: comp documentation: comp