From 474ec2199cd75b71d6cefe98d99a5b7e64d47667 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 2 Feb 2022 18:56:04 +0100 Subject: [PATCH] Improve formatting of variable/field completions for Rust Co-Authored-By: Nathan Sobo Co-Authored-By: Max Brunsfeld --- crates/zed/src/language.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/zed/src/language.rs b/crates/zed/src/language.rs index b268647c0b..9a27e117b6 100644 --- a/crates/zed/src/language.rs +++ b/crates/zed/src/language.rs @@ -34,10 +34,29 @@ impl LspPostProcessor for RustPostProcessor { fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option { let detail = completion.detail.as_ref()?; - if detail.starts_with("fn(") { - Some(completion.label.replace("(…)", &detail[2..])) - } else { - None + match completion.kind { + Some( + lsp::CompletionItemKind::CONSTANT + | lsp::CompletionItemKind::FIELD + | lsp::CompletionItemKind::VARIABLE, + ) => { + let mut label = completion.label.clone(); + label.push_str(": "); + label.push_str(detail); + Some(label) + } + Some(lsp::CompletionItemKind::FUNCTION | lsp::CompletionItemKind::METHOD) => { + lazy_static! { + static ref REGEX: Regex = Regex::new("\\(…?\\)").unwrap(); + } + + if detail.starts_with("fn(") { + Some(REGEX.replace(&completion.label, &detail[2..]).to_string()) + } else { + None + } + } + _ => None, } } }