Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into vim-search

Conrad Irwinf887a17ffea2
diff --cc Cargo.lock
index c6f5e70c9f0,57604d28b8e..387842287ed
--- a/Cargo.lock
+++ b/Cargo.lock
@@@ -6428,7 -6419,6 +6419,7 @@@ name = "search
version = "0.1.0"
dependencies = [
"anyhow",
- "bitflags",
++ "bitflags 1.3.2",
"client",
"collections",
"editor",
diff --cc crates/editor/src/items.rs
index 6b2cdacaa27,9498be18448..c7a93e754ac
--- a/crates/editor/src/items.rs
+++ b/crates/editor/src/items.rs
@@@ -936,12 -946,16 +946,17 @@@ impl SearchableItem for Editor
cx: &mut ViewContext<Self>,
) {
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([matches[index].clone()])
- });
+ s.select_ranges([range]);
+ })
}
+ fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
+ self.unfold_ranges(matches.clone(), false, false, cx);
+ self.change_selections(None, cx, |s| s.select_ranges(matches));
+ }
+
fn match_index_for_direction(
&mut self,
matches: &Vec<Range<Anchor>>,
@@@ -951,8 -964,16 +966,17 @@@
cx: &mut ViewContext<Self>,
) -> usize {
let buffer = self.buffer().read(cx).snapshot(cx);
- let cursor = self.selections.newest_anchor().head();
- if count.is_none() && matches[current_index].start.cmp(&cursor, &buffer).is_gt() {
+ let current_index_position = if self.selections.disjoint_anchors().len() == 1 {
+ self.selections.newest_anchor().head()
+ } else {
+ matches[current_index].start
+ };
- if matches[current_index]
- .start
- .cmp(&current_index_position, &buffer)
- .is_gt()
++ if count.is_none()
++ && matches[current_index]
++ .start
++ .cmp(&current_index_position, &buffer)
++ .is_gt()
+ {
if direction == Direction::Prev {
if current_index == 0 {
current_index = matches.len() - 1;
@@@ -960,7 -981,11 +984,12 @@@
current_index -= 1;
}
}
- } else if count.is_none() && matches[current_index].end.cmp(&cursor, &buffer).is_lt() {
- } else if matches[current_index]
- .end
- .cmp(&current_index_position, &buffer)
- .is_lt()
++ } else if count.is_none()
++ && matches[current_index]
++ .end
++ .cmp(&current_index_position, &buffer)
++ .is_lt()
+ {
if direction == Direction::Next {
current_index = 0;
}
diff --cc crates/search/src/buffer_search.rs
index 2bd765f8bbd,f6466c85af3..7d50794108c
--- a/crates/search/src/buffer_search.rs
+++ b/crates/search/src/buffer_search.rs
@@@ -1,6 -1,6 +1,6 @@@
use crate::{
- SearchOptions, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,
- ToggleWholeWord,
- SearchOption, SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive,
++ SearchOptions, SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive,
+ ToggleRegex, ToggleWholeWord,
};
use collections::HashMap;
use editor::Editor;
@@@ -41,22 -39,25 +41,24 @@@ pub fn init(cx: &mut AppContext)
cx.add_action(BufferSearchBar::focus_editor);
cx.add_action(BufferSearchBar::select_next_match);
cx.add_action(BufferSearchBar::select_prev_match);
+ cx.add_action(BufferSearchBar::select_all_matches);
cx.add_action(BufferSearchBar::select_next_match_on_pane);
cx.add_action(BufferSearchBar::select_prev_match_on_pane);
+ cx.add_action(BufferSearchBar::select_all_matches_on_pane);
cx.add_action(BufferSearchBar::handle_editor_cancel);
- add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
- add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
- add_toggle_option_action::<ToggleRegex>(SearchOption::Regex, cx);
+ add_toggle_option_action::<ToggleCaseSensitive>(SearchOptions::CASE_SENSITIVE, cx);
+ add_toggle_option_action::<ToggleWholeWord>(SearchOptions::WHOLE_WORD, cx);
+ add_toggle_option_action::<ToggleRegex>(SearchOptions::REGEX, cx);
}
-fn add_toggle_option_action<A: Action>(option: SearchOption, cx: &mut AppContext) {
+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::<BufferSearchBar>() {
- if search_bar.update(cx, |search_bar, cx| search_bar.show(false, false, cx)) {
- search_bar.update(cx, |search_bar, cx| {
+ search_bar.update(cx, |search_bar, cx| {
+ if search_bar.show(cx) {
search_bar.toggle_search_option(option, cx);
- });
- return;
- }
+ }
+ });
}
cx.propagate_action();
});
@@@ -67,11 -68,12 +69,11 @@@ pub struct BufferSearchBar
active_searchable_item: Option<Box<dyn SearchableItemHandle>>,
active_match_index: Option<usize>,
active_searchable_item_subscription: Option<Subscription>,
- seachable_items_with_matches:
+ searchable_items_with_matches:
HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,
pending_search: Option<Task<()>>,
- case_sensitive: bool,
- whole_word: bool,
- regex: bool,
+ search_options: SearchOptions,
+ default_options: SearchOptions,
query_contains_error: bool,
dismissed: bool,
}
@@@ -249,9 -252,10 +252,9 @@@ impl BufferSearchBar
active_searchable_item: None,
active_searchable_item_subscription: None,
active_match_index: None,
- seachable_items_with_matches: Default::default(),
+ searchable_items_with_matches: Default::default(),
- case_sensitive: false,
- whole_word: false,
- regex: false,
+ default_options: SearchOptions::NONE,
+ search_options: SearchOptions::NONE,
pending_search: None,
query_contains_error: false,
dismissed: true,
@@@ -288,36 -310,12 +291,36 @@@
true
}
- fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
+ 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
- .seachable_items_with_matches
++ .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.buffer().update(cx, |query_buffer, cx| {
- let len = query_buffer.len(cx);
- query_buffer.edit([(0..len, query)], None, cx);
- });
+ query_editor.select_all(&Default::default(), cx);
});
}
@@@ -530,23 -519,32 +564,37 @@@
}
fn select_prev_match(&mut self, _: &SelectPrevMatch, cx: &mut ViewContext<Self>) {
- self.select_match(Direction::Prev, cx);
+ self.select_match(Direction::Prev, None, cx);
}
+ fn select_all_matches(&mut self, _: &SelectAllMatches, cx: &mut ViewContext<Self>) {
+ if !self.dismissed {
+ 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, cx: &mut ViewContext<Self>) {
+ pub fn select_match(
+ &mut self,
+ direction: Direction,
+ count: Option<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
- .seachable_items_with_matches
+ .searchable_items_with_matches
.get(&searchable_item.downgrade())
{
- let new_match_index =
- searchable_item.match_index_for_direction(matches, index, direction, cx);
+ 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);
}
@@@ -1027,100 -1025,132 +1085,227 @@@ mod tests
});
}
- /*
+ #[gpui::test]
+ async fn test_search_with_options(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_with_options(false, false, SearchOptions::CASE_SENSITIVE, cx);
+ search_bar.search("us", cx);
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_background_highlights(cx),
+ &[(
+ DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
+ Color::red(),
+ )]
+ );
+ });
+
+ // show should return to the default options (case insensitive)
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.show(true, true, cx);
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_background_highlights(cx),
+ &[
+ (
+ DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
+ Color::red(),
+ )
+ ]
+ );
+ });
+
+ // toggling a search option (even in show_with_options mode) should update the defaults
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.search("regex", cx);
+ search_bar.show_with_options(false, false, SearchOptions::CASE_SENSITIVE, cx);
+ search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx)
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_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.show(true, true, cx);
+ });
+ editor.next_notification(cx).await;
+ editor.update(cx, |editor, cx| {
+ assert_eq!(
+ editor.all_background_highlights(cx),
+ &[(
+ DisplayPoint::new(0, 35)..DisplayPoint::new(0, 40),
+ Color::red(),
+ ),]
+ );
+ });
+
+ // removing whole word changes the search again
+ 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_background_highlights(cx),
+ &[
+ (
+ DisplayPoint::new(0, 35)..DisplayPoint::new(0, 40),
+ Color::red(),
+ ),
+ (
+ DisplayPoint::new(0, 44)..DisplayPoint::new(0, 49),
+ Color::red()
+ )
+ ]
+ );
+ });
+ }
- */
++
+ #[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, buffer_text, cx));
+ let (window_id, _root_view) = cx.add_window(|_| EmptyView);
+
+ let editor = cx.add_view(window_id, |cx| Editor::for_buffer(buffer.clone(), None, cx));
+
+ let search_bar = cx.add_view(window_id, |cx| {
+ let mut search_bar = BufferSearchBar::new(cx);
+ search_bar.set_active_pane_item(Some(&editor), cx);
+ search_bar.show(false, true, cx);
+ search_bar
+ });
+
+ search_bar.update(cx, |search_bar, cx| {
+ search_bar.set_query("a", cx);
+ });
+
+ editor.next_notification(cx).await;
+ 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| {
+ search_bar.select_all_matches(&SelectAllMatches, 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:?}"
+ );
+ });
+ search_bar.update(cx, |search_bar, _| {
+ 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);
+ 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"
+ );
+ });
+ search_bar.update(cx, |search_bar, _| {
+ 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| {
+ search_bar.select_all_matches(&SelectAllMatches, 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:?}"
+ );
+ });
+ search_bar.update(cx, |search_bar, _| {
+ 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);
+ 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"
+ );
+ });
+ search_bar.update(cx, |search_bar, _| {
+ assert_eq!(
+ search_bar.active_match_index,
+ Some(0),
+ "Match index should be updated to the previous one"
+ );
+ });
+ }
}