Allow going to next/prev match even if FindBar has been dismissed

This commit is contained in:
Antonio Scandurra
2022-02-18 16:33:26 +01:00
parent 78ce479496
commit 0eb0faa2a1
+29 -23
View File
@@ -1,6 +1,6 @@
use aho_corasick::AhoCorasickBuilder; use aho_corasick::AhoCorasickBuilder;
use anyhow::Result; use anyhow::Result;
use collections::HashSet; use collections::HashMap;
use editor::{ use editor::{
char_kind, display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor, EditorSettings, char_kind, display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor, EditorSettings,
MultiBufferSnapshot, MultiBufferSnapshot,
@@ -63,12 +63,13 @@ struct FindBar {
active_editor: Option<ViewHandle<Editor>>, active_editor: Option<ViewHandle<Editor>>,
active_match_index: Option<usize>, active_match_index: Option<usize>,
active_editor_subscription: Option<Subscription>, active_editor_subscription: Option<Subscription>,
highlighted_editors: HashSet<WeakViewHandle<Editor>>, editors_with_matches: HashMap<WeakViewHandle<Editor>, Vec<Range<Anchor>>>,
pending_search: Option<Task<()>>, pending_search: Option<Task<()>>,
case_sensitive_mode: bool, case_sensitive_mode: bool,
whole_word_mode: bool, whole_word_mode: bool,
regex_mode: bool, regex_mode: bool,
query_contains_error: bool, query_contains_error: bool,
dismissed: bool,
} }
impl Entity for FindBar { impl Entity for FindBar {
@@ -119,10 +120,9 @@ impl View for FindBar {
.boxed(), .boxed(),
) )
.with_children(self.active_editor.as_ref().and_then(|editor| { .with_children(self.active_editor.as_ref().and_then(|editor| {
let (_, highlighted_ranges) = let matches = self.editors_with_matches.get(&editor.downgrade())?;
editor.read(cx).highlighted_ranges_for_type::<Self>()?;
let message = if let Some(match_ix) = self.active_match_index { let message = if let Some(match_ix) = self.active_match_index {
format!("{}/{}", match_ix + 1, highlighted_ranges.len()) format!("{}/{}", match_ix + 1, matches.len())
} else { } else {
"No matches".to_string() "No matches".to_string()
}; };
@@ -165,11 +165,12 @@ impl Toolbar for FindBar {
} }
fn on_dismiss(&mut self, cx: &mut ViewContext<Self>) { fn on_dismiss(&mut self, cx: &mut ViewContext<Self>) {
self.active_editor.take(); self.dismissed = true;
self.active_editor_subscription.take(); for (editor, _) in &self.editors_with_matches {
self.active_match_index.take(); if let Some(editor) = editor.upgrade(cx) {
self.pending_search.take(); editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
self.clear_matches(cx); }
}
} }
} }
@@ -200,13 +201,14 @@ impl FindBar {
active_editor: None, active_editor: None,
active_editor_subscription: None, active_editor_subscription: None,
active_match_index: None, active_match_index: None,
highlighted_editors: Default::default(), editors_with_matches: Default::default(),
case_sensitive_mode: false, case_sensitive_mode: false,
whole_word_mode: false, whole_word_mode: false,
regex_mode: false, regex_mode: false,
settings, settings,
pending_search: None, pending_search: None,
query_contains_error: false, query_contains_error: false,
dismissed: false,
} }
} }
@@ -277,6 +279,7 @@ impl FindBar {
.active_toolbar() .active_toolbar()
.and_then(|toolbar| toolbar.downcast::<Self>()) .and_then(|toolbar| toolbar.downcast::<Self>())
{ {
find_bar.update(cx, |find_bar, _| find_bar.dismissed = false);
let editor = pane.active_item().unwrap().act_as::<Editor>(cx).unwrap(); let editor = pane.active_item().unwrap().act_as::<Editor>(cx).unwrap();
let display_map = editor let display_map = editor
.update(cx, |editor, cx| editor.snapshot(cx)) .update(cx, |editor, cx| editor.snapshot(cx))
@@ -353,7 +356,7 @@ impl FindBar {
if let Some(editor) = self.active_editor.as_ref() { if let Some(editor) = self.active_editor.as_ref() {
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
let newest_selection = editor.newest_anchor_selection().clone(); let newest_selection = editor.newest_anchor_selection().clone();
if let Some((_, ranges)) = editor.highlighted_ranges_for_type::<Self>() { if let Some(ranges) = self.editors_with_matches.get(&cx.weak_handle()) {
let position = newest_selection.head(); let position = newest_selection.head();
let buffer = editor.buffer().read(cx).read(cx); let buffer = editor.buffer().read(cx).read(cx);
if ranges[index].start.cmp(&position, &buffer).unwrap().is_gt() { if ranges[index].start.cmp(&position, &buffer).unwrap().is_gt() {
@@ -428,7 +431,7 @@ impl FindBar {
} }
fn clear_matches(&mut self, cx: &mut ViewContext<Self>) { fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
for editor in self.highlighted_editors.drain() { for (editor, _) in self.editors_with_matches.drain() {
if let Some(editor) = editor.upgrade(cx) { if let Some(editor) = editor.upgrade(cx) {
if Some(&editor) != self.active_editor.as_ref() { if Some(&editor) != self.active_editor.as_ref() {
editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx)); editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
@@ -463,15 +466,18 @@ impl FindBar {
Ok(ranges) => { Ok(ranges) => {
if let Some(editor) = editor.upgrade(&cx) { if let Some(editor) = editor.upgrade(&cx) {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.highlighted_editors.insert(editor.downgrade()); this.editors_with_matches
editor.update(cx, |editor, cx| { .insert(editor.downgrade(), ranges.clone());
let theme = &this.settings.borrow().theme.find; if !this.dismissed {
editor.highlight_ranges::<Self>( editor.update(cx, |editor, cx| {
ranges, let theme = &this.settings.borrow().theme.find;
theme.match_background, editor.highlight_ranges::<Self>(
cx, ranges,
) theme.match_background,
}); cx,
)
});
}
this.update_match_index(cx); this.update_match_index(cx);
}); });
} }
@@ -495,9 +501,9 @@ impl FindBar {
fn active_match_index(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> { fn active_match_index(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> {
let editor = self.active_editor.as_ref()?; let editor = self.active_editor.as_ref()?;
let ranges = self.editors_with_matches.get(&editor.downgrade())?;
let editor = editor.read(cx); let editor = editor.read(cx);
let position = editor.newest_anchor_selection().head(); let position = editor.newest_anchor_selection().head();
let ranges = editor.highlighted_ranges_for_type::<Self>()?.1;
if ranges.is_empty() { if ranges.is_empty() {
None None
} else { } else {