From 776c6baf4447f68d0be98fe30ed868cea3b4485e Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 12 Nov 2025 12:33:05 +0200 Subject: [PATCH] Simplify --- crates/editor/src/bracket_colorization.rs | 84 ++++++----------------- crates/editor/src/display_map.rs | 15 +++- crates/language/src/language.rs | 5 +- 3 files changed, 35 insertions(+), 69 deletions(-) diff --git a/crates/editor/src/bracket_colorization.rs b/crates/editor/src/bracket_colorization.rs index 6770ddcbeb42c76e57934b0f65fbfb781ce6a877..d15cdc11573690f419920f103d169da3e5f00121 100644 --- a/crates/editor/src/bracket_colorization.rs +++ b/crates/editor/src/bracket_colorization.rs @@ -107,90 +107,33 @@ impl Editor { #[cfg(test)] mod tests { - use std::{collections::HashSet, ops::Range, time::Duration}; + use std::{collections::HashSet, sync::Arc, time::Duration}; use super::*; use crate::{ DisplayPoint, display_map::{DisplayRow, ToDisplayPoint}, editor_tests::init_test, - test::{ - editor_lsp_test_context::EditorLspTestContext, editor_test_context::EditorTestContext, - }, + test::editor_lsp_test_context::EditorLspTestContext, }; - use gpui::Hsla; use indoc::indoc; - use language::{BracketPair, BracketPairConfig, Language, LanguageConfig, LanguageMatcher}; - use multi_buffer::AnchorRangeExt as _; + use languages::rust_lang; use rope::Point; use text::OffsetRangeExt; #[gpui::test] async fn test_rainbow_bracket_highlights(cx: &mut gpui::TestAppContext) { - fn collect_colored_brackets( - cx: &mut EditorTestContext, - ) -> Vec<(Option, Range)> { - cx.update_editor(|editor, window, cx| { - let snapshot = editor.snapshot(window, cx); - snapshot - .all_text_highlight_ranges::() - .iter() - .flat_map(|ranges| { - ranges.1.iter().map(|range| { - (ranges.0.color, range.to_point(&snapshot.buffer_snapshot())) - }) - }) - .collect::>() - }) - } - init_test(cx, |language_settings| { language_settings.defaults.colorize_brackets = Some(true); }); let mut cx = EditorLspTestContext::new( - Language::new( - LanguageConfig { - name: "Rust".into(), - matcher: LanguageMatcher { - path_suffixes: vec!["rs".to_string()], - ..LanguageMatcher::default() - }, - brackets: BracketPairConfig { - pairs: vec![ - BracketPair { - start: "{".to_string(), - end: "}".to_string(), - close: false, - surround: false, - newline: true, - }, - BracketPair { - start: "(".to_string(), - end: ")".to_string(), - close: false, - surround: false, - newline: true, - }, - ], - ..BracketPairConfig::default() - }, - ..LanguageConfig::default() - }, - Some(tree_sitter_rust::LANGUAGE.into()), - ) - .with_brackets_query(indoc! {r#" - ("{" @open "}" @close) - ("(" @open ")" @close) - "#}) - .unwrap(), + Arc::into_inner(rust_lang()).unwrap(), lsp::ServerCapabilities::default(), cx, ) .await; - let mut highlighted_brackets = HashMap::default(); - // taken from r-a https://github.com/rust-lang/rust-analyzer/blob/d733c07552a2dc0ec0cc8f4df3f0ca969a93fd90/crates/ide/src/inlay_hints.rs#L81-L297 cx.set_state(indoc! {r#"ˇ pub(crate) fn inlay_hints( @@ -414,8 +357,13 @@ mod tests { cx.executor().advance_clock(Duration::from_millis(100)); cx.executor().run_until_parked(); - let actual_ranges = collect_colored_brackets(&mut cx); + let actual_ranges = cx.update_editor(|editor, window, cx| { + editor + .snapshot(window, cx) + .all_text_highlight_ranges::() + }); + let mut highlighted_brackets = HashMap::default(); for (color, range) in actual_ranges.iter().cloned() { highlighted_brackets.insert(range, color); } @@ -437,7 +385,11 @@ mod tests { cx.executor().advance_clock(Duration::from_millis(100)); cx.executor().run_until_parked(); - let ranges_after_scrolling = collect_colored_brackets(&mut cx); + let ranges_after_scrolling = cx.update_editor(|editor, window, cx| { + editor + .snapshot(window, cx) + .all_text_highlight_ranges::() + }); let new_last_bracket = ranges_after_scrolling .iter() .max_by_key(|(_, p)| p.end.row) @@ -460,7 +412,11 @@ mod tests { }); cx.executor().run_until_parked(); - let colored_brackets = collect_colored_brackets(&mut cx); + let colored_brackets = cx.update_editor(|editor, window, cx| { + editor + .snapshot(window, cx) + .all_text_highlight_ranges::() + }); for (color, range) in colored_brackets.clone() { assert!( highlighted_brackets.entry(range).or_insert(color) == &color, diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index a6a2b6e216212db3df32d703d33428ffb341d823..d5339c2734763b9bcbdfa6d5328980090d5a0324 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -39,6 +39,8 @@ pub use crease_map::*; pub use fold_map::{ ChunkRenderer, ChunkRendererContext, ChunkRendererId, Fold, FoldId, FoldPlaceholder, FoldPoint, }; +#[cfg(any(test, feature = "test-support"))] +use gpui::Hsla; pub use inlay_map::{InlayOffset, InlayPoint}; pub use invisibles::{is_invisible, replacement}; @@ -1409,9 +1411,7 @@ impl DisplaySnapshot { } #[cfg(any(test, feature = "test-support"))] - pub fn all_text_highlight_ranges( - &self, - ) -> Vec>)>> { + pub fn all_text_highlight_ranges(&self) -> Vec<(Hsla, Range)> { let needed_type_id = TypeId::of::(); self.text_highlights .iter() @@ -1420,6 +1420,15 @@ impl DisplaySnapshot { HighlightKey::TypePlus(type_id, _) => type_id == &needed_type_id, }) .map(|(_, value)| value.clone()) + .flat_map(|ranges| { + ranges + .1 + .iter() + .flat_map(|range| { + Some((ranges.0.color?, range.to_point(self.buffer_snapshot()))) + }) + .collect::>() + }) .collect() } diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index e10bee3c940f0a3f37588a8eabb08cedd4fe37b3..f30ec155b6e60f635c9112c5fd40cf9f170a91b0 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -2644,8 +2644,9 @@ pub fn rust_lang() -> Arc { ("[" @open "]" @close) ("{" @open "}" @close) ("<" @open ">" @close) -("\"" @open "\"" @close) -(closure_parameters "|" @open "|" @close)"#, +(closure_parameters "|" @open "|" @close) +(("\"" @open "\"" @close) (#set! rainbow.exclude)) +(("'" @open "'" @close) (#set! rainbow.exclude))"#, )), text_objects: Some(Cow::from( r#"