use ropey::Rope; use rust_i18n::t; use std::ops::Range; use gpui::{ App, AppContext as _, Context, Empty, Entity, FocusHandle, Focusable, Half, InteractiveElement as _, IntoElement, ParentElement as _, Pixels, Render, Styled, Subscription, WeakEntity, Window, actions, div, prelude::FluentBuilder as _, }; use crate::{ ActiveTheme, Disableable, ElementExt, IconName, Selectable, Sizable, button::{Button, ButtonVariants}, h_flex, input::{ Enter, Escape, IndentInline, Input, InputBaseState, InputEvent, InputState, OutdentInline, Replace, }, label::Label, v_flex, }; const CONTEXT: &'static str = "SearchPanel"; actions!(input, [Tab]); #[cfg(test)] use gpui_base::input::SearchMatcher; #[derive(Clone, Copy)] #[cfg(test)] enum MoveDirection { Up, Down, } #[cfg(test)] fn next_scroll_direction( previous_match_ix: usize, current_match_ix: usize, ) -> Option { if current_match_ix <= previous_match_ix { None } else { Some(MoveDirection::Down) } } #[cfg(test)] fn prev_scroll_direction( previous_match_ix: usize, current_match_ix: usize, ) -> Option { if current_match_ix >= previous_match_ix { None } else { Some(MoveDirection::Up) } } pub(super) struct SearchPanel { editor: WeakEntity>, search_input: Entity, replace_input: Entity, session: gpui_base::input::SearchSession, input_width: Pixels, _subscriptions: Vec, } impl SearchPanel { pub(super) fn replace_mode(&self) -> bool { self.session.replace_mode } pub(super) fn sync_session(&mut self, session: &gpui_base::input::SearchSession) { self.session = session.clone(); } /// The query the panel's search input currently holds. pub(super) fn query(&self, cx: &App) -> gpui::SharedString { self.search_input.read(cx).value() } /// The panel's search input, so a test can assert what re-invoking search /// does to its value and selection. #[cfg(test)] pub(super) fn search_input(&self) -> &Entity { &self.search_input } pub(crate) fn new( editor: Entity>, window: &mut Window, cx: &mut App, ) -> Entity { let search_input = cx.new(|cx| InputState::new(window, cx)); let replace_input = cx.new(|cx| InputState::new(window, cx)); cx.new(|cx| { let _subscriptions = vec![ cx.subscribe(&search_input, |this: &mut Self, _, ev: &InputEvent, cx| { // Handle search input changes match ev { InputEvent::Change => { this.update_search_query(None, cx); } _ => {} } }), ]; Self { editor: editor.downgrade(), search_input, replace_input, session: gpui_base::input::SearchSession::default(), input_width: Pixels::ZERO, _subscriptions, } }) } pub(super) fn show_with_focus( &mut self, selected_text: &Rope, replace_mode: bool, visible_range_offset: Option>, focus: bool, window: &mut Window, cx: &mut Context, ) { self.session.open = true; self.session.replace_mode = replace_mode; if focus { self.search_input .update(cx, |input, cx| input.focus(window, cx)); } self.search_input.update(cx, |this, cx| { if selected_text.len() > 0 { this.set_value(selected_text.to_string(), window, cx); } this.select_all(window, cx); }); // The `set_value` does not emit `InputEvent::Change`, so update the query // here to match the value of the search input. self.update_search_query(visible_range_offset, cx); } /// Update the matcher by the value of the search input. /// /// The `visible_range_offset` is to select the nearest match of the visible range, /// it is passed in, because the editor may be borrowed by the caller. fn update_search_query( &mut self, visible_range_offset: Option>, cx: &mut Context, ) { let query = self.search_input.read(cx).value(); let editor = self.editor.clone(); let _ = editor.update(cx, |state, cx| { state.set_search_query(query.clone(), self.session.case_insensitive, cx); }); if let Ok(session) = editor.read_with(cx, |state, _| state.search_session().clone()) { self.session = session; } if let Some(visible_range_offset) = visible_range_offset { self.session .matcher .update_cursor_by_offset(visible_range_offset.start); } cx.notify(); } fn replaceable(&self, cx: &App) -> bool { self.editor .read_with(cx, |editor, _| editor.is_replaceable()) .unwrap_or(false) } pub(super) fn hide(&mut self, window: &mut Window, cx: &mut Context) { self.hide_with_focus(true, window, cx); } pub(super) fn hide_with_focus( &mut self, focus_editor: bool, window: &mut Window, cx: &mut Context, ) { self.session.open = false; let _ = self.editor.update(cx, |state, cx| { state.close_search(cx); if focus_editor { state.focus(window, cx); } }); cx.notify(); } fn on_action_enter(&mut self, action: &Enter, window: &mut Window, cx: &mut Context) { if action.shift { self.prev(window, cx); } else { self.next(window, cx); } } fn on_action_escape(&mut self, _: &Escape, window: &mut Window, cx: &mut Context) { self.hide(window, cx); } fn on_action_tab(&mut self, _: &IndentInline, window: &mut Window, cx: &mut Context) { self.cycle_focus(window, cx); } fn on_action_tab_prev( &mut self, _: &OutdentInline, window: &mut Window, cx: &mut Context, ) { self.cycle_focus(window, cx); } /// Cycle focus between the search and the replace input, to keep the Tab key /// staying in the panel. /// /// There are only 2 inputs, so the forward and the backward are the same. fn cycle_focus(&mut self, window: &mut Window, cx: &mut Context) { if !self.session.replace_mode || !self.replaceable(cx) { return; } let search_focus_handle = self.search_input.read(cx).focus_handle(cx); let focus_handle = if search_focus_handle.is_focused(window) { self.replace_input.read(cx).focus_handle(cx) } else { search_focus_handle }; focus_handle.focus(window, cx); } fn on_action_replace(&mut self, _: &Replace, window: &mut Window, cx: &mut Context) { self.toggle_replace_mode(window, cx); } /// Toggle the replace field, and move focus to the field that is going to be used. fn toggle_replace_mode(&mut self, window: &mut Window, cx: &mut Context) { if !self.replaceable(cx) { return; } self.session.replace_mode = !self.session.replace_mode; let replace_mode = self.session.replace_mode; let _ = self.editor.update(cx, |state, cx| { state.set_search_replace_mode(replace_mode, cx); }); let focus_handle = if self.session.replace_mode { self.replace_input.read(cx).focus_handle(cx) } else { self.search_input.read(cx).focus_handle(cx) }; focus_handle.focus(window, cx); cx.notify(); } fn prev(&mut self, _: &mut Window, cx: &mut Context) { let _ = self.editor.update(cx, |state, cx| { _ = state.previous_search_match(cx); }); } fn next(&mut self, _: &mut Window, cx: &mut Context) { let _ = self.editor.update(cx, |state, cx| { _ = state.next_search_match(cx); }); } fn replace_next(&mut self, window: &mut Window, cx: &mut Context) { if !self.replaceable(cx) { self.session.replace_mode = false; cx.notify(); return; } let replacement = self.replace_input.read(cx).value(); let _ = self.editor.update(cx, |state, cx| { _ = state.replace_current_search_match(&replacement, window, cx); }); } fn replace_all(&mut self, window: &mut Window, cx: &mut Context) { if !self.replaceable(cx) { self.session.replace_mode = false; cx.notify(); return; } let replacement = self.replace_input.read(cx).value(); let _ = self.editor.update(cx, |state, cx| { _ = state.replace_all_search_matches(&replacement, window, cx); }); } } impl Focusable for SearchPanel { fn focus_handle(&self, cx: &App) -> FocusHandle { self.search_input.read(cx).focus_handle(cx) } } impl Render for SearchPanel { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { if !self.session.open { return Empty.into_any_element(); } let has_matches = !self.session.matcher.is_empty(); let allow_replace = self.replaceable(cx); if !allow_replace { self.session.replace_mode = false; } v_flex() .id("search-panel") .occlude() .track_focus(&self.focus_handle(cx)) .key_context(CONTEXT) .on_action(cx.listener(Self::on_action_enter)) .on_action(cx.listener(Self::on_action_escape)) .on_action(cx.listener(Self::on_action_tab)) .on_action(cx.listener(Self::on_action_tab_prev)) .on_action(cx.listener(Self::on_action_replace)) .font_family(cx.theme().font_family.clone()) .items_center() .py_2() .px_3() .w_full() .gap_1() .bg(cx.theme().tokens.popover) .border_b_1() .rounded(cx.theme().radius.half()) .border_color(cx.theme().border) .child( h_flex() .w_full() .gap_2() .child( div() .flex() .flex_1() .gap_1() .child( Input::new(&self.search_input) .focus_bordered(false) .suffix( Button::new("case-insensitive") .selected(!self.session.case_insensitive) .toggled(!self.session.case_insensitive) .xsmall() .compact() .text() .icon(IconName::CaseSensitive) .on_click(cx.listener(|this, _, _, cx| { this.session.case_insensitive = !this.session.case_insensitive; this.update_search_query(None, cx); cx.notify(); })), ) .small() .w_full() .shadow_none(), ) .on_prepaint({ let view = cx.entity(); move |bounds, _, cx| { view.update(cx, |r, _| r.input_width = bounds.size.width) } }), ) .when(allow_replace, |this| { this.child( Button::new("replace-mode") .xsmall() .ghost() .icon(IconName::Replace) .selected(self.session.replace_mode) .toggled(self.session.replace_mode) .on_click(cx.listener(|this, _, window, cx| { this.toggle_replace_mode(window, cx); })), ) }) .child( Button::new("prev") .xsmall() .ghost() .icon(IconName::ChevronLeft) .disabled(!has_matches) .on_click(cx.listener(|this, _, window, cx| { this.prev(window, cx); })), ) .child( Button::new("next") .xsmall() .ghost() .icon(IconName::ChevronRight) .disabled(!has_matches) .on_click(cx.listener(|this, _, window, cx| { this.next(window, cx); })), ) .child( Label::new(self.session.matcher.label()) .when(!has_matches, |this| { this.text_color(cx.theme().muted_foreground) }) .text_left() .min_w_16(), ) .child(div().w_7()) .child( Button::new("close") .xsmall() .ghost() .icon(IconName::Close) .on_click(cx.listener(|this, _, window, cx| { this.on_action_escape(&Escape, window, cx); })), ), ) .when(self.session.replace_mode && allow_replace, |this| { this.child( h_flex() .w_full() .gap_2() .child( Input::new(&self.replace_input) .focus_bordered(false) .small() .w(self.input_width) .shadow_none(), ) .child( Button::new("replace-one") .small() .label(t!("Input.Replace")) .disabled(!has_matches) .on_click(cx.listener(|this, _, window, cx| { this.replace_next(window, cx); })), ) .child( Button::new("replace-all") .small() .label(t!("Input.Replace All")) .disabled(!has_matches) .on_click(cx.listener(|this, _, window, cx| { this.replace_all(window, cx); })), ), ) }) .into_any_element() } } #[cfg(test)] mod tests { use super::*; use ropey::Rope; #[test] fn test_search() { let mut matcher = SearchMatcher::new(); matcher.update(&Rope::from("Hello 世界 this is a Is test string.")); matcher.update_query("Is", true); assert_eq!(matcher.len(), 3); let mut matches = matcher.clone(); assert_eq!(matches.current_match_index(), 0); assert_eq!(matches.next(), Some(18..20)); assert_eq!(matches.next(), Some(23..25)); assert_eq!(matches.current_match_index(), 2); assert_eq!(matches.next(), Some(15..17)); assert_eq!(matches.current_match_index(), 0); assert_eq!(matches.next_back(), Some(23..25)); assert_eq!(matches.current_match_index(), 2); assert_eq!(matches.next_back(), Some(18..20)); assert_eq!(matches.current_match_index(), 1); assert_eq!(matches.next_back(), Some(15..17)); assert_eq!(matches.current_match_index(), 0); assert_eq!(matches.next_back(), Some(23..25)); matcher.update_query("IS", false); assert_eq!(matcher.len(), 0); assert_eq!(matcher.next(), None); assert_eq!(matcher.next_back(), None); } #[test] fn test_search_label() { let mut matcher = SearchMatcher::new(); matcher.update(&Rope::from("Hello 世界 this is a Is test string.")); matcher.update_query("Is", true); assert_eq!(matcher.label(), "1/3"); matcher.next(); assert_eq!(matcher.label(), "2/3"); matcher.next(); assert_eq!(matcher.label(), "3/3"); matcher.next(); assert_eq!(matcher.label(), "1/3"); matcher.update_query("IS", false); assert_eq!(matcher.label(), "0/0"); } #[test] fn test_select_range_start() { let mut matcher = SearchMatcher::new(); matcher.update(&Rope::from(".....aaaaa.....aaaaa.....aaaaa")); matcher.update_query("aaaaa", false); matcher.update_cursor_by_offset(0); assert_eq!(matcher.current_match_index(), 0); matcher.update_cursor_by_offset(5); assert_eq!(matcher.current_match_index(), 0); matcher.update_cursor_by_offset(12); assert_eq!(matcher.current_match_index(), 1); matcher.update_cursor_by_offset(16); assert_eq!(matcher.current_match_index(), 1); matcher.update_cursor_by_offset(30); assert_eq!(matcher.current_match_index(), 2); matcher.update_cursor_by_offset(31); assert_eq!(matcher.current_match_index(), 2); } #[test] fn test_next_scroll_direction_returns_down_without_wrap() { assert!(matches!( next_scroll_direction(0, 1), Some(MoveDirection::Down) )); } #[test] fn test_next_scroll_direction_returns_none_on_wrap() { assert!(next_scroll_direction(2, 0).is_none()); } #[test] fn test_next_scroll_direction_returns_none_for_single_match() { assert!(next_scroll_direction(0, 0).is_none()); } #[test] fn test_prev_scroll_direction_returns_up_without_wrap() { assert!(matches!( prev_scroll_direction(2, 1), Some(MoveDirection::Up) )); } #[test] fn test_prev_scroll_direction_returns_none_on_wrap() { assert!(prev_scroll_direction(0, 2).is_none()); } #[test] fn test_prev_scroll_direction_returns_none_for_single_match() { assert!(prev_scroll_direction(0, 0).is_none()); } }