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.

search: Fix regex replace with lookahead and lookbehind (#61900)

Ibrahim Khan828544342d20
diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs
index 9edfc3f5879..2e82e514ff8 100644
--- a/crates/editor/src/items.rs
+++ b/crates/editor/src/items.rs
@@ -24,7 +24,7 @@ use language::{
proto::serialize_anchor as serialize_text_anchor,
};
use lsp::DiagnosticSeverity;
-use multi_buffer::{BufferOffset, MultiBufferOffset, PathKey};
+use multi_buffer::{BufferOffset, MultiBufferOffset, MultiBufferRow, PathKey};
use project::{
File, Project, ProjectItem as _, ProjectPath, git_store::GitStore, lsp_store::FormatTrigger,
project_settings::ProjectSettings, search::SearchQuery,
@@ -1652,6 +1652,41 @@ impl Editor {
}
}
+// Replace-all commonly expands several hits against the same line.
+#[derive(Default)]
+struct SearchHitContext {
+ row: Option<u32>,
+ text: String,
+}
+
+impl SearchHitContext {
+ fn for_hit(
+ &mut self,
+ snapshot: &MultiBufferSnapshot,
+ hit: &Range<Anchor>,
+ ) -> (&str, Range<usize>) {
+ let start = hit.start.to_point(snapshot);
+ let end = hit.end.to_point(snapshot);
+ let range = if start.row == end.row {
+ if self.row != Some(start.row) {
+ self.text.clear();
+ self.text.extend(snapshot.text_for_range(
+ Point::new(start.row, 0)
+ ..Point::new(start.row, snapshot.line_len(MultiBufferRow(start.row))),
+ ));
+ self.row = Some(start.row);
+ }
+ start.column as usize..end.column as usize
+ } else {
+ self.row = None;
+ self.text.clear();
+ self.text.extend(snapshot.text_for_range(start..end));
+ 0..self.text.len()
+ };
+ (&self.text, range)
+ }
+}
+
impl SearchableItem for Editor {
type Match = Range<Anchor>;
@@ -1837,19 +1872,20 @@ impl SearchableItem for Editor {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let text = self.buffer.read(cx);
- let text = text.snapshot(cx);
- let text = text.text_for_range(identifier.clone()).collect::<Vec<_>>();
- let text: Cow<_> = if text.len() == 1 {
- text.first().cloned().unwrap().into()
+ let replacement = if query.replacement_requires_context() {
+ let snapshot = self.buffer.read(cx).snapshot(cx);
+ let mut context = SearchHitContext::default();
+ let (line, hit) = context.for_hit(&snapshot, identifier);
+ query
+ .replacement_for(line, hit)
+ .map(|replacement| Arc::<str>::from(&*replacement))
} else {
- let joined_chunks = text.concat();
- joined_chunks.into()
+ query.replacement().map(Arc::<str>::from)
};
- if let Some(replacement) = query.replacement_for(&text) {
+ if let Some(replacement) = replacement {
self.transact(window, cx, |this, _, cx| {
- this.edit([(identifier.clone(), Arc::from(&*replacement))], cx);
+ this.edit([(identifier.clone(), replacement)], cx);
});
}
}
@@ -1861,26 +1897,18 @@ impl SearchableItem for Editor {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let text = self.buffer.read(cx);
- let text = text.snapshot(cx);
+ let snapshot = self.buffer.read(cx).snapshot(cx);
let mut edits = vec![];
// A regex might have replacement variables so we cannot apply
// the same replacement to all matches
- if query.is_regex() {
+ if query.replacement_requires_context() {
+ let mut context = SearchHitContext::default();
edits = matches
.filter_map(|m| {
- let text = text.text_for_range(m.clone()).collect::<Vec<_>>();
-
- let text: Cow<_> = if text.len() == 1 {
- text.first().cloned().unwrap().into()
- } else {
- let joined_chunks = text.concat();
- joined_chunks.into()
- };
-
+ let (line, hit) = context.for_hit(&snapshot, m);
query
- .replacement_for(&text)
+ .replacement_for(line, hit)
.map(|replacement| (m.clone(), Arc::from(&*replacement)))
})
.collect();
diff --git a/crates/project/src/search.rs b/crates/project/src/search.rs
index d11f82d0091..9fdf3a16611 100644
--- a/crates/project/src/search.rs
+++ b/crates/project/src/search.rs
@@ -457,8 +457,8 @@ impl SearchQuery {
}
}
}
- /// Replaces search hits if replacement is set. `text` is assumed to be a string that matches this `SearchQuery` exactly, without any leftovers on either side.
- pub fn replacement_for<'a>(&self, text: &'a str) -> Option<Cow<'a, str>> {
+ /// Expands `hit` against its line so lookaround assertions retain context.
+ pub fn replacement_for<'a>(&self, line: &'a str, hit: Range<usize>) -> Option<Cow<'a, str>> {
match self {
SearchQuery::Text { replacement, .. }
| SearchQuery::Regex {
@@ -484,7 +484,20 @@ impl SearchQuery {
x => unreachable!("Unexpected escape sequence: {}", x),
},
);
- Some(regex.replace(text, replacement))
+ let captures = regex
+ .captures_from_pos(line, hit.start)
+ .ok()
+ .flatten()
+ .filter(|captures| captures.get(0).is_some_and(|m| m.range() == hit));
+ let Some(captures) = captures else {
+ // The pattern is not guaranteed to match the whole line, for instance when
+ // searching within a selection that starts or ends mid-line, so fall back to
+ // matching the hit on its own.
+ return Some(regex.replace(line.get(hit)?, replacement));
+ };
+ let mut replaced = String::new();
+ captures.expand(&replacement, &mut replaced);
+ Some(Cow::Owned(replaced))
}
SearchQuery::Regex {
@@ -615,6 +628,10 @@ impl SearchQuery {
matches!(self, Self::Regex { .. })
}
+ pub fn replacement_requires_context(&self) -> bool {
+ matches!(self, Self::Regex { escaped: false, .. })
+ }
+
pub fn files_to_include(&self) -> &PathMatcher {
self.as_inner().files_to_include()
}
diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs
index c883cca47b2..dbd0cfe6cd0 100644
--- a/crates/search/src/buffer_search.rs
+++ b/crates/search/src/buffer_search.rs
@@ -3176,6 +3176,83 @@ mod tests {
.await;
}
+ #[gpui::test]
+ async fn test_replace_with_lookaround(cx: &mut TestAppContext) {
+ let (editor, search_bar, cx) = init_test(cx);
+
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("316227766016837933199\n", window, cx)
+ });
+
+ run_replacement_test(ReplacementTestParams {
+ editor: &editor,
+ search_bar: &search_bar,
+ cx,
+ search_text: r"(\d)(?=(\d{4})+$)",
+ search_options: Some(SearchOptions::REGEX),
+ replacement_text: "$1,",
+ replace_all: true,
+ expected_text: "3,1622,7766,0168,3793,3199\n".to_string(),
+ })
+ .await;
+
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("Xfoo\nbar\n", window, cx)
+ });
+
+ run_replacement_test(ReplacementTestParams {
+ editor: &editor,
+ search_bar: &search_bar,
+ cx,
+ search_text: r"foo\n(?<=Xfoo\n)bar",
+ search_options: Some(SearchOptions::REGEX),
+ replacement_text: "BAZ",
+ replace_all: true,
+ expected_text: "Xfoo\nbar\n".to_string(),
+ })
+ .await;
+
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("food: bar\nfoo: bar\n", window, cx)
+ });
+
+ run_replacement_test(ReplacementTestParams {
+ editor: &editor,
+ search_bar: &search_bar,
+ cx,
+ search_text: r"(?<=foo: )bar",
+ search_options: Some(SearchOptions::REGEX),
+ replacement_text: "BAZ",
+ replace_all: false,
+ expected_text: "food: bar\nfoo: BAZ\n".to_string(),
+ })
+ .await;
+ }
+
+ #[gpui::test]
+ async fn test_replace_with_lookaround_in_multibuffer(cx: &mut TestAppContext) {
+ let (editor, search_bar, cx) = init_multibuffer_test(cx);
+
+ run_replacement_test(ReplacementTestParams {
+ editor: &editor,
+ search_bar: &search_bar,
+ cx,
+ search_text: r"\w+(?= expression)",
+ search_options: Some(SearchOptions::REGEX),
+ replacement_text: "SOME",
+ replace_all: true,
+ expected_text: r#"
+ A SOME expression (shortened as regex or regexp;[1] also referred to as
+ SOME 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.
+ Some Additional text with the term SOME expression in it.
+ There two lines."#
+ .unindent(),
+ })
+ .await;
+ }
+
#[gpui::test]
async fn test_deploy_replace_focuses_replacement_editor(cx: &mut TestAppContext) {
init_globals(cx);