Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Expose an interface to apply fixes on-by-one #140

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}