summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Kulikov <evgeny@unity3d.com>2017-09-01 20:13:44 +0300
committerAndrew Gallant <jamslam@gmail.com>2017-10-20 20:58:27 -0400
commitf887bc1f8673fea9969adae6e70b68e3761ba171 (patch)
treee01d4d6f917dd0959a295f53442208bec7d166d9
parent363a4fa9b733c02b47c63f37c85244f2352fe260 (diff)
printer: --only-matching works with --replace
When -o/--only-matching is used with -r/--replace, the replacement works as expected. This is not a breaking change because the flags were previously set to conflict.
-rw-r--r--src/app.rs2
-rw-r--r--src/printer.rs7
-rw-r--r--tests/tests.rs14
3 files changed, 21 insertions, 2 deletions
diff --git a/src/app.rs b/src/app.rs
index 9f5a18b6..c92e6814 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -162,7 +162,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag("no-ignore-parent"))
.arg(flag("no-ignore-vcs"))
.arg(flag("null").short("0"))
- .arg(flag("only-matching").short("o").conflicts_with("replace"))
+ .arg(flag("only-matching").short("o"))
.arg(flag("path-separator").value_name("SEPARATOR").takes_value(true))
.arg(flag("pretty").short("p"))
.arg(flag("replace").short("r")
diff --git a/src/printer.rs b/src/printer.rs
index 1a6f7dac..ebc5b2d4 100644
--- a/src/printer.rs
+++ b/src/printer.rs
@@ -316,7 +316,12 @@ impl<W: WriteColor> Printer<W> {
let line = {
let replacer = CountingReplacer::new(
self.replace.as_ref().unwrap(), &mut count, &mut offsets);
- re.replace_all(&buf[start..end], replacer)
+ if self.only_matching {
+ re.replace_all(
+ &buf[start + match_start..start + match_end], replacer)
+ } else {
+ re.replace_all(&buf[start..end], replacer)
+ }
};
if self.max_columns.map_or(false, |m| line.len() > m) {
let msg = format!(
diff --git a/tests/tests.rs b/tests/tests.rs
index 4de13216..0c9446c1 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -266,6 +266,20 @@ but Watson, Doctor has to have it taken out for him and dusted,
assert_eq!(lines, expected);
});
+sherlock!(replace_with_only_matching, "of (\\w+)",
+|wd: WorkDir, mut cmd: Command| {
+ cmd.arg("-o").arg("-r").arg("$1");
+ let lines: String = wd.stdout(&mut cmd);
+ let expected = "\
+this
+detective
+luck
+straw
+cigar
+";
+ assert_eq!(lines, expected);
+});
+
sherlock!(file_types, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| {
wd.create("file.py", "Sherlock");
wd.create("file.rs", "Sherlock");