дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
With Regex on, Replace in Files read $1_new as a group named 1_new, which does not exist, so matches were replaced with nothing in every file. $1 now takes further digits only while the pattern has that group, and a template naming a group the pattern lacks is reported at the replacement field and changes no file. The editor's Replace now expands the same template, so $0, ${1}, $name and \$1 work there too and an optional group that did not match no longer writes undefined.
Co-authored-by: ntbang0901 <[email protected]>
325 рядки
13 KiB
Rust
325 рядки
13 KiB
Rust
use ide_core::{CancellationToken, IdeError, IdeResult, Pos, TextEdit};
|
|
use ide_search::{
|
|
LiteralKind, LiteralRange, ReplaceEvent, ReplaceHost, ReplaceTarget, SearchQuery, SearchScope, preserve_case,
|
|
preview, replace_files, replace_files_in_context, replace_in_text, replacement_edits, validate_replacement,
|
|
};
|
|
use std::collections::HashMap;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
fn query(pattern: &str) -> SearchQuery {
|
|
SearchQuery {
|
|
pattern: pattern.into(),
|
|
is_regex: false,
|
|
case_sensitive: false,
|
|
whole_word: false,
|
|
include: vec![],
|
|
exclude: vec![],
|
|
scope: SearchScope::Project,
|
|
max_matches: 0,
|
|
context: Default::default(),
|
|
include_excluded: false,
|
|
skip_large_files: false,
|
|
}
|
|
}
|
|
|
|
fn regex(pattern: &str) -> SearchQuery {
|
|
SearchQuery { is_regex: true, ..query(pattern) }
|
|
}
|
|
|
|
/// In-memory documents: each `edit_file` is one atomic step (like one undoable
|
|
/// `DocPatched`), recorded in `steps`.
|
|
#[derive(Default)]
|
|
struct FakeDocs {
|
|
files: Mutex<HashMap<PathBuf, String>>,
|
|
steps: Mutex<Vec<(PathBuf, usize)>>,
|
|
/// Cancel this token after the first edited file.
|
|
cancel_after_first: Option<CancellationToken>,
|
|
}
|
|
|
|
impl FakeDocs {
|
|
fn with(files: &[(&str, &str)]) -> Self {
|
|
let docs = Self::default();
|
|
for (p, t) in files {
|
|
docs.files.lock().unwrap().insert(PathBuf::from(p), (*t).to_owned());
|
|
}
|
|
docs
|
|
}
|
|
fn text(&self, p: &str) -> String {
|
|
self.files.lock().unwrap()[Path::new(p)].clone()
|
|
}
|
|
}
|
|
|
|
impl ReplaceHost for FakeDocs {
|
|
fn edit_file(
|
|
&self,
|
|
path: &Path,
|
|
compute: &mut dyn FnMut(&str) -> IdeResult<Vec<TextEdit>>,
|
|
) -> IdeResult<(u32, bool)> {
|
|
let mut files = self.files.lock().unwrap();
|
|
let text = files.get(path).cloned().ok_or_else(|| IdeError::not_found("no such file"))?;
|
|
let edits = compute(&text)?;
|
|
if !edits.is_empty() {
|
|
files.insert(path.to_owned(), ide_search::apply_edits(&text, &edits));
|
|
self.steps.lock().unwrap().push((path.to_owned(), edits.len()));
|
|
}
|
|
if let Some(c) = &self.cancel_after_first {
|
|
c.cancel();
|
|
}
|
|
Ok((edits.len() as u32, false))
|
|
}
|
|
}
|
|
|
|
fn targets(paths: &[&str]) -> Vec<ReplaceTarget> {
|
|
paths.iter().map(|p| ReplaceTarget { path: PathBuf::from(p).into(), only: None, skip: None }).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn edits_use_utf16_positions() {
|
|
let edits = replacement_edits("é foo\n😀 foo foo", &query("foo"), "bar", false, None).unwrap();
|
|
let starts: Vec<Pos> = edits.iter().map(|e| e.range.start).collect();
|
|
assert_eq!(starts, vec![Pos::new(0, 2), Pos::new(1, 3), Pos::new(1, 7)]);
|
|
assert_eq!(edits[0].range.end, Pos::new(0, 5));
|
|
}
|
|
|
|
#[test]
|
|
fn only_selected_matches_are_replaced() {
|
|
let text = "foo foo\nfoo";
|
|
let only = [Pos::new(0, 4), Pos::new(1, 0), Pos::new(5, 5)];
|
|
let p = preview(text, &query("foo"), "x", false, Some(&only)).unwrap();
|
|
assert_eq!(p.after, "foo x\nx");
|
|
assert_eq!(p.edits.len(), 2);
|
|
assert_eq!(p.before, text);
|
|
}
|
|
|
|
#[test]
|
|
fn regex_groups_and_escapes() {
|
|
let p = preview("let a = 1;\nlet bb = 22;", ®ex(r"(\w+) = (\d+)"), r"$2 = $1\t", false, None).unwrap();
|
|
assert_eq!(p.after, "let 1 = a\t;\nlet 22 = bb\t;");
|
|
let p = preview("k=v", ®ex("(?P<key>\\w)=(?P<val>\\w)"), "${val}=${key}", false, None).unwrap();
|
|
assert_eq!(p.after, "v=k");
|
|
}
|
|
|
|
/// Replaces through every entry point, which must agree.
|
|
fn replace_everywhere(text: &str, pattern: &str, template: &str) -> IdeResult<String> {
|
|
let q = regex(pattern);
|
|
let docs = FakeDocs::with(&[("a", text)]);
|
|
let replaced =
|
|
replace_files(&docs, &q, template, false, &targets(&["a"]), &CancellationToken::new(), &|_| {}).map(|_| ());
|
|
let preview = preview(text, &q, template, false, None).map(|p| p.after);
|
|
let in_text = replace_in_text(text, &q, template).map(|(out, _)| out);
|
|
let valid = validate_replacement(&q, template);
|
|
assert_eq!(valid.err().map(|e| e.message).as_ref(), preview.as_ref().err().map(|e| &e.message));
|
|
match &preview {
|
|
Ok(after) => {
|
|
replaced.unwrap();
|
|
assert_eq!((&docs.text("a"), &in_text.unwrap()), (after, after), "s/{pattern}/{template}/");
|
|
}
|
|
Err(e) => {
|
|
assert_eq!(replaced.err().map(|e| e.message).as_ref(), Some(&e.message), "s/{pattern}/{template}/");
|
|
assert_eq!(in_text.err().map(|e| e.message).as_ref(), Some(&e.message), "s/{pattern}/{template}/");
|
|
assert_eq!(docs.text("a"), text, "s/{pattern}/{template}/");
|
|
}
|
|
}
|
|
preview
|
|
}
|
|
|
|
#[test]
|
|
fn regex_templates() {
|
|
let ten = "(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)";
|
|
let cases = [
|
|
// `$n` takes digits only while the pattern has that group.
|
|
("foo.old();", r"(\w+)\.old", "$1_new", "foo_new();"),
|
|
("getName()", r"get(\w+)", "fetch$1Value", "fetchNameValue()"),
|
|
("a", "(a)", "$10", "a0"),
|
|
("a", "(a)", "$01", "a"),
|
|
("abcdefghij", ten, "$10$1$11", "jaa1"),
|
|
("ab", "a(b)", "<$0>", "<ab>"),
|
|
("foo.old", r"(\w+)\.old", "${1}_new", "foo_new"),
|
|
("k1", r"(?P<key>\w)(?<n_1>\d)", "$n_1-$key=${key}${n_1}", "1-k=k1"),
|
|
// A group that took no part in the match is empty.
|
|
("a", "(a)(b)?", "[$2${2}$1]", "[a]"),
|
|
("a", "(?P<x>a)|(?P<y>b)", "$x|$y", "a|"),
|
|
("a", "(a)", r"$$1 \$1 \\$1", r"$1 $1 \a"),
|
|
("a", "(a)", r"\n\t\r\w\", "\n\t\r\\w\\"),
|
|
// `$` that starts no reference is text.
|
|
("a", "(a)", "US$ $", "US$ $"),
|
|
("a", "(a)", "${ ${} ${x.y} ${1", "${ ${} ${x.y} ${1"),
|
|
];
|
|
for (text, pattern, template, expected) in cases {
|
|
assert_eq!(replace_everywhere(text, pattern, template).unwrap(), expected, "s/{pattern}/{template}/");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_groups_are_an_invalid_replacement() {
|
|
let cases = [
|
|
("$2", "2"),
|
|
("$0$1$9x", "9"),
|
|
("${2}", "2"),
|
|
("${10}", "10"),
|
|
("${99999999999999999999}", "99999999999999999999"),
|
|
("$name", "name"),
|
|
("$key_new", "key_new"),
|
|
("${key_new}", "key_new"),
|
|
("${1_new}", "1_new"),
|
|
];
|
|
for (template, group) in cases {
|
|
// The template is checked before any match is looked for.
|
|
for text in ["a a", "zzz"] {
|
|
let err = replace_everywhere(text, "(?P<key>a)", template).unwrap_err();
|
|
assert_eq!(err.kind, ide_core::error::ErrorKind::InvalidInput, "{template}");
|
|
assert_eq!(err.message, format!("Invalid replacement: the pattern has no group {group}"), "{template}");
|
|
}
|
|
}
|
|
let err = replace_everywhere("a", "(a)", "<$&>").unwrap_err();
|
|
assert_eq!(err.message, "Invalid replacement: use $0 for the whole match instead of $&");
|
|
}
|
|
|
|
#[test]
|
|
fn an_invalid_replacement_changes_no_file() {
|
|
let docs = FakeDocs::with(&[("a", "foo"), ("b", "foo")]);
|
|
let t = targets(&["a", "b"]);
|
|
let events = Mutex::new(0);
|
|
let err = replace_files(&docs, ®ex("(foo)"), "$2", false, &t, &CancellationToken::new(), &|_| {
|
|
*events.lock().unwrap() += 1;
|
|
})
|
|
.unwrap_err();
|
|
assert_eq!(err.kind, ide_core::error::ErrorKind::InvalidInput);
|
|
assert_eq!((docs.text("a").as_str(), docs.text("b").as_str()), ("foo", "foo"));
|
|
assert_eq!((docs.steps.lock().unwrap().len(), *events.lock().unwrap()), (0, 0));
|
|
}
|
|
|
|
#[test]
|
|
fn literal_replacements_are_written_as_is() {
|
|
let template = r"$1 ${x} $& $$ \n";
|
|
assert_eq!(preview("foo", &query("foo"), template, false, None).unwrap().after, template);
|
|
}
|
|
|
|
#[test]
|
|
fn preserve_case_applies_to_the_expanded_groups() {
|
|
let p = preview("getName GETNAME", ®ex("get(name)"), "fetch$1Value", true, None).unwrap();
|
|
assert_eq!(p.after, "fetchNameValue FETCHNAMEVALUE");
|
|
}
|
|
|
|
#[test]
|
|
fn preserve_case_rules() {
|
|
assert_eq!(preserve_case("FOO", "bar"), "BAR");
|
|
assert_eq!(preserve_case("Foo", "bar"), "Bar");
|
|
assert_eq!(preserve_case("foo", "Bar"), "bar");
|
|
assert_eq!(preserve_case("F", "bar"), "Bar");
|
|
assert_eq!(preserve_case("123", "bar"), "bar");
|
|
let p = preview("foo Foo FOO", &query("foo"), "bar", true, None).unwrap();
|
|
assert_eq!(p.after, "bar Bar BAR");
|
|
}
|
|
|
|
#[test]
|
|
fn replace_files_one_step_per_file_with_progress() {
|
|
let docs = FakeDocs::with(&[("a", "foo foo"), ("b", "nothing"), ("c", "x foo")]);
|
|
let events = Arc::new(Mutex::new(Vec::new()));
|
|
let ev = events.clone();
|
|
let summary = replace_files(
|
|
&docs,
|
|
&query("foo"),
|
|
"bar",
|
|
false,
|
|
&targets(&["a", "b", "c", "missing"]),
|
|
&CancellationToken::new(),
|
|
&move |e| ev.lock().unwrap().push(e),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(docs.text("a"), "bar bar");
|
|
assert_eq!(docs.text("c"), "x bar");
|
|
assert_eq!(*docs.steps.lock().unwrap(), vec![(PathBuf::from("a"), 2), (PathBuf::from("c"), 1)]);
|
|
assert_eq!(
|
|
(summary.files, summary.replacements, summary.failed, summary.unsaved, summary.cancelled),
|
|
(2, 3, 1, 0, false)
|
|
);
|
|
let events = events.lock().unwrap();
|
|
assert_eq!(events.len(), 4);
|
|
assert!(matches!(&events[0], ReplaceEvent::File { replacements: 2, done: 1, total: 4, .. }));
|
|
assert!(matches!(&events[1], ReplaceEvent::File { replacements: 0, done: 2, .. }));
|
|
assert!(matches!(&events[3], ReplaceEvent::Failed { done: 4, .. }));
|
|
}
|
|
|
|
#[test]
|
|
fn replace_files_honours_cancellation() {
|
|
let cancel = CancellationToken::new();
|
|
let docs = FakeDocs { cancel_after_first: Some(cancel.clone()), ..FakeDocs::with(&[("a", "foo"), ("b", "foo")]) };
|
|
let summary = replace_files(&docs, &query("foo"), "bar", false, &targets(&["a", "b"]), &cancel, &|_| {}).unwrap();
|
|
assert!(summary.cancelled);
|
|
assert_eq!(summary.files, 1);
|
|
assert_eq!(docs.text("a"), "bar");
|
|
assert_eq!(docs.text("b"), "foo");
|
|
}
|
|
|
|
#[test]
|
|
fn replace_files_runs_off_thread_and_can_be_cancelled_from_outside() {
|
|
let cancel = CancellationToken::new();
|
|
cancel.cancel();
|
|
let docs = Arc::new(FakeDocs::with(&[("a", "foo")]));
|
|
let d = docs.clone();
|
|
let c = cancel.clone();
|
|
let summary = std::thread::spawn(move || {
|
|
replace_files(&*d, &query("foo"), "bar", false, &targets(&["a"]), &c, &|_| {}).unwrap()
|
|
})
|
|
.join()
|
|
.unwrap();
|
|
assert!(summary.cancelled);
|
|
assert_eq!(docs.text("a"), "foo");
|
|
}
|
|
|
|
#[test]
|
|
fn replace_files_respects_selected_matches_and_invalid_patterns() {
|
|
let docs = FakeDocs::with(&[("a", "foo\nfoo\nfoo")]);
|
|
let t = vec![ReplaceTarget { path: "a".into(), only: Some(vec![Pos::new(1, 0)]), skip: None }];
|
|
let s = replace_files(&docs, &query("foo"), "x", false, &t, &CancellationToken::new(), &|_| {}).unwrap();
|
|
assert_eq!(s.replacements, 1);
|
|
assert_eq!(docs.text("a"), "foo\nx\nfoo");
|
|
let err = replace_files(&docs, ®ex("("), "x", false, &t, &CancellationToken::new(), &|_| {}).unwrap_err();
|
|
assert_eq!(err.kind, ide_core::error::ErrorKind::InvalidInput);
|
|
}
|
|
|
|
/// `// …` comments and `"…"` strings of `.rs` files; other files have no syntax tree.
|
|
fn toy_literals(path: &Path, text: &str) -> Option<Vec<LiteralRange>> {
|
|
if path.extension().is_none_or(|e| e != "rs") {
|
|
return None;
|
|
}
|
|
let mut out = Vec::new();
|
|
for (i, _) in text.match_indices("//") {
|
|
let end = text[i..].find('\n').map_or(text.len(), |n| i + n);
|
|
out.push(LiteralRange { start: i, end, kind: LiteralKind::Comment });
|
|
}
|
|
let quotes: Vec<usize> = text.match_indices('"').map(|(i, _)| i).collect();
|
|
for pair in quotes.chunks(2) {
|
|
if let [a, b] = pair {
|
|
out.push(LiteralRange { start: *a, end: b + 1, kind: LiteralKind::String });
|
|
}
|
|
}
|
|
out.sort_by_key(|l| l.start);
|
|
Some(out)
|
|
}
|
|
|
|
#[test]
|
|
fn replace_files_in_context_keeps_to_the_search_context() {
|
|
use ide_search::SearchContext as C;
|
|
let text = "foo(\"foo\"); // foo\nfoo\n";
|
|
let plain = "foo // foo\n";
|
|
let cases = [
|
|
(C::Any, "x(\"x\"); // x\nx\n", "x // x\n"),
|
|
(C::InComments, "foo(\"foo\"); // x\nfoo\n", plain),
|
|
(C::InStringLiterals, "foo(\"x\"); // foo\nfoo\n", plain),
|
|
(C::ExceptComments, "x(\"x\"); // foo\nx\n", "x // x\n"),
|
|
(C::ExceptStringLiterals, "x(\"foo\"); // x\nx\n", "x // x\n"),
|
|
(C::ExceptCommentsAndStringLiterals, "x(\"foo\"); // foo\nx\n", "x // x\n"),
|
|
];
|
|
for (context, in_rs, in_txt) in cases {
|
|
let docs = FakeDocs::with(&[("a.rs", text), ("b.txt", plain)]);
|
|
let q = SearchQuery { context, ..query("foo") };
|
|
let t = targets(&["a.rs", "b.txt"]);
|
|
replace_files_in_context(&docs, &q, "x", false, &t, Some(&toy_literals), &CancellationToken::new(), &|_| {})
|
|
.unwrap();
|
|
assert_eq!((docs.text("a.rs").as_str(), docs.text("b.txt").as_str()), (in_rs, in_txt), "{context:?}");
|
|
}
|
|
}
|