From 47eb0e8e67d08b9850c14c644cb008663ae296ce Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Wed, 19 Jun 2024 18:49:20 +0000 Subject: [PATCH] Add `CodeFix::apply_solution` and impl `Clone` --- Cargo.lock | 2 +- crates/rustfix/Cargo.toml | 2 +- crates/rustfix/src/lib.rs | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2366615cd27a..d6edc276410f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2943,7 +2943,7 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustfix" -version = "0.8.4" +version = "0.8.5" dependencies = [ "anyhow", "proptest", diff --git a/crates/rustfix/Cargo.toml b/crates/rustfix/Cargo.toml index 1f9b61cd5e29..ce455bf6f84c 100644 --- a/crates/rustfix/Cargo.toml +++ b/crates/rustfix/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustfix" -version = "0.8.4" +version = "0.8.5" authors = [ "Pascal Hertleif ", "Oliver Schneider ", diff --git a/crates/rustfix/src/lib.rs b/crates/rustfix/src/lib.rs index 2f6acaf30287..a4691317ed1c 100644 --- a/crates/rustfix/src/lib.rs +++ b/crates/rustfix/src/lib.rs @@ -213,6 +213,7 @@ pub fn collect_suggestions( /// 1. Feeds the source of a file to [`CodeFix::new`]. /// 2. Calls [`CodeFix::apply`] to apply suggestions to the source code. /// 3. Calls [`CodeFix::finish`] to get the "fixed" code. +#[derive(Clone)] pub struct CodeFix { data: replace::Data, /// Whether or not the data has been modified. @@ -230,12 +231,18 @@ impl CodeFix { /// Applies a suggestion to the code. pub fn apply(&mut self, suggestion: &Suggestion) -> Result<(), Error> { - for sol in &suggestion.solutions { - for r in &sol.replacements { - self.data - .replace_range(r.snippet.range.clone(), r.replacement.as_bytes())?; - self.modified = true; - } + for solution in &suggestion.solutions { + self.apply_solution(solution)?; + } + Ok(()) + } + + /// Applies an individual solution from a [`Suggestion`] + pub fn apply_solution(&mut self, solution: &Solution) -> Result<(), Error> { + for r in &solution.replacements { + self.data + .replace_range(r.snippet.range.clone(), r.replacement.as_bytes())?; + self.modified = true; } Ok(()) }