From cac98b7bbff89e6b0c7f632940ce55597fa1c55c Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 3 Oct 2024 14:38:17 -0400 Subject: [PATCH] Show color swatches for LSP completions (#18665) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #11991 Release Notes: - Added support for color swatches for language server completions. Screenshot 2024-10-02 at 19 02 22 Screenshot 2024-10-02 at 19 02 48 This implementation is mostly a port of the VSCode version of the ColorExtractor. It seems reasonable the we should support _at least_ what VSCode does for detecting color swatches from LSP completions. This implementation could definitely be better perf-wise by writing a dedicated color parser. I also think it would be neat if, in the future, Zed handled _more_ color formats — especially wide-gamut colors. There are a few differences to the regexes in the VSCode implementation but mainly so simplify the implementation : - The hex vs rgb/hsl regexes were split into two parts - The rgb/hsl regexes allow 3 or 4 color components whether hsla/rgba or not and the parsing implementation accepts/rejects colors as needed --------- Co-authored-by: Marshall Bowers --- crates/editor/src/editor.rs | 5 + crates/project/src/color_extractor.rs | 297 ++++++++++++++++++++++++++ crates/project/src/project.rs | 17 +- typos.toml | 4 +- 4 files changed, 320 insertions(+), 3 deletions(-) create mode 100644 crates/project/src/color_extractor.rs diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 525a94f258..e235546104 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1228,6 +1228,10 @@ impl CompletionsMenu { None }; + let color_swatch = completion + .color() + .map(|color| div().size_4().bg(color).rounded(px(2.))); + div().min_w(px(220.)).max_w(px(540.)).child( ListItem::new(mat.candidate_id) .inset(true) @@ -1243,6 +1247,7 @@ impl CompletionsMenu { task.detach_and_log_err(cx) } })) + .start_slot::
(color_swatch) .child(h_flex().overflow_hidden().child(completion_label)) .end_slot::