Detailed changes
@@ -7855,6 +7855,35 @@ dependencies = [
"workspace",
]
+[[package]]
+name = "search2"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "bitflags 1.3.2",
+ "client2",
+ "collections",
+ "editor2",
+ "futures 0.3.28",
+ "gpui2",
+ "language2",
+ "log",
+ "menu2",
+ "postage",
+ "project2",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "settings2",
+ "smallvec",
+ "smol",
+ "theme2",
+ "ui2",
+ "unindent",
+ "util",
+ "workspace2",
+]
+
[[package]]
name = "security-framework"
version = "2.9.2"
@@ -11425,6 +11454,7 @@ dependencies = [
"rsa 0.4.0",
"rust-embed",
"schemars",
+ "search2",
"serde",
"serde_derive",
"serde_json",
@@ -86,6 +86,7 @@ members = [
"crates/rpc",
"crates/rpc2",
"crates/search",
+ "crates/search2",
"crates/settings",
"crates/settings2",
"crates/snippet",
@@ -906,17 +906,16 @@ impl SearchableItem for Editor {
type Match = Range<Anchor>;
fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
- todo!()
- // self.clear_background_highlights::<BufferSearchHighlights>(cx);
+ self.clear_background_highlights::<BufferSearchHighlights>(cx);
}
fn update_matches(&mut self, matches: Vec<Range<Anchor>>, cx: &mut ViewContext<Self>) {
- todo!()
- // self.highlight_background::<BufferSearchHighlights>(
- // matches,
- // |theme| theme.search.match_background,
- // cx,
- // );
+ dbg!(&matches);
+ self.highlight_background::<BufferSearchHighlights>(
+ matches,
+ |theme| theme.title_bar_background, // todo: update theme
+ cx,
+ );
}
fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
@@ -951,22 +950,20 @@ impl SearchableItem for Editor {
matches: Vec<Range<Anchor>>,
cx: &mut ViewContext<Self>,
) {
- todo!()
- // self.unfold_ranges([matches[index].clone()], false, true, cx);
- // let range = self.range_for_match(&matches[index]);
- // self.change_selections(Some(Autoscroll::fit()), cx, |s| {
- // s.select_ranges([range]);
- // })
+ self.unfold_ranges([matches[index].clone()], false, true, cx);
+ let range = self.range_for_match(&matches[index]);
+ self.change_selections(Some(Autoscroll::fit()), cx, |s| {
+ s.select_ranges([range]);
+ })
}
fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
- todo!()
- // self.unfold_ranges(matches.clone(), false, false, cx);
- // let mut ranges = Vec::new();
- // for m in &matches {
- // ranges.push(self.range_for_match(&m))
- // }
- // self.change_selections(None, cx, |s| s.select_ranges(ranges));
+ self.unfold_ranges(matches.clone(), false, false, cx);
+ let mut ranges = Vec::new();
+ for m in &matches {
+ ranges.push(self.range_for_match(&m))
+ }
+ self.change_selections(None, cx, |s| s.select_ranges(ranges));
}
fn replace(
&mut self,
@@ -0,0 +1,40 @@
+[package]
+name = "search2"
+version = "0.1.0"
+edition = "2021"
+publish = false
+
+[lib]
+path = "src/search.rs"
+doctest = false
+
+[dependencies]
+bitflags = "1"
+collections = { path = "../collections" }
+editor = { package = "editor2", path = "../editor2" }
+gpui = { package = "gpui2", path = "../gpui2" }
+language = { package = "language2", path = "../language2" }
+menu = { package = "menu2", path = "../menu2" }
+project = { package = "project2", path = "../project2" }
+settings = { package = "settings2", path = "../settings2" }
+theme = { package = "theme2", path = "../theme2" }
+util = { path = "../util" }
+ui = {package = "ui2", path = "../ui2"}
+workspace = { package = "workspace2", path = "../workspace2" }
+#semantic_index = { path = "../semantic_index" }
+anyhow.workspace = true
+futures.workspace = true
+log.workspace = true
+postage.workspace = true
+serde.workspace = true
+serde_derive.workspace = true
+smallvec.workspace = true
+smol.workspace = true
+serde_json.workspace = true
+[dev-dependencies]
+client = { package = "client2", path = "../client2", features = ["test-support"] }
+editor = { package = "editor2", path = "../editor2", features = ["test-support"] }
+gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] }
+
+workspace = { package = "workspace2", path = "../workspace2", features = ["test-support"] }
+unindent.workspace = true
@@ -0,0 +1,1797 @@
+use crate::{
+ history::SearchHistory,
+ mode::{next_mode, SearchMode, Side},
+ search_bar::{render_nav_button, render_search_mode_button},
+ CycleMode, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOptions,
+ SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleReplace,
+ ToggleWholeWord,
+};
+use collections::HashMap;
+use editor::Editor;
+use futures::channel::oneshot;
+use gpui::{
+ action, actions, div, Action, AnyElement, AnyView, AppContext, Component, Div, Entity,
+ EventEmitter, ParentElement as _, Render, Subscription, Svg, Task, View, ViewContext,
+ VisualContext as _, WindowContext,
+};
+use project::search::SearchQuery;
+use serde::Deserialize;
+use std::{any::Any, sync::Arc};
+use theme::ActiveTheme;
+
+use ui::{IconButton, Label};
+use util::ResultExt;
+use workspace::{
+ item::ItemHandle,
+ searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle},
+ Pane, ToolbarItemLocation, ToolbarItemView, Workspace,
+};
+
+#[action]
+pub struct Deploy {
+ pub focus: bool,
+}
+
+actions!(Dismiss, FocusEditor);
+
+pub enum Event {
+ UpdateLocation,
+}
+
+pub fn init(cx: &mut AppContext) {
+ dbg!("Registered");
+ cx.observe_new_views(|workspace: &mut Workspace, _| BufferSearchBar::register(workspace))
+ .detach();
+}
+
+pub struct BufferSearchBar {
+ query_editor: View<Editor>,
+ replacement_editor: View<Editor>,
+ active_searchable_item: Option<Box<dyn SearchableItemHandle>>,
+ active_match_index: Option<usize>,
+ active_searchable_item_subscription: Option<Subscription>,
+ active_search: Option<Arc<SearchQuery>>,
+ searchable_items_with_matches:
+ HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,
+ pending_search: Option<Task<()>>,
+ search_options: SearchOptions,
+ default_options: SearchOptions,
+ query_contains_error: bool,
+ dismissed: bool,
+ search_history: SearchHistory,
+ current_mode: SearchMode,
+ replace_enabled: bool,
+}
+
+impl EventEmitter<Event> for BufferSearchBar {}
+impl EventEmitter<workspace::ToolbarItemEvent> for BufferSearchBar {}
+impl Render for BufferSearchBar {
+ // fn ui_name() -> &'static str {
+ // "BufferSearchBar"
+ // }
+
+ // fn update_keymap_context(
+ // &self,
+ // keymap: &mut gpui::keymap_matcher::KeymapContext,
+ // cx: &AppContext,
+ // ) {
+ // Self::reset_to_default_keymap_context(keymap);
+ // let in_replace = self
+ // .replacement_editor
+ // .read_with(cx, |_, cx| cx.is_self_focused())
+ // .unwrap_or(false);
+ // if in_replace {
+ // keymap.add_identifier("in_replace");
+ // }
+ // }
+
+ // fn focus_in(&mut self, _: View, cx: &mut ViewContext<Self>) {
+ // cx.focus(&self.query_editor);
+ // }
+ type Element = Div<Self>;
+ fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
+ let theme = cx.theme().clone();
+ // let query_container_style = if self.query_contains_error {
+ // theme.search.invalid_editor
+ // } 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 previous_query_keystrokes =
+ // cx.binding_for_action(&PreviousHistoryQuery {})
+ // .map(|binding| {
+ // binding
+ // .keystrokes()
+ // .iter()
+ // .map(|k| k.to_string())
+ // .collect::<Vec<_>>()
+ // });
+ // let next_query_keystrokes = cx.binding_for_action(&NextHistoryQuery {}).map(|binding| {
+ // binding
+ // .keystrokes()
+ // .iter()
+ // .map(|k| k.to_string())
+ // .collect::<Vec<_>>()
+ // });
+ // let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) {
+ // (Some(previous_query_keystrokes), Some(next_query_keystrokes)) => {
+ // format!(
+ // "Search ({}/{} for previous/next query)",
+ // previous_query_keystrokes.join(" "),
+ // next_query_keystrokes.join(" ")
+ // )
+ // }
+ // (None, Some(next_query_keystrokes)) => {
+ // format!(
+ // "Search ({} for next query)",
+ // next_query_keystrokes.join(" ")
+ // )
+ // }
+ // (Some(previous_query_keystrokes), None) => {
+ // format!(
+ // "Search ({} for previous query)",
+ // previous_query_keystrokes.join(" ")
+ // )
+ // }
+ // (None, None) => String::new(),
+ // };
+ let new_placeholder_text = Arc::from("Fix this up!");
+ self.query_editor.update(cx, |editor, cx| {
+ editor.set_placeholder_text(new_placeholder_text, cx);
+ });
+ self.replacement_editor.update(cx, |editor, cx| {
+ editor.set_placeholder_text("Replace with...", cx);
+ });
+ div()
+ .child(self.query_editor.clone())
+ .child(self.replacement_editor.clone())
+ // let search_button_for_mode = |mode, side, cx: &mut ViewContext<BufferSearchBar>| {
+ // let is_active = self.current_mode == mode;
+
+ // render_search_mode_button(
+ // mode,
+ // side,
+ // is_active,
+ // move |_, this, cx| {
+ // this.activate_search_mode(mode, cx);
+ // },
+ // cx,
+ // )
+ // };
+ // let search_option_button = |option| {
+ // let is_active = self.search_options.contains(option);
+ // option.as_button(is_active)
+ // };
+ // let match_count = self
+ // .active_searchable_item
+ // .as_ref()
+ // .and_then(|searchable_item| {
+ // if self.query(cx).is_empty() {
+ // return None;
+ // }
+ // let matches = self
+ // .searchable_items_with_matches
+ // .get(&searchable_item.downgrade())?;
+ // let message = if let Some(match_ix) = self.active_match_index {
+ // format!("{}/{}", match_ix + 1, matches.len())
+ // } else {
+ // "No matches".to_string()
+ // };
+
+ // Some(
+ // Label::new(message)
+ // .contained()
+ // .with_style(theme.search.match_index.container)
+ // .aligned(),
+ // )
+ // });
+ // let nav_button_for_direction = |label, direction, cx: &mut ViewContext<Self>| {
+ // render_nav_button(
+ // label,
+ // direction,
+ // self.active_match_index.is_some(),
+ // move |_, this, cx| match direction {
+ // Direction::Prev => this.select_prev_match(&Default::default(), cx),
+ // Direction::Next => this.select_next_match(&Default::default(), cx),
+ // },
+ // cx,
+ // )
+ // };
+ // let query_column = Flex::row()
+ // .with_child(
+ // Svg::for_style(theme.search.editor_icon.clone().icon)
+ // .contained()
+ // .with_style(theme.search.editor_icon.clone().container),
+ // )
+ // .with_child(ChildView::new(&self.query_editor, cx).flex(1., true))
+ // .with_child(
+ // Flex::row()
+ // .with_children(
+ // supported_options
+ // .case
+ // .then(|| search_option_button(SearchOptions::CASE_SENSITIVE)),
+ // )
+ // .with_children(
+ // supported_options
+ // .word
+ // .then(|| search_option_button(SearchOptions::WHOLE_WORD)),
+ // )
+ // .flex_float()
+ // .contained(),
+ // )
+ // .align_children_center()
+ // .contained()
+ // .with_style(query_container_style)
+ // .constrained()
+ // .with_min_width(theme.search.editor.min_width)
+ // .with_max_width(theme.search.editor.max_width)
+ // .with_height(theme.search.search_bar_row_height)
+ // .flex(1., false);
+ // let should_show_replace_input = self.replace_enabled && supported_options.replacement;
+
+ // let replacement = should_show_replace_input.then(|| {
+ // div()
+ // .child(
+ // Svg::for_style(theme.search.replace_icon.clone().icon)
+ // .contained()
+ // .with_style(theme.search.replace_icon.clone().container),
+ // )
+ // .child(self.replacement_editor)
+ // .align_children_center()
+ // .flex(1., true)
+ // .contained()
+ // .with_style(query_container_style)
+ // .constrained()
+ // .with_min_width(theme.search.editor.min_width)
+ // .with_max_width(theme.search.editor.max_width)
+ // .with_height(theme.search.search_bar_row_height)
+ // .flex(1., false)
+ // });
+ // let replace_all =
+ // should_show_replace_input.then(|| super::replace_action(ReplaceAll, "Replace all"));
+ // let replace_next =
+ // should_show_replace_input.then(|| super::replace_action(ReplaceNext, "Replace next"));
+ // let switches_column = supported_options.replacement.then(|| {
+ // Flex::row()
+ // .align_children_center()
+ // .with_child(super::toggle_replace_button(self.replace_enabled))
+ // .constrained()
+ // .with_height(theme.search.search_bar_row_height)
+ // .contained()
+ // .with_style(theme.search.option_button_group)
+ // });
+ // let mode_column = div()
+ // .child(search_button_for_mode(
+ // SearchMode::Text,
+ // Some(Side::Left),
+ // cx,
+ // ))
+ // .child(search_button_for_mode(
+ // SearchMode::Regex,
+ // Some(Side::Right),
+ // cx,
+ // ))
+ // .contained()
+ // .with_style(theme.search.modes_container)
+ // .constrained()
+ // .with_height(theme.search.search_bar_row_height);
+
+ // let nav_column = div()
+ // .align_children_center()
+ // .with_children(replace_next)
+ // .with_children(replace_all)
+ // .with_child(self.render_action_button("icons/select-all.svg", cx))
+ // .with_child(div().children(match_count))
+ // .with_child(nav_button_for_direction("<", Direction::Prev, cx))
+ // .with_child(nav_button_for_direction(">", Direction::Next, cx))
+ // .constrained()
+ // .with_height(theme.search.search_bar_row_height)
+ // .flex_float();
+
+ // div()
+ // .child(query_column)
+ // .child(mode_column)
+ // .children(switches_column)
+ // .children(replacement)
+ // .child(nav_column)
+ // .contained()
+ // .with_style(theme.search.container)
+ // .into_any_named("search bar")
+ }
+}
+
+impl ToolbarItemView for BufferSearchBar {
+ fn set_active_pane_item(
+ &mut self,
+ item: Option<&dyn ItemHandle>,
+ cx: &mut ViewContext<Self>,
+ ) -> ToolbarItemLocation {
+ cx.notify();
+ self.active_searchable_item_subscription.take();
+ self.active_searchable_item.take();
+ dbg!("Take?");
+ self.pending_search.take();
+
+ if let Some(searchable_item_handle) =
+ item.and_then(|item| item.to_searchable_item_handle(cx))
+ {
+ let this = cx.view().downgrade();
+ self.active_searchable_item_subscription =
+ Some(searchable_item_handle.subscribe_to_search_events(
+ cx,
+ Box::new(move |search_event, cx| {
+ if let Some(this) = this.upgrade() {
+ this.update(cx, |this, cx| {
+ this.on_active_searchable_item_event(search_event, cx)
+ });
+ }
+ }),
+ ));
+
+ self.active_searchable_item = Some(searchable_item_handle);
+ let _ = self.update_matches(cx);
+ if !self.dismissed {
+ return ToolbarItemLocation::Secondary;
+ }
+ }
+
+ ToolbarItemLocation::Hidden
+ }
+
+ fn row_count(&self, _: &WindowContext<'_>) -> usize {
+ 1
+ }
+}
+
+impl BufferSearchBar {
+ pub fn register(workspace: &mut Workspace) {
+ workspace.register_action(|workspace, a: &Deploy, cx| {
+ dbg!("Setting");
+ workspace.active_pane().update(cx, |this, cx| {
+ this.toolbar().update(cx, |this, cx| {
+ let view = cx.build_view(|cx| BufferSearchBar::new(cx));
+ this.add_item(view.clone(), cx);
+ view.update(cx, |this, cx| this.deploy(a, cx));
+ cx.notify();
+ })
+ });
+ });
+ }
+ pub fn new(cx: &mut ViewContext<Self>) -> Self {
+ dbg!("New");
+ let query_editor = cx.build_view(|cx| Editor::single_line(cx));
+ cx.subscribe(&query_editor, Self::on_query_editor_event)
+ .detach();
+ let replacement_editor = cx.build_view(|cx| Editor::single_line(cx));
+ cx.subscribe(&replacement_editor, Self::on_query_editor_event)
+ .detach();
+ Self {
+ query_editor,
+ replacement_editor,
+ active_searchable_item: None,
+ active_searchable_item_subscription: None,
+ active_match_index: None,
+ searchable_items_with_matches: Default::default(),
+ default_options: SearchOptions::NONE,
+ search_options: SearchOptions::NONE,
+ pending_search: None,
+ query_contains_error: false,
+ dismissed: true,
+ search_history: SearchHistory::default(),
+ current_mode: SearchMode::default(),
+ active_search: None,
+ replace_enabled: false,
+ }
+ }
+
+ pub fn is_dismissed(&self) -> bool {
+ self.dismissed
+ }
+
+ pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext<Self>) {
+ self.dismissed = true;
+ dbg!("Dismissed :(");
+ for searchable_item in self.searchable_items_with_matches.keys() {
+ if let Some(searchable_item) =
+ WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx)
+ {
+ searchable_item.clear_matches(cx);
+ }
+ }
+ if let Some(active_editor) = self.active_searchable_item.as_ref() {
+ let handle = active_editor.focus_handle(cx);
+ cx.focus(&handle);
+ }
+ cx.emit(Event::UpdateLocation);
+ cx.notify();
+ }
+
+ pub fn deploy(&mut self, deploy: &Deploy, cx: &mut ViewContext<Self>) -> bool {
+ if self.show(cx) {
+ self.search_suggested(cx);
+ if deploy.focus {
+ self.select_query(cx);
+ let handle = cx.focus_handle();
+ cx.focus(&handle);
+ }
+ return true;
+ }
+
+ false
+ }
+
+ pub fn show(&mut self, cx: &mut ViewContext<Self>) -> bool {
+ if self.active_searchable_item.is_none() {
+ dbg!("Hey");
+ return false;
+ }
+ dbg!("not dismissed");
+ self.dismissed = false;
+ cx.notify();
+ cx.emit(Event::UpdateLocation);
+ true
+ }
+
+ pub fn search_suggested(&mut self, cx: &mut ViewContext<Self>) {
+ let search = self
+ .query_suggestion(cx)
+ .map(|suggestion| self.search(&suggestion, Some(self.default_options), cx));
+
+ if let Some(search) = search {
+ cx.spawn(|this, mut cx| async move {
+ search.await?;
+ this.update(&mut cx, |this, cx| this.activate_current_match(cx))
+ })
+ .detach_and_log_err(cx);
+ }
+ }
+
+ pub fn activate_current_match(&mut self, cx: &mut ViewContext<Self>) {
+ if let Some(match_ix) = self.active_match_index {
+ if let Some(active_searchable_item) = self.active_searchable_item.as_ref() {
+ if let Some(matches) = self
+ .searchable_items_with_matches
+ .get(&active_searchable_item.downgrade())
+ {
+ active_searchable_item.activate_match(match_ix, matches, cx)
+ }
+ }
+ }
+ }
+
+ pub fn select_query(&mut self, cx: &mut ViewContext<Self>) {
+ self.query_editor.update(cx, |query_editor, cx| {
+ query_editor.select_all(&Default::default(), cx);
+ });
+ }
+
+ pub fn query(&self, cx: &WindowContext) -> String {
+ self.query_editor.read(cx).text(cx)
+ }
+ pub fn replacement(&self, cx: &WindowContext) -> String {
+ self.replacement_editor.read(cx).text(cx)
+ }
+ pub fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> Option<String> {
+ self.active_searchable_item
+ .as_ref()
+ .map(|searchable_item| searchable_item.query_suggestion(cx))
+ .filter(|suggestion| !suggestion.is_empty())
+ }
+
+ pub fn set_replacement(&mut self, replacement: Option<&str>, cx: &mut ViewContext<Self>) {
+ if replacement.is_none() {
+ self.replace_enabled = false;
+ return;
+ }
+ self.replace_enabled = true;
+ self.replacement_editor
+ .update(cx, |replacement_editor, cx| {
+ replacement_editor
+ .buffer()
+ .update(cx, |replacement_buffer, cx| {
+ let len = replacement_buffer.len(cx);
+ replacement_buffer.edit([(0..len, replacement.unwrap())], None, cx);
+ });
+ });
+ }
+
+ pub fn search(
+ &mut self,
+ query: &str,
+ options: Option<SearchOptions>,
+ cx: &mut ViewContext<Self>,
+ ) -> oneshot::Receiver<()> {
+ let options = options.unwrap_or(self.default_options);
+ if query != self.query(cx) || self.search_options != options {
+ self.query_editor.update(cx, |query_editor, cx| {
+ query_editor.buffer().update(cx, |query_buffer, cx| {
+ let len = query_buffer.len(cx);
+ query_buffer.edit([(0..len, query)], None, cx);
+ });
+ });
+ self.search_options = options;
+ self.query_contains_error = false;
+ self.clear_matches(cx);
+ cx.notify();
+ }
+ self.update_matches(cx)
+ }
+
+ fn render_action_button(
+ &self,
+ icon: &'static str,
+ cx: &mut ViewContext<Self>,
+ ) -> impl Component<Self> {
+ let tooltip = "Select All Matches";
+ let theme = cx.theme();
+ // let tooltip_style = theme.tooltip.clone();
+
+ // let style = theme.search.action_button.clone();
+
+ IconButton::new(0, ui::Icon::SelectAll)
+ .on_click(|_: &mut Self, cx| cx.dispatch_action(Box::new(SelectAllMatches)))
+ }
+
+ pub fn activate_search_mode(&mut self, mode: SearchMode, cx: &mut ViewContext<Self>) {
+ assert_ne!(
+ mode,
+ SearchMode::Semantic,
+ "Semantic search is not supported in buffer search"
+ );
+ if mode == self.current_mode {
+ return;
+ }
+ self.current_mode = mode;
+ let _ = self.update_matches(cx);
+ cx.notify();
+ }
+
+ fn deploy_bar(pane: &mut Pane, action: &Deploy, cx: &mut ViewContext<Pane>) {
+ let mut propagate_action = true;
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |search_bar, cx| {
+ if search_bar.deploy(action, cx) {
+ propagate_action = false;
+ }
+ });
+ }
+ if !propagate_action {
+ cx.stop_propagation();
+ }
+ }
+
+ fn handle_editor_cancel(pane: &mut Pane, _: &editor::Cancel, cx: &mut ViewContext<Pane>) {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ 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<Self>) {
+ if let Some(active_editor) = self.active_searchable_item.as_ref() {
+ let handle = active_editor.focus_handle(cx);
+ cx.focus(&handle);
+ }
+ }
+
+ fn toggle_search_option(&mut self, search_option: SearchOptions, cx: &mut ViewContext<Self>) {
+ self.search_options.toggle(search_option);
+ self.default_options = self.search_options;
+ let _ = self.update_matches(cx);
+ cx.notify();
+ }
+
+ pub fn set_search_options(
+ &mut self,
+ search_options: SearchOptions,
+ cx: &mut ViewContext<Self>,
+ ) {
+ self.search_options = search_options;
+ cx.notify();
+ }
+
+ fn select_next_match(&mut self, _: &SelectNextMatch, cx: &mut ViewContext<Self>) {
+ self.select_match(Direction::Next, 1, cx);
+ }
+
+ fn select_prev_match(&mut self, _: &SelectPrevMatch, cx: &mut ViewContext<Self>) {
+ self.select_match(Direction::Prev, 1, cx);
+ }
+
+ fn select_all_matches(&mut self, _: &SelectAllMatches, cx: &mut ViewContext<Self>) {
+ if !self.dismissed && self.active_match_index.is_some() {
+ if let Some(searchable_item) = self.active_searchable_item.as_ref() {
+ if let Some(matches) = self
+ .searchable_items_with_matches
+ .get(&searchable_item.downgrade())
+ {
+ searchable_item.select_matches(matches, cx);
+ self.focus_editor(&FocusEditor, cx);
+ }
+ }
+ }
+ }
+
+ pub fn select_match(&mut self, direction: Direction, count: usize, cx: &mut ViewContext<Self>) {
+ if let Some(index) = self.active_match_index {
+ if let Some(searchable_item) = self.active_searchable_item.as_ref() {
+ if let Some(matches) = self
+ .searchable_items_with_matches
+ .get(&searchable_item.downgrade())
+ {
+ let new_match_index = searchable_item
+ .match_index_for_direction(matches, index, direction, count, cx);
+ searchable_item.update_matches(matches, cx);
+ searchable_item.activate_match(new_match_index, matches, cx);
+ }
+ }
+ }
+ }
+
+ pub fn select_last_match(&mut self, cx: &mut ViewContext<Self>) {
+ if let Some(searchable_item) = self.active_searchable_item.as_ref() {
+ if let Some(matches) = self
+ .searchable_items_with_matches
+ .get(&searchable_item.downgrade())
+ {
+ if matches.len() == 0 {
+ return;
+ }
+ let new_match_index = matches.len() - 1;
+ searchable_item.update_matches(matches, cx);
+ searchable_item.activate_match(new_match_index, matches, cx);
+ }
+ }
+ }
+
+ fn select_next_match_on_pane(
+ pane: &mut Pane,
+ action: &SelectNextMatch,
+ cx: &mut ViewContext<Pane>,
+ ) {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |bar, cx| bar.select_next_match(action, cx));
+ }
+ }
+
+ fn select_prev_match_on_pane(
+ pane: &mut Pane,
+ action: &SelectPrevMatch,
+ cx: &mut ViewContext<Pane>,
+ ) {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |bar, cx| bar.select_prev_match(action, cx));
+ }
+ }
+
+ fn select_all_matches_on_pane(
+ pane: &mut Pane,
+ action: &SelectAllMatches,
+ cx: &mut ViewContext<Pane>,
+ ) {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |bar, cx| bar.select_all_matches(action, cx));
+ }
+ }
+
+ fn on_query_editor_event(
+ &mut self,
+ _: View<Editor>,
+ event: &editor::Event,
+ cx: &mut ViewContext<Self>,
+ ) {
+ if let editor::Event::Edited { .. } = event {
+ self.query_contains_error = false;
+ self.clear_matches(cx);
+ let search = self.update_matches(cx);
+ cx.spawn(|this, mut cx| async move {
+ search.await?;
+ this.update(&mut cx, |this, cx| this.activate_current_match(cx))
+ })
+ .detach_and_log_err(cx);
+ }
+ }
+
+ fn on_active_searchable_item_event(&mut self, event: &SearchEvent, cx: &mut ViewContext<Self>) {
+ match event {
+ SearchEvent::MatchesInvalidated => {
+ let _ = self.update_matches(cx);
+ }
+ SearchEvent::ActiveMatchChanged => self.update_match_index(cx),
+ }
+ }
+
+ fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
+ let mut active_item_matches = None;
+ for (searchable_item, matches) in self.searchable_items_with_matches.drain() {
+ if let Some(searchable_item) =
+ WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx)
+ {
+ if Some(&searchable_item) == self.active_searchable_item.as_ref() {
+ active_item_matches = Some((searchable_item.downgrade(), matches));
+ } else {
+ searchable_item.clear_matches(cx);
+ }
+ }
+ }
+
+ self.searchable_items_with_matches
+ .extend(active_item_matches);
+ }
+
+ fn update_matches(&mut self, cx: &mut ViewContext<Self>) -> oneshot::Receiver<()> {
+ let (done_tx, done_rx) = oneshot::channel();
+ let query = self.query(cx);
+ self.pending_search.take();
+ dbg!("update_matches");
+ if let Some(active_searchable_item) = self.active_searchable_item.as_ref() {
+ if query.is_empty() {
+ self.active_match_index.take();
+ active_searchable_item.clear_matches(cx);
+ let _ = done_tx.send(());
+ cx.notify();
+ } else {
+ let query: Arc<_> = if self.current_mode == SearchMode::Regex {
+ match SearchQuery::regex(
+ query,
+ self.search_options.contains(SearchOptions::WHOLE_WORD),
+ self.search_options.contains(SearchOptions::CASE_SENSITIVE),
+ Vec::new(),
+ Vec::new(),
+ ) {
+ Ok(query) => query.with_replacement(self.replacement(cx)),
+ Err(_) => {
+ self.query_contains_error = true;
+ cx.notify();
+ return done_rx;
+ }
+ }
+ } else {
+ match SearchQuery::text(
+ query,
+ self.search_options.contains(SearchOptions::WHOLE_WORD),
+ self.search_options.contains(SearchOptions::CASE_SENSITIVE),
+ Vec::new(),
+ Vec::new(),
+ ) {
+ Ok(query) => query.with_replacement(self.replacement(cx)),
+ Err(_) => {
+ self.query_contains_error = true;
+ cx.notify();
+ return done_rx;
+ }
+ }
+ }
+ .into();
+ self.active_search = Some(query.clone());
+ let query_text = query.as_str().to_string();
+ dbg!(&query_text);
+ let matches = active_searchable_item.find_matches(query, cx);
+
+ let active_searchable_item = active_searchable_item.downgrade();
+ self.pending_search = Some(cx.spawn(|this, mut cx| async move {
+ let matches = matches.await;
+ //dbg!(&matches);
+ this.update(&mut cx, |this, cx| {
+ dbg!("Updating!!");
+ if let Some(active_searchable_item) =
+ WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx)
+ {
+ dbg!("in if!!");
+ this.searchable_items_with_matches
+ .insert(active_searchable_item.downgrade(), matches);
+
+ this.update_match_index(cx);
+ this.search_history.add(query_text);
+ if !this.dismissed {
+ dbg!("Not dismissed");
+ let matches = this
+ .searchable_items_with_matches
+ .get(&active_searchable_item.downgrade())
+ .unwrap();
+ active_searchable_item.update_matches(matches, cx);
+ let _ = done_tx.send(());
+ }
+ cx.notify();
+ }
+ })
+ .log_err();
+ }));
+ }
+ }
+ done_rx
+ }
+
+ fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
+ let new_index = self
+ .active_searchable_item
+ .as_ref()
+ .and_then(|searchable_item| {
+ let matches = self
+ .searchable_items_with_matches
+ .get(&searchable_item.downgrade())?;
+ searchable_item.active_match_index(matches, cx)
+ });
+ if new_index != self.active_match_index {
+ self.active_match_index = new_index;
+ cx.notify();
+ }
+ }
+
+ fn next_history_query(&mut self, _: &NextHistoryQuery, cx: &mut ViewContext<Self>) {
+ if let Some(new_query) = self.search_history.next().map(str::to_string) {
+ let _ = self.search(&new_query, Some(self.search_options), cx);
+ } else {
+ self.search_history.reset_selection();
+ let _ = self.search("", Some(self.search_options), cx);
+ }
+ }
+
+ fn previous_history_query(&mut self, _: &PreviousHistoryQuery, cx: &mut ViewContext<Self>) {
+ if self.query(cx).is_empty() {
+ if let Some(new_query) = self.search_history.current().map(str::to_string) {
+ let _ = self.search(&new_query, Some(self.search_options), cx);
+ return;
+ }
+ }
+
+ if let Some(new_query) = self.search_history.previous().map(str::to_string) {
+ let _ = self.search(&new_query, Some(self.search_options), cx);
+ }
+ }
+ fn cycle_mode(&mut self, _: &CycleMode, cx: &mut ViewContext<Self>) {
+ self.activate_search_mode(next_mode(&self.current_mode, false), cx);
+ }
+ fn cycle_mode_on_pane(pane: &mut Pane, action: &CycleMode, cx: &mut ViewContext<Pane>) {
+ let mut should_propagate = true;
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |bar, cx| {
+ if bar.show(cx) {
+ should_propagate = false;
+ bar.cycle_mode(action, cx);
+ false
+ } else {
+ true
+ }
+ });
+ }
+ if !should_propagate {
+ cx.stop_propagation();
+ }
+ }
+ fn toggle_replace(&mut self, _: &ToggleReplace, cx: &mut ViewContext<Self>) {
+ if let Some(_) = &self.active_searchable_item {
+ self.replace_enabled = !self.replace_enabled;
+ if !self.replace_enabled {
+ let handle = self.query_editor.focus_handle(cx);
+ cx.focus(&handle);
+ }
+ cx.notify();
+ }
+ }
+ fn toggle_replace_on_a_pane(pane: &mut Pane, _: &ToggleReplace, cx: &mut ViewContext<Pane>) {
+ let mut should_propagate = true;
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ 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<Self>) {
+ let mut should_propagate = true;
+ if !self.dismissed && self.active_search.is_some() {
+ if let Some(searchable_item) = self.active_searchable_item.as_ref() {
+ if let Some(query) = self.active_search.as_ref() {
+ if let Some(matches) = self
+ .searchable_items_with_matches
+ .get(&searchable_item.downgrade())
+ {
+ if let Some(active_index) = self.active_match_index {
+ let query = query
+ .as_ref()
+ .clone()
+ .with_replacement(self.replacement(cx));
+ searchable_item.replace(&matches[active_index], &query, cx);
+ self.select_next_match(&SelectNextMatch, cx);
+ }
+ should_propagate = false;
+ self.focus_editor(&FocusEditor, cx);
+ }
+ }
+ }
+ }
+ if !should_propagate {
+ cx.stop_propagation();
+ }
+ }
+ pub fn replace_all(&mut self, _: &ReplaceAll, cx: &mut ViewContext<Self>) {
+ if !self.dismissed && self.active_search.is_some() {
+ if let Some(searchable_item) = self.active_searchable_item.as_ref() {
+ if let Some(query) = self.active_search.as_ref() {
+ if let Some(matches) = self
+ .searchable_items_with_matches
+ .get(&searchable_item.downgrade())
+ {
+ let query = query
+ .as_ref()
+ .clone()
+ .with_replacement(self.replacement(cx));
+ for m in matches {
+ searchable_item.replace(m, &query, cx);
+ }
+ }
+ }
+ }
+ }
+ }
+ fn replace_next_on_pane(pane: &mut Pane, action: &ReplaceNext, cx: &mut ViewContext<Pane>) {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |bar, cx| bar.replace_next(action, cx));
+ cx.stop_propagation();
+ return;
+ }
+ }
+ fn replace_all_on_pane(pane: &mut Pane, action: &ReplaceAll, cx: &mut ViewContext<Pane>) {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
+ search_bar.update(cx, |bar, cx| bar.replace_all(action, cx));
+ cx.stop_propagation();
+ return;
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use editor::{DisplayPoint, Editor};
+ use gpui::{color::Color, test::EmptyView, TestAppContext};
+ use language::Buffer;
+ use unindent::Unindent as _;
+
+ fn init_test(cx: &mut TestAppContext) -> (ViewHandle<Editor>, ViewHandle<BufferSearchBar>) {
+ crate::project_search::tests::init_test(cx);
+
+ let buffer = cx.add_model(|cx| {
+ Buffer::new(
+ 0,
+ cx.model_id() as u64,
+ r#"
+ A regular expression (shortened as regex or regexp;[1] also referred to as
+ rational expression[2][3]) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching algorithms
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent(),
+ )
+ });
+ let window = cx.add_window(|_| EmptyView);
+ let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx));
+
+ let search_bar = window.add_view(cx, |cx| {
+ let mut search_bar = BufferSearchBar::new(cx);
+ search_bar.set_active_pane_item(Some(&editor), cx);
+ search_bar.show(cx);
+ search_bar
+ });
+
+ (editor, search_bar)
+ }
+
+ #[gpui::test]
+ async fn test_search_simple(cx: &mut TestAppContext) {
+ let (editor, search_bar) = init_test(cx);
+
+ // Search for a string that appears with different casing.
+ // By default, search is case-insensitive.
+ search_bar
+ .update(cx, |search_bar, cx| search_bar.search("us", None, cx))
+ .await
+ .unwrap();
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_text_background_highlights(cx),
+ &[
+ (
+ DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
+ Color::red(),
+ ),
+ ]
+ );
+ });
+
+ // Switch to a case sensitive search.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_text_background_highlights(cx),
+ &[(
+ DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
+ Color::red(),
+ )]
+ );
+ });
+
+ // Search for a string that appears both as a whole word and
+ // within other words. By default, all results are found.
+ search_bar
+ .update(cx, |search_bar, cx| search_bar.search("or", None, cx))
+ .await
+ .unwrap();
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_text_background_highlights(cx),
+ &[
+ (
+ DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62),
+ Color::red(),
+ ),
+ ]
+ );
+ });
+
+ // Switch to a whole word search.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx);
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_text_background_highlights(cx),
+ &[
+ (
+ DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
+ Color::red(),
+ ),
+ ]
+ );
+ });
+
+ editor.update(cx, |editor, cx| {
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)])
+ });
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(1));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(2));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(2));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(1));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ });
+
+ // Park the cursor in between matches and ensure that going to the previous match selects
+ // the closest match to the left.
+ editor.update(cx, |editor, cx| {
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)])
+ });
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ assert_eq!(search_bar.active_match_index, Some(1));
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ });
+
+ // Park the cursor in between matches and ensure that going to the next match selects the
+ // closest match to the right.
+ editor.update(cx, |editor, cx| {
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)])
+ });
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ assert_eq!(search_bar.active_match_index, Some(1));
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(1));
+ });
+
+ // Park the cursor after the last match and ensure that going to the previous match selects
+ // the last match.
+ editor.update(cx, |editor, cx| {
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)])
+ });
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ assert_eq!(search_bar.active_match_index, Some(2));
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(2));
+ });
+
+ // Park the cursor after the last match and ensure that going to the next match selects the
+ // first match.
+ editor.update(cx, |editor, cx| {
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)])
+ });
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ assert_eq!(search_bar.active_match_index, Some(2));
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ });
+
+ // Park the cursor before the first match and ensure that going to the previous match
+ // selects the last match.
+ editor.update(cx, |editor, cx| {
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)])
+ });
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ assert_eq!(
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
+ );
+ });
+ search_bar.read_with(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(2));
+ });
+ }
+
+ #[gpui::test]
+ async fn test_search_option_handling(cx: &mut TestAppContext) {
+ let (editor, search_bar) = init_test(cx);
+
+ // show with options should make current search case sensitive
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.show(cx);
+ search_bar.search("us", Some(SearchOptions::CASE_SENSITIVE), cx)
+ })
+ .await
+ .unwrap();
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_text_background_highlights(cx),
+ &[(
+ DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
+ Color::red(),
+ )]
+ );
+ });
+
+ // search_suggested should restore default options
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.search_suggested(cx);
+ assert_eq!(search_bar.search_options, SearchOptions::NONE)
+ });
+
+ // toggling a search option should update the defaults
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.search("regex", Some(SearchOptions::CASE_SENSITIVE), cx)
+ })
+ .await
+ .unwrap();
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx)
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_text_background_highlights(cx),
+ &[(
+ DisplayPoint::new(0, 35)..DisplayPoint::new(0, 40),
+ Color::red(),
+ ),]
+ );
+ });
+
+ // defaults should still include whole word
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.search_suggested(cx);
+ assert_eq!(
+ search_bar.search_options,
+ SearchOptions::CASE_SENSITIVE | SearchOptions::WHOLE_WORD
+ )
+ });
+ }
+
+ #[gpui::test]
+ async fn test_search_select_all_matches(cx: &mut TestAppContext) {
+ crate::project_search::tests::init_test(cx);
+
+ let buffer_text = r#"
+ A regular expression (shortened as regex or regexp;[1] also referred to as
+ rational expression[2][3]) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching algorithms
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent();
+ let expected_query_matches_count = buffer_text
+ .chars()
+ .filter(|c| c.to_ascii_lowercase() == 'a')
+ .count();
+ assert!(
+ expected_query_matches_count > 1,
+ "Should pick a query with multiple results"
+ );
+ let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, buffer_text));
+ let window = cx.add_window(|_| EmptyView);
+ let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx));
+
+ let search_bar = window.add_view(cx, |cx| {
+ let mut search_bar = BufferSearchBar::new(cx);
+ search_bar.set_active_pane_item(Some(&editor), cx);
+ search_bar.show(cx);
+ search_bar
+ });
+
+ search_bar
+ .update(cx, |search_bar, cx| search_bar.search("a", None, cx))
+ .await
+ .unwrap();
+ search_bar.update(cx, |search_bar, cx| {
+ cx.focus(search_bar.query_editor.as_any());
+ search_bar.activate_current_match(cx);
+ });
+
+ window.read_with(cx, |cx| {
+ assert!(
+ !editor.is_focused(cx),
+ "Initially, the editor should not be focused"
+ );
+ });
+
+ let initial_selections = editor.update(cx, |editor, cx| {
+ let initial_selections = editor.selections.display_ranges(cx);
+ assert_eq!(
+ initial_selections.len(), 1,
+ "Expected to have only one selection before adding carets to all matches, but got: {initial_selections:?}",
+ );
+ initial_selections
+ });
+ search_bar.update(cx, |search_bar, _| {
+ assert_eq!(search_bar.active_match_index, Some(0));
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ cx.focus(search_bar.query_editor.as_any());
+ search_bar.select_all_matches(&SelectAllMatches, cx);
+ });
+ window.read_with(cx, |cx| {
+ assert!(
+ editor.is_focused(cx),
+ "Should focus editor after successful SelectAllMatches"
+ );
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ let all_selections =
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
+ assert_eq!(
+ all_selections.len(),
+ expected_query_matches_count,
+ "Should select all `a` characters in the buffer, but got: {all_selections:?}"
+ );
+ assert_eq!(
+ search_bar.active_match_index,
+ Some(0),
+ "Match index should not change after selecting all matches"
+ );
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_next_match(&SelectNextMatch, cx);
+ });
+ window.read_with(cx, |cx| {
+ assert!(
+ editor.is_focused(cx),
+ "Should still have editor focused after SelectNextMatch"
+ );
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ let all_selections =
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
+ assert_eq!(
+ all_selections.len(),
+ 1,
+ "On next match, should deselect items and select the next match"
+ );
+ assert_ne!(
+ all_selections, initial_selections,
+ "Next match should be different from the first selection"
+ );
+ assert_eq!(
+ search_bar.active_match_index,
+ Some(1),
+ "Match index should be updated to the next one"
+ );
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ cx.focus(search_bar.query_editor.as_any());
+ search_bar.select_all_matches(&SelectAllMatches, cx);
+ });
+ window.read_with(cx, |cx| {
+ assert!(
+ editor.is_focused(cx),
+ "Should focus editor after successful SelectAllMatches"
+ );
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ let all_selections =
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
+ assert_eq!(
+ all_selections.len(),
+ expected_query_matches_count,
+ "Should select all `a` characters in the buffer, but got: {all_selections:?}"
+ );
+ assert_eq!(
+ search_bar.active_match_index,
+ Some(1),
+ "Match index should not change after selecting all matches"
+ );
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_prev_match(&SelectPrevMatch, cx);
+ });
+ window.read_with(cx, |cx| {
+ assert!(
+ editor.is_focused(cx),
+ "Should still have editor focused after SelectPrevMatch"
+ );
+ });
+ let last_match_selections = search_bar.update(cx, |search_bar, cx| {
+ let all_selections =
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
+ assert_eq!(
+ all_selections.len(),
+ 1,
+ "On previous match, should deselect items and select the previous item"
+ );
+ assert_eq!(
+ all_selections, initial_selections,
+ "Previous match should be the same as the first selection"
+ );
+ assert_eq!(
+ search_bar.active_match_index,
+ Some(0),
+ "Match index should be updated to the previous one"
+ );
+ all_selections
+ });
+
+ search_bar
+ .update(cx, |search_bar, cx| {
+ cx.focus(search_bar.query_editor.as_any());
+ search_bar.search("abas_nonexistent_match", None, cx)
+ })
+ .await
+ .unwrap();
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.select_all_matches(&SelectAllMatches, cx);
+ });
+ window.read_with(cx, |cx| {
+ assert!(
+ !editor.is_focused(cx),
+ "Should not switch focus to editor if SelectAllMatches does not find any matches"
+ );
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ let all_selections =
+ editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
+ assert_eq!(
+ all_selections, last_match_selections,
+ "Should not select anything new if there are no matches"
+ );
+ assert!(
+ search_bar.active_match_index.is_none(),
+ "For no matches, there should be no active match index"
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_search_query_history(cx: &mut TestAppContext) {
+ crate::project_search::tests::init_test(cx);
+
+ let buffer_text = r#"
+ A regular expression (shortened as regex or regexp;[1] also referred to as
+ rational expression[2][3]) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching algorithms
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent();
+ let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, buffer_text));
+ let window = cx.add_window(|_| EmptyView);
+
+ let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx));
+
+ let search_bar = window.add_view(cx, |cx| {
+ let mut search_bar = BufferSearchBar::new(cx);
+ search_bar.set_active_pane_item(Some(&editor), cx);
+ search_bar.show(cx);
+ search_bar
+ });
+
+ // Add 3 search items into the history.
+ search_bar
+ .update(cx, |search_bar, cx| search_bar.search("a", None, cx))
+ .await
+ .unwrap();
+ search_bar
+ .update(cx, |search_bar, cx| search_bar.search("b", None, cx))
+ .await
+ .unwrap();
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.search("c", Some(SearchOptions::CASE_SENSITIVE), cx)
+ })
+ .await
+ .unwrap();
+ // Ensure that the latest search is active.
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "c");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Next history query after the latest should set the query to the empty string.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // First previous query for empty current query should set the query to the latest.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "c");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Further previous items should go over the history in reverse order.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "b");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Previous items should never go behind the first history item.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "a");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "a");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Next items should go over the history in the original order.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "b");
+ assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ search_bar
+ .update(cx, |search_bar, cx| search_bar.search("ba", None, cx))
+ .await
+ .unwrap();
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "ba");
+ assert_eq!(search_bar.search_options, SearchOptions::NONE);
+ });
+
+ // New search input should add another entry to history and move the selection to the end of the history.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "c");
+ assert_eq!(search_bar.search_options, SearchOptions::NONE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "b");
+ assert_eq!(search_bar.search_options, SearchOptions::NONE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "c");
+ assert_eq!(search_bar.search_options, SearchOptions::NONE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "ba");
+ assert_eq!(search_bar.search_options, SearchOptions::NONE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_bar.read_with(cx, |search_bar, cx| {
+ assert_eq!(search_bar.query(cx), "");
+ assert_eq!(search_bar.search_options, SearchOptions::NONE);
+ });
+ }
+ #[gpui::test]
+ async fn test_replace_simple(cx: &mut TestAppContext) {
+ let (editor, search_bar) = init_test(cx);
+
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.search("expression", None, cx)
+ })
+ .await
+ .unwrap();
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.replacement_editor.update(cx, |editor, cx| {
+ // We use $1 here as initially we should be in Text mode, where `$1` should be treated literally.
+ editor.set_text("expr$1", cx);
+ });
+ search_bar.replace_all(&ReplaceAll, cx)
+ });
+ assert_eq!(
+ editor.read_with(cx, |this, cx| { this.text(cx) }),
+ r#"
+ A regular expr$1 (shortened as regex or regexp;[1] also referred to as
+ rational expr$1[2][3]) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching algorithms
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent()
+ );
+
+ // Search for word boundaries and replace just a single one.
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.search("or", Some(SearchOptions::WHOLE_WORD), cx)
+ })
+ .await
+ .unwrap();
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.replacement_editor.update(cx, |editor, cx| {
+ editor.set_text("banana", cx);
+ });
+ search_bar.replace_next(&ReplaceNext, cx)
+ });
+ // Notice how the first or in the text (shORtened) is not replaced. Neither are the remaining hits of `or` in the text.
+ assert_eq!(
+ editor.read_with(cx, |this, cx| { this.text(cx) }),
+ r#"
+ A regular expr$1 (shortened as regex banana regexp;[1] also referred to as
+ rational expr$1[2][3]) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching algorithms
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent()
+ );
+ // Let's turn on regex mode.
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.activate_search_mode(SearchMode::Regex, cx);
+ search_bar.search("\\[([^\\]]+)\\]", None, cx)
+ })
+ .await
+ .unwrap();
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.replacement_editor.update(cx, |editor, cx| {
+ editor.set_text("${1}number", cx);
+ });
+ search_bar.replace_all(&ReplaceAll, cx)
+ });
+ assert_eq!(
+ editor.read_with(cx, |this, cx| { this.text(cx) }),
+ r#"
+ A regular expr$1 (shortened as regex banana regexp;1number also referred to as
+ rational expr$12number3number) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching algorithms
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent()
+ );
+ // Now with a whole-word twist.
+ search_bar
+ .update(cx, |search_bar, cx| {
+ search_bar.activate_search_mode(SearchMode::Regex, cx);
+ search_bar.search("a\\w+s", Some(SearchOptions::WHOLE_WORD), cx)
+ })
+ .await
+ .unwrap();
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.replacement_editor.update(cx, |editor, cx| {
+ editor.set_text("things", cx);
+ });
+ search_bar.replace_all(&ReplaceAll, cx)
+ });
+ // The only word affected by this edit should be `algorithms`, even though there's a bunch
+ // of words in this text that would match this regex if not for WHOLE_WORD.
+ assert_eq!(
+ editor.read_with(cx, |this, cx| { this.text(cx) }),
+ r#"
+ A regular expr$1 (shortened as regex banana regexp;1number also referred to as
+ rational expr$12number3number) is a sequence of characters that specifies a search
+ pattern in text. Usually such patterns are used by string-searching things
+ for "find" or "find and replace" operations on strings, or for input validation.
+ "#
+ .unindent()
+ );
+ }
+}
@@ -0,0 +1,184 @@
+use smallvec::SmallVec;
+const SEARCH_HISTORY_LIMIT: usize = 20;
+
+#[derive(Default, Debug, Clone)]
+pub struct SearchHistory {
+ history: SmallVec<[String; SEARCH_HISTORY_LIMIT]>,
+ selected: Option<usize>,
+}
+
+impl SearchHistory {
+ pub fn add(&mut self, search_string: String) {
+ if let Some(i) = self.selected {
+ if search_string == self.history[i] {
+ return;
+ }
+ }
+
+ if let Some(previously_searched) = self.history.last_mut() {
+ if search_string.find(previously_searched.as_str()).is_some() {
+ *previously_searched = search_string;
+ self.selected = Some(self.history.len() - 1);
+ return;
+ }
+ }
+
+ self.history.push(search_string);
+ if self.history.len() > SEARCH_HISTORY_LIMIT {
+ self.history.remove(0);
+ }
+ self.selected = Some(self.history.len() - 1);
+ }
+
+ pub fn next(&mut self) -> Option<&str> {
+ let history_size = self.history.len();
+ if history_size == 0 {
+ return None;
+ }
+
+ let selected = self.selected?;
+ if selected == history_size - 1 {
+ return None;
+ }
+ let next_index = selected + 1;
+ self.selected = Some(next_index);
+ Some(&self.history[next_index])
+ }
+
+ pub fn current(&self) -> Option<&str> {
+ Some(&self.history[self.selected?])
+ }
+
+ pub fn previous(&mut self) -> Option<&str> {
+ let history_size = self.history.len();
+ if history_size == 0 {
+ return None;
+ }
+
+ let prev_index = match self.selected {
+ Some(selected_index) => {
+ if selected_index == 0 {
+ return None;
+ } else {
+ selected_index - 1
+ }
+ }
+ None => history_size - 1,
+ };
+
+ self.selected = Some(prev_index);
+ Some(&self.history[prev_index])
+ }
+
+ pub fn reset_selection(&mut self) {
+ self.selected = None;
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_add() {
+ let mut search_history = SearchHistory::default();
+ assert_eq!(
+ search_history.current(),
+ None,
+ "No current selection should be set fo the default search history"
+ );
+
+ search_history.add("rust".to_string());
+ assert_eq!(
+ search_history.current(),
+ Some("rust"),
+ "Newly added item should be selected"
+ );
+
+ // check if duplicates are not added
+ search_history.add("rust".to_string());
+ assert_eq!(
+ search_history.history.len(),
+ 1,
+ "Should not add a duplicate"
+ );
+ assert_eq!(search_history.current(), Some("rust"));
+
+ // check if new string containing the previous string replaces it
+ search_history.add("rustlang".to_string());
+ assert_eq!(
+ search_history.history.len(),
+ 1,
+ "Should replace previous item if it's a substring"
+ );
+ assert_eq!(search_history.current(), Some("rustlang"));
+
+ // push enough items to test SEARCH_HISTORY_LIMIT
+ for i in 0..SEARCH_HISTORY_LIMIT * 2 {
+ search_history.add(format!("item{i}"));
+ }
+ assert!(search_history.history.len() <= SEARCH_HISTORY_LIMIT);
+ }
+
+ #[test]
+ fn test_next_and_previous() {
+ let mut search_history = SearchHistory::default();
+ assert_eq!(
+ search_history.next(),
+ None,
+ "Default search history should not have a next item"
+ );
+
+ search_history.add("Rust".to_string());
+ assert_eq!(search_history.next(), None);
+ search_history.add("JavaScript".to_string());
+ assert_eq!(search_history.next(), None);
+ search_history.add("TypeScript".to_string());
+ assert_eq!(search_history.next(), None);
+
+ assert_eq!(search_history.current(), Some("TypeScript"));
+
+ assert_eq!(search_history.previous(), Some("JavaScript"));
+ assert_eq!(search_history.current(), Some("JavaScript"));
+
+ assert_eq!(search_history.previous(), Some("Rust"));
+ assert_eq!(search_history.current(), Some("Rust"));
+
+ assert_eq!(search_history.previous(), None);
+ assert_eq!(search_history.current(), Some("Rust"));
+
+ assert_eq!(search_history.next(), Some("JavaScript"));
+ assert_eq!(search_history.current(), Some("JavaScript"));
+
+ assert_eq!(search_history.next(), Some("TypeScript"));
+ assert_eq!(search_history.current(), Some("TypeScript"));
+
+ assert_eq!(search_history.next(), None);
+ assert_eq!(search_history.current(), Some("TypeScript"));
+ }
+
+ #[test]
+ fn test_reset_selection() {
+ let mut search_history = SearchHistory::default();
+ search_history.add("Rust".to_string());
+ search_history.add("JavaScript".to_string());
+ search_history.add("TypeScript".to_string());
+
+ assert_eq!(search_history.current(), Some("TypeScript"));
+ search_history.reset_selection();
+ assert_eq!(search_history.current(), None);
+ assert_eq!(
+ search_history.previous(),
+ Some("TypeScript"),
+ "Should start from the end after reset on previous item query"
+ );
+
+ search_history.previous();
+ assert_eq!(search_history.current(), Some("JavaScript"));
+ search_history.previous();
+ assert_eq!(search_history.current(), Some("Rust"));
+
+ search_history.reset_selection();
+ assert_eq!(search_history.current(), None);
+ }
+}
@@ -0,0 +1,65 @@
+use gpui::Action;
+
+use crate::{ActivateRegexMode, ActivateSemanticMode, ActivateTextMode};
+// TODO: Update the default search mode to get from config
+#[derive(Copy, Clone, Debug, Default, PartialEq)]
+pub enum SearchMode {
+ #[default]
+ Text,
+ Semantic,
+ Regex,
+}
+
+#[derive(Copy, Clone, Debug, PartialEq)]
+pub(crate) enum Side {
+ Left,
+ Right,
+}
+
+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<dyn Action> {
+ match self {
+ SearchMode::Text => Box::new(ActivateTextMode),
+ SearchMode::Semantic => Box::new(ActivateSemanticMode),
+ SearchMode::Regex => Box::new(ActivateRegexMode),
+ }
+ }
+}
+
+pub(crate) fn next_mode(mode: &SearchMode, semantic_enabled: bool) -> SearchMode {
+ match mode {
+ SearchMode::Text => SearchMode::Regex,
+ SearchMode::Regex => {
+ if semantic_enabled {
+ SearchMode::Semantic
+ } else {
+ SearchMode::Text
+ }
+ }
+ SearchMode::Semantic => SearchMode::Text,
+ }
+}
@@ -0,0 +1,2661 @@
+use crate::{
+ history::SearchHistory,
+ mode::{SearchMode, Side},
+ search_bar::{render_nav_button, render_option_button_icon, render_search_mode_button},
+ ActivateRegexMode, ActivateSemanticMode, ActivateTextMode, CycleMode, NextHistoryQuery,
+ PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOptions, SelectNextMatch, SelectPrevMatch,
+ ToggleCaseSensitive, ToggleReplace, ToggleWholeWord,
+};
+use anyhow::{Context, Result};
+use collections::HashMap;
+use editor::{
+ items::active_match_index, scroll::autoscroll::Autoscroll, Anchor, Editor, MultiBuffer,
+ SelectAll, MAX_TAB_TITLE_LEN,
+};
+use futures::StreamExt;
+use gpui::{
+ actions,
+ elements::*,
+ platform::{MouseButton, PromptLevel},
+ Action, AnyElement, AnyViewHandle, AppContext, Entity, ModelContext, ModelHandle, Subscription,
+ Task, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
+};
+use menu::Confirm;
+use project::{
+ search::{SearchInputs, SearchQuery},
+ Entry, Project,
+};
+use semantic_index::{SemanticIndex, SemanticIndexStatus};
+use smallvec::SmallVec;
+use std::{
+ any::{Any, TypeId},
+ borrow::Cow,
+ collections::HashSet,
+ mem,
+ ops::{Not, Range},
+ path::PathBuf,
+ sync::Arc,
+ time::{Duration, Instant},
+};
+use util::{paths::PathMatcher, ResultExt as _};
+use workspace::{
+ item::{BreadcrumbText, Item, ItemEvent, ItemHandle},
+ searchable::{Direction, SearchableItem, SearchableItemHandle},
+ ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceId,
+};
+
+actions!(
+ project_search,
+ [SearchInNew, ToggleFocus, NextField, ToggleFilters,]
+);
+
+#[derive(Default)]
+struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
+
+#[derive(Default)]
+struct ActiveSettings(HashMap<WeakModelHandle<Project>, ProjectSearchSettings>);
+
+pub fn init(cx: &mut AppContext) {
+ cx.set_global(ActiveSearches::default());
+ cx.set_global(ActiveSettings::default());
+ cx.add_action(ProjectSearchView::deploy);
+ cx.add_action(ProjectSearchView::move_focus_to_results);
+ cx.add_action(ProjectSearchBar::confirm);
+ cx.add_action(ProjectSearchBar::search_in_new);
+ cx.add_action(ProjectSearchBar::select_next_match);
+ cx.add_action(ProjectSearchBar::select_prev_match);
+ cx.add_action(ProjectSearchBar::replace_next);
+ cx.add_action(ProjectSearchBar::replace_all);
+ cx.add_action(ProjectSearchBar::cycle_mode);
+ cx.add_action(ProjectSearchBar::next_history_query);
+ cx.add_action(ProjectSearchBar::previous_history_query);
+ cx.add_action(ProjectSearchBar::activate_regex_mode);
+ cx.add_action(ProjectSearchBar::toggle_replace);
+ cx.add_action(ProjectSearchBar::toggle_replace_on_a_pane);
+ cx.add_action(ProjectSearchBar::activate_text_mode);
+
+ // This action should only be registered if the semantic index is enabled
+ // We are registering it all the time, as I dont want to introduce a dependency
+ // for Semantic Index Settings globally whenever search is tested.
+ cx.add_action(ProjectSearchBar::activate_semantic_mode);
+
+ cx.capture_action(ProjectSearchBar::tab);
+ cx.capture_action(ProjectSearchBar::tab_previous);
+ cx.capture_action(ProjectSearchView::replace_all);
+ cx.capture_action(ProjectSearchView::replace_next);
+ add_toggle_option_action::<ToggleCaseSensitive>(SearchOptions::CASE_SENSITIVE, cx);
+ add_toggle_option_action::<ToggleWholeWord>(SearchOptions::WHOLE_WORD, cx);
+ add_toggle_filters_action::<ToggleFilters>(cx);
+}
+
+fn add_toggle_filters_action<A: Action>(cx: &mut AppContext) {
+ cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
+ if search_bar.update(cx, |search_bar, cx| search_bar.toggle_filters(cx)) {
+ return;
+ }
+ }
+ cx.propagate_action();
+ });
+}
+
+fn add_toggle_option_action<A: Action>(option: SearchOptions, cx: &mut AppContext) {
+ cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
+ if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
+ if search_bar.update(cx, |search_bar, cx| {
+ search_bar.toggle_search_option(option, cx)
+ }) {
+ return;
+ }
+ }
+ cx.propagate_action();
+ });
+}
+
+struct ProjectSearch {
+ project: ModelHandle<Project>,
+ excerpts: ModelHandle<MultiBuffer>,
+ pending_search: Option<Task<Option<()>>>,
+ match_ranges: Vec<Range<Anchor>>,
+ active_query: Option<SearchQuery>,
+ search_id: usize,
+ search_history: SearchHistory,
+ no_results: Option<bool>,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+enum InputPanel {
+ Query,
+ Exclude,
+ Include,
+}
+
+pub struct ProjectSearchView {
+ model: ModelHandle<ProjectSearch>,
+ query_editor: ViewHandle<Editor>,
+ replacement_editor: ViewHandle<Editor>,
+ results_editor: ViewHandle<Editor>,
+ semantic_state: Option<SemanticState>,
+ semantic_permissioned: Option<bool>,
+ search_options: SearchOptions,
+ panels_with_errors: HashSet<InputPanel>,
+ active_match_index: Option<usize>,
+ search_id: usize,
+ query_editor_was_focused: bool,
+ included_files_editor: ViewHandle<Editor>,
+ excluded_files_editor: ViewHandle<Editor>,
+ filters_enabled: bool,
+ replace_enabled: bool,
+ current_mode: SearchMode,
+}
+
+struct SemanticState {
+ index_status: SemanticIndexStatus,
+ maintain_rate_limit: Option<Task<()>>,
+ _subscription: Subscription,
+}
+
+#[derive(Debug, Clone)]
+struct ProjectSearchSettings {
+ search_options: SearchOptions,
+ filters_enabled: bool,
+ current_mode: SearchMode,
+}
+
+pub struct ProjectSearchBar {
+ active_project_search: Option<ViewHandle<ProjectSearchView>>,
+ subscription: Option<Subscription>,
+}
+
+impl Entity for ProjectSearch {
+ type Event = ();
+}
+
+impl ProjectSearch {
+ fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
+ let replica_id = project.read(cx).replica_id();
+ Self {
+ project,
+ excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
+ pending_search: Default::default(),
+ match_ranges: Default::default(),
+ active_query: None,
+ search_id: 0,
+ search_history: SearchHistory::default(),
+ no_results: None,
+ }
+ }
+
+ fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
+ cx.add_model(|cx| Self {
+ project: self.project.clone(),
+ excerpts: self
+ .excerpts
+ .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
+ pending_search: Default::default(),
+ match_ranges: self.match_ranges.clone(),
+ active_query: self.active_query.clone(),
+ search_id: self.search_id,
+ search_history: self.search_history.clone(),
+ no_results: self.no_results.clone(),
+ })
+ }
+
+ fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
+ let search = self
+ .project
+ .update(cx, |project, cx| project.search(query.clone(), cx));
+ self.search_id += 1;
+ self.search_history.add(query.as_str().to_string());
+ self.active_query = Some(query);
+ self.match_ranges.clear();
+ self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
+ let mut matches = search;
+ let this = this.upgrade(&cx)?;
+ this.update(&mut cx, |this, cx| {
+ this.match_ranges.clear();
+ this.excerpts.update(cx, |this, cx| this.clear(cx));
+ this.no_results = Some(true);
+ });
+
+ while let Some((buffer, anchors)) = matches.next().await {
+ let mut ranges = this.update(&mut cx, |this, cx| {
+ this.no_results = Some(false);
+ this.excerpts.update(cx, |excerpts, cx| {
+ excerpts.stream_excerpts_with_context_lines(buffer, anchors, 1, cx)
+ })
+ });
+
+ while let Some(range) = ranges.next().await {
+ this.update(&mut cx, |this, _| this.match_ranges.push(range));
+ }
+ this.update(&mut cx, |_, cx| cx.notify());
+ }
+
+ this.update(&mut cx, |this, cx| {
+ this.pending_search.take();
+ cx.notify();
+ });
+
+ None
+ }));
+ cx.notify();
+ }
+
+ fn semantic_search(&mut self, inputs: &SearchInputs, cx: &mut ModelContext<Self>) {
+ let search = SemanticIndex::global(cx).map(|index| {
+ index.update(cx, |semantic_index, cx| {
+ semantic_index.search_project(
+ self.project.clone(),
+ inputs.as_str().to_owned(),
+ 10,
+ inputs.files_to_include().to_vec(),
+ inputs.files_to_exclude().to_vec(),
+ cx,
+ )
+ })
+ });
+ self.search_id += 1;
+ self.match_ranges.clear();
+ self.search_history.add(inputs.as_str().to_string());
+ self.no_results = None;
+ self.pending_search = Some(cx.spawn(|this, mut cx| async move {
+ let results = search?.await.log_err()?;
+ let matches = results
+ .into_iter()
+ .map(|result| (result.buffer, vec![result.range.start..result.range.start]));
+
+ this.update(&mut cx, |this, cx| {
+ this.no_results = Some(true);
+ this.excerpts.update(cx, |excerpts, cx| {
+ excerpts.clear(cx);
+ });
+ });
+ for (buffer, ranges) in matches {
+ let mut match_ranges = this.update(&mut cx, |this, cx| {
+ this.no_results = Some(false);
+ this.excerpts.update(cx, |excerpts, cx| {
+ excerpts.stream_excerpts_with_context_lines(buffer, ranges, 3, cx)
+ })
+ });
+ while let Some(match_range) = match_ranges.next().await {
+ this.update(&mut cx, |this, cx| {
+ this.match_ranges.push(match_range);
+ while let Ok(Some(match_range)) = match_ranges.try_next() {
+ this.match_ranges.push(match_range);
+ }
+ cx.notify();
+ });
+ }
+ }
+
+ this.update(&mut cx, |this, cx| {
+ this.pending_search.take();
+ cx.notify();
+ });
+
+ None
+ }));
+ cx.notify();
+ }
+}
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum ViewEvent {
+ UpdateTab,
+ Activate,
+ EditorEvent(editor::Event),
+ Dismiss,
+}
+
+impl Entity for ProjectSearchView {
+ type Event = ViewEvent;
+}
+
+impl View for ProjectSearchView {
+ fn ui_name() -> &'static str {
+ "ProjectSearchView"
+ }
+
+ fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
+ let model = &self.model.read(cx);
+ if model.match_ranges.is_empty() {
+ enum Status {}
+
+ let theme = theme::current(cx).clone();
+
+ // If Search is Active -> Major: Searching..., Minor: None
+ // If Semantic -> Major: "Search using Natural Language", Minor: {Status}/n{ex...}/n{ex...}
+ // If Regex -> Major: "Search using Regex", Minor: {ex...}
+ // If Text -> Major: "Text search all files and folders", Minor: {...}
+
+ let current_mode = self.current_mode;
+ let mut major_text = if model.pending_search.is_some() {
+ Cow::Borrowed("Searching...")
+ } else if model.no_results.is_some_and(|v| v) {
+ Cow::Borrowed("No Results")
+ } else {
+ match current_mode {
+ SearchMode::Text => Cow::Borrowed("Text search all files and folders"),
+ SearchMode::Semantic => {
+ Cow::Borrowed("Search all code objects using Natural Language")
+ }
+ SearchMode::Regex => Cow::Borrowed("Regex search all files and folders"),
+ }
+ };
+
+ let mut show_minor_text = true;
+ let semantic_status = self.semantic_state.as_ref().and_then(|semantic| {
+ let status = semantic.index_status;
+ match status {
+ SemanticIndexStatus::NotAuthenticated => {
+ major_text = Cow::Borrowed("Not Authenticated");
+ show_minor_text = false;
+ Some(vec![
+ "API Key Missing: Please set 'OPENAI_API_KEY' in Environment Variables."
+ .to_string(), "If you authenticated using the Assistant Panel, please restart Zed to Authenticate.".to_string()])
+ }
+ SemanticIndexStatus::Indexed => Some(vec!["Indexing complete".to_string()]),
+ SemanticIndexStatus::Indexing {
+ remaining_files,
+ rate_limit_expiry,
+ } => {
+ if remaining_files == 0 {
+ Some(vec![format!("Indexing...")])
+ } else {
+ if let Some(rate_limit_expiry) = rate_limit_expiry {
+ let remaining_seconds =
+ rate_limit_expiry.duration_since(Instant::now());
+ if remaining_seconds > Duration::from_secs(0) {
+ Some(vec![format!(
+ "Remaining files to index (rate limit resets in {}s): {}",
+ remaining_seconds.as_secs(),
+ remaining_files
+ )])
+ } else {
+ Some(vec![format!("Remaining files to index: {}", remaining_files)])
+ }
+ } else {
+ Some(vec![format!("Remaining files to index: {}", remaining_files)])
+ }
+ }
+ }
+ SemanticIndexStatus::NotIndexed => None,
+ }
+ });
+
+ let minor_text = if let Some(no_results) = model.no_results {
+ if model.pending_search.is_none() && no_results {
+ vec!["No results found in this project for the provided query".to_owned()]
+ } else {
+ vec![]
+ }
+ } else {
+ match current_mode {
+ SearchMode::Semantic => {
+ let mut minor_text: Vec<String> = Vec::new();
+ minor_text.push("".into());
+ if let Some(semantic_status) = semantic_status {
+ minor_text.extend(semantic_status);
+ }
+ if show_minor_text {
+ minor_text
+ .push("Simply explain the code you are looking to find.".into());
+ minor_text.push(
+ "ex. 'prompt user for permissions to index their project'".into(),
+ );
+ }
+ minor_text
+ }
+ _ => vec![
+ "".to_owned(),
+ "Include/exclude specific paths with the filter option.".to_owned(),
+ "Matching exact word and/or casing is available too.".to_owned(),
+ ],
+ }
+ };
+
+ let previous_query_keystrokes =
+ cx.binding_for_action(&PreviousHistoryQuery {})
+ .map(|binding| {
+ binding
+ .keystrokes()
+ .iter()
+ .map(|k| k.to_string())
+ .collect::<Vec<_>>()
+ });
+ let next_query_keystrokes =
+ cx.binding_for_action(&NextHistoryQuery {}).map(|binding| {
+ binding
+ .keystrokes()
+ .iter()
+ .map(|k| k.to_string())
+ .collect::<Vec<_>>()
+ });
+ let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) {
+ (Some(previous_query_keystrokes), Some(next_query_keystrokes)) => {
+ format!(
+ "Search ({}/{} for previous/next query)",
+ previous_query_keystrokes.join(" "),
+ next_query_keystrokes.join(" ")
+ )
+ }
+ (None, Some(next_query_keystrokes)) => {
+ format!(
+ "Search ({} for next query)",
+ next_query_keystrokes.join(" ")
+ )
+ }
+ (Some(previous_query_keystrokes), None) => {
+ format!(
+ "Search ({} for previous query)",
+ previous_query_keystrokes.join(" ")
+ )
+ }
+ (None, None) => String::new(),
+ };
+ self.query_editor.update(cx, |editor, cx| {
+ editor.set_placeholder_text(new_placeholder_text, cx);
+ });
+
+ MouseEventHandler::new::<Status, _>(0, cx, |_, _| {
+ Flex::column()
+ .with_child(Flex::column().contained().flex(1., true))
+ .with_child(
+ Flex::column()
+ .align_children_center()
+ .with_child(Label::new(
+ major_text,
+ theme.search.major_results_status.clone(),
+ ))
+ .with_children(
+ minor_text.into_iter().map(|x| {
+ Label::new(x, theme.search.minor_results_status.clone())
+ }),
+ )
+ .aligned()
+ .top()
+ .contained()
+ .flex(7., true),
+ )
+ .contained()
+ .with_background_color(theme.editor.background)
+ })
+ .on_down(MouseButton::Left, |_, _, cx| {
+ cx.focus_parent();
+ })
+ .into_any_named("project search view")
+ } else {
+ ChildView::new(&self.results_editor, cx)
+ .flex(1., true)
+ .into_any_named("project search view")
+ }
+ }
+
+ fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
+ let handle = cx.weak_handle();
+ cx.update_global(|state: &mut ActiveSearches, cx| {
+ state
+ .0
+ .insert(self.model.read(cx).project.downgrade(), handle)
+ });
+
+ cx.update_global(|state: &mut ActiveSettings, cx| {
+ state.0.insert(
+ self.model.read(cx).project.downgrade(),
+ self.current_settings(),
+ );
+ });
+
+ if cx.is_self_focused() {
+ if self.query_editor_was_focused {
+ cx.focus(&self.query_editor);
+ } else {
+ cx.focus(&self.results_editor);
+ }
+ }
+ }
+}
+
+impl Item for ProjectSearchView {
+ fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
+ let query_text = self.query_editor.read(cx).text(cx);
+
+ query_text
+ .is_empty()
+ .not()
+ .then(|| query_text.into())
+ .or_else(|| Some("Project Search".into()))
+ }
+ fn should_close_item_on_event(event: &Self::Event) -> bool {
+ event == &Self::Event::Dismiss
+ }
+
+ fn act_as_type<'a>(
+ &'a self,
+ type_id: TypeId,
+ self_handle: &'a ViewHandle<Self>,
+ _: &'a AppContext,
+ ) -> Option<&'a AnyViewHandle> {
+ if type_id == TypeId::of::<Self>() {
+ Some(self_handle)
+ } else if type_id == TypeId::of::<Editor>() {
+ Some(&self.results_editor)
+ } else {
+ None
+ }
+ }
+
+ fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
+ self.results_editor
+ .update(cx, |editor, cx| editor.deactivated(cx));
+ }
+
+ fn tab_content<T: 'static>(
+ &self,
+ _detail: Option<usize>,
+ tab_theme: &theme::Tab,
+ cx: &AppContext,
+ ) -> AnyElement<T> {
+ Flex::row()
+ .with_child(
+ Svg::new("icons/magnifying_glass.svg")
+ .with_color(tab_theme.label.text.color)
+ .constrained()
+ .with_width(tab_theme.type_icon_width)
+ .aligned()
+ .contained()
+ .with_margin_right(tab_theme.spacing),
+ )
+ .with_child({
+ let tab_name: Option<Cow<_>> = self
+ .model
+ .read(cx)
+ .search_history
+ .current()
+ .as_ref()
+ .map(|query| {
+ let query_text = util::truncate_and_trailoff(query, MAX_TAB_TITLE_LEN);
+ query_text.into()
+ });
+ Label::new(
+ tab_name
+ .filter(|name| !name.is_empty())
+ .unwrap_or("Project search".into()),
+ tab_theme.label.clone(),
+ )
+ .aligned()
+ })
+ .into_any()
+ }
+
+ fn for_each_project_item(&self, cx: &AppContext, f: &mut dyn FnMut(usize, &dyn project::Item)) {
+ self.results_editor.for_each_project_item(cx, f)
+ }
+
+ fn is_singleton(&self, _: &AppContext) -> bool {
+ false
+ }
+
+ fn can_save(&self, _: &AppContext) -> bool {
+ true
+ }
+
+ fn is_dirty(&self, cx: &AppContext) -> bool {
+ self.results_editor.read(cx).is_dirty(cx)
+ }
+
+ fn has_conflict(&self, cx: &AppContext) -> bool {
+ self.results_editor.read(cx).has_conflict(cx)
+ }
+
+ fn save(
+ &mut self,
+ project: ModelHandle<Project>,
+ cx: &mut ViewContext<Self>,
+ ) -> Task<anyhow::Result<()>> {
+ self.results_editor
+ .update(cx, |editor, cx| editor.save(project, cx))
+ }
+
+ fn save_as(
+ &mut self,
+ _: ModelHandle<Project>,
+ _: PathBuf,
+ _: &mut ViewContext<Self>,
+ ) -> Task<anyhow::Result<()>> {
+ unreachable!("save_as should not have been called")
+ }
+
+ fn reload(
+ &mut self,
+ project: ModelHandle<Project>,
+ cx: &mut ViewContext<Self>,
+ ) -> Task<anyhow::Result<()>> {
+ self.results_editor
+ .update(cx, |editor, cx| editor.reload(project, cx))
+ }
+
+ fn clone_on_split(&self, _workspace_id: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self>
+ where
+ Self: Sized,
+ {
+ let model = self.model.update(cx, |model, cx| model.clone(cx));
+ Some(Self::new(model, cx, None))
+ }
+
+ fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
+ self.results_editor
+ .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx));
+ }
+
+ fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
+ self.results_editor.update(cx, |editor, _| {
+ editor.set_nav_history(Some(nav_history));
+ });
+ }
+
+ fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
+ self.results_editor
+ .update(cx, |editor, cx| editor.navigate(data, cx))
+ }
+
+ fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
+ match event {
+ ViewEvent::UpdateTab => {
+ smallvec::smallvec![ItemEvent::UpdateBreadcrumbs, ItemEvent::UpdateTab]
+ }
+ ViewEvent::EditorEvent(editor_event) => Editor::to_item_events(editor_event),
+ ViewEvent::Dismiss => smallvec::smallvec![ItemEvent::CloseItem],
+ _ => SmallVec::new(),
+ }
+ }
+
+ fn breadcrumb_location(&self) -> ToolbarItemLocation {
+ if self.has_matches() {
+ ToolbarItemLocation::Secondary
+ } else {
+ ToolbarItemLocation::Hidden
+ }
+ }
+
+ fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
+ self.results_editor.breadcrumbs(theme, cx)
+ }
+
+ fn serialized_item_kind() -> Option<&'static str> {
+ None
+ }
+
+ fn deserialize(
+ _project: ModelHandle<Project>,
+ _workspace: WeakViewHandle<Workspace>,
+ _workspace_id: workspace::WorkspaceId,
+ _item_id: workspace::ItemId,
+ _cx: &mut ViewContext<Pane>,
+ ) -> Task<anyhow::Result<ViewHandle<Self>>> {
+ unimplemented!()
+ }
+}
+
+impl ProjectSearchView {
+ fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) {
+ self.filters_enabled = !self.filters_enabled;
+ cx.update_global(|state: &mut ActiveSettings, cx| {
+ state.0.insert(
+ self.model.read(cx).project.downgrade(),
+ self.current_settings(),
+ );
+ });
+ }
+
+ fn current_settings(&self) -> ProjectSearchSettings {
+ ProjectSearchSettings {
+ search_options: self.search_options,
+ filters_enabled: self.filters_enabled,
+ current_mode: self.current_mode,
+ }
+ }
+ fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) {
+ self.search_options.toggle(option);
+ cx.update_global(|state: &mut ActiveSettings, cx| {
+ state.0.insert(
+ self.model.read(cx).project.downgrade(),
+ self.current_settings(),
+ );
+ });
+ }
+
+ fn index_project(&mut self, cx: &mut ViewContext<Self>) {
+ if let Some(semantic_index) = SemanticIndex::global(cx) {
+ // Semantic search uses no options
+ self.search_options = SearchOptions::none();
+
+ let project = self.model.read(cx).project.clone();
+
+ semantic_index.update(cx, |semantic_index, cx| {
+ semantic_index
+ .index_project(project.clone(), cx)
+ .detach_and_log_err(cx);
+ });
+
+ self.semantic_state = Some(SemanticState {
+ index_status: semantic_index.read(cx).status(&project),
+ maintain_rate_limit: None,
+ _subscription: cx.observe(&semantic_index, Self::semantic_index_changed),
+ });
+ self.semantic_index_changed(semantic_index, cx);
+ }
+ }
+
+ fn semantic_index_changed(
+ &mut self,
+ semantic_index: ModelHandle<SemanticIndex>,
+ cx: &mut ViewContext<Self>,
+ ) {
+ let project = self.model.read(cx).project.clone();
+ if let Some(semantic_state) = self.semantic_state.as_mut() {
+ cx.notify();
+ semantic_state.index_status = semantic_index.read(cx).status(&project);
+ if let SemanticIndexStatus::Indexing {
+ rate_limit_expiry: Some(_),
+ ..
+ } = &semantic_state.index_status
+ {
+ if semantic_state.maintain_rate_limit.is_none() {
+ semantic_state.maintain_rate_limit =
+ Some(cx.spawn(|this, mut cx| async move {
+ loop {
+ cx.background().timer(Duration::from_secs(1)).await;
+ this.update(&mut cx, |_, cx| cx.notify()).log_err();
+ }
+ }));
+ return;
+ }
+ } else {
+ semantic_state.maintain_rate_limit = None;
+ }
+ }
+ }
+
+ fn clear_search(&mut self, cx: &mut ViewContext<Self>) {
+ self.model.update(cx, |model, cx| {
+ model.pending_search = None;
+ model.no_results = None;
+ model.match_ranges.clear();
+
+ model.excerpts.update(cx, |excerpts, cx| {
+ excerpts.clear(cx);
+ });
+ });
+ }
+
+ fn activate_search_mode(&mut self, mode: SearchMode, cx: &mut ViewContext<Self>) {
+ let previous_mode = self.current_mode;
+ if previous_mode == mode {
+ return;
+ }
+
+ self.clear_search(cx);
+ self.current_mode = mode;
+ self.active_match_index = None;
+
+ match mode {
+ SearchMode::Semantic => {
+ let has_permission = self.semantic_permissioned(cx);
+ self.active_match_index = None;
+ cx.spawn(|this, mut cx| async move {
+ let has_permission = has_permission.await?;
+
+ if !has_permission {
+ let mut answer = this.update(&mut cx, |this, cx| {
+ let project = this.model.read(cx).project.clone();
+ let project_name = project
+ .read(cx)
+ .worktree_root_names(cx)
+ .collect::<Vec<&str>>()
+ .join("/");
+ let is_plural =
+ project_name.chars().filter(|letter| *letter == '/').count() > 0;
+ let prompt_text = format!("Would you like to index the '{}' project{} for semantic search? This requires sending code to the OpenAI API", project_name,
+ if is_plural {
+ "s"
+ } else {""});
+ cx.prompt(
+ PromptLevel::Info,
+ prompt_text.as_str(),
+ &["Continue", "Cancel"],
+ )
+ })?;
+
+ if answer.next().await == Some(0) {
+ this.update(&mut cx, |this, _| {
+ this.semantic_permissioned = Some(true);
+ })?;
+ } else {
+ this.update(&mut cx, |this, cx| {
+ this.semantic_permissioned = Some(false);
+ debug_assert_ne!(previous_mode, SearchMode::Semantic, "Tried to re-enable semantic search mode after user modal was rejected");
+ this.activate_search_mode(previous_mode, cx);
+ })?;
+ return anyhow::Ok(());
+ }
+ }
+
+ this.update(&mut cx, |this, cx| {
+ this.index_project(cx);
+ })?;
+
+ anyhow::Ok(())
+ }).detach_and_log_err(cx);
+ }
+ SearchMode::Regex | SearchMode::Text => {
+ self.semantic_state = None;
+ self.active_match_index = None;
+ self.search(cx);
+ }
+ }
+
+ cx.update_global(|state: &mut ActiveSettings, cx| {
+ state.0.insert(
+ self.model.read(cx).project.downgrade(),
+ self.current_settings(),
+ );
+ });
+
+ cx.notify();
+ }
+ fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext<Self>) {
+ let model = self.model.read(cx);
+ if let Some(query) = model.active_query.as_ref() {
+ if model.match_ranges.is_empty() {
+ return;
+ }
+ if let Some(active_index) = self.active_match_index {
+ let query = query.clone().with_replacement(self.replacement(cx));
+ self.results_editor.replace(
+ &(Box::new(model.match_ranges[active_index].clone()) as _),
+ &query,
+ cx,
+ );
+ self.select_match(Direction::Next, cx)
+ }
+ }
+ }
+ pub fn replacement(&self, cx: &AppContext) -> String {
+ self.replacement_editor.read(cx).text(cx)
+ }
+ fn replace_all(&mut self, _: &ReplaceAll, cx: &mut ViewContext<Self>) {
+ let model = self.model.read(cx);
+ if let Some(query) = model.active_query.as_ref() {
+ if model.match_ranges.is_empty() {
+ return;
+ }
+ if self.active_match_index.is_some() {
+ let query = query.clone().with_replacement(self.replacement(cx));
+ let matches = model
+ .match_ranges
+ .iter()
+ .map(|item| Box::new(item.clone()) as _)
+ .collect::<Vec<_>>();
+ for item in matches {
+ self.results_editor.replace(&item, &query, cx);
+ }
+ }
+ }
+ }
+
+ fn new(
+ model: ModelHandle<ProjectSearch>,
+ cx: &mut ViewContext<Self>,
+ settings: Option<ProjectSearchSettings>,
+ ) -> Self {
+ let project;
+ let excerpts;
+ let mut replacement_text = None;
+ let mut query_text = String::new();
+
+ // Read in settings if available
+ let (mut options, current_mode, filters_enabled) = if let Some(settings) = settings {
+ (
+ settings.search_options,
+ settings.current_mode,
+ settings.filters_enabled,
+ )
+ } else {
+ (SearchOptions::NONE, Default::default(), false)
+ };
+
+ {
+ let model = model.read(cx);
+ project = model.project.clone();
+ excerpts = model.excerpts.clone();
+ if let Some(active_query) = model.active_query.as_ref() {
+ query_text = active_query.as_str().to_string();
+ replacement_text = active_query.replacement().map(ToOwned::to_owned);
+ options = SearchOptions::from_query(active_query);
+ }
+ }
+ cx.observe(&model, |this, _, cx| this.model_changed(cx))
+ .detach();
+
+ let query_editor = cx.add_view(|cx| {
+ let mut editor = Editor::single_line(
+ Some(Arc::new(|theme| theme.search.editor.input.clone())),
+ cx,
+ );
+ editor.set_placeholder_text("Text search all files", cx);
+ editor.set_text(query_text, cx);
+ editor
+ });
+ // Subscribe to query_editor in order to reraise editor events for workspace item activation purposes
+ cx.subscribe(&query_editor, |_, _, event, cx| {
+ cx.emit(ViewEvent::EditorEvent(event.clone()))
+ })
+ .detach();
+ let replacement_editor = cx.add_view(|cx| {
+ let mut editor = Editor::single_line(
+ Some(Arc::new(|theme| theme.search.editor.input.clone())),
+ cx,
+ );
+ editor.set_placeholder_text("Replace in project..", cx);
+ if let Some(text) = replacement_text {
+ editor.set_text(text, cx);
+ }
+ editor
+ });
+ let results_editor = cx.add_view(|cx| {
+ let mut editor = Editor::for_multibuffer(excerpts, Some(project.clone()), cx);
+ editor.set_searchable(false);
+ editor
+ });
+ cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
+ .detach();
+
+ cx.subscribe(&results_editor, |this, _, event, cx| {
+ if matches!(event, editor::Event::SelectionsChanged { .. }) {
+ this.update_match_index(cx);
+ }
+ // Reraise editor events for workspace item activation purposes
+ cx.emit(ViewEvent::EditorEvent(event.clone()));
+ })
+ .detach();
+
+ let included_files_editor = cx.add_view(|cx| {
+ let mut editor = Editor::single_line(
+ Some(Arc::new(|theme| {
+ theme.search.include_exclude_editor.input.clone()
+ })),
+ cx,
+ );
+ editor.set_placeholder_text("Include: crates/**/*.toml", cx);
+
+ editor
+ });
+ // Subscribe to include_files_editor in order to reraise editor events for workspace item activation purposes
+ cx.subscribe(&included_files_editor, |_, _, event, cx| {
+ cx.emit(ViewEvent::EditorEvent(event.clone()))
+ })
+ .detach();
+
+ let excluded_files_editor = cx.add_view(|cx| {
+ let mut editor = Editor::single_line(
+ Some(Arc::new(|theme| {
+ theme.search.include_exclude_editor.input.clone()
+ })),
+ cx,
+ );
+ editor.set_placeholder_text("Exclude: vendor/*, *.lock", cx);
+
+ editor
+ });
+ // Subscribe to excluded_files_editor in order to reraise editor events for workspace item activation purposes
+ cx.subscribe(&excluded_files_editor, |_, _, event, cx| {
+ cx.emit(ViewEvent::EditorEvent(event.clone()))
+ })
+ .detach();
+
+ // Check if Worktrees have all been previously indexed
+ let mut this = ProjectSearchView {
+ replacement_editor,
+ search_id: model.read(cx).search_id,
+ model,
+ query_editor,
+ results_editor,
+ semantic_state: None,
+ semantic_permissioned: None,
+ search_options: options,
+ panels_with_errors: HashSet::new(),
+ active_match_index: None,
+ query_editor_was_focused: false,
+ included_files_editor,
+ excluded_files_editor,
+ filters_enabled,
+ current_mode,
+ replace_enabled: false,
+ };
+ this.model_changed(cx);
+ this
+ }
+
+ fn semantic_permissioned(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<bool>> {
+ if let Some(value) = self.semantic_permissioned {
+ return Task::ready(Ok(value));
+ }
+
+ SemanticIndex::global(cx)
+ .map(|semantic| {
+ let project = self.model.read(cx).project.clone();
+ semantic.update(cx, |this, cx| this.project_previously_indexed(&project, cx))
+ })
+ .unwrap_or(Task::ready(Ok(false)))
+ }
+ pub fn new_search_in_directory(
+ workspace: &mut Workspace,
+ dir_entry: &Entry,
+ cx: &mut ViewContext<Workspace>,
+ ) {
+ if !dir_entry.is_dir() {
+ return;
+ }
+ let Some(filter_str) = dir_entry.path.to_str() else {
+ return;
+ };
+
+ let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
+ let search = cx.add_view(|cx| ProjectSearchView::new(model, cx, None));
+ workspace.add_item(Box::new(search.clone()), cx);
+ search.update(cx, |search, cx| {
+ search
+ .included_files_editor
+ .update(cx, |editor, cx| editor.set_text(filter_str, cx));
+ search.filters_enabled = true;
+ search.focus_query_editor(cx)
+ });
+ }
+
+ // Re-activate the most recently activated search or the most recent if it has been closed.
+ // If no search exists in the workspace, create a new one.
+ fn deploy(
+ workspace: &mut Workspace,
+ _: &workspace::NewSearch,
+ cx: &mut ViewContext<Workspace>,
+ ) {
+ // Clean up entries for dropped projects
+ cx.update_global(|state: &mut ActiveSearches, cx| {
+ state.0.retain(|project, _| project.is_upgradable(cx))
+ });
+
+ let active_search = cx
+ .global::<ActiveSearches>()
+ .0
+ .get(&workspace.project().downgrade());
+
+ let existing = active_search
+ .and_then(|active_search| {
+ workspace
+ .items_of_type::<ProjectSearchView>(cx)
+ .find(|search| search == active_search)
+ })
+ .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
+
+ let query = workspace.active_item(cx).and_then(|item| {
+ let editor = item.act_as::<Editor>(cx)?;
+ let query = editor.query_suggestion(cx);
+ if query.is_empty() {
+ None
+ } else {
+ Some(query)
+ }
+ });
+
+ let search = if let Some(existing) = existing {
+ workspace.activate_item(&existing, cx);
+ existing
+ } else {
+ let settings = cx
+ .global::<ActiveSettings>()
+ .0
+ .get(&workspace.project().downgrade());
+
+ let settings = if let Some(settings) = settings {
+ Some(settings.clone())
+ } else {
+ None
+ };
+
+ let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
+ let view = cx.add_view(|cx| ProjectSearchView::new(model, cx, settings));
+
+ workspace.add_item(Box::new(view.clone()), cx);
+ view
+ };
+
+ search.update(cx, |search, cx| {
+ if let Some(query) = query {
+ search.set_query(&query, cx);
+ }
+ search.focus_query_editor(cx)
+ });
+ }
+
+ fn search(&mut self, cx: &mut ViewContext<Self>) {
+ let mode = self.current_mode;
+ match mode {
+ SearchMode::Semantic => {
+ if self.semantic_state.is_some() {
+ if let Some(query) = self.build_search_query(cx) {
+ self.model
+ .update(cx, |model, cx| model.semantic_search(query.as_inner(), cx));
+ }
+ }
+ }
+
+ _ => {
+ if let Some(query) = self.build_search_query(cx) {
+ self.model.update(cx, |model, cx| model.search(query, cx));
+ }
+ }
+ }
+ }
+
+ fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
+ let text = self.query_editor.read(cx).text(cx);
+ let included_files =
+ match Self::parse_path_matches(&self.included_files_editor.read(cx).text(cx)) {
+ Ok(included_files) => {
+ self.panels_with_errors.remove(&InputPanel::Include);
+ included_files
+ }
+ Err(_e) => {
+ self.panels_with_errors.insert(InputPanel::Include);
+ cx.notify();
+ return None;
+ }
+ };
+ let excluded_files =
+ match Self::parse_path_matches(&self.excluded_files_editor.read(cx).text(cx)) {
+ Ok(excluded_files) => {
+ self.panels_with_errors.remove(&InputPanel::Exclude);
+ excluded_files
+ }
+ Err(_e) => {
+ self.panels_with_errors.insert(InputPanel::Exclude);
+ cx.notify();
+ return None;
+ }
+ };
+ let current_mode = self.current_mode;
+ match current_mode {
+ SearchMode::Regex => {
+ match SearchQuery::regex(
+ text,
+ self.search_options.contains(SearchOptions::WHOLE_WORD),
+ self.search_options.contains(SearchOptions::CASE_SENSITIVE),
+ included_files,
+ excluded_files,
+ ) {
+ Ok(query) => {
+ self.panels_with_errors.remove(&InputPanel::Query);
+ Some(query)
+ }
+ Err(_e) => {
+ self.panels_with_errors.insert(InputPanel::Query);
+ cx.notify();
+ None
+ }
+ }
+ }
+ _ => match SearchQuery::text(
+ text,
+ self.search_options.contains(SearchOptions::WHOLE_WORD),
+ self.search_options.contains(SearchOptions::CASE_SENSITIVE),
+ included_files,
+ excluded_files,
+ ) {
+ Ok(query) => {
+ self.panels_with_errors.remove(&InputPanel::Query);
+ Some(query)
+ }
+ Err(_e) => {
+ self.panels_with_errors.insert(InputPanel::Query);
+ cx.notify();
+ None
+ }
+ },
+ }
+ }
+
+ fn parse_path_matches(text: &str) -> anyhow::Result<Vec<PathMatcher>> {
+ text.split(',')
+ .map(str::trim)
+ .filter(|maybe_glob_str| !maybe_glob_str.is_empty())
+ .map(|maybe_glob_str| {
+ PathMatcher::new(maybe_glob_str)
+ .with_context(|| format!("parsing {maybe_glob_str} as path matcher"))
+ })
+ .collect()
+ }
+
+ fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
+ if let Some(index) = self.active_match_index {
+ let match_ranges = self.model.read(cx).match_ranges.clone();
+ let new_index = self.results_editor.update(cx, |editor, cx| {
+ editor.match_index_for_direction(&match_ranges, index, direction, 1, cx)
+ });
+
+ let range_to_select = match_ranges[new_index].clone();
+ self.results_editor.update(cx, |editor, cx| {
+ let range_to_select = editor.range_for_match(&range_to_select);
+ editor.unfold_ranges([range_to_select.clone()], false, true, cx);
+ editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
+ s.select_ranges([range_to_select])
+ });
+ });
+ }
+ }
+
+ fn focus_query_editor(&mut self, cx: &mut ViewContext<Self>) {
+ self.query_editor.update(cx, |query_editor, cx| {
+ query_editor.select_all(&SelectAll, cx);
+ });
+ self.query_editor_was_focused = true;
+ cx.focus(&self.query_editor);
+ }
+
+ fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
+ self.query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text(query, cx));
+ }
+
+ fn focus_results_editor(&mut self, cx: &mut ViewContext<Self>) {
+ self.query_editor.update(cx, |query_editor, cx| {
+ let cursor = query_editor.selections.newest_anchor().head();
+ query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor]));
+ });
+ self.query_editor_was_focused = false;
+ cx.focus(&self.results_editor);
+ }
+
+ fn model_changed(&mut self, cx: &mut ViewContext<Self>) {
+ let match_ranges = self.model.read(cx).match_ranges.clone();
+ if match_ranges.is_empty() {
+ self.active_match_index = None;
+ } else {
+ self.active_match_index = Some(0);
+ self.update_match_index(cx);
+ let prev_search_id = mem::replace(&mut self.search_id, self.model.read(cx).search_id);
+ let is_new_search = self.search_id != prev_search_id;
+ self.results_editor.update(cx, |editor, cx| {
+ if is_new_search {
+ let range_to_select = match_ranges
+ .first()
+ .clone()
+ .map(|range| editor.range_for_match(range));
+ editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
+ s.select_ranges(range_to_select)
+ });
+ }
+ editor.highlight_background::<Self>(
+ match_ranges,
+ |theme| theme.search.match_background,
+ cx,
+ );
+ });
+ if is_new_search && self.query_editor.is_focused(cx) {
+ self.focus_results_editor(cx);
+ }
+ }
+
+ cx.emit(ViewEvent::UpdateTab);
+ cx.notify();
+ }
+
+ fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
+ let results_editor = self.results_editor.read(cx);
+ let new_index = active_match_index(
+ &self.model.read(cx).match_ranges,
+ &results_editor.selections.newest_anchor().head(),
+ &results_editor.buffer().read(cx).snapshot(cx),
+ );
+ if self.active_match_index != new_index {
+ self.active_match_index = new_index;
+ cx.notify();
+ }
+ }
+
+ pub fn has_matches(&self) -> bool {
+ self.active_match_index.is_some()
+ }
+
+ fn move_focus_to_results(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |search_view, cx| {
+ if !search_view.results_editor.is_focused(cx)
+ && !search_view.model.read(cx).match_ranges.is_empty()
+ {
+ return search_view.focus_results_editor(cx);
+ }
+ });
+ }
+
+ cx.propagate_action();
+ }
+}
+
+impl Default for ProjectSearchBar {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl ProjectSearchBar {
+ pub fn new() -> Self {
+ Self {
+ active_project_search: Default::default(),
+ subscription: Default::default(),
+ }
+ }
+ fn cycle_mode(workspace: &mut Workspace, _: &CycleMode, cx: &mut ViewContext<Workspace>) {
+ if let Some(search_view) = workspace
+ .active_item(cx)
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |this, cx| {
+ let new_mode =
+ crate::mode::next_mode(&this.current_mode, SemanticIndex::enabled(cx));
+ this.activate_search_mode(new_mode, cx);
+ cx.focus(&this.query_editor);
+ })
+ }
+ }
+ fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
+ let mut should_propagate = true;
+ if let Some(search_view) = self.active_project_search.as_ref() {
+ search_view.update(cx, |search_view, cx| {
+ if !search_view.replacement_editor.is_focused(cx) {
+ should_propagate = false;
+ search_view.search(cx);
+ }
+ });
+ }
+ if should_propagate {
+ cx.propagate_action();
+ }
+ }
+
+ fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
+ if let Some(search_view) = workspace
+ .active_item(cx)
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ let new_query = search_view.update(cx, |search_view, cx| {
+ let new_query = search_view.build_search_query(cx);
+ if new_query.is_some() {
+ if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
+ search_view.query_editor.update(cx, |editor, cx| {
+ editor.set_text(old_query.as_str(), cx);
+ });
+ search_view.search_options = SearchOptions::from_query(&old_query);
+ }
+ }
+ new_query
+ });
+ if let Some(new_query) = new_query {
+ let model = cx.add_model(|cx| {
+ let mut model = ProjectSearch::new(workspace.project().clone(), cx);
+ model.search(new_query, cx);
+ model
+ });
+ workspace.add_item(
+ Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx, None))),
+ cx,
+ );
+ }
+ }
+ }
+
+ fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx));
+ } else {
+ cx.propagate_action();
+ }
+ }
+
+ fn replace_next(pane: &mut Pane, _: &ReplaceNext, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| view.replace_next(&ReplaceNext, cx));
+ } else {
+ cx.propagate_action();
+ }
+ }
+ fn replace_all(pane: &mut Pane, _: &ReplaceAll, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| view.replace_all(&ReplaceAll, cx));
+ } else {
+ cx.propagate_action();
+ }
+ }
+ fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| view.select_match(Direction::Prev, cx));
+ } else {
+ cx.propagate_action();
+ }
+ }
+
+ fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
+ self.cycle_field(Direction::Next, cx);
+ }
+
+ fn tab_previous(&mut self, _: &editor::TabPrev, cx: &mut ViewContext<Self>) {
+ self.cycle_field(Direction::Prev, cx);
+ }
+
+ fn cycle_field(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
+ let active_project_search = match &self.active_project_search {
+ Some(active_project_search) => active_project_search,
+
+ None => {
+ cx.propagate_action();
+ return;
+ }
+ };
+
+ active_project_search.update(cx, |project_view, cx| {
+ let mut views = vec![&project_view.query_editor];
+ if project_view.filters_enabled {
+ views.extend([
+ &project_view.included_files_editor,
+ &project_view.excluded_files_editor,
+ ]);
+ }
+ if project_view.replace_enabled {
+ views.push(&project_view.replacement_editor);
+ }
+ let current_index = match views
+ .iter()
+ .enumerate()
+ .find(|(_, view)| view.is_focused(cx))
+ {
+ Some((index, _)) => index,
+
+ None => {
+ cx.propagate_action();
+ return;
+ }
+ };
+
+ let new_index = match direction {
+ Direction::Next => (current_index + 1) % views.len(),
+ Direction::Prev if current_index == 0 => views.len() - 1,
+ Direction::Prev => (current_index - 1) % views.len(),
+ };
+ cx.focus(views[new_index]);
+ });
+ }
+
+ fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) -> bool {
+ if let Some(search_view) = self.active_project_search.as_ref() {
+ search_view.update(cx, |search_view, cx| {
+ search_view.toggle_search_option(option, cx);
+ search_view.search(cx);
+ });
+
+ cx.notify();
+ true
+ } else {
+ false
+ }
+ }
+ fn toggle_replace(&mut self, _: &ToggleReplace, cx: &mut ViewContext<Self>) {
+ if let Some(search) = &self.active_project_search {
+ search.update(cx, |this, cx| {
+ this.replace_enabled = !this.replace_enabled;
+ if !this.replace_enabled {
+ cx.focus(&this.query_editor);
+ }
+ cx.notify();
+ });
+ }
+ }
+ fn toggle_replace_on_a_pane(pane: &mut Pane, _: &ToggleReplace, cx: &mut ViewContext<Pane>) {
+ let mut should_propagate = true;
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |this, cx| {
+ should_propagate = false;
+ this.replace_enabled = !this.replace_enabled;
+ if !this.replace_enabled {
+ cx.focus(&this.query_editor);
+ }
+ cx.notify();
+ });
+ }
+ if should_propagate {
+ cx.propagate_action();
+ }
+ }
+ fn activate_text_mode(pane: &mut Pane, _: &ActivateTextMode, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| {
+ view.activate_search_mode(SearchMode::Text, cx)
+ });
+ } else {
+ cx.propagate_action();
+ }
+ }
+
+ fn activate_regex_mode(pane: &mut Pane, _: &ActivateRegexMode, cx: &mut ViewContext<Pane>) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| {
+ view.activate_search_mode(SearchMode::Regex, cx)
+ });
+ } else {
+ cx.propagate_action();
+ }
+ }
+
+ fn activate_semantic_mode(
+ pane: &mut Pane,
+ _: &ActivateSemanticMode,
+ cx: &mut ViewContext<Pane>,
+ ) {
+ if SemanticIndex::enabled(cx) {
+ if let Some(search_view) = pane
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ {
+ search_view.update(cx, |view, cx| {
+ view.activate_search_mode(SearchMode::Semantic, cx)
+ });
+ } else {
+ cx.propagate_action();
+ }
+ }
+ }
+
+ fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) -> bool {
+ if let Some(search_view) = self.active_project_search.as_ref() {
+ search_view.update(cx, |search_view, cx| {
+ search_view.toggle_filters(cx);
+ search_view
+ .included_files_editor
+ .update(cx, |_, cx| cx.notify());
+ search_view
+ .excluded_files_editor
+ .update(cx, |_, cx| cx.notify());
+ cx.refresh_windows();
+ cx.notify();
+ });
+ cx.notify();
+ true
+ } else {
+ false
+ }
+ }
+
+ fn activate_search_mode(&self, mode: SearchMode, cx: &mut ViewContext<Self>) {
+ // Update Current Mode
+ if let Some(search_view) = self.active_project_search.as_ref() {
+ search_view.update(cx, |search_view, cx| {
+ search_view.activate_search_mode(mode, cx);
+ });
+ cx.notify();
+ }
+ }
+
+ fn is_option_enabled(&self, option: SearchOptions, cx: &AppContext) -> bool {
+ if let Some(search) = self.active_project_search.as_ref() {
+ search.read(cx).search_options.contains(option)
+ } else {
+ false
+ }
+ }
+
+ fn next_history_query(&mut self, _: &NextHistoryQuery, cx: &mut ViewContext<Self>) {
+ if let Some(search_view) = self.active_project_search.as_ref() {
+ search_view.update(cx, |search_view, cx| {
+ let new_query = search_view.model.update(cx, |model, _| {
+ if let Some(new_query) = model.search_history.next().map(str::to_string) {
+ new_query
+ } else {
+ model.search_history.reset_selection();
+ String::new()
+ }
+ });
+ search_view.set_query(&new_query, cx);
+ });
+ }
+ }
+
+ fn previous_history_query(&mut self, _: &PreviousHistoryQuery, cx: &mut ViewContext<Self>) {
+ if let Some(search_view) = self.active_project_search.as_ref() {
+ search_view.update(cx, |search_view, cx| {
+ if search_view.query_editor.read(cx).text(cx).is_empty() {
+ if let Some(new_query) = search_view
+ .model
+ .read(cx)
+ .search_history
+ .current()
+ .map(str::to_string)
+ {
+ search_view.set_query(&new_query, cx);
+ return;
+ }
+ }
+
+ if let Some(new_query) = search_view.model.update(cx, |model, _| {
+ model.search_history.previous().map(str::to_string)
+ }) {
+ search_view.set_query(&new_query, cx);
+ }
+ });
+ }
+ }
+}
+
+impl Entity for ProjectSearchBar {
+ type Event = ();
+}
+
+impl View for ProjectSearchBar {
+ fn ui_name() -> &'static str {
+ "ProjectSearchBar"
+ }
+
+ fn update_keymap_context(
+ &self,
+ keymap: &mut gpui::keymap_matcher::KeymapContext,
+ cx: &AppContext,
+ ) {
+ Self::reset_to_default_keymap_context(keymap);
+ let in_replace = self
+ .active_project_search
+ .as_ref()
+ .map(|search| {
+ search
+ .read(cx)
+ .replacement_editor
+ .read_with(cx, |_, cx| cx.is_self_focused())
+ })
+ .flatten()
+ .unwrap_or(false);
+ if in_replace {
+ keymap.add_identifier("in_replace");
+ }
+ }
+
+ fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
+ if let Some(_search) = self.active_project_search.as_ref() {
+ let search = _search.read(cx);
+ let theme = theme::current(cx).clone();
+ let query_container_style = if search.panels_with_errors.contains(&InputPanel::Query) {
+ theme.search.invalid_editor
+ } else {
+ theme.search.editor.input.container
+ };
+
+ let search = _search.read(cx);
+ let filter_button = render_option_button_icon(
+ search.filters_enabled,
+ "icons/filter.svg",
+ 0,
+ "Toggle filters",
+ Box::new(ToggleFilters),
+ move |_, this, cx| {
+ this.toggle_filters(cx);
+ },
+ cx,
+ );
+
+ let search = _search.read(cx);
+ let is_semantic_available = SemanticIndex::enabled(cx);
+ let is_semantic_disabled = search.semantic_state.is_none();
+ let icon_style = theme.search.editor_icon.clone();
+ let is_active = search.active_match_index.is_some();
+
+ let render_option_button_icon = |path, option, cx: &mut ViewContext<Self>| {
+ crate::search_bar::render_option_button_icon(
+ self.is_option_enabled(option, cx),
+ path,
+ option.bits as usize,
+ format!("Toggle {}", option.label()),
+ option.to_toggle_action(),
+ move |_, this, cx| {
+ this.toggle_search_option(option, cx);
+ },
+ cx,
+ )
+ };
+ let case_sensitive = is_semantic_disabled.then(|| {
+ render_option_button_icon(
+ "icons/case_insensitive.svg",
+ SearchOptions::CASE_SENSITIVE,
+ cx,
+ )
+ });
+
+ let whole_word = is_semantic_disabled.then(|| {
+ render_option_button_icon("icons/word_search.svg", SearchOptions::WHOLE_WORD, cx)
+ });
+
+ let search_button_for_mode = |mode, side, cx: &mut ViewContext<ProjectSearchBar>| {
+ let is_active = if let Some(search) = self.active_project_search.as_ref() {
+ let search = search.read(cx);
+ search.current_mode == mode
+ } else {
+ false
+ };
+ render_search_mode_button(
+ mode,
+ side,
+ is_active,
+ move |_, this, cx| {
+ this.activate_search_mode(mode, cx);
+ },
+ cx,
+ )
+ };
+
+ let search = _search.read(cx);
+
+ let include_container_style =
+ if search.panels_with_errors.contains(&InputPanel::Include) {
+ theme.search.invalid_include_exclude_editor
+ } else {
+ theme.search.include_exclude_editor.input.container
+ };
+
+ let exclude_container_style =
+ if search.panels_with_errors.contains(&InputPanel::Exclude) {
+ theme.search.invalid_include_exclude_editor
+ } else {
+ theme.search.include_exclude_editor.input.container
+ };
+
+ let matches = search.active_match_index.map(|match_ix| {
+ Label::new(
+ format!(
+ "{}/{}",
+ match_ix + 1,
+ search.model.read(cx).match_ranges.len()
+ ),
+ theme.search.match_index.text.clone(),
+ )
+ .contained()
+ .with_style(theme.search.match_index.container)
+ .aligned()
+ });
+ let should_show_replace_input = search.replace_enabled;
+ let replacement = should_show_replace_input.then(|| {
+ Flex::row()
+ .with_child(
+ Svg::for_style(theme.search.replace_icon.clone().icon)
+ .contained()
+ .with_style(theme.search.replace_icon.clone().container),
+ )
+ .with_child(ChildView::new(&search.replacement_editor, cx).flex(1., true))
+ .align_children_center()
+ .flex(1., true)
+ .contained()
+ .with_style(query_container_style)
+ .constrained()
+ .with_min_width(theme.search.editor.min_width)
+ .with_max_width(theme.search.editor.max_width)
+ .with_height(theme.search.search_bar_row_height)
+ .flex(1., false)
+ });
+ let replace_all = should_show_replace_input.then(|| {
+ super::replace_action(
+ ReplaceAll,
+ "Replace all",
+ "icons/replace_all.svg",
+ theme.tooltip.clone(),
+ theme.search.action_button.clone(),
+ )
+ });
+ let replace_next = should_show_replace_input.then(|| {
+ super::replace_action(
+ ReplaceNext,
+ "Replace next",
+ "icons/replace_next.svg",
+ theme.tooltip.clone(),
+ theme.search.action_button.clone(),
+ )
+ });
+ let query_column = Flex::column()
+ .with_spacing(theme.search.search_row_spacing)
+ .with_child(
+ Flex::row()
+ .with_child(
+ Svg::for_style(icon_style.icon)
+ .contained()
+ .with_style(icon_style.container),
+ )
+ .with_child(ChildView::new(&search.query_editor, cx).flex(1., true))
+ .with_child(
+ Flex::row()
+ .with_child(filter_button)
+ .with_children(case_sensitive)
+ .with_children(whole_word)
+ .flex(1., false)
+ .constrained()
+ .contained(),
+ )
+ .align_children_center()
+ .contained()
+ .with_style(query_container_style)
+ .constrained()
+ .with_min_width(theme.search.editor.min_width)
+ .with_max_width(theme.search.editor.max_width)
+ .with_height(theme.search.search_bar_row_height)
+ .flex(1., false),
+ )
+ .with_children(search.filters_enabled.then(|| {
+ Flex::row()
+ .with_child(
+ ChildView::new(&search.included_files_editor, cx)
+ .contained()
+ .with_style(include_container_style)
+ .constrained()
+ .with_height(theme.search.search_bar_row_height)
+ .flex(1., true),
+ )
+ .with_child(
+ ChildView::new(&search.excluded_files_editor, cx)
+ .contained()
+ .with_style(exclude_container_style)
+ .constrained()
+ .with_height(theme.search.search_bar_row_height)
+ .flex(1., true),
+ )
+ .constrained()
+ .with_min_width(theme.search.editor.min_width)
+ .with_max_width(theme.search.editor.max_width)
+ .flex(1., false)
+ }))
+ .flex(1., false);
+ let switches_column = Flex::row()
+ .align_children_center()
+ .with_child(super::toggle_replace_button(
+ search.replace_enabled,
+ theme.tooltip.clone(),
+ theme.search.option_button_component.clone(),
+ ))
+ .constrained()
+ .with_height(theme.search.search_bar_row_height)
+ .contained()
+ .with_style(theme.search.option_button_group);
+ let mode_column =
+ Flex::row()
+ .with_child(search_button_for_mode(
+ SearchMode::Text,
+ Some(Side::Left),
+ cx,
+ ))
+ .with_child(search_button_for_mode(
+ SearchMode::Regex,
+ if is_semantic_available {
+ None
+ } else {
+ Some(Side::Right)
+ },
+ cx,
+ ))
+ .with_children(is_semantic_available.then(|| {
+ search_button_for_mode(SearchMode::Semantic, Some(Side::Right), cx)
+ }))
+ .contained()
+ .with_style(theme.search.modes_container);
+
+ let nav_button_for_direction = |label, direction, cx: &mut ViewContext<Self>| {
+ render_nav_button(
+ label,
+ direction,
+ is_active,
+ move |_, this, cx| {
+ if let Some(search) = this.active_project_search.as_ref() {
+ search.update(cx, |search, cx| search.select_match(direction, cx));
+ }
+ },
+ cx,
+ )
+ };
+
+ let nav_column = Flex::row()
+ .with_children(replace_next)
+ .with_children(replace_all)
+ .with_child(Flex::row().with_children(matches))
+ .with_child(nav_button_for_direction("<", Direction::Prev, cx))
+ .with_child(nav_button_for_direction(">", Direction::Next, cx))
+ .constrained()
+ .with_height(theme.search.search_bar_row_height)
+ .flex_float();
+
+ Flex::row()
+ .with_child(query_column)
+ .with_child(mode_column)
+ .with_child(switches_column)
+ .with_children(replacement)
+ .with_child(nav_column)
+ .contained()
+ .with_style(theme.search.container)
+ .into_any_named("project search")
+ } else {
+ Empty::new().into_any()
+ }
+ }
+}
+
+impl ToolbarItemView for ProjectSearchBar {
+ fn set_active_pane_item(
+ &mut self,
+ active_pane_item: Option<&dyn ItemHandle>,
+ cx: &mut ViewContext<Self>,
+ ) -> ToolbarItemLocation {
+ cx.notify();
+ self.subscription = None;
+ self.active_project_search = None;
+ if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
+ search.update(cx, |search, cx| {
+ if search.current_mode == SearchMode::Semantic {
+ search.index_project(cx);
+ }
+ });
+
+ self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
+ self.active_project_search = Some(search);
+ ToolbarItemLocation::PrimaryLeft {
+ flex: Some((1., true)),
+ }
+ } else {
+ ToolbarItemLocation::Hidden
+ }
+ }
+
+ fn row_count(&self, cx: &ViewContext<Self>) -> usize {
+ if let Some(search) = self.active_project_search.as_ref() {
+ if search.read(cx).filters_enabled {
+ return 2;
+ }
+ }
+ 1
+ }
+}
+
+#[cfg(test)]
+pub mod tests {
+ use super::*;
+ use editor::DisplayPoint;
+ use gpui::{color::Color, executor::Deterministic, TestAppContext};
+ use project::FakeFs;
+ use semantic_index::semantic_index_settings::SemanticIndexSettings;
+ use serde_json::json;
+ use settings::SettingsStore;
+ use std::sync::Arc;
+ use theme::ThemeSettings;
+
+ #[gpui::test]
+ async fn test_project_search(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.background());
+ fs.insert_tree(
+ "/dir",
+ json!({
+ "one.rs": "const ONE: usize = 1;",
+ "two.rs": "const TWO: usize = one::ONE + one::ONE;",
+ "three.rs": "const THREE: usize = one::ONE + two::TWO;",
+ "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
+ let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
+ let search_view = cx
+ .add_window(|cx| ProjectSearchView::new(search.clone(), cx, None))
+ .root(cx);
+
+ search_view.update(cx, |search_view, cx| {
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
+ search_view.search(cx);
+ });
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.display_text(cx)),
+ "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
+ );
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.all_text_background_highlights(cx)),
+ &[
+ (
+ DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
+ Color::red()
+ ),
+ (
+ DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
+ Color::red()
+ ),
+ (
+ DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
+ Color::red()
+ )
+ ]
+ );
+ assert_eq!(search_view.active_match_index, Some(0));
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
+ );
+
+ search_view.select_match(Direction::Next, cx);
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.active_match_index, Some(1));
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
+ );
+ search_view.select_match(Direction::Next, cx);
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.active_match_index, Some(2));
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
+ );
+ search_view.select_match(Direction::Next, cx);
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.active_match_index, Some(0));
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
+ );
+ search_view.select_match(Direction::Prev, cx);
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.active_match_index, Some(2));
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
+ );
+ search_view.select_match(Direction::Prev, cx);
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.active_match_index, Some(1));
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
+ [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_project_search_focus(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.background());
+ fs.insert_tree(
+ "/dir",
+ json!({
+ "one.rs": "const ONE: usize = 1;",
+ "two.rs": "const TWO: usize = one::ONE + one::ONE;",
+ "three.rs": "const THREE: usize = one::ONE + two::TWO;",
+ "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
+ let window = cx.add_window(|cx| Workspace::test_new(project, cx));
+ let workspace = window.root(cx);
+
+ let active_item = cx.read(|cx| {
+ workspace
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ });
+ assert!(
+ active_item.is_none(),
+ "Expected no search panel to be active, but got: {active_item:?}"
+ );
+
+ workspace.update(cx, |workspace, cx| {
+ ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
+ });
+
+ let Some(search_view) = cx.read(|cx| {
+ workspace
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ }) else {
+ panic!("Search view expected to appear after new search event trigger")
+ };
+ let search_view_id = search_view.id();
+
+ cx.spawn(|mut cx| async move {
+ window.dispatch_action(search_view_id, &ToggleFocus, &mut cx);
+ })
+ .detach();
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert!(
+ search_view.query_editor.is_focused(cx),
+ "Empty search view should be focused after the toggle focus event: no results panel to focus on",
+ );
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ let query_editor = &search_view.query_editor;
+ assert!(
+ query_editor.is_focused(cx),
+ "Search view should be focused after the new search view is activated",
+ );
+ let query_text = query_editor.read(cx).text(cx);
+ assert!(
+ query_text.is_empty(),
+ "New search query should be empty but got '{query_text}'",
+ );
+ let results_text = search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.display_text(cx));
+ assert!(
+ results_text.is_empty(),
+ "Empty search view should have no results but got '{results_text}'"
+ );
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ search_view.query_editor.update(cx, |query_editor, cx| {
+ query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", cx)
+ });
+ search_view.search(cx);
+ });
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ let results_text = search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.display_text(cx));
+ assert!(
+ results_text.is_empty(),
+ "Search view for mismatching query should have no results but got '{results_text}'"
+ );
+ assert!(
+ search_view.query_editor.is_focused(cx),
+ "Search view should be focused after mismatching query had been used in search",
+ );
+ });
+ cx.spawn(
+ |mut cx| async move { window.dispatch_action(search_view_id, &ToggleFocus, &mut cx) },
+ )
+ .detach();
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert!(
+ search_view.query_editor.is_focused(cx),
+ "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
+ );
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
+ search_view.search(cx);
+ });
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.display_text(cx)),
+ "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
+ "Search view results should match the query"
+ );
+ assert!(
+ search_view.results_editor.is_focused(cx),
+ "Search view with mismatching query should be focused after search results are available",
+ );
+ });
+ cx.spawn(|mut cx| async move {
+ window.dispatch_action(search_view_id, &ToggleFocus, &mut cx);
+ })
+ .detach();
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert!(
+ search_view.results_editor.is_focused(cx),
+ "Search view with matching query should still have its results editor focused after the toggle focus event",
+ );
+ });
+
+ workspace.update(cx, |workspace, cx| {
+ ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "two", "Query should be updated to first search result after search view 2nd open in a row");
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.display_text(cx)),
+ "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
+ "Results should be unchanged after search view 2nd open in a row"
+ );
+ assert!(
+ search_view.query_editor.is_focused(cx),
+ "Focus should be moved into query editor again after search view 2nd open in a row"
+ );
+ });
+
+ cx.spawn(|mut cx| async move {
+ window.dispatch_action(search_view_id, &ToggleFocus, &mut cx);
+ })
+ .detach();
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert!(
+ search_view.results_editor.is_focused(cx),
+ "Search view with matching query should switch focus to the results editor after the toggle focus event",
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_new_project_search_in_directory(
+ deterministic: Arc<Deterministic>,
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.background());
+ fs.insert_tree(
+ "/dir",
+ json!({
+ "a": {
+ "one.rs": "const ONE: usize = 1;",
+ "two.rs": "const TWO: usize = one::ONE + one::ONE;",
+ },
+ "b": {
+ "three.rs": "const THREE: usize = one::ONE + two::TWO;",
+ "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
+ },
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
+ let worktree_id = project.read_with(cx, |project, cx| {
+ project.worktrees(cx).next().unwrap().read(cx).id()
+ });
+ let workspace = cx
+ .add_window(|cx| Workspace::test_new(project, cx))
+ .root(cx);
+
+ let active_item = cx.read(|cx| {
+ workspace
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ });
+ assert!(
+ active_item.is_none(),
+ "Expected no search panel to be active, but got: {active_item:?}"
+ );
+
+ let one_file_entry = cx.update(|cx| {
+ workspace
+ .read(cx)
+ .project()
+ .read(cx)
+ .entry_for_path(&(worktree_id, "a/one.rs").into(), cx)
+ .expect("no entry for /a/one.rs file")
+ });
+ assert!(one_file_entry.is_file());
+ workspace.update(cx, |workspace, cx| {
+ ProjectSearchView::new_search_in_directory(workspace, &one_file_entry, cx)
+ });
+ let active_search_entry = cx.read(|cx| {
+ workspace
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ });
+ assert!(
+ active_search_entry.is_none(),
+ "Expected no search panel to be active for file entry"
+ );
+
+ let a_dir_entry = cx.update(|cx| {
+ workspace
+ .read(cx)
+ .project()
+ .read(cx)
+ .entry_for_path(&(worktree_id, "a").into(), cx)
+ .expect("no entry for /a/ directory")
+ });
+ assert!(a_dir_entry.is_dir());
+ workspace.update(cx, |workspace, cx| {
+ ProjectSearchView::new_search_in_directory(workspace, &a_dir_entry, cx)
+ });
+
+ let Some(search_view) = cx.read(|cx| {
+ workspace
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ }) else {
+ panic!("Search view expected to appear after new search in directory event trigger")
+ };
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert!(
+ search_view.query_editor.is_focused(cx),
+ "On new search in directory, focus should be moved into query editor"
+ );
+ search_view.excluded_files_editor.update(cx, |editor, cx| {
+ assert!(
+ editor.display_text(cx).is_empty(),
+ "New search in directory should not have any excluded files"
+ );
+ });
+ search_view.included_files_editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.display_text(cx),
+ a_dir_entry.path.to_str().unwrap(),
+ "New search in directory should have included dir entry path"
+ );
+ });
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("const", cx));
+ search_view.search(cx);
+ });
+ deterministic.run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(
+ search_view
+ .results_editor
+ .update(cx, |editor, cx| editor.display_text(cx)),
+ "\n\nconst ONE: usize = 1;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
+ "New search in directory should have a filter that matches a certain directory"
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_search_query_history(cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.background());
+ fs.insert_tree(
+ "/dir",
+ json!({
+ "one.rs": "const ONE: usize = 1;",
+ "two.rs": "const TWO: usize = one::ONE + one::ONE;",
+ "three.rs": "const THREE: usize = one::ONE + two::TWO;",
+ "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
+ let window = cx.add_window(|cx| Workspace::test_new(project, cx));
+ let workspace = window.root(cx);
+ workspace.update(cx, |workspace, cx| {
+ ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
+ });
+
+ let search_view = cx.read(|cx| {
+ workspace
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .active_item()
+ .and_then(|item| item.downcast::<ProjectSearchView>())
+ .expect("Search view expected to appear after new search event trigger")
+ });
+
+ let search_bar = window.add_view(cx, |cx| {
+ let mut search_bar = ProjectSearchBar::new();
+ search_bar.set_active_pane_item(Some(&search_view), cx);
+ // search_bar.show(cx);
+ search_bar
+ });
+
+ // Add 3 search items into the history + another unsubmitted one.
+ search_view.update(cx, |search_view, cx| {
+ search_view.search_options = SearchOptions::CASE_SENSITIVE;
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("ONE", cx));
+ search_view.search(cx);
+ });
+ cx.foreground().run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
+ search_view.search(cx);
+ });
+ cx.foreground().run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("THREE", cx));
+ search_view.search(cx);
+ });
+ cx.foreground().run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ search_view.query_editor.update(cx, |query_editor, cx| {
+ query_editor.set_text("JUST_TEXT_INPUT", cx)
+ });
+ });
+ cx.foreground().run_until_parked();
+
+ // Ensure that the latest input with search settings is active.
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(
+ search_view.query_editor.read(cx).text(cx),
+ "JUST_TEXT_INPUT"
+ );
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Next history query after the latest should set the query to the empty string.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // First previous query for empty current query should set the query to the latest submitted one.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Further previous items should go over the history in reverse order.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Previous items should never go behind the first history item.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // Next items should go over the history in the original order.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ search_view.update(cx, |search_view, cx| {
+ search_view
+ .query_editor
+ .update(cx, |query_editor, cx| query_editor.set_text("TWO_NEW", cx));
+ search_view.search(cx);
+ });
+ cx.foreground().run_until_parked();
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+
+ // New search input should add another entry to history and move the selection to the end of the history.
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.previous_history_query(&PreviousHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.next_history_query(&NextHistoryQuery, cx);
+ });
+ search_view.update(cx, |search_view, cx| {
+ assert_eq!(search_view.query_editor.read(cx).text(cx), "");
+ assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
+ });
+ }
+
+ pub fn init_test(cx: &mut TestAppContext) {
+ cx.foreground().forbid_parking();
+ let fonts = cx.font_cache();
+ let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default);
+ theme.search.match_background = Color::red();
+
+ cx.update(|cx| {
+ cx.set_global(SettingsStore::test(cx));
+ cx.set_global(ActiveSearches::default());
+ settings::register::<SemanticIndexSettings>(cx);
+
+ theme::init((), cx);
+ cx.update_global::<SettingsStore, _, _>(|store, _| {
+ let mut settings = store.get::<ThemeSettings>(None).clone();
+ settings.theme = Arc::new(theme);
+ store.override_global(settings)
+ });
+
+ language::init(cx);
+ client::init_settings(cx);
+ editor::init(cx);
+ workspace::init_settings(cx);
+ Project::init_settings(cx);
+ super::init(cx);
+ });
+ }
+}
@@ -0,0 +1,115 @@
+use bitflags::bitflags;
+pub use buffer_search::BufferSearchBar;
+use gpui::{actions, Action, AnyElement, AppContext, Component, Element, Svg, View};
+pub use mode::SearchMode;
+use project::search::SearchQuery;
+use ui::ButtonVariant;
+//pub use project_search::{ProjectSearchBar, ProjectSearchView};
+// use theme::components::{
+// action_button::Button, svg::Svg, ComponentExt, IconButtonStyle, ToggleIconButtonStyle,
+// };
+
+pub mod buffer_search;
+mod history;
+mod mode;
+//pub mod project_search;
+pub(crate) mod search_bar;
+
+pub fn init(cx: &mut AppContext) {
+ buffer_search::init(cx);
+ //project_search::init(cx);
+}
+
+actions!(
+ CycleMode,
+ ToggleWholeWord,
+ ToggleCaseSensitive,
+ ToggleReplace,
+ SelectNextMatch,
+ SelectPrevMatch,
+ SelectAllMatches,
+ NextHistoryQuery,
+ PreviousHistoryQuery,
+ ActivateTextMode,
+ ActivateSemanticMode,
+ ActivateRegexMode,
+ ReplaceAll,
+ ReplaceNext,
+);
+
+bitflags! {
+ #[derive(Default)]
+ pub struct SearchOptions: u8 {
+ const NONE = 0b000;
+ const WHOLE_WORD = 0b001;
+ const CASE_SENSITIVE = 0b010;
+ }
+}
+
+impl SearchOptions {
+ pub fn label(&self) -> &'static str {
+ match *self {
+ SearchOptions::WHOLE_WORD => "Match Whole Word",
+ SearchOptions::CASE_SENSITIVE => "Match Case",
+ _ => panic!("{:?} is not a named SearchOption", self),
+ }
+ }
+
+ pub fn icon(&self) -> ui::Icon {
+ match *self {
+ SearchOptions::WHOLE_WORD => ui::Icon::WholeWord,
+ SearchOptions::CASE_SENSITIVE => ui::Icon::CaseSensitive,
+ _ => panic!("{:?} is not a named SearchOption", self),
+ }
+ }
+
+ pub fn to_toggle_action(&self) -> Box<dyn Action + Sync + Send + 'static> {
+ match *self {
+ SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord),
+ SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive),
+ _ => panic!("{:?} is not a named SearchOption", self),
+ }
+ }
+
+ pub fn none() -> SearchOptions {
+ SearchOptions::NONE
+ }
+
+ pub fn from_query(query: &SearchQuery) -> SearchOptions {
+ let mut options = SearchOptions::NONE;
+ options.set(SearchOptions::WHOLE_WORD, query.whole_word());
+ options.set(SearchOptions::CASE_SENSITIVE, query.case_sensitive());
+ options
+ }
+
+ pub fn as_button<V: 'static>(&self, active: bool) -> impl Component<V> {
+ ui::IconButton::new(0, self.icon())
+ .on_click({
+ let action = self.to_toggle_action();
+ move |_: &mut V, cx| {
+ cx.dispatch_action(action.boxed_clone());
+ }
+ })
+ .variant(ui::ButtonVariant::Ghost)
+ .when(active, |button| button.variant(ButtonVariant::Filled))
+ }
+}
+
+fn toggle_replace_button<V: 'static>(active: bool) -> impl Component<V> {
+ // todo: add toggle_replace button
+ ui::IconButton::new(0, ui::Icon::Replace)
+ .on_click(|_: &mut V, cx| {
+ cx.dispatch_action(Box::new(ToggleReplace));
+ })
+ .variant(ui::ButtonVariant::Ghost)
+ .when(active, |button| button.variant(ButtonVariant::Filled))
+}
+
+fn replace_action<V: 'static>(
+ action: impl Action + 'static + Send + Sync,
+ name: &'static str,
+) -> impl Component<V> {
+ ui::IconButton::new(0, ui::Icon::Replace).on_click(move |_: &mut V, cx| {
+ cx.dispatch_action(action.boxed_clone());
+ })
+}
@@ -0,0 +1,177 @@
+use std::borrow::Cow;
+
+use gpui::{
+ div, Action, AnyElement, Component, CursorStyle, Element, MouseButton, MouseDownEvent, Svg,
+ View, ViewContext,
+};
+use theme::ActiveTheme;
+use ui::Label;
+use workspace::searchable::Direction;
+
+use crate::{
+ mode::{SearchMode, Side},
+ SelectNextMatch, SelectPrevMatch,
+};
+
+pub(super) fn render_nav_button<V: 'static>(
+ icon: &'static str,
+ direction: Direction,
+ active: bool,
+ on_click: impl Fn(MouseDownEvent, &mut V, &mut ViewContext<V>) + 'static,
+ cx: &mut ViewContext<V>,
+) -> impl Component<V> {
+ let action: Box<dyn Action>;
+ let tooltip;
+
+ match direction {
+ Direction::Prev => {
+ action = Box::new(SelectPrevMatch);
+ tooltip = "Select Previous Match";
+ }
+ Direction::Next => {
+ action = Box::new(SelectNextMatch);
+ tooltip = "Select Next Match";
+ }
+ };
+ // let tooltip_style = cx.theme().tooltip.clone();
+ // let cursor_style = if active {
+ // CursorStyle::PointingHand
+ // } else {
+ // CursorStyle::default()
+ // };
+ // enum NavButton {}
+ div()
+ // MouseEventHandler::new::<NavButton, _>(direction as usize, cx, |state, cx| {
+ // let theme = cx.theme();
+ // let style = theme
+ // .search
+ // .nav_button
+ // .in_state(active)
+ // .style_for(state)
+ // .clone();
+ // let mut container_style = style.container.clone();
+ // let label = Label::new(icon, style.label.clone()).aligned().contained();
+ // container_style.corner_radii = match direction {
+ // Direction::Prev => CornerRadii {
+ // bottom_right: 0.,
+ // top_right: 0.,
+ // ..container_style.corner_radii
+ // },
+ // Direction::Next => CornerRadii {
+ // bottom_left: 0.,
+ // top_left: 0.,
+ // ..container_style.corner_radii
+ // },
+ // };
+ // if direction == Direction::Prev {
+ // // Remove right border so that when both Next and Prev buttons are
+ // // next to one another, there's no double border between them.
+ // container_style.border.right = false;
+ // }
+ // label.with_style(container_style)
+ // })
+ // .on_click(MouseButton::Left, on_click)
+ // .with_cursor_style(cursor_style)
+ // .with_tooltip::<NavButton>(
+ // direction as usize,
+ // tooltip.to_string(),
+ // Some(action),
+ // tooltip_style,
+ // cx,
+ // )
+ // .into_any()
+}
+
+pub(crate) fn render_search_mode_button<V: 'static>(
+ mode: SearchMode,
+ side: Option<Side>,
+ is_active: bool,
+ //on_click: impl Fn(MouseClick, &mut V, &mut ViewContext<V>) + 'static,
+ cx: &mut ViewContext<V>,
+) -> impl Component<V> {
+ //let tooltip_style = cx.theme().tooltip.clone();
+ enum SearchModeButton {}
+ div()
+ // MouseEventHandler::new::<SearchModeButton, _>(mode.region_id(), cx, |state, cx| {
+ // let theme = cx.theme();
+ // let style = theme
+ // .search
+ // .mode_button
+ // .in_state(is_active)
+ // .style_for(state)
+ // .clone();
+
+ // let mut container_style = style.container;
+ // if let Some(button_side) = side {
+ // if button_side == Side::Left {
+ // container_style.border.left = true;
+ // container_style.corner_radii = CornerRadii {
+ // bottom_right: 0.,
+ // top_right: 0.,
+ // ..container_style.corner_radii
+ // };
+ // } else {
+ // container_style.border.left = false;
+ // container_style.corner_radii = CornerRadii {
+ // bottom_left: 0.,
+ // top_left: 0.,
+ // ..container_style.corner_radii
+ // };
+ // }
+ // } else {
+ // container_style.border.left = false;
+ // container_style.corner_radii = CornerRadii::default();
+ // }
+
+ // Label::new(mode.label(), style.text)
+ // .aligned()
+ // .contained()
+ // .with_style(container_style)
+ // .constrained()
+ // .with_height(theme.search.search_bar_row_height)
+ // })
+ // .on_click(MouseButton::Left, on_click)
+ // .with_cursor_style(CursorStyle::PointingHand)
+ // .with_tooltip::<SearchModeButton>(
+ // mode.region_id(),
+ // mode.tooltip_text().to_owned(),
+ // Some(mode.activate_action()),
+ // tooltip_style,
+ // cx,
+ // )
+ // .into_any()
+}
+
+pub(crate) fn render_option_button_icon<V: 'static>(
+ is_active: bool,
+ icon: &'static str,
+ id: usize,
+ label: impl Into<Cow<'static, str>>,
+ action: Box<dyn Action>,
+ //on_click: impl Fn(MouseClick, &mut V, &mut EventContext<V>) + 'static,
+ cx: &mut ViewContext<V>,
+) -> impl Component<V> {
+ //let tooltip_style = cx.theme().tooltip.clone();
+ div()
+ // MouseEventHandler::new::<V, _>(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::<V>(id, label, Some(action), tooltip_style, cx)
+ // .into_any()
+}
@@ -97,6 +97,8 @@ pub enum Icon {
BellRing,
MailOpen,
AtSign,
+ WholeWord,
+ CaseSensitive,
}
impl Icon {
@@ -155,6 +157,8 @@ impl Icon {
Icon::BellRing => "icons/bell-ring.svg",
Icon::MailOpen => "icons/mail-open.svg",
Icon::AtSign => "icons/at-sign.svg",
+ Icon::WholeWord => "icons/word_search.svg",
+ Icon::CaseSensitive => "icons/case_insensitive.svg",
}
}
}
@@ -1909,7 +1909,7 @@ impl Render for Pane {
v_stack()
.size_full()
.child(self.render_tab_bar(cx))
- .child(div() /* todo!(toolbar) */)
+ .child(self.toolbar.clone())
.child(if let Some(item) = self.active_item() {
div().flex_1().child(item.to_any())
} else {
@@ -1,7 +1,8 @@
use std::{any::Any, sync::Arc};
use gpui::{
- AnyView, AppContext, EventEmitter, Subscription, Task, View, ViewContext, WindowContext,
+ AnyView, AppContext, EventEmitter, Subscription, Task, View, ViewContext, WeakView,
+ WindowContext,
};
use project2::search::SearchQuery;
@@ -129,8 +130,7 @@ pub trait SearchableItemHandle: ItemHandle {
// todo!("here is where we need to use AnyWeakView");
impl<T: SearchableItem> SearchableItemHandle for View<T> {
fn downgrade(&self) -> Box<dyn WeakSearchableItemHandle> {
- // Box::new(self.downgrade())
- todo!()
+ Box::new(self.downgrade())
}
fn boxed_clone(&self) -> Box<dyn SearchableItemHandle> {
@@ -252,16 +252,15 @@ pub trait WeakSearchableItemHandle: WeakItemHandle {
// fn into_any(self) -> AnyWeakView;
}
-// todo!()
-// impl<T: SearchableItem> WeakSearchableItemHandle for WeakView<T> {
-// fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn SearchableItemHandle>> {
-// Some(Box::new(self.upgrade(cx)?))
-// }
+impl<T: SearchableItem> WeakSearchableItemHandle for WeakView<T> {
+ fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn SearchableItemHandle>> {
+ Some(Box::new(self.upgrade()?))
+ }
-// // fn into_any(self) -> AnyView {
-// // self.into_any()
-// // }
-// }
+ // fn into_any(self) -> AnyView {
+ // self.into_any()
+ // }
+}
impl PartialEq for Box<dyn WeakSearchableItemHandle> {
fn eq(&self, other: &Self) -> bool {
@@ -1,6 +1,7 @@
use crate::ItemHandle;
use gpui::{
- AnyView, Div, Entity, EntityId, EventEmitter, Render, View, ViewContext, WindowContext,
+ div, AnyView, Div, Entity, EntityId, EventEmitter, ParentElement, Render, View, ViewContext,
+ WindowContext,
};
pub enum ToolbarItemEvent {
@@ -55,7 +56,8 @@ impl Render for Toolbar {
type Element = Div<Self>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
- todo!()
+ //dbg!(&self.items.len());
+ div().children(self.items.iter().map(|(child, _)| child.to_any()))
}
}
@@ -68,7 +68,7 @@ use std::{
time::Duration,
};
use theme2::ActiveTheme;
-pub use toolbar::{ToolbarItemLocation, ToolbarItemView};
+pub use toolbar::{ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
use ui::{h_stack, Label};
use util::ResultExt;
use uuid::Uuid;
@@ -37,7 +37,7 @@ db = { package = "db2", path = "../db2" }
editor = { package="editor2", path = "../editor2" }
# feedback = { path = "../feedback" }
# file_finder = { path = "../file_finder" }
-# search = { path = "../search" }
+search = { package = "search2", path = "../search2" }
fs = { package = "fs2", path = "../fs2" }
fsevent = { path = "../fsevent" }
fuzzy = { path = "../fuzzy" }
@@ -194,7 +194,7 @@ fn main() {
// project_panel::init(Assets, cx);
// channel::init(&client, user_store.clone(), cx);
// diagnostics::init(cx);
- // search::init(cx);
+ search::init(cx);
// semantic_index::init(fs.clone(), http.clone(), languages.clone(), cx);
// vim::init(cx);
// terminal_view::init(cx);
@@ -8,8 +8,8 @@ mod open_listener;
pub use assets::*;
use gpui::{
- point, px, AppContext, AsyncWindowContext, Task, TitlebarOptions, WeakView, WindowBounds,
- WindowKind, WindowOptions,
+ point, px, AppContext, AsyncWindowContext, Task, TitlebarOptions, VisualContext as _, WeakView,
+ WindowBounds, WindowKind, WindowOptions,
};
pub use only_instance::*;
pub use open_listener::*;
@@ -64,8 +64,8 @@ pub fn initialize_workspace(
// todo!()
// let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace));
// toolbar.add_item(breadcrumbs, cx);
- // let buffer_search_bar = cx.add_view(BufferSearchBar::new);
- // toolbar.add_item(buffer_search_bar.clone(), cx);
+ let buffer_search_bar = cx.build_view(search::BufferSearchBar::new);
+ toolbar.add_item(buffer_search_bar.clone(), cx);
// let quick_action_bar = cx.add_view(|_| {
// QuickActionBar::new(buffer_search_bar, workspace)
// });