Register buffer search listener on new workspaces and not editors

This commit is contained in:
Piotr Osiewicz 2023-12-12 14:23:52 +01:00
parent a9f817fc14
commit 1c437a2b92

View file

@ -7,12 +7,12 @@ use crate::{
ToggleCaseSensitive, ToggleReplace, ToggleWholeWord, ToggleCaseSensitive, ToggleReplace, ToggleWholeWord,
}; };
use collections::HashMap; use collections::HashMap;
use editor::{Editor, EditorMode}; use editor::Editor;
use futures::channel::oneshot; use futures::channel::oneshot;
use gpui::{ use gpui::{
actions, div, impl_actions, red, Action, AppContext, Div, EventEmitter, FocusableView, actions, div, impl_actions, red, Action, AppContext, Div, EventEmitter, FocusableView,
InteractiveElement as _, IntoElement, KeyContext, ParentElement as _, Render, Styled, InteractiveElement as _, IntoElement, KeyContext, ParentElement as _, Render, Styled,
Subscription, Task, View, ViewContext, VisualContext as _, WeakView, WindowContext, Subscription, Task, View, ViewContext, VisualContext as _, WindowContext,
}; };
use project::search::SearchQuery; use project::search::SearchQuery;
use serde::Deserialize; use serde::Deserialize;
@ -23,7 +23,7 @@ use util::ResultExt;
use workspace::{ use workspace::{
item::ItemHandle, item::ItemHandle,
searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle}, searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle},
ToolbarItemLocation, ToolbarItemView, ToolbarItemLocation, ToolbarItemView, Workspace,
}; };
#[derive(PartialEq, Clone, Deserialize)] #[derive(PartialEq, Clone, Deserialize)]
@ -40,7 +40,7 @@ pub enum Event {
} }
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.observe_new_views(|editor: &mut Editor, cx| BufferSearchBar::register(editor, cx)) cx.observe_new_views(|editor: &mut Workspace, _| BufferSearchBar::register(editor))
.detach(); .detach();
} }
@ -315,17 +315,9 @@ impl ToolbarItemView for BufferSearchBar {
} }
impl BufferSearchBar { impl BufferSearchBar {
pub fn register(editor: &mut Editor, cx: &mut ViewContext<Editor>) { fn register(workspace: &mut Workspace) {
if editor.mode() != EditorMode::Full { workspace.register_action(move |workspace, deploy: &Deploy, cx| {
return; let pane = workspace.active_pane();
};
let handle = cx.view().downgrade();
editor.register_action(move |deploy: &Deploy, cx| {
let Some(pane) = handle.upgrade().and_then(|editor| editor.read(cx).pane(cx)) else {
return;
};
pane.update(cx, |this, cx| { pane.update(cx, |this, cx| {
this.toolbar().update(cx, |this, cx| { this.toolbar().update(cx, |this, cx| {
@ -341,15 +333,11 @@ impl BufferSearchBar {
}); });
}); });
fn register_action<A: Action>( fn register_action<A: Action>(
editor: &mut Editor, workspace: &mut Workspace,
handle: WeakView<Editor>,
update: fn(&mut BufferSearchBar, &A, &mut ViewContext<BufferSearchBar>), update: fn(&mut BufferSearchBar, &A, &mut ViewContext<BufferSearchBar>),
) { ) {
editor.register_action(move |action: &A, cx| { workspace.register_action(move |workspace, action: &A, cx| {
let Some(pane) = handle.upgrade().and_then(|editor| editor.read(cx).pane(cx)) let pane = workspace.active_pane();
else {
return;
};
pane.update(cx, move |this, cx| { pane.update(cx, move |this, cx| {
this.toolbar().update(cx, move |this, cx| { this.toolbar().update(cx, move |this, cx| {
if let Some(search_bar) = this.item_of_type::<BufferSearchBar>() { if let Some(search_bar) = this.item_of_type::<BufferSearchBar>() {
@ -361,71 +349,46 @@ impl BufferSearchBar {
}); });
} }
let handle = cx.view().downgrade(); register_action(workspace, |this, action: &ToggleCaseSensitive, cx| {
register_action( if this.supported_options().case {
editor, this.toggle_case_sensitive(action, cx);
handle.clone(), }
|this, action: &ToggleCaseSensitive, cx| { });
if this.supported_options().case { register_action(workspace, |this, action: &ToggleWholeWord, cx| {
this.toggle_case_sensitive(action, cx); if this.supported_options().word {
} this.toggle_whole_word(action, cx);
}, }
); });
register_action( register_action(workspace, |this, action: &ToggleReplace, cx| {
editor, if this.supported_options().replacement {
handle.clone(), this.toggle_replace(action, cx);
|this, action: &ToggleWholeWord, cx| { }
if this.supported_options().word { });
this.toggle_whole_word(action, cx); register_action(workspace, |this, _: &ActivateRegexMode, cx| {
}
},
);
register_action(
editor,
handle.clone(),
|this, action: &ToggleReplace, cx| {
if this.supported_options().replacement {
this.toggle_replace(action, cx);
}
},
);
register_action(editor, handle.clone(), |this, _: &ActivateRegexMode, cx| {
if this.supported_options().regex { if this.supported_options().regex {
this.activate_search_mode(SearchMode::Regex, cx); this.activate_search_mode(SearchMode::Regex, cx);
} }
}); });
register_action(editor, handle.clone(), |this, _: &ActivateTextMode, cx| { register_action(workspace, |this, _: &ActivateTextMode, cx| {
this.activate_search_mode(SearchMode::Text, cx); this.activate_search_mode(SearchMode::Text, cx);
}); });
register_action(editor, handle.clone(), |this, action: &CycleMode, cx| { register_action(workspace, |this, action: &CycleMode, cx| {
if this.supported_options().regex { if this.supported_options().regex {
// If regex is not supported then search has just one mode (text) - in that case there's no point in supporting // If regex is not supported then search has just one mode (text) - in that case there's no point in supporting
// cycling. // cycling.
this.cycle_mode(action, cx) this.cycle_mode(action, cx)
} }
}); });
register_action( register_action(workspace, |this, action: &SelectNextMatch, cx| {
editor, this.select_next_match(action, cx);
handle.clone(), });
|this, action: &SelectNextMatch, cx| { register_action(workspace, |this, action: &SelectPrevMatch, cx| {
this.select_next_match(action, cx); this.select_prev_match(action, cx);
}, });
); register_action(workspace, |this, action: &SelectAllMatches, cx| {
register_action( this.select_all_matches(action, cx);
editor, });
handle.clone(), register_action(workspace, |this, _: &editor::Cancel, cx| {
|this, action: &SelectPrevMatch, cx| {
this.select_prev_match(action, cx);
},
);
register_action(
editor,
handle.clone(),
|this, action: &SelectAllMatches, cx| {
this.select_all_matches(action, cx);
},
);
register_action(editor, handle.clone(), |this, _: &editor::Cancel, cx| {
if !this.dismissed { if !this.dismissed {
this.dismiss(&Dismiss, cx); this.dismiss(&Dismiss, cx);
return; return;