Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
text_finder: Escape the search query in regex mode (#65321)
Serhii Rubanskyib01d137ad733
diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rsindex 51be4def1a9..904516f0743 100644--- a/crates/search/src/project_search.rs+++ b/crates/search/src/project_search.rs@@ -1771,17 +1771,6 @@ impl ProjectSearchView {window: &mut Window,cx: &mut Context<Workspace>,) {- enum QuerySeed {- /// Content of the buffer search bar: already query syntax, with- /// escaping already applied if it was seeded in regex mode, so it- /// must never be re-escaped. It's carried over verbatim even if- /// the buffer search's mode differs from the project search's.- Query(String),- /// Raw text from the editor's selection or the word under the- /// cursor, so it gets escaped when entering a regex query.- Text(String),- }-let query_seed = workspace.active_item(cx).and_then(|item| {if let Some(buffer_search_query) = buffer_search_query(workspace, item.as_ref(), cx) {return Some(QuerySeed::Query(buffer_search_query));@@ -1847,15 +1836,7 @@ impl ProjectSearchView {if let Some(query) = action.query.as_deref().filter(|query| !query.is_empty()) {search.set_query(query, window, cx);} else if let Some(query_seed) = query_seed {- let query = match query_seed {- QuerySeed::Query(query) => query,- QuerySeed::Text(text)- if search.search_options.contains(SearchOptions::REGEX) =>- {- regex::escape(&text)- }- QuerySeed::Text(text) => text,- };+ let query = query_seed.into_query(search.search_options);search.set_query(&query, window, cx);}if let Some(included_files) = action.included_files.as_deref() {@@ -2583,6 +2564,26 @@ impl ProjectSearchView {}}+pub(crate) enum QuerySeed {+ /// Content of a search bar: already query syntax, with escaping already+ /// applied if it was seeded in regex mode, so it must never be re-escaped.+ /// It's carried over verbatim even if the source search's mode differs.+ Query(String),+ /// Raw text from a selection or the word under the cursor, so it gets+ /// escaped when entering a regex query.+ Text(String),+}++impl QuerySeed {+ pub(crate) fn into_query(self, options: SearchOptions) -> String {+ match self {+ QuerySeed::Query(query) => query,+ QuerySeed::Text(text) if options.contains(SearchOptions::REGEX) => regex::escape(&text),+ QuerySeed::Text(text) => text,+ }+ }+}+pub(crate) fn buffer_search_query(workspace: &mut Workspace,item: &dyn ItemHandle,diff --git a/crates/search/src/text_finder.rs b/crates/search/src/text_finder.rsindex c2b15fa2522..f7935091d6f 100644--- a/crates/search/src/text_finder.rs+++ b/crates/search/src/text_finder.rs@@ -24,7 +24,10 @@ mod render;use delegate::{Delegate, matches_to_multibuffer};use util::ResultExt as _;-use crate::{ProjectSearchView, SearchOptions, text_finder::delegate::PopulateProjectSearch};+use crate::{+ ProjectSearchView, SearchOptions, project_search::QuerySeed,+ text_finder::delegate::PopulateProjectSearch,+};actions!(text_finder, [ToProjectSearch, Fold, Unfold, ToggleFoldAll]);@@ -81,7 +84,7 @@ impl TextFinderDb {/// `None` only the first time the finder is used in a workspace, leaving the filters at their/// setting-derived defaults.pub(crate) struct SearchSeed {- query: String,+ query: QuerySeed,options: Option<SearchOptions>,}@@ -111,7 +114,7 @@ fn load_last_search(workspace_id: Option<WorkspaceId>, cx: &App) -> Option<Searcreturn None;}Some(SearchSeed {- query,+ query: QuerySeed::Query(query),options: Some(SearchOptions::from_bits_truncate(search_options as u8)),})}@@ -340,7 +343,7 @@ impl TextFinder {workspace: &mut Workspace,window: &mut Window,cx: &mut Context<Workspace>,- ) -> Option<String> {+ ) -> Option<QuerySeed> {let focused_item = workspace.focused_pane(window, cx).read(cx).active_item();let active_item = workspace.active_item(cx)@@ -366,23 +369,23 @@ impl TextFinder {item: &dyn ItemHandle,window: &mut Window,cx: &mut Context<Workspace>,- ) -> Option<String> {+ ) -> Option<QuerySeed> {if let Some(project_search) = item.downcast::<ProjectSearchView>() {let query = project_search.read(cx).search_query_text(cx);if !query.is_empty() {- return Some(query);+ return Some(QuerySeed::Query(query));}}if let Some(query) = crate::project_search::buffer_search_query(workspace, item, cx) {- return Some(query);+ return Some(QuerySeed::Query(query));}if let Some(searchable_item) = item.to_searchable_item_handle(cx) {let query =searchable_item.query_suggestion(Some(SeedQuerySetting::Selection), window, cx);if !query.is_empty() {- return Some(query);+ return Some(QuerySeed::Text(query));}}@@ -445,7 +448,8 @@ impl TextFinder {if let Some(options) = seed_query.options {picker.delegate.search_options = options;}- picker.set_query(&seed_query.query, window, cx);+ let query = seed_query.query.into_query(picker.delegate.search_options);+ picker.set_query(&query, window, cx);picker.select_query(window, cx);}});@@ -578,7 +582,7 @@ mod tests {let cx = &mut VisualTestContext::from_window(window.into(), cx);let seed_query = SearchSeed {- query: "ONE".to_string(),+ query: QuerySeed::Query("ONE".to_string()),options: None,};workspace@@ -624,7 +628,7 @@ mod tests {let initial_query = "unique_search_query";let seed_query = SearchSeed {- query: initial_query.to_string(),+ query: QuerySeed::Query(initial_query.to_string()),options: None,};workspace@@ -640,10 +644,10 @@ mod tests {let seed_query = workspace.update_in(cx, |workspace, window, cx| {TextFinder::seed_query(workspace, window, cx)});- assert_eq!(- seed_query.as_ref().map(|seed| seed.query.as_str()),- Some(initial_query)- );+ assert!(matches!(+ seed_query.as_ref().map(|seed| &seed.query),+ Some(QuerySeed::Query(query)) if query == initial_query+ ));workspace.update_in(cx, |_, window, cx| TextFinder::open(seed_query, window, cx)).await;@@ -671,7 +675,7 @@ mod tests {}#[gpui::test]- async fn test_query_highlighted_as_regex_while_regex_filter_is_on(cx: &mut TestAppContext) {+ async fn test_selection_seed_escaped_and_highlighted_in_regex_mode(cx: &mut TestAppContext) {init_test(cx);let fs = FakeFs::new(cx.background_executor.clone());@@ -693,10 +697,18 @@ mod tests {.unwrap();let cx = &mut VisualTestContext::from_window(window.into(), cx);- let seed_query = SearchSeed {- query: "O.E".to_string(),- options: Some(SearchOptions::REGEX),- };+ let seed_query = workspace.update_in(cx, |workspace, window, cx| {+ let editor = cx.new(|cx| {+ let mut editor = Editor::multi_line(window, cx);+ editor.set_text("O.E(", window, cx);+ editor.select_all(&editor::actions::SelectAll, window, cx);+ editor+ });+ workspace.add_item_to_active_pane(Box::new(editor), None, true, window, cx);+ let mut seed_query = TextFinder::seed_query(workspace, window, cx).unwrap();+ seed_query.options = Some(SearchOptions::REGEX);+ seed_query+ });workspace.update_in(cx, |_, window, cx| {TextFinder::open(Some(seed_query), window, cx)@@ -714,6 +726,7 @@ mod tests {});picker.read_with(cx, |picker, cx| {+ assert_eq!(picker.query(cx), "O\\.E\\(");assert_eq!(query_language_name(&picker.delegate, cx).as_deref(),Some("regex"),