Skip to content

Commit 8dbe902

Browse files
committed
fix: last line change may disable hunk operation in side-by-side diff (#2027)
Signed-off-by: leo <[email protected]>
1 parent d373e18 commit 8dbe902

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/ViewModels/TextDiffContext.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public override TextDiffContext SwitchMode()
223223
return new CombinedTextDiff(_option, _data, this);
224224
}
225225

226-
public void ConvertsToCombinedRange(ref int startLine, ref int endLine, bool isOldSide)
226+
public void GetCombinedRangeForSingleSide(ref int startLine, ref int endLine, bool isOldSide)
227227
{
228228
endLine = Math.Min(endLine, _data.Lines.Count - 1);
229229

@@ -262,6 +262,32 @@ public void ConvertsToCombinedRange(ref int startLine, ref int endLine, bool isO
262262
endLine = _data.Lines.IndexOf(endContent);
263263
}
264264

265+
public void GetCombinedRangeForBothSides(ref int startLine, ref int endLine, bool isOldSide)
266+
{
267+
var fromSide = isOldSide ? Old : New;
268+
endLine = Math.Min(endLine, fromSide.Count - 1);
269+
270+
// Since this function is only used for auto-detected hunk, we just need to find out the a first changed line
271+
// and then use `FindRangeByIndex` to get the range of hunk.
272+
for (int i = startLine; i <= endLine; i++)
273+
{
274+
var line = fromSide[i];
275+
if (line.Type == Models.TextDiffLineType.Added || line.Type == Models.TextDiffLineType.Deleted)
276+
{
277+
(startLine, endLine) = FindRangeByIndex(_data.Lines, _data.Lines.IndexOf(line));
278+
return;
279+
}
280+
281+
if (line.Type == Models.TextDiffLineType.None)
282+
{
283+
var otherSide = isOldSide ? New : Old;
284+
var changedLine = otherSide[i]; // Find the changed line on the other side in the same position
285+
(startLine, endLine) = FindRangeByIndex(_data.Lines, _data.Lines.IndexOf(changedLine));
286+
return;
287+
}
288+
}
289+
}
290+
265291
private void FillEmptyLines()
266292
{
267293
if (Old.Count < New.Count)

src/Views/TextDiffView.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ protected override void UpdateSelectedChunk(double y)
11831183
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
11841184
view.Bounds.Height;
11851185

1186-
diff.ConvertsToCombinedRange(ref startIdx, ref endIdx, IsOld);
1186+
diff.GetCombinedRangeForSingleSide(ref startIdx, ref endIdx, IsOld);
11871187
TrySetChunk(new(rectStartY, rectEndY - rectStartY, startIdx, endIdx, false, IsOld));
11881188
}
11891189
else
@@ -1229,7 +1229,7 @@ protected override void UpdateSelectedChunk(double y)
12291229
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
12301230
view.Bounds.Height;
12311231

1232-
diff.ConvertsToCombinedRange(ref startIdx, ref endIdx, IsOld);
1232+
diff.GetCombinedRangeForBothSides(ref startIdx, ref endIdx, IsOld);
12331233
TrySetChunk(new(rectStartY, rectEndY - rectStartY, startIdx, endIdx, true, false));
12341234
}
12351235
}

0 commit comments

Comments
 (0)