From 27600b6b8d921bc2584b7ef21f6a2b2b07ba3c48 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:42:20 +0100 Subject: [PATCH] Remove dead code (for now). Ensure actions are registed just once (previously some were registered on both Workspace and search bar itself). --- crates/search2/src/buffer_search.rs | 88 ++++++++++------------------- crates/search2/src/mode.rs | 24 -------- crates/search2/src/search_bar.rs | 32 ----------- 3 files changed, 30 insertions(+), 114 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 5e2138062944a598ad18f2ad4173a837853f76f4..d1f9103b671e2639ec6e2eb6b4db5d09f94af866 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -23,7 +23,7 @@ use util::ResultExt; use workspace::{ item::ItemHandle, searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle}, - Pane, ToolbarItemLocation, ToolbarItemView, Workspace, + ToolbarItemLocation, ToolbarItemView, Workspace, }; #[derive(PartialEq, Clone, Deserialize, Default, Action)] @@ -71,11 +71,7 @@ impl Render for BufferSearchBar { // } else { // theme.search.editor.input.container // }; - let supported_options = self - .active_searchable_item - .as_ref() - .map(|active_searchable_item| active_searchable_item.supported_options()) - .unwrap_or_default(); + let supported_options = self.supported_options(); let previous_query_keystrokes = cx .bindings_for_action(&PreviousHistoryQuery {}) @@ -184,18 +180,6 @@ impl Render for BufferSearchBar { }) .on_action(Self::previous_history_query) .on_action(Self::next_history_query) - .when(supported_options.case, |this| { - this.on_action(Self::toggle_case_sensitive) - }) - .when(supported_options.word, |this| { - this.on_action(Self::toggle_whole_word) - }) - .when(supported_options.replacement, |this| { - this.on_action(Self::toggle_replace) - }) - .on_action(Self::select_next_match) - .on_action(Self::select_prev_match) - .on_action(Self::cycle_mode) .w_full() .p_1() .child( @@ -292,7 +276,6 @@ impl ToolbarItemView for BufferSearchBar { return ToolbarItemLocation::Secondary; } } - ToolbarItemLocation::Hidden } @@ -334,22 +317,34 @@ impl BufferSearchBar { } register_action(workspace, |this, action: &ToggleCaseSensitive, cx| { - this.toggle_case_sensitive(action, cx); + if this.supported_options().case { + this.toggle_case_sensitive(action, cx); + } }); register_action(workspace, |this, action: &ToggleWholeWord, cx| { - this.toggle_whole_word(action, cx); + if this.supported_options().word { + this.toggle_whole_word(action, cx); + } }); register_action(workspace, |this, action: &ToggleReplace, cx| { - this.toggle_replace(action, cx); + if this.supported_options().replacement { + this.toggle_replace(action, cx); + } }); register_action(workspace, |this, _: &ActivateRegexMode, cx| { - this.activate_search_mode(SearchMode::Regex, cx); + if this.supported_options().regex { + this.activate_search_mode(SearchMode::Regex, cx); + } }); register_action(workspace, |this, _: &ActivateTextMode, cx| { this.activate_search_mode(SearchMode::Text, cx); }); register_action(workspace, |this, action: &CycleMode, cx| { - this.cycle_mode(action, cx) + 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 + // cycling. + this.cycle_mode(action, cx) + } }); register_action(workspace, |this, action: &SelectNextMatch, cx| { this.select_next_match(action, cx); @@ -360,6 +355,11 @@ impl BufferSearchBar { register_action(workspace, |this, action: &SelectAllMatches, cx| { this.select_all_matches(action, cx); }); + register_action(workspace, |this, _: &editor::Cancel, cx| { + if !this.dismissed { + this.dismiss(&Dismiss, cx); + } + }); } pub fn new(cx: &mut ViewContext) -> Self { let query_editor = cx.build_view(|cx| Editor::single_line(cx)); @@ -393,7 +393,6 @@ impl BufferSearchBar { pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext) { self.dismissed = true; - for searchable_item in self.searchable_items_with_matches.keys() { if let Some(searchable_item) = WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx) @@ -427,13 +426,18 @@ impl BufferSearchBar { if self.active_searchable_item.is_none() { return false; } - self.dismissed = false; cx.notify(); cx.emit(Event::UpdateLocation); true } + fn supported_options(&self) -> workspace::searchable::SearchOptions { + self.active_searchable_item + .as_deref() + .map(SearchableItemHandle::supported_options) + .unwrap_or_default() + } pub fn search_suggested(&mut self, cx: &mut ViewContext) { let search = self .query_suggestion(cx) @@ -542,16 +546,6 @@ impl BufferSearchBar { cx.notify(); } - fn handle_editor_cancel(pane: &mut Pane, _: &editor::Cancel, cx: &mut ViewContext) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - if !search_bar.read(cx).dismissed { - search_bar.update(cx, |search_bar, cx| search_bar.dismiss(&Dismiss, cx)); - cx.stop_propagation(); - return; - } - } - } - pub fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext) { if let Some(active_editor) = self.active_searchable_item.as_ref() { let handle = active_editor.focus_handle(cx); @@ -810,28 +804,6 @@ impl BufferSearchBar { cx.notify(); } } - fn toggle_replace_on_a_pane(pane: &mut Pane, _: &ToggleReplace, cx: &mut ViewContext) { - let mut should_propagate = true; - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| { - if let Some(_) = &bar.active_searchable_item { - should_propagate = false; - bar.replace_enabled = !bar.replace_enabled; - if bar.dismissed { - bar.show(cx); - } - if !bar.replace_enabled { - let handle = bar.query_editor.focus_handle(cx); - cx.focus(&handle); - } - cx.notify(); - } - }); - } - if !should_propagate { - cx.stop_propagation(); - } - } fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext) { let mut should_propagate = true; if !self.dismissed && self.active_search.is_some() { diff --git a/crates/search2/src/mode.rs b/crates/search2/src/mode.rs index bb729cb6c0c198cb1034f04a31125b6df7325e96..4b036d29a5aeb419a808b05ad4a31bd023313431 100644 --- a/crates/search2/src/mode.rs +++ b/crates/search2/src/mode.rs @@ -11,30 +11,6 @@ pub enum SearchMode { } impl SearchMode { - pub(crate) fn label(&self) -> &'static str { - match self { - SearchMode::Text => "Text", - SearchMode::Semantic => "Semantic", - SearchMode::Regex => "Regex", - } - } - - pub(crate) fn region_id(&self) -> usize { - match self { - SearchMode::Text => 3, - SearchMode::Semantic => 4, - SearchMode::Regex => 5, - } - } - - pub(crate) fn tooltip_text(&self) -> &'static str { - match self { - SearchMode::Text => "Activate Text Search", - SearchMode::Semantic => "Activate Semantic Search", - SearchMode::Regex => "Activate Regex Search", - } - } - pub(crate) fn activate_action(&self) -> Box { match self { SearchMode::Text => Box::new(ActivateTextMode), diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 8879644eabe938c3307e83e1e12a5cf270bcbcb9..cd3b5e474bb6ee448e6bd39aa4d05e621c5126ae 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -35,35 +35,3 @@ pub(crate) fn render_search_mode_button( .on_click(Arc::new(on_click)) .variant(button_variant) } - -pub(crate) fn render_option_button_icon( - _is_active: bool, - _icon: &'static str, - _id: usize, - _label: impl Into>, - _action: Box, -) -> impl Component { - //let tooltip_style = cx.theme().tooltip.clone(); - div() - // MouseEventHandler::new::(id, cx, |state, cx| { - // let theme = cx.theme(); - // let style = theme - // .search - // .option_button - // .in_state(is_active) - // .style_for(state); - // Svg::new(icon) - // .with_color(style.color.clone()) - // .constrained() - // .with_width(style.icon_width) - // .contained() - // .with_style(style.container) - // .constrained() - // .with_height(theme.search.option_button_height) - // .with_width(style.button_width) - // }) - // .on_click(MouseButton::Left, on_click) - // .with_cursor_style(CursorStyle::PointingHand) - // .with_tooltip::(id, label, Some(action), tooltip_style, cx) - // .into_any() -}