Skip to content

Commit

Permalink
Merge pull request rust-lang#140 from alexcrichton/rustfix-incremental
Browse files Browse the repository at this point in the history
Expose an interface to apply fixes on-by-one
  • Loading branch information
alexcrichton authored Jul 31, 2018
2 parents ac3022e + cb3229b commit 8921742
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,39 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
}
}

pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> Result<String, Error> {
use replace::Data;
pub struct CodeFix {
data: replace::Data,
}

let mut fixed = Data::new(code.as_bytes());
impl CodeFix {
pub fn new(s: &str) -> CodeFix {
CodeFix {
data: replace::Data::new(s.as_bytes()),
}
}

for sug in suggestions.iter().rev() {
for sol in &sug.solutions {
pub fn apply(&mut self, suggestion: &Suggestion) -> Result<(), Error> {
for sol in &suggestion.solutions {
for r in &sol.replacements {
fixed.replace_range(
self.data.replace_range(
r.snippet.range.start,
r.snippet.range.end.saturating_sub(1),
r.replacement.as_bytes(),
)?;
}
}
Ok(())
}

Ok(String::from_utf8(fixed.to_vec())?)
pub fn finish(&self) -> Result<String, Error> {
Ok(String::from_utf8(self.data.to_vec())?)
}
}

pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> Result<String, Error> {
let mut fix = CodeFix::new(code);
for suggestion in suggestions.iter().rev() {
fix.apply(suggestion)?;
}
fix.finish()
}

0 comments on commit 8921742

Please sign in to comment.