2022-02-23 11:38:36 +00:00
|
|
|
mod project_find;
|
|
|
|
|
2022-01-28 00:16:51 +00:00
|
|
|
use aho_corasick::AhoCorasickBuilder;
|
2022-01-28 15:15:18 +00:00
|
|
|
use anyhow::Result;
|
2022-02-18 15:33:26 +00:00
|
|
|
use collections::HashMap;
|
2022-01-30 15:00:16 +00:00
|
|
|
use editor::{
|
|
|
|
char_kind, display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor, EditorSettings,
|
|
|
|
MultiBufferSnapshot,
|
|
|
|
};
|
2022-01-27 16:39:58 +00:00
|
|
|
use gpui::{
|
2022-01-31 10:00:29 +00:00
|
|
|
action, elements::*, keymap::Binding, platform::CursorStyle, Entity, MutableAppContext,
|
|
|
|
RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
|
2022-01-27 16:39:58 +00:00
|
|
|
};
|
2022-01-27 20:58:21 +00:00
|
|
|
use postage::watch;
|
2022-01-28 15:15:18 +00:00
|
|
|
use regex::RegexBuilder;
|
2022-01-28 11:15:55 +00:00
|
|
|
use smol::future::yield_now;
|
2022-01-29 13:38:58 +00:00
|
|
|
use std::{
|
|
|
|
cmp::{self, Ordering},
|
|
|
|
ops::Range,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2022-01-31 03:59:20 +00:00
|
|
|
use workspace::{ItemViewHandle, Pane, Settings, Toolbar, Workspace};
|
2022-01-27 16:39:58 +00:00
|
|
|
|
2022-01-30 15:11:48 +00:00
|
|
|
action!(Deploy, bool);
|
2022-01-31 08:58:03 +00:00
|
|
|
action!(Dismiss);
|
2022-01-30 15:30:07 +00:00
|
|
|
action!(FocusEditor);
|
2022-01-28 00:16:51 +00:00
|
|
|
action!(ToggleMode, SearchMode);
|
2022-01-28 22:00:00 +00:00
|
|
|
action!(GoToMatch, Direction);
|
|
|
|
|
2022-01-29 15:16:48 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2022-01-28 22:00:00 +00:00
|
|
|
pub enum Direction {
|
|
|
|
Prev,
|
|
|
|
Next,
|
|
|
|
}
|
2022-01-28 00:16:51 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum SearchMode {
|
|
|
|
WholeWord,
|
|
|
|
CaseSensitive,
|
|
|
|
Regex,
|
|
|
|
}
|
2022-01-27 16:39:58 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
2022-01-27 21:00:51 +00:00
|
|
|
cx.add_bindings([
|
2022-01-30 15:11:48 +00:00
|
|
|
Binding::new("cmd-f", Deploy(true), Some("Editor && mode == full")),
|
|
|
|
Binding::new("cmd-e", Deploy(false), Some("Editor && mode == full")),
|
2022-01-31 08:58:03 +00:00
|
|
|
Binding::new("escape", Dismiss, Some("FindBar")),
|
2022-01-30 15:30:07 +00:00
|
|
|
Binding::new("cmd-f", FocusEditor, Some("FindBar")),
|
2022-01-30 14:20:57 +00:00
|
|
|
Binding::new("enter", GoToMatch(Direction::Next), Some("FindBar")),
|
|
|
|
Binding::new("shift-enter", GoToMatch(Direction::Prev), Some("FindBar")),
|
2022-01-31 03:59:20 +00:00
|
|
|
Binding::new("cmd-g", GoToMatch(Direction::Next), Some("Pane")),
|
|
|
|
Binding::new("cmd-shift-G", GoToMatch(Direction::Prev), Some("Pane")),
|
2022-01-27 21:00:51 +00:00
|
|
|
]);
|
2022-01-27 16:39:58 +00:00
|
|
|
cx.add_action(FindBar::deploy);
|
2022-01-31 08:58:03 +00:00
|
|
|
cx.add_action(FindBar::dismiss);
|
2022-01-30 15:30:07 +00:00
|
|
|
cx.add_action(FindBar::focus_editor);
|
2022-01-28 00:16:51 +00:00
|
|
|
cx.add_action(FindBar::toggle_mode);
|
2022-01-28 22:00:00 +00:00
|
|
|
cx.add_action(FindBar::go_to_match);
|
2022-01-31 03:59:20 +00:00
|
|
|
cx.add_action(FindBar::go_to_match_on_pane);
|
2022-01-27 16:39:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 20:58:21 +00:00
|
|
|
struct FindBar {
|
|
|
|
settings: watch::Receiver<Settings>,
|
|
|
|
query_editor: ViewHandle<Editor>,
|
|
|
|
active_editor: Option<ViewHandle<Editor>>,
|
2022-01-28 22:00:00 +00:00
|
|
|
active_match_index: Option<usize>,
|
2022-01-28 14:19:58 +00:00
|
|
|
active_editor_subscription: Option<Subscription>,
|
2022-02-18 15:33:26 +00:00
|
|
|
editors_with_matches: HashMap<WeakViewHandle<Editor>, Vec<Range<Anchor>>>,
|
2022-01-28 11:15:55 +00:00
|
|
|
pending_search: Option<Task<()>>,
|
2022-01-28 00:16:51 +00:00
|
|
|
case_sensitive_mode: bool,
|
|
|
|
whole_word_mode: bool,
|
|
|
|
regex_mode: bool,
|
2022-01-28 15:15:18 +00:00
|
|
|
query_contains_error: bool,
|
2022-02-18 15:33:26 +00:00
|
|
|
dismissed: bool,
|
2022-01-27 20:58:21 +00:00
|
|
|
}
|
2022-01-27 16:39:58 +00:00
|
|
|
|
|
|
|
impl Entity for FindBar {
|
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for FindBar {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"FindBar"
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:00:51 +00:00
|
|
|
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
cx.focus(&self.query_editor);
|
|
|
|
}
|
|
|
|
|
2022-01-28 00:16:51 +00:00
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
2022-01-31 10:00:29 +00:00
|
|
|
let theme = &self.settings.borrow().theme;
|
2022-01-28 15:15:18 +00:00
|
|
|
let editor_container = if self.query_contains_error {
|
2022-01-31 10:00:29 +00:00
|
|
|
theme.find.invalid_editor
|
2022-01-28 15:15:18 +00:00
|
|
|
} else {
|
2022-01-31 10:00:29 +00:00
|
|
|
theme.find.editor.input.container
|
2022-01-28 15:15:18 +00:00
|
|
|
};
|
2022-01-28 01:59:44 +00:00
|
|
|
Flex::row()
|
2022-01-28 00:16:51 +00:00
|
|
|
.with_child(
|
|
|
|
ChildView::new(&self.query_editor)
|
|
|
|
.contained()
|
2022-01-28 15:15:18 +00:00
|
|
|
.with_style(editor_container)
|
2022-01-31 10:00:29 +00:00
|
|
|
.aligned()
|
2022-01-28 01:59:44 +00:00
|
|
|
.constrained()
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_max_width(theme.find.editor.max_width)
|
2022-01-28 00:16:51 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_child(
|
2022-01-28 01:59:44 +00:00
|
|
|
Flex::row()
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_child(self.render_mode_button("Case", SearchMode::CaseSensitive, cx))
|
|
|
|
.with_child(self.render_mode_button("Word", SearchMode::WholeWord, cx))
|
|
|
|
.with_child(self.render_mode_button("Regex", SearchMode::Regex, cx))
|
2022-01-28 00:16:51 +00:00
|
|
|
.contained()
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_style(theme.find.mode_button_group)
|
|
|
|
.aligned()
|
2022-01-28 22:00:00 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
2022-01-31 13:06:49 +00:00
|
|
|
.with_child(
|
|
|
|
Flex::row()
|
|
|
|
.with_child(self.render_nav_button("<", Direction::Prev, cx))
|
|
|
|
.with_child(self.render_nav_button(">", Direction::Next, cx))
|
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
)
|
2022-01-29 13:38:58 +00:00
|
|
|
.with_children(self.active_editor.as_ref().and_then(|editor| {
|
2022-02-18 15:33:26 +00:00
|
|
|
let matches = self.editors_with_matches.get(&editor.downgrade())?;
|
2022-01-31 12:52:06 +00:00
|
|
|
let message = if let Some(match_ix) = self.active_match_index {
|
2022-02-18 15:33:26 +00:00
|
|
|
format!("{}/{}", match_ix + 1, matches.len())
|
2022-01-29 17:23:14 +00:00
|
|
|
} else {
|
2022-01-31 12:52:06 +00:00
|
|
|
"No matches".to_string()
|
2022-01-29 17:23:14 +00:00
|
|
|
};
|
2022-01-31 12:52:06 +00:00
|
|
|
|
2022-01-29 13:38:58 +00:00
|
|
|
Some(
|
2022-01-31 10:00:29 +00:00
|
|
|
Label::new(message, theme.find.match_index.text.clone())
|
2022-01-29 17:23:14 +00:00
|
|
|
.contained()
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_style(theme.find.match_index.container)
|
|
|
|
.aligned()
|
2022-01-29 17:23:14 +00:00
|
|
|
.boxed(),
|
2022-01-29 13:38:58 +00:00
|
|
|
)
|
|
|
|
}))
|
2022-01-27 16:39:58 +00:00
|
|
|
.contained()
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_style(theme.find.container)
|
|
|
|
.constrained()
|
|
|
|
.with_height(theme.workspace.toolbar.height)
|
|
|
|
.named("find bar")
|
2022-01-27 16:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 20:58:21 +00:00
|
|
|
impl Toolbar for FindBar {
|
|
|
|
fn active_item_changed(
|
|
|
|
&mut self,
|
|
|
|
item: Option<Box<dyn ItemViewHandle>>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> bool {
|
2022-01-28 14:19:58 +00:00
|
|
|
self.active_editor_subscription.take();
|
|
|
|
self.active_editor.take();
|
|
|
|
self.pending_search.take();
|
|
|
|
|
|
|
|
if let Some(editor) = item.and_then(|item| item.act_as::<Editor>(cx)) {
|
|
|
|
self.active_editor_subscription =
|
|
|
|
Some(cx.subscribe(&editor, Self::on_active_editor_event));
|
|
|
|
self.active_editor = Some(editor);
|
2022-02-18 15:45:59 +00:00
|
|
|
self.update_matches(false, cx);
|
2022-01-28 14:19:58 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2022-01-27 20:58:21 +00:00
|
|
|
}
|
2022-01-31 08:58:03 +00:00
|
|
|
|
|
|
|
fn on_dismiss(&mut self, cx: &mut ViewContext<Self>) {
|
2022-02-18 15:33:26 +00:00
|
|
|
self.dismissed = true;
|
|
|
|
for (editor, _) in &self.editors_with_matches {
|
|
|
|
if let Some(editor) = editor.upgrade(cx) {
|
|
|
|
editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
|
|
|
|
}
|
|
|
|
}
|
2022-01-31 08:58:03 +00:00
|
|
|
}
|
2022-01-27 20:58:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 16:39:58 +00:00
|
|
|
impl FindBar {
|
2022-01-27 20:58:21 +00:00
|
|
|
fn new(settings: watch::Receiver<Settings>, cx: &mut ViewContext<Self>) -> Self {
|
|
|
|
let query_editor = cx.add_view(|cx| {
|
2022-01-28 16:26:08 +00:00
|
|
|
Editor::auto_height(
|
|
|
|
2,
|
2022-01-27 20:58:21 +00:00
|
|
|
{
|
|
|
|
let settings = settings.clone();
|
|
|
|
Arc::new(move |_| {
|
|
|
|
let settings = settings.borrow();
|
|
|
|
EditorSettings {
|
2022-01-28 01:59:44 +00:00
|
|
|
style: settings.theme.find.editor.input.as_editor(),
|
2022-01-27 20:58:21 +00:00
|
|
|
tab_size: settings.tab_size,
|
|
|
|
soft_wrap: editor::SoftWrap::None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
cx.subscribe(&query_editor, Self::on_query_editor_event)
|
|
|
|
.detach();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
query_editor,
|
|
|
|
active_editor: None,
|
2022-01-28 14:19:58 +00:00
|
|
|
active_editor_subscription: None,
|
2022-01-28 22:00:00 +00:00
|
|
|
active_match_index: None,
|
2022-02-18 15:33:26 +00:00
|
|
|
editors_with_matches: Default::default(),
|
2022-01-28 00:16:51 +00:00
|
|
|
case_sensitive_mode: false,
|
|
|
|
whole_word_mode: false,
|
|
|
|
regex_mode: false,
|
2022-01-27 20:58:21 +00:00
|
|
|
settings,
|
2022-01-28 11:15:55 +00:00
|
|
|
pending_search: None,
|
2022-01-28 15:15:18 +00:00
|
|
|
query_contains_error: false,
|
2022-02-18 15:33:26 +00:00
|
|
|
dismissed: false,
|
2022-01-27 20:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 21:05:29 +00:00
|
|
|
fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
|
|
|
|
self.query_editor.update(cx, |query_editor, cx| {
|
|
|
|
query_editor.buffer().update(cx, |query_buffer, cx| {
|
|
|
|
let len = query_buffer.read(cx).len();
|
|
|
|
query_buffer.edit([0..len], query, cx);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-28 00:16:51 +00:00
|
|
|
fn render_mode_button(
|
|
|
|
&self,
|
|
|
|
icon: &str,
|
|
|
|
mode: SearchMode,
|
|
|
|
cx: &mut RenderContext<Self>,
|
|
|
|
) -> ElementBox {
|
2022-01-31 10:00:29 +00:00
|
|
|
let theme = &self.settings.borrow().theme.find;
|
2022-01-28 00:16:51 +00:00
|
|
|
let is_active = self.is_mode_enabled(mode);
|
2022-02-17 21:43:58 +00:00
|
|
|
MouseEventHandler::new::<Self, _, _>(mode as usize, cx, |state, _| {
|
2022-01-28 00:16:51 +00:00
|
|
|
let style = match (is_active, state.hovered) {
|
|
|
|
(false, false) => &theme.mode_button,
|
|
|
|
(false, true) => &theme.hovered_mode_button,
|
|
|
|
(true, false) => &theme.active_mode_button,
|
|
|
|
(true, true) => &theme.active_hovered_mode_button,
|
|
|
|
};
|
|
|
|
Label::new(icon.to_string(), style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.on_click(move |cx| cx.dispatch_action(ToggleMode(mode)))
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
2022-01-28 00:16:51 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:00:00 +00:00
|
|
|
fn render_nav_button(
|
|
|
|
&self,
|
|
|
|
icon: &str,
|
|
|
|
direction: Direction,
|
|
|
|
cx: &mut RenderContext<Self>,
|
|
|
|
) -> ElementBox {
|
2022-01-31 10:00:29 +00:00
|
|
|
let theme = &self.settings.borrow().theme.find;
|
2022-02-17 21:43:58 +00:00
|
|
|
enum NavButton {}
|
|
|
|
MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, _| {
|
|
|
|
let style = if state.hovered {
|
|
|
|
&theme.hovered_mode_button
|
|
|
|
} else {
|
|
|
|
&theme.mode_button
|
|
|
|
};
|
|
|
|
Label::new(icon.to_string(), style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
2022-01-28 22:00:00 +00:00
|
|
|
.on_click(move |cx| cx.dispatch_action(GoToMatch(direction)))
|
2022-01-31 10:00:29 +00:00
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
2022-01-28 22:00:00 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:11:48 +00:00
|
|
|
fn deploy(workspace: &mut Workspace, Deploy(focus): &Deploy, cx: &mut ViewContext<Workspace>) {
|
2022-01-27 20:58:21 +00:00
|
|
|
let settings = workspace.settings();
|
|
|
|
workspace.active_pane().update(cx, |pane, cx| {
|
|
|
|
pane.show_toolbar(cx, |cx| FindBar::new(settings, cx));
|
2022-01-30 15:44:42 +00:00
|
|
|
|
2022-01-30 15:00:16 +00:00
|
|
|
if let Some(find_bar) = pane
|
|
|
|
.active_toolbar()
|
|
|
|
.and_then(|toolbar| toolbar.downcast::<Self>())
|
|
|
|
{
|
2022-02-18 15:33:26 +00:00
|
|
|
find_bar.update(cx, |find_bar, _| find_bar.dismissed = false);
|
2022-02-04 15:13:58 +00:00
|
|
|
let editor = pane.active_item().unwrap().act_as::<Editor>(cx).unwrap();
|
|
|
|
let display_map = editor
|
|
|
|
.update(cx, |editor, cx| editor.snapshot(cx))
|
|
|
|
.display_snapshot;
|
|
|
|
let selection = editor
|
|
|
|
.read(cx)
|
|
|
|
.newest_selection::<usize>(&display_map.buffer_snapshot);
|
2022-01-30 15:00:16 +00:00
|
|
|
|
2022-02-04 15:13:58 +00:00
|
|
|
let mut text: String;
|
|
|
|
if selection.start == selection.end {
|
|
|
|
let point = selection.start.to_display_point(&display_map);
|
|
|
|
let range = editor::movement::surrounding_word(&display_map, point);
|
|
|
|
let range = range.start.to_offset(&display_map, Bias::Left)
|
|
|
|
..range.end.to_offset(&display_map, Bias::Right);
|
|
|
|
text = display_map.buffer_snapshot.text_for_range(range).collect();
|
|
|
|
if text.trim().is_empty() {
|
|
|
|
text = String::new();
|
2022-01-30 15:00:16 +00:00
|
|
|
}
|
2022-02-04 15:13:58 +00:00
|
|
|
} else {
|
|
|
|
text = display_map
|
|
|
|
.buffer_snapshot
|
|
|
|
.text_for_range(selection.start..selection.end)
|
|
|
|
.collect();
|
|
|
|
}
|
2022-01-30 15:00:16 +00:00
|
|
|
|
2022-02-04 15:13:58 +00:00
|
|
|
if !text.is_empty() {
|
|
|
|
find_bar.update(cx, |find_bar, cx| find_bar.set_query(&text, cx));
|
2022-01-30 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *focus {
|
2022-02-04 15:13:58 +00:00
|
|
|
let query_editor = find_bar.read(cx).query_editor.clone();
|
|
|
|
query_editor.update(cx, |query_editor, cx| {
|
|
|
|
query_editor.select_all(&editor::SelectAll, cx);
|
|
|
|
});
|
2022-01-30 15:11:48 +00:00
|
|
|
cx.focus(&find_bar);
|
2022-01-30 15:00:16 +00:00
|
|
|
}
|
2022-01-27 20:58:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-04 15:11:48 +00:00
|
|
|
fn dismiss(pane: &mut Pane, _: &Dismiss, cx: &mut ViewContext<Pane>) {
|
|
|
|
if pane.toolbar::<FindBar>().is_some() {
|
|
|
|
pane.dismiss_toolbar(cx);
|
|
|
|
}
|
2022-01-27 16:39:58 +00:00
|
|
|
}
|
2022-01-27 21:00:51 +00:00
|
|
|
|
2022-01-30 15:30:07 +00:00
|
|
|
fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext<Self>) {
|
|
|
|
if let Some(active_editor) = self.active_editor.as_ref() {
|
|
|
|
cx.focus(active_editor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 00:16:51 +00:00
|
|
|
fn is_mode_enabled(&self, mode: SearchMode) -> bool {
|
|
|
|
match mode {
|
|
|
|
SearchMode::WholeWord => self.whole_word_mode,
|
|
|
|
SearchMode::CaseSensitive => self.case_sensitive_mode,
|
|
|
|
SearchMode::Regex => self.regex_mode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn toggle_mode(&mut self, ToggleMode(mode): &ToggleMode, cx: &mut ViewContext<Self>) {
|
|
|
|
let value = match mode {
|
|
|
|
SearchMode::WholeWord => &mut self.whole_word_mode,
|
|
|
|
SearchMode::CaseSensitive => &mut self.case_sensitive_mode,
|
|
|
|
SearchMode::Regex => &mut self.regex_mode,
|
|
|
|
};
|
|
|
|
*value = !*value;
|
2022-02-18 15:45:59 +00:00
|
|
|
self.update_matches(true, cx);
|
2022-01-28 00:16:51 +00:00
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:00:00 +00:00
|
|
|
fn go_to_match(&mut self, GoToMatch(direction): &GoToMatch, cx: &mut ViewContext<Self>) {
|
|
|
|
if let Some(mut index) = self.active_match_index {
|
|
|
|
if let Some(editor) = self.active_editor.as_ref() {
|
|
|
|
editor.update(cx, |editor, cx| {
|
2022-02-10 17:16:54 +00:00
|
|
|
let newest_selection = editor.newest_anchor_selection().clone();
|
2022-02-18 15:33:26 +00:00
|
|
|
if let Some(ranges) = self.editors_with_matches.get(&cx.weak_handle()) {
|
2022-01-29 15:16:48 +00:00
|
|
|
let position = newest_selection.head();
|
|
|
|
let buffer = editor.buffer().read(cx).read(cx);
|
2022-01-29 15:34:49 +00:00
|
|
|
if ranges[index].start.cmp(&position, &buffer).unwrap().is_gt() {
|
2022-01-29 15:16:48 +00:00
|
|
|
if *direction == Direction::Prev {
|
2022-01-28 22:00:00 +00:00
|
|
|
if index == 0 {
|
|
|
|
index = ranges.len() - 1;
|
|
|
|
} else {
|
|
|
|
index -= 1;
|
|
|
|
}
|
2022-01-29 15:34:49 +00:00
|
|
|
}
|
|
|
|
} else if ranges[index].end.cmp(&position, &buffer).unwrap().is_lt() {
|
|
|
|
if *direction == Direction::Next {
|
|
|
|
index = 0;
|
2022-01-28 22:00:00 +00:00
|
|
|
}
|
2022-01-29 15:16:48 +00:00
|
|
|
} else if *direction == Direction::Prev {
|
|
|
|
if index == 0 {
|
|
|
|
index = ranges.len() - 1;
|
|
|
|
} else {
|
|
|
|
index -= 1;
|
|
|
|
}
|
2022-01-29 15:34:49 +00:00
|
|
|
} else if *direction == Direction::Next {
|
|
|
|
if index == ranges.len() - 1 {
|
|
|
|
index = 0
|
|
|
|
} else {
|
|
|
|
index += 1;
|
|
|
|
}
|
2022-01-28 22:00:00 +00:00
|
|
|
}
|
2022-01-29 13:38:58 +00:00
|
|
|
|
|
|
|
let range_to_select = ranges[index].clone();
|
2022-01-29 15:16:48 +00:00
|
|
|
drop(buffer);
|
2022-01-29 13:38:58 +00:00
|
|
|
editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
|
2022-01-28 22:00:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 03:59:20 +00:00
|
|
|
fn go_to_match_on_pane(pane: &mut Pane, action: &GoToMatch, cx: &mut ViewContext<Pane>) {
|
|
|
|
if let Some(find_bar) = pane.toolbar::<FindBar>() {
|
|
|
|
find_bar.update(cx, |find_bar, cx| find_bar.go_to_match(action, cx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:00:51 +00:00
|
|
|
fn on_query_editor_event(
|
|
|
|
&mut self,
|
|
|
|
_: ViewHandle<Editor>,
|
2022-01-28 14:19:58 +00:00
|
|
|
event: &editor::Event,
|
2022-01-27 21:00:51 +00:00
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
2022-01-28 14:19:58 +00:00
|
|
|
match event {
|
|
|
|
editor::Event::Edited => {
|
2022-01-28 15:15:18 +00:00
|
|
|
self.query_contains_error = false;
|
2022-01-31 08:58:03 +00:00
|
|
|
self.clear_matches(cx);
|
2022-02-18 15:45:59 +00:00
|
|
|
self.update_matches(true, cx);
|
2022-01-28 15:15:18 +00:00
|
|
|
cx.notify();
|
2022-01-28 14:19:58 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_active_editor_event(
|
|
|
|
&mut self,
|
2022-01-29 13:38:58 +00:00
|
|
|
_: ViewHandle<Editor>,
|
2022-01-28 14:19:58 +00:00
|
|
|
event: &editor::Event,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
match event {
|
2022-02-18 15:45:59 +00:00
|
|
|
editor::Event::Edited => self.update_matches(false, cx),
|
2022-01-28 22:00:00 +00:00
|
|
|
editor::Event::SelectionsChanged => self.update_match_index(cx),
|
2022-01-28 14:19:58 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2022-01-28 11:15:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 08:58:03 +00:00
|
|
|
fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
|
2022-02-18 16:01:22 +00:00
|
|
|
let mut active_editor_matches = None;
|
|
|
|
for (editor, ranges) in self.editors_with_matches.drain() {
|
2022-01-31 08:58:03 +00:00
|
|
|
if let Some(editor) = editor.upgrade(cx) {
|
2022-02-18 16:01:22 +00:00
|
|
|
if Some(&editor) == self.active_editor.as_ref() {
|
|
|
|
active_editor_matches = Some((editor.downgrade(), ranges));
|
|
|
|
} else {
|
2022-01-31 08:58:03 +00:00
|
|
|
editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 16:01:22 +00:00
|
|
|
self.editors_with_matches.extend(active_editor_matches);
|
2022-01-31 08:58:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 15:45:59 +00:00
|
|
|
fn update_matches(&mut self, select_closest_match: bool, cx: &mut ViewContext<Self>) {
|
2022-01-28 15:15:18 +00:00
|
|
|
let query = self.query_editor.read(cx).text(cx);
|
2022-01-28 11:15:55 +00:00
|
|
|
self.pending_search.take();
|
|
|
|
if let Some(editor) = self.active_editor.as_ref() {
|
2022-01-28 15:15:18 +00:00
|
|
|
if query.is_empty() {
|
2022-01-29 13:38:58 +00:00
|
|
|
self.active_match_index.take();
|
2022-01-28 11:15:55 +00:00
|
|
|
editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
|
|
|
|
} else {
|
|
|
|
let buffer = editor.read(cx).buffer().read(cx).snapshot(cx);
|
2022-01-28 15:15:18 +00:00
|
|
|
let case_sensitive = self.case_sensitive_mode;
|
|
|
|
let whole_word = self.whole_word_mode;
|
|
|
|
let ranges = if self.regex_mode {
|
|
|
|
cx.background()
|
|
|
|
.spawn(regex_search(buffer, query, case_sensitive, whole_word))
|
|
|
|
} else {
|
|
|
|
cx.background().spawn(async move {
|
|
|
|
Ok(search(buffer, query, case_sensitive, whole_word).await)
|
|
|
|
})
|
|
|
|
};
|
2022-01-28 11:15:55 +00:00
|
|
|
|
2022-01-28 15:15:18 +00:00
|
|
|
let editor = editor.downgrade();
|
|
|
|
self.pending_search = Some(cx.spawn(|this, mut cx| async move {
|
|
|
|
match ranges.await {
|
|
|
|
Ok(ranges) => {
|
2022-02-11 13:41:19 +00:00
|
|
|
if let Some(editor) = editor.upgrade(&cx) {
|
2022-01-28 15:15:18 +00:00
|
|
|
this.update(&mut cx, |this, cx| {
|
2022-02-18 15:33:26 +00:00
|
|
|
this.editors_with_matches
|
|
|
|
.insert(editor.downgrade(), ranges.clone());
|
2022-02-18 15:45:59 +00:00
|
|
|
this.update_match_index(cx);
|
2022-02-18 15:33:26 +00:00
|
|
|
if !this.dismissed {
|
|
|
|
editor.update(cx, |editor, cx| {
|
|
|
|
let theme = &this.settings.borrow().theme.find;
|
2022-02-18 15:45:59 +00:00
|
|
|
|
|
|
|
if select_closest_match {
|
|
|
|
if let Some(match_ix) = this.active_match_index {
|
|
|
|
editor.select_ranges(
|
|
|
|
[ranges[match_ix].clone()],
|
|
|
|
Some(Autoscroll::Fit),
|
|
|
|
cx,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 15:33:26 +00:00
|
|
|
editor.highlight_ranges::<Self>(
|
|
|
|
ranges,
|
|
|
|
theme.match_background,
|
|
|
|
cx,
|
2022-02-18 15:45:59 +00:00
|
|
|
);
|
2022-02-18 15:33:26 +00:00
|
|
|
});
|
|
|
|
}
|
2022-01-28 15:15:18 +00:00
|
|
|
});
|
2022-01-28 10:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-28 15:15:18 +00:00
|
|
|
Err(_) => {
|
|
|
|
this.update(&mut cx, |this, cx| {
|
|
|
|
this.query_contains_error = true;
|
|
|
|
cx.notify();
|
2022-01-28 11:15:55 +00:00
|
|
|
});
|
2022-01-28 15:15:18 +00:00
|
|
|
}
|
2022-01-28 11:15:55 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2022-01-27 21:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-28 22:00:00 +00:00
|
|
|
|
|
|
|
fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
self.active_match_index = self.active_match_index(cx);
|
2022-01-29 13:38:58 +00:00
|
|
|
cx.notify();
|
2022-01-28 22:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn active_match_index(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> {
|
|
|
|
let editor = self.active_editor.as_ref()?;
|
2022-02-18 15:33:26 +00:00
|
|
|
let ranges = self.editors_with_matches.get(&editor.downgrade())?;
|
2022-01-28 22:00:00 +00:00
|
|
|
let editor = editor.read(cx);
|
2022-02-10 17:16:54 +00:00
|
|
|
let position = editor.newest_anchor_selection().head();
|
2022-01-31 10:04:53 +00:00
|
|
|
if ranges.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let buffer = editor.buffer().read(cx).read(cx);
|
|
|
|
match ranges.binary_search_by(|probe| {
|
|
|
|
if probe.end.cmp(&position, &*buffer).unwrap().is_lt() {
|
|
|
|
Ordering::Less
|
|
|
|
} else if probe.start.cmp(&position, &*buffer).unwrap().is_gt() {
|
|
|
|
Ordering::Greater
|
|
|
|
} else {
|
|
|
|
Ordering::Equal
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)),
|
2022-01-28 22:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 16:39:58 +00:00
|
|
|
}
|
2022-01-28 15:15:18 +00:00
|
|
|
|
|
|
|
const YIELD_INTERVAL: usize = 20000;
|
|
|
|
|
|
|
|
async fn search(
|
|
|
|
buffer: MultiBufferSnapshot,
|
|
|
|
query: String,
|
|
|
|
case_sensitive: bool,
|
|
|
|
whole_word: bool,
|
|
|
|
) -> Vec<Range<Anchor>> {
|
|
|
|
let mut ranges = Vec::new();
|
|
|
|
|
|
|
|
let search = AhoCorasickBuilder::new()
|
|
|
|
.auto_configure(&[&query])
|
|
|
|
.ascii_case_insensitive(!case_sensitive)
|
|
|
|
.build(&[&query]);
|
|
|
|
for (ix, mat) in search
|
|
|
|
.stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
if (ix + 1) % YIELD_INTERVAL == 0 {
|
|
|
|
yield_now().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mat = mat.unwrap();
|
|
|
|
|
|
|
|
if whole_word {
|
|
|
|
let prev_kind = buffer.reversed_chars_at(mat.start()).next().map(char_kind);
|
|
|
|
let start_kind = char_kind(buffer.chars_at(mat.start()).next().unwrap());
|
|
|
|
let end_kind = char_kind(buffer.reversed_chars_at(mat.end()).next().unwrap());
|
|
|
|
let next_kind = buffer.chars_at(mat.end()).next().map(char_kind);
|
|
|
|
if Some(start_kind) == prev_kind || Some(end_kind) == next_kind {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn regex_search(
|
|
|
|
buffer: MultiBufferSnapshot,
|
|
|
|
mut query: String,
|
|
|
|
case_sensitive: bool,
|
|
|
|
whole_word: bool,
|
|
|
|
) -> Result<Vec<Range<Anchor>>> {
|
|
|
|
if whole_word {
|
|
|
|
let mut word_query = String::new();
|
|
|
|
word_query.push_str("\\b");
|
|
|
|
word_query.push_str(&query);
|
|
|
|
word_query.push_str("\\b");
|
|
|
|
query = word_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ranges = Vec::new();
|
|
|
|
|
2022-01-28 16:26:08 +00:00
|
|
|
if query.contains("\n") || query.contains("\\n") {
|
|
|
|
let regex = RegexBuilder::new(&query)
|
|
|
|
.case_insensitive(!case_sensitive)
|
|
|
|
.multi_line(true)
|
|
|
|
.build()?;
|
|
|
|
for (ix, mat) in regex.find_iter(&buffer.text()).enumerate() {
|
|
|
|
if (ix + 1) % YIELD_INTERVAL == 0 {
|
|
|
|
yield_now().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
|
2022-01-28 15:15:18 +00:00
|
|
|
}
|
2022-01-28 16:26:08 +00:00
|
|
|
} else {
|
|
|
|
let regex = RegexBuilder::new(&query)
|
|
|
|
.case_insensitive(!case_sensitive)
|
|
|
|
.build()?;
|
|
|
|
|
|
|
|
let mut line = String::new();
|
|
|
|
let mut line_offset = 0;
|
|
|
|
for (chunk_ix, chunk) in buffer
|
2022-02-03 10:21:30 +00:00
|
|
|
.chunks(0..buffer.len(), false)
|
2022-01-28 16:26:08 +00:00
|
|
|
.map(|c| c.text)
|
|
|
|
.chain(["\n"])
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
if (chunk_ix + 1) % YIELD_INTERVAL == 0 {
|
|
|
|
yield_now().await;
|
|
|
|
}
|
2022-01-28 15:15:18 +00:00
|
|
|
|
2022-01-28 16:26:08 +00:00
|
|
|
for (newline_ix, text) in chunk.split('\n').enumerate() {
|
|
|
|
if newline_ix > 0 {
|
|
|
|
for mat in regex.find_iter(&line) {
|
|
|
|
let start = line_offset + mat.start();
|
|
|
|
let end = line_offset + mat.end();
|
|
|
|
ranges.push(buffer.anchor_after(start)..buffer.anchor_before(end));
|
|
|
|
}
|
|
|
|
|
|
|
|
line_offset += line.len() + 1;
|
|
|
|
line.clear();
|
|
|
|
}
|
|
|
|
line.push_str(text);
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ranges)
|
|
|
|
}
|
2022-01-28 21:05:29 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use editor::{DisplayPoint, Editor, EditorSettings, MultiBuffer};
|
|
|
|
use gpui::{color::Color, TestAppContext};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use unindent::Unindent as _;
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_find_simple(mut cx: TestAppContext) {
|
|
|
|
let fonts = cx.font_cache();
|
|
|
|
let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
|
|
|
|
theme.find.match_background = Color::red();
|
|
|
|
let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
|
|
|
|
|
|
|
|
let buffer = cx.update(|cx| {
|
|
|
|
MultiBuffer::build_simple(
|
|
|
|
&r#"
|
|
|
|
A regular expression (shortened as regex or regexp;[1] also referred to as
|
|
|
|
rational expression[2][3]) is a sequence of characters that specifies a search
|
|
|
|
pattern in text. Usually such patterns are used by string-searching algorithms
|
|
|
|
for "find" or "find and replace" operations on strings, or for input validation.
|
|
|
|
"#
|
|
|
|
.unindent(),
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
let editor = cx.add_view(Default::default(), |cx| {
|
2022-02-08 22:24:45 +00:00
|
|
|
Editor::new(buffer.clone(), Arc::new(EditorSettings::test), None, cx)
|
2022-01-28 21:05:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let find_bar = cx.add_view(Default::default(), |cx| {
|
|
|
|
let mut find_bar = FindBar::new(watch::channel_with(settings).1, cx);
|
|
|
|
find_bar.active_item_changed(Some(Box::new(editor.clone())), cx);
|
|
|
|
find_bar
|
|
|
|
});
|
|
|
|
|
2022-01-28 22:00:00 +00:00
|
|
|
// Search for a string that appears with different casing.
|
|
|
|
// By default, search is case-insensitive.
|
2022-01-28 21:05:29 +00:00
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.set_query("us", cx);
|
|
|
|
});
|
|
|
|
editor.next_notification(&cx).await;
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
assert_eq!(
|
2022-01-28 22:00:00 +00:00
|
|
|
editor.all_highlighted_ranges(cx),
|
2022-01-28 21:05:29 +00:00
|
|
|
&[
|
|
|
|
(
|
|
|
|
DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-01-28 22:00:00 +00:00
|
|
|
// Switch to a case sensitive search.
|
2022-01-28 21:05:29 +00:00
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.toggle_mode(&ToggleMode(SearchMode::CaseSensitive), cx);
|
|
|
|
});
|
|
|
|
editor.next_notification(&cx).await;
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
assert_eq!(
|
2022-01-28 22:00:00 +00:00
|
|
|
editor.all_highlighted_ranges(cx),
|
2022-01-28 21:05:29 +00:00
|
|
|
&[(
|
|
|
|
DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
|
|
|
|
Color::red(),
|
2022-01-28 22:00:00 +00:00
|
|
|
)]
|
2022-01-28 21:05:29 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-01-28 22:00:00 +00:00
|
|
|
// Search for a string that appears both as a whole word and
|
|
|
|
// within other words. By default, all results are found.
|
2022-01-28 21:05:29 +00:00
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.set_query("or", cx);
|
|
|
|
});
|
|
|
|
editor.next_notification(&cx).await;
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
assert_eq!(
|
2022-01-28 22:00:00 +00:00
|
|
|
editor.all_highlighted_ranges(cx),
|
2022-01-28 21:05:29 +00:00
|
|
|
&[
|
|
|
|
(
|
|
|
|
DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-01-28 22:00:00 +00:00
|
|
|
// Switch to a whole word search.
|
2022-01-28 21:05:29 +00:00
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.toggle_mode(&ToggleMode(SearchMode::WholeWord), cx);
|
|
|
|
});
|
|
|
|
editor.next_notification(&cx).await;
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
assert_eq!(
|
2022-01-28 22:00:00 +00:00
|
|
|
editor.all_highlighted_ranges(cx),
|
2022-01-28 21:05:29 +00:00
|
|
|
&[
|
|
|
|
(
|
|
|
|
DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
|
|
|
|
Color::red(),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
2022-01-28 22:00:00 +00:00
|
|
|
|
2022-01-29 15:34:49 +00:00
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
|
|
|
|
});
|
2022-01-28 22:00:00 +00:00
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
2022-01-29 15:34:49 +00:00
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
2022-01-28 22:00:00 +00:00
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
|
2022-01-29 15:16:48 +00:00
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
|
|
|
});
|
|
|
|
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(2));
|
|
|
|
});
|
|
|
|
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
|
|
|
});
|
|
|
|
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(2));
|
|
|
|
});
|
|
|
|
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
|
|
|
});
|
|
|
|
|
2022-01-29 15:34:49 +00:00
|
|
|
// Park the cursor in between matches and ensure that going to the previous match selects
|
|
|
|
// the closest match to the left.
|
2022-01-29 15:16:48 +00:00
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
|
|
|
|
});
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(1));
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
2022-01-28 22:00:00 +00:00
|
|
|
});
|
2022-01-29 15:34:49 +00:00
|
|
|
|
|
|
|
// Park the cursor in between matches and ensure that going to the next match selects the
|
|
|
|
// closest match to the right.
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
|
|
|
|
});
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(1));
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Park the cursor after the last match and ensure that going to the previous match selects
|
|
|
|
// the last match.
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
|
|
|
|
});
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(2));
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(2));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Park the cursor after the last match and ensure that going to the next match selects the
|
|
|
|
// first match.
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
|
|
|
|
});
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(2));
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Park the cursor before the first match and ensure that going to the previous match
|
|
|
|
// selects the last match.
|
|
|
|
editor.update(&mut cx, |editor, cx| {
|
|
|
|
editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
|
|
|
|
});
|
|
|
|
find_bar.update(&mut cx, |find_bar, cx| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(0));
|
|
|
|
find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
|
|
|
|
assert_eq!(
|
|
|
|
editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
find_bar.read_with(&cx, |find_bar, _| {
|
|
|
|
assert_eq!(find_bar.active_match_index, Some(2));
|
|
|
|
});
|
2022-01-28 21:05:29 +00:00
|
|
|
}
|
|
|
|
}
|