From 4adc91e0706e8d05d45199d16752ee8024b003e8 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 7 Nov 2025 15:12:11 +0200 Subject: [PATCH] Simplify --- crates/editor/src/bracket_colorization.rs | 67 ++++++++++------------- crates/editor/src/editor.rs | 14 ++--- crates/editor/src/scroll.rs | 2 +- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/crates/editor/src/bracket_colorization.rs b/crates/editor/src/bracket_colorization.rs index f5d6a298a6c26b30ee54c2fefcbfa09878a2ab92..cee4f62d9ad47c75875cb264c0db62c711e382e1 100644 --- a/crates/editor/src/bracket_colorization.rs +++ b/crates/editor/src/bracket_colorization.rs @@ -1,38 +1,23 @@ use crate::{Editor, RangeToAnchorExt}; -use gpui::{Context, HighlightStyle, Window}; +use gpui::{Context, HighlightStyle}; use itertools::Itertools; use language::language_settings; -use multi_buffer::ToPoint; -use text::{Bias, Point}; use ui::{ActiveTheme, utils::ensure_minimum_contrast}; struct RainbowBracketHighlight; impl Editor { - pub(crate) fn colorize_brackets(&mut self, window: &mut Window, cx: &mut Context) { + pub(crate) fn colorize_brackets(&mut self, cx: &mut Context) { if !self.mode.is_full() { return; } - let snapshot = self.snapshot(window, cx); - let multi_buffer_snapshot = snapshot.buffer_snapshot(); - - let multi_buffer_visible_start = snapshot - .scroll_anchor - .anchor - .to_point(multi_buffer_snapshot); - - // todo! deduplicate? - let multi_buffer_visible_end = multi_buffer_snapshot.clip_point( - multi_buffer_visible_start - + Point::new(self.visible_line_count().unwrap_or(40.).ceil() as u32, 0), - Bias::Left, - ); - - let bracket_matches = multi_buffer_snapshot - .range_to_buffer_ranges(multi_buffer_visible_start..multi_buffer_visible_end) + let multi_buffer_snapshot = self.buffer().read(cx).snapshot(cx); + let bracket_matches = self + .visible_excerpts(cx) .into_iter() - .filter_map(|(buffer_snapshot, buffer_range, _)| { + .filter_map(|(excerpt_id, (buffer, _, buffer_range))| { + let buffer_snapshot = buffer.read(cx).snapshot(); let colorize_brackets = language_settings::language_settings( buffer_snapshot.language().map(|language| language.name()), buffer_snapshot.file(), @@ -43,26 +28,30 @@ impl Editor { return None; } - let buffer_brackets = - buffer_snapshot.bracket_ranges(buffer_range.start..buffer_range.end); - - // todo! is there a good way to use the excerpt_id instead? - let mut excerpt = multi_buffer_snapshot.excerpt_containing(buffer_range.clone())?; - Some( - buffer_brackets + buffer_snapshot + .bracket_ranges(buffer_range.start..buffer_range.end) .into_iter() .filter_map(|pair| { - let buffer_range = pair.open_range.start..pair.close_range.end; - if excerpt.contains_buffer_range(buffer_range) { - Some(( - pair.depth, - excerpt.map_range_from_buffer(pair.open_range), - excerpt.map_range_from_buffer(pair.close_range), - )) - } else { - None - } + let buffer_open_range = buffer_snapshot + .anchor_before(pair.open_range.start) + ..buffer_snapshot.anchor_after(pair.open_range.end); + let multi_buffer_open_range = multi_buffer_snapshot + .anchor_in_excerpt(excerpt_id, buffer_open_range.start)? + ..multi_buffer_snapshot + .anchor_in_excerpt(excerpt_id, buffer_open_range.end)?; + let buffer_close_range = buffer_snapshot + .anchor_before(pair.close_range.start) + ..buffer_snapshot.anchor_after(pair.close_range.end); + let multi_buffer_close_range = multi_buffer_snapshot + .anchor_in_excerpt(excerpt_id, buffer_close_range.start)? + ..multi_buffer_snapshot + .anchor_in_excerpt(excerpt_id, buffer_close_range.end)?; + Some(( + pair.depth, + multi_buffer_open_range, + multi_buffer_close_range, + )) }) .collect::>(), ) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index f9c2e308f77236d87ea41d209d13434478bc70bf..3cfe9f7744f4a5938fb5fe56e978e4a8813f7af2 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -2353,7 +2353,7 @@ impl Editor { editor.refresh_colors_for_visible_range(None, window, cx); editor .refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx); - editor.colorize_brackets(window, cx); + editor.colorize_brackets(cx); }) .ok(); }); @@ -3243,7 +3243,7 @@ impl Editor { refresh_linked_ranges(self, window, cx); self.refresh_selected_text_highlights(false, window, cx); - self.colorize_brackets(window, cx); + self.colorize_brackets(cx); self.refresh_matching_bracket_highlights(window, cx); self.update_visible_edit_prediction(window, cx); self.edit_prediction_requires_modifier_in_indent_conflict = true; @@ -21030,7 +21030,7 @@ impl Editor { self.refresh_code_actions(window, cx); self.refresh_selected_text_highlights(true, window, cx); self.refresh_single_line_folds(window, cx); - self.colorize_brackets(window, cx); + self.colorize_brackets(cx); self.refresh_matching_bracket_highlights(window, cx); if self.has_active_edit_prediction() { self.update_visible_edit_prediction(window, cx); @@ -21085,7 +21085,7 @@ impl Editor { } self.update_lsp_data(Some(buffer_id), window, cx); self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx); - self.colorize_brackets(window, cx); + self.colorize_brackets(cx); cx.emit(EditorEvent::ExcerptsAdded { buffer: buffer.clone(), predecessor: *predecessor, @@ -21123,12 +21123,12 @@ impl Editor { multi_buffer::Event::ExcerptsExpanded { ids } => { self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx); self.refresh_document_highlights(cx); - self.colorize_brackets(window, cx); + self.colorize_brackets(cx); cx.emit(EditorEvent::ExcerptsExpanded { ids: ids.clone() }) } multi_buffer::Event::Reparsed(buffer_id) => { self.tasks_update_task = Some(self.refresh_runnables(window, cx)); - self.colorize_brackets(window, cx); + self.colorize_brackets(cx); jsx_tag_auto_close::refresh_enabled_in_any_buffer(self, multibuffer, cx); cx.emit(EditorEvent::Reparsed(*buffer_id)); @@ -21272,7 +21272,7 @@ impl Editor { } } - self.colorize_brackets(window, cx); + self.colorize_brackets(cx); if let Some(inlay_splice) = self.colors.as_mut().and_then(|colors| { colors.render_mode_updated(EditorSettings::get_global(cx).lsp_document_colors) diff --git a/crates/editor/src/scroll.rs b/crates/editor/src/scroll.rs index 53fb0e8b5ef2d0ca053c4a2a2a6a6e3cde877b84..1f443d9909a9b410047ab71b8ee9798c0f9aa22c 100644 --- a/crates/editor/src/scroll.rs +++ b/crates/editor/src/scroll.rs @@ -500,7 +500,7 @@ impl Editor { editor.register_visible_buffers(cx); editor.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx); editor.update_lsp_data(None, window, cx); - editor.colorize_brackets(window, cx); + editor.colorize_brackets(cx); }) .ok(); });