diff --git a/crates/zed/src/languages/cpp.rs b/crates/zed/src/languages/cpp.rs index f624c0e930..44ccb36e63 100644 --- a/crates/zed/src/languages/cpp.rs +++ b/crates/zed/src/languages/cpp.rs @@ -30,4 +30,80 @@ impl super::LspAdapter for CppLspAdapter { fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option> { super::c::CLspAdapter.cached_server_binary(container_dir) } + + fn label_for_completion( + &self, + completion: &lsp::CompletionItem, + language: &Language, + ) -> Option { + let label = completion + .label + .strip_prefix("•") + .unwrap_or(&completion.label) + .trim(); + + match completion.kind { + Some(lsp::CompletionItemKind::FIELD) if completion.detail.is_some() => { + let detail = completion.detail.as_ref().unwrap(); + let text = format!("{} {}", detail, label); + let source = Rope::from(format!("struct S {{ {} }}", text).as_str()); + let runs = language.highlight_text(&source, 11..11 + text.len()); + return Some(CodeLabel { + filter_range: detail.len() + 1..text.len(), + text, + runs, + }); + } + Some(lsp::CompletionItemKind::CONSTANT | lsp::CompletionItemKind::VARIABLE) + if completion.detail.is_some() => + { + let detail = completion.detail.as_ref().unwrap(); + let text = format!("{} {}", detail, label); + let runs = language.highlight_text(&Rope::from(text.as_str()), 0..text.len()); + return Some(CodeLabel { + filter_range: detail.len() + 1..text.len(), + text, + runs, + }); + } + Some(lsp::CompletionItemKind::FUNCTION | lsp::CompletionItemKind::METHOD) + if completion.detail.is_some() => + { + let detail = completion.detail.as_ref().unwrap(); + let text = format!("{} {}", detail, label); + let runs = language.highlight_text(&Rope::from(text.as_str()), 0..text.len()); + return Some(CodeLabel { + filter_range: detail.len() + 1..text.rfind('(').unwrap_or(text.len()), + text, + runs, + }); + } + Some(kind) => { + let highlight_name = match kind { + lsp::CompletionItemKind::STRUCT + | lsp::CompletionItemKind::INTERFACE + | lsp::CompletionItemKind::ENUM => Some("type"), + lsp::CompletionItemKind::ENUM_MEMBER => Some("variant"), + lsp::CompletionItemKind::KEYWORD => Some("keyword"), + lsp::CompletionItemKind::VALUE | lsp::CompletionItemKind::CONSTANT => { + Some("constant") + } + _ => None, + }; + if let Some(highlight_id) = language + .grammar() + .and_then(|g| g.highlight_id_for_name(highlight_name?)) + { + let mut label = CodeLabel::plain(label.to_string(), None); + label.runs.push(( + 0..label.text.rfind('(').unwrap_or(label.text.len()), + highlight_id, + )); + return Some(label); + } + } + _ => {} + } + Some(CodeLabel::plain(label.to_string(), None)) + } }