2022-01-27 21:00:51 +00:00
|
|
|
use aho_corasick::AhoCorasick;
|
2022-01-27 20:58:21 +00:00
|
|
|
use editor::{Editor, EditorSettings};
|
2022-01-27 16:39:58 +00:00
|
|
|
use gpui::{
|
2022-01-27 20:58:21 +00:00
|
|
|
action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View,
|
|
|
|
ViewContext, ViewHandle,
|
2022-01-27 16:39:58 +00:00
|
|
|
};
|
2022-01-27 20:58:21 +00:00
|
|
|
use postage::watch;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use workspace::{ItemViewHandle, Settings, Toolbar, Workspace};
|
2022-01-27 16:39:58 +00:00
|
|
|
|
|
|
|
action!(Deploy);
|
2022-01-27 21:00:51 +00:00
|
|
|
action!(Cancel);
|
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([
|
|
|
|
Binding::new("cmd-f", Deploy, Some("Editor && mode == full")),
|
|
|
|
Binding::new("escape", Cancel, Some("FindBar")),
|
|
|
|
]);
|
2022-01-27 16:39:58 +00:00
|
|
|
cx.add_action(FindBar::deploy);
|
2022-01-27 21:00:51 +00:00
|
|
|
cx.add_action(FindBar::cancel);
|
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-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-27 16:39:58 +00:00
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2022-01-27 20:58:21 +00:00
|
|
|
ChildView::new(&self.query_editor)
|
2022-01-27 16:39:58 +00:00
|
|
|
.contained()
|
2022-01-27 20:58:21 +00:00
|
|
|
.with_style(self.settings.borrow().theme.selector.input_editor.container)
|
2022-01-27 16:39:58 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
self.active_editor = item.and_then(|item| item.act_as::<Editor>(cx));
|
|
|
|
self.active_editor.is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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| {
|
|
|
|
Editor::single_line(
|
|
|
|
{
|
|
|
|
let settings = settings.clone();
|
|
|
|
Arc::new(move |_| {
|
|
|
|
let settings = settings.borrow();
|
|
|
|
EditorSettings {
|
|
|
|
style: settings.theme.selector.input_editor.as_editor(),
|
|
|
|
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,
|
|
|
|
settings,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:39:58 +00:00
|
|
|
fn deploy(workspace: &mut Workspace, _: &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));
|
|
|
|
if let Some(toolbar) = pane.active_toolbar() {
|
|
|
|
cx.focus(toolbar);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cancel(workspace: &mut Workspace, _: &Cancel, cx: &mut ViewContext<Workspace>) {
|
2022-01-27 16:39:58 +00:00
|
|
|
workspace
|
|
|
|
.active_pane()
|
2022-01-27 20:58:21 +00:00
|
|
|
.update(cx, |pane, cx| pane.hide_toolbar(cx));
|
2022-01-27 16:39:58 +00:00
|
|
|
}
|
2022-01-27 21:00:51 +00:00
|
|
|
|
|
|
|
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);
|
2022-01-27 23:19:52 +00:00
|
|
|
let theme = &self.settings.borrow().theme.find;
|
2022-01-27 21:00:51 +00:00
|
|
|
editor.update(cx, |editor, cx| {
|
2022-01-27 23:19:52 +00:00
|
|
|
if search.is_empty() {
|
|
|
|
editor.clear_highlighted_ranges::<Self>(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let search = AhoCorasick::new_auto_configured(&[search]);
|
2022-01-27 21:00:51 +00:00
|
|
|
let buffer = editor.buffer().read(cx).snapshot(cx);
|
2022-01-27 23:19:52 +00:00
|
|
|
let ranges = search
|
2022-01-27 21:00:51 +00:00
|
|
|
.stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
|
|
|
|
.map(|mat| {
|
|
|
|
let mat = mat.unwrap();
|
2022-01-27 23:19:52 +00:00
|
|
|
buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end())
|
2022-01-27 21:00:51 +00:00
|
|
|
})
|
2022-01-27 23:19:52 +00:00
|
|
|
.collect();
|
|
|
|
editor.highlight_ranges::<Self>(ranges, theme.match_background, cx);
|
2022-01-27 21:00:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 16:39:58 +00:00
|
|
|
}
|