WIP - Run substring search when typing in find bar

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-01-27 13:00:51 -08:00
parent 05e20ca72b
commit d8e4464a89
3 changed files with 41 additions and 5 deletions

1
Cargo.lock generated
View file

@ -1723,6 +1723,7 @@ dependencies = [
name = "find" name = "find"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"aho-corasick",
"editor", "editor",
"gpui", "gpui",
"postage", "postage",

View file

@ -7,6 +7,7 @@ edition = "2021"
path = "src/find.rs" path = "src/find.rs"
[dependencies] [dependencies]
aho-corasick = "0.7"
editor = { path = "../editor" } editor = { path = "../editor" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
workspace = { path = "../workspace" } workspace = { path = "../workspace" }

View file

@ -1,3 +1,4 @@
use aho_corasick::AhoCorasick;
use editor::{Editor, EditorSettings}; use editor::{Editor, EditorSettings};
use gpui::{ use gpui::{
action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View, action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View,
@ -8,14 +9,15 @@ use std::sync::Arc;
use workspace::{ItemViewHandle, Settings, Toolbar, Workspace}; use workspace::{ItemViewHandle, Settings, Toolbar, Workspace};
action!(Deploy); action!(Deploy);
action!(Cancel);
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings([Binding::new( cx.add_bindings([
"cmd-f", Binding::new("cmd-f", Deploy, Some("Editor && mode == full")),
Deploy, Binding::new("escape", Cancel, Some("FindBar")),
Some("Editor && mode == full"), ]);
)]);
cx.add_action(FindBar::deploy); cx.add_action(FindBar::deploy);
cx.add_action(FindBar::cancel);
} }
struct FindBar { struct FindBar {
@ -33,6 +35,10 @@ impl View for FindBar {
"FindBar" "FindBar"
} }
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
cx.focus(&self.query_editor);
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox { fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
ChildView::new(&self.query_editor) ChildView::new(&self.query_editor)
.contained() .contained()
@ -95,4 +101,32 @@ impl FindBar {
.active_pane() .active_pane()
.update(cx, |pane, cx| pane.hide_toolbar(cx)); .update(cx, |pane, cx| pane.hide_toolbar(cx));
} }
fn on_query_editor_event(
&mut self,
_: ViewHandle<Editor>,
_: &editor::Event,
cx: &mut ViewContext<Self>,
) {
if let Some(editor) = &self.active_editor {
let search = self.query_editor.read(cx).text(cx);
if search.is_empty() {
return;
}
let search = AhoCorasick::new_auto_configured(&[search]);
editor.update(cx, |editor, cx| {
let buffer = editor.buffer().read(cx).snapshot(cx);
let mut ranges = search
.stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
.map(|mat| {
let mat = mat.unwrap();
mat.start()..mat.end()
})
.peekable();
if ranges.peek().is_some() {
editor.select_ranges(ranges, None, cx);
}
});
}
}
} }