Skip to content

Commit

Permalink
refactor: apply hunk changes in reverse to make our life easier
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian8j2 committed Jan 14, 2025
1 parent fe0b674 commit 76c4cc4
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions crates/cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,11 @@ fn apply_hunk_changes(input: &str, mut hunks: Vec<HunkChange>) -> String {
if hunks.is_empty() {
return input.to_string();
}
hunks.sort_by_key(|hunk| hunk.starting_byte);
let mut buffer = String::new();
let mut last_ending_byte = 0;
for (index, hunk) in hunks.iter().enumerate() {
buffer.push_str(&input[last_ending_byte..hunk.starting_byte]);
buffer.push_str(&hunk.new_content);
last_ending_byte = hunk.ending_byte;

if index == hunks.len() - 1 {
buffer.push_str(&input[last_ending_byte..]);
}
hunks.sort_by_key(|hunk| -(hunk.starting_byte as isize));
let mut buffer = input.to_owned();
for hunk in hunks {
let hunk_range = hunk.starting_byte..hunk.ending_byte;
buffer.replace_range(hunk_range, &hunk.new_content);
}
buffer
}

0 comments on commit 76c4cc4

Please sign in to comment.