Skip to content

Commit

Permalink
fix(Studio): Insertion/Deletion cancelling while merging patches
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Nov 29, 2024
1 parent 16bbb88 commit 6225345
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Studio/CelesteStudio/Editing/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,24 @@ public Patch CopySwapped() {

/// Merges the patches A and B, where A is based on the initial state and B is based on after A is applied
public static Patch Merge(Patch a, Patch b) {
List<int> rowsToShift = [];

// Cancel out deletions / insertions
foreach (var row in a.Insertions.Keys.Intersect(b.Deletions.Keys)) {
int[] intersections = a.Insertions.Keys.Intersect(b.Deletions.Keys).ToArray();
foreach (int row in intersections.OrderBy(row => row)) {
a.Insertions.Remove(row);
b.Deletions.Remove(row);
}

List<int> rowsToShift = [];
// Shift up insertions in A
rowsToShift.Clear();
rowsToShift.AddRange(a.Insertions.Keys.Where(aRow => aRow > row));

foreach (int aRow in rowsToShift.OrderBy(aRow => aRow)) {
string value = a.Insertions[aRow];
a.Insertions[aRow - 1] = value;
a.Insertions.Remove(aRow);
}
}

// Shift down deletions in B
foreach ((int aRow, _) in a.Deletions.OrderBy(entry => entry.Key)) {
Expand Down

0 comments on commit 6225345

Please sign in to comment.