From 8d632958db199ffc6061be0b1a1fa6c1289ac34d Mon Sep 17 00:00:00 2001 From: Caleb Van Dyke Date: Mon, 10 Nov 2025 06:28:12 -0600 Subject: [PATCH] Add better labels for completions for ty lsp (#42233) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Verified that this works locally. I modeled it after how basedpyright and pyright work. Here is a screenshot of what it looks like (issue has screenshots of the old state): Screenshot 2025-11-07 at 2 40 50 PM Closes #42232 Release Notes: - python/ty: Code completion menu now shows packages that will be imported when a given entry is accepted. --- crates/languages/src/python.rs | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/crates/languages/src/python.rs b/crates/languages/src/python.rs index 3f25a5c5ce50d0aade7b1b575443b5d681f67c63..03ce559b87bb5f318758735c5903bfc51b7c1267 100644 --- a/crates/languages/src/python.rs +++ b/crates/languages/src/python.rs @@ -163,6 +163,45 @@ impl LspAdapter for TyLspAdapter { Self::SERVER_NAME } + async fn label_for_completion( + &self, + item: &lsp::CompletionItem, + language: &Arc, + ) -> Option { + let label = &item.label; + let label_len = label.len(); + let grammar = language.grammar()?; + let highlight_id = match item.kind? { + lsp::CompletionItemKind::METHOD => grammar.highlight_id_for_name("function.method"), + lsp::CompletionItemKind::FUNCTION => grammar.highlight_id_for_name("function"), + lsp::CompletionItemKind::CLASS => grammar.highlight_id_for_name("type"), + lsp::CompletionItemKind::CONSTANT => grammar.highlight_id_for_name("constant"), + lsp::CompletionItemKind::VARIABLE => grammar.highlight_id_for_name("variable"), + _ => { + return None; + } + }; + + let mut text = label.clone(); + if let Some(completion_details) = item + .label_details + .as_ref() + .and_then(|details| details.detail.as_ref()) + { + write!(&mut text, " {}", completion_details).ok(); + } + + Some(language::CodeLabel::filtered( + text, + label_len, + item.filter_text.as_deref(), + highlight_id + .map(|id| (0..label_len, id)) + .into_iter() + .collect(), + )) + } + async fn workspace_configuration( self: Arc, delegate: &Arc,