From 3b5443ba9698016a9831c22943a71d6fdc3cf04f Mon Sep 17 00:00:00 2001 From: Finn Evers Date: Mon, 26 Jan 2026 12:07:01 +0100 Subject: [PATCH] syntax_tree_view: Fix highlights persisting after view was closed (#47638) Also ensured that we clear only the highlights of the current view (which probably would have been the the case almost always anyway). Release Notes: - Fixed a small issue where highlights of the syntax tree view would persist after the view was closed. --- crates/editor/src/editor.rs | 15 ++++++ crates/editor/src/items.rs | 2 +- crates/language_tools/src/syntax_tree_view.rs | 49 +++++++++++++------ crates/workspace/src/item.rs | 8 +-- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c3999f585b067bb7a81bf397503c74932fbc52c8..c78cc57c45802c3bcadf12b3c95168ef503b7d39 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -23098,6 +23098,21 @@ impl Editor { Some(text_highlights) } + pub fn clear_background_highlights_key( + &mut self, + key: usize, + cx: &mut Context, + ) -> Option { + let text_highlights = self + .background_highlights + .remove(&HighlightKey::TypePlus(TypeId::of::(), key))?; + if !text_highlights.1.is_empty() { + self.scrollbar_marker_state.dirty = true; + cx.notify(); + } + Some(text_highlights) + } + pub fn highlight_gutter( &mut self, ranges: impl Into>>, diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index e8237e756bf57c83b2215ab0e264604fe9af8b4a..14b736acff64ac855b79ce54dcb56d09c7afb868 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -789,7 +789,7 @@ impl Item for Editor { self.nav_history = Some(history); } - fn on_removed(&self, cx: &App) { + fn on_removed(&self, cx: &mut Context) { self.report_editor_event(ReportEditorEvent::Closed, None, cx); } diff --git a/crates/language_tools/src/syntax_tree_view.rs b/crates/language_tools/src/syntax_tree_view.rs index e869a342a9eb3ddb65eb895ee4215be093f54d95..1f75fda012f977173679590344314cbd1ca49bea 100644 --- a/crates/language_tools/src/syntax_tree_view.rs +++ b/crates/language_tools/src/syntax_tree_view.rs @@ -339,7 +339,7 @@ impl SyntaxTreeView { descendant_ix: usize, window: &mut Window, cx: &mut Context, - mut f: impl FnMut(&mut Editor, Range, &mut Window, &mut Context), + mut f: impl FnMut(&mut Editor, Range, usize, &mut Window, &mut Context), ) -> Option<()> { let editor_state = self.editor.as_ref()?; let buffer_state = editor_state.active_buffer.as_ref()?; @@ -360,10 +360,11 @@ impl SyntaxTreeView { let multibuffer = multibuffer.read(cx).snapshot(cx); let excerpt_id = buffer_state.excerpt_id; let range = multibuffer.anchor_range_in_excerpt(excerpt_id, range)?; + let key = cx.entity_id().as_u64() as usize; // Update the editor with the anchor range. editor_state.editor.update(cx, |editor, cx| { - f(editor, range, window, cx); + f(editor, range, key, window, cx); }); Some(()) } @@ -431,7 +432,7 @@ impl SyntaxTreeView { descendant_ix, window, cx, - |editor, mut range, window, cx| { + |editor, mut range, _, window, cx| { // Put the cursor at the beginning of the node. mem::swap(&mut range.start, &mut range.end); @@ -440,7 +441,7 @@ impl SyntaxTreeView { window, cx, |selections| { - selections.select_ranges(vec![range]); + selections.select_ranges([range]); }, ); }, @@ -455,17 +456,8 @@ impl SyntaxTreeView { descendant_ix, window, cx, - |editor, range, _, cx| { - editor.clear_background_highlights::(cx); - editor.highlight_background::( - &[range], - |_, theme| { - theme - .colors() - .editor_document_highlight_write_background - }, - cx, - ); + |editor, range, key, _, cx| { + Self::set_editor_highlights(editor, key, &[range], cx); }, ); cx.notify(); @@ -483,6 +475,27 @@ impl SyntaxTreeView { } items } + + fn set_editor_highlights( + editor: &mut Editor, + key: usize, + ranges: &[Range], + cx: &mut Context, + ) { + editor.highlight_background_key::( + key, + ranges, + |_, theme| theme.colors().editor_document_highlight_write_background, + cx, + ); + } + + fn clear_editor_highlights(editor: &Entity, cx: &mut Context) { + let highlight_key = cx.entity_id().as_u64() as usize; + editor.update(cx, |editor, cx| { + editor.clear_background_highlights_key::(highlight_key, cx); + }); + } } impl Render for SyntaxTreeView { @@ -589,6 +602,12 @@ impl Item for SyntaxTreeView { clone }))) } + + fn on_removed(&self, cx: &mut Context) { + if let Some(state) = self.editor.as_ref() { + Self::clear_editor_highlights(&state.editor, cx); + } + } } impl Default for SyntaxTreeToolbarItemView { diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index f0ed65c8dac06ad88ad38d190ca22f50b319b2b1..2d8a485432fc367eb12b85647c38e06a9624ae4d 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -217,7 +217,7 @@ pub trait Item: Focusable + EventEmitter + Render + Sized { fn deactivated(&mut self, _window: &mut Window, _: &mut Context) {} fn discarded(&self, _project: Entity, _window: &mut Window, _cx: &mut Context) {} - fn on_removed(&self, _cx: &App) {} + fn on_removed(&self, _cx: &mut Context) {} fn workspace_deactivated(&mut self, _window: &mut Window, _: &mut Context) {} fn navigate( &mut self, @@ -483,7 +483,7 @@ pub trait ItemHandle: 'static + Send { cx: &mut Context, ); fn deactivated(&self, window: &mut Window, cx: &mut App); - fn on_removed(&self, cx: &App); + fn on_removed(&self, cx: &mut App); fn workspace_deactivated(&self, window: &mut Window, cx: &mut App); fn navigate(&self, data: Arc, window: &mut Window, cx: &mut App) -> bool; fn item_id(&self) -> EntityId; @@ -941,8 +941,8 @@ impl ItemHandle for Entity { self.update(cx, |this, cx| this.deactivated(window, cx)); } - fn on_removed(&self, cx: &App) { - self.read(cx).on_removed(cx); + fn on_removed(&self, cx: &mut App) { + self.update(cx, |item, cx| item.on_removed(cx)); } fn workspace_deactivated(&self, window: &mut Window, cx: &mut App) {