Skip to content

Commit

Permalink
Fix rejecting change w/zero-length replacement
Browse files Browse the repository at this point in the history
Summary:
If you had a zero-length replacement and you pressed `n` to
reject the patch, we would never advance due to the zero-length
replacement special case. Now, we always advance if you rejected the
patch.

Reviewed By: adamjernst, dshahidehpour

Differential Revision: D9015257

fbshipit-source-id: e544c2a9b16c56db85555b952ed0dc798775ad88
  • Loading branch information
swolchok authored and facebook-github-bot committed Jul 26, 2018
1 parent 768e8cf commit 95ae97a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastmod"
version = "0.2.0"
version = "0.2.1"
authors = ["Scott Wolchok <[email protected]>"]
description = "Fast partial replacement for codemod (find/replace tool for programmers)"
homepage = "https://github.com/facebookincubator/fastmod"
Expand Down
30 changes: 21 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,21 @@ impl Fastmod {
new_contents.push_str(&new_trailing_contents);
let (start_line, _) = index_to_row_col(&contents, mat.start() + offset);
let (end_line, _) = index_to_row_col(&contents, mat.end() + offset - 1);
// If the substitution is zero length, need to
// restart from the *same* position!
offset = offset + mat.start() + min(1, subst.len());
self.ask_about_patch(
let accepted = self.ask_about_patch(
path,
&contents,
start_line + 1,
end_line + 1,
&new_contents,
)?;
if accepted {
// If the substitution is zero length, need to
// restart from the *same* position!
offset = offset + mat.start() + min(1, subst.len());
} else {
// Need to advance even if the substitution is zero length.
offset = offset + mat.start() + 1;
}
}
}
}
Expand All @@ -331,19 +336,20 @@ impl Fastmod {
Ok(())
}

/// Returns true if the patch was accepted, false otherwise.
fn ask_about_patch<'a>(
&mut self,
path: &Path,
old: &'a str,
start_line: usize,
end_line: usize,
new: &'a str,
) -> Result<()> {
) -> Result<bool> {
self.term.clear();

let diffs = self.diffs_to_print(old, new);
if diffs.is_empty() {
return Ok(());
return Ok(false);
}

if start_line == end_line {
Expand All @@ -367,20 +373,26 @@ impl Fastmod {
user_input = 'y';
}
match user_input {
'y' => self.save(path, new)?,
'y' => {
self.save(path, new)?;
return Ok(true);
}
'E' => {
self.save(path, new)?;
run_editor(path, start_line)?;
return Ok(true);
}
'e' => {
self.record_change(path.to_owned());
run_editor(path, start_line)?;
return Ok(true);
}
'q' => exit(0),
'n' => {}
'n' => {
return Ok(false);
}
_ => unreachable!(),
}
Ok(())
}

fn diffs_to_print<'a>(&self, orig: &'a str, edit: &'a str) -> Vec<DiffResult<&'a str>> {
Expand Down

0 comments on commit 95ae97a

Please sign in to comment.