From ea39eb384d2abaddffce7ff453469ed3ff707c3f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 18 Feb 2022 16:45:59 +0100 Subject: [PATCH] Select closest match when find query changes --- crates/find/src/find.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/crates/find/src/find.rs b/crates/find/src/find.rs index 338c7073ea..6f7312c7d1 100644 --- a/crates/find/src/find.rs +++ b/crates/find/src/find.rs @@ -157,7 +157,7 @@ impl Toolbar for FindBar { self.active_editor_subscription = Some(cx.subscribe(&editor, Self::on_active_editor_event)); self.active_editor = Some(editor); - self.update_matches(cx); + self.update_matches(false, cx); true } else { false @@ -347,7 +347,7 @@ impl FindBar { SearchMode::Regex => &mut self.regex_mode, }; *value = !*value; - self.update_matches(cx); + self.update_matches(true, cx); cx.notify(); } @@ -410,7 +410,7 @@ impl FindBar { editor::Event::Edited => { self.query_contains_error = false; self.clear_matches(cx); - self.update_matches(cx); + self.update_matches(true, cx); cx.notify(); } _ => {} @@ -424,7 +424,7 @@ impl FindBar { cx: &mut ViewContext, ) { match event { - editor::Event::Edited => self.update_matches(cx), + editor::Event::Edited => self.update_matches(false, cx), editor::Event::SelectionsChanged => self.update_match_index(cx), _ => {} } @@ -440,7 +440,7 @@ impl FindBar { } } - fn update_matches(&mut self, cx: &mut ViewContext) { + fn update_matches(&mut self, select_closest_match: bool, cx: &mut ViewContext) { let query = self.query_editor.read(cx).text(cx); self.pending_search.take(); if let Some(editor) = self.active_editor.as_ref() { @@ -468,17 +468,28 @@ impl FindBar { this.update(&mut cx, |this, cx| { this.editors_with_matches .insert(editor.downgrade(), ranges.clone()); + this.update_match_index(cx); if !this.dismissed { editor.update(cx, |editor, cx| { let theme = &this.settings.borrow().theme.find; + + if select_closest_match { + if let Some(match_ix) = this.active_match_index { + editor.select_ranges( + [ranges[match_ix].clone()], + Some(Autoscroll::Fit), + cx, + ); + } + } + editor.highlight_ranges::( ranges, theme.match_background, cx, - ) + ); }); } - this.update_match_index(cx); }); } }