Skip to content

Commit b2ed1b2

Browse files
committed
enhance: only replace hunk data if needed
1 parent 4b07a38 commit b2ed1b2

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/Views/TextDiffView.axaml.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ public class TextViewHighlightChunk
2929
public double Height { get; set; } = 0.0;
3030
public int StartIdx { get; set; } = 0;
3131
public int EndIdx { get; set; } = 0;
32+
33+
public bool ShouldReplace(TextViewHighlightChunk old)
34+
{
35+
if (old == null)
36+
return true;
37+
38+
return Math.Abs(Y - old.Y) > 0.001 ||
39+
Math.Abs(Height - old.Height) > 0.001 ||
40+
StartIdx != old.StartIdx ||
41+
EndIdx != old.EndIdx;
42+
}
3243
}
3344

3445
public class ThemedTextDiffPresenter : TextEditor
@@ -214,6 +225,21 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
214225
}
215226
}
216227

228+
protected void TrySetHighlightChunk(TextViewHighlightChunk chunk)
229+
{
230+
var old = HighlightChunk;
231+
if (chunk == null)
232+
{
233+
if (old != null)
234+
SetCurrentValue(HighlightChunkProperty, null);
235+
236+
return;
237+
}
238+
239+
if (chunk.ShouldReplace(old))
240+
SetCurrentValue(HighlightChunkProperty, chunk);
241+
}
242+
217243
protected (int, int) FindRangeByIndex(List<Models.TextDiffLine> lines, int lineIdx)
218244
{
219245
var startIdx = -1;
@@ -598,7 +624,7 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
598624

599625
if (!string.IsNullOrEmpty(SelectedText))
600626
{
601-
SetCurrentValue(HighlightChunkProperty, null);
627+
TrySetHighlightChunk(null);
602628
return;
603629
}
604630

@@ -622,14 +648,14 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
622648

623649
if (lineIdx == -1)
624650
{
625-
SetCurrentValue(HighlightChunkProperty, null);
651+
TrySetHighlightChunk(null);
626652
return;
627653
}
628654

629655
var (startIdx, endIdx) = FindRangeByIndex(DiffData.Lines, lineIdx);
630656
if (startIdx == -1)
631657
{
632-
SetCurrentValue(HighlightChunkProperty, null);
658+
TrySetHighlightChunk(null);
633659
return;
634660
}
635661

@@ -643,14 +669,13 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
643669
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset:
644670
view.Bounds.Height;
645671

646-
var hightlight = new TextViewHighlightChunk()
672+
TrySetHighlightChunk(new TextViewHighlightChunk()
647673
{
648674
Y = rectStartY,
649675
Height = rectEndY - rectStartY,
650676
StartIdx = startIdx,
651677
EndIdx = endIdx,
652-
};
653-
SetCurrentValue(HighlightChunkProperty, hightlight);
678+
});
654679
}
655680
}
656681

@@ -982,14 +1007,14 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
9821007

9831008
if (!string.IsNullOrEmpty(SelectedText))
9841009
{
985-
SetCurrentValue(HighlightChunkProperty, null);
1010+
TrySetHighlightChunk(null);
9861011
return;
9871012
}
9881013

9891014
var parentView = this.FindAncestorOfType<TextDiffView>();
9901015
if (parentView == null || parentView.DataContext == null)
9911016
{
992-
SetCurrentValue(HighlightChunkProperty, null);
1017+
TrySetHighlightChunk(null);
9931018
return;
9941019
}
9951020

@@ -1015,14 +1040,14 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
10151040

10161041
if (lineIdx == -1)
10171042
{
1018-
SetCurrentValue(HighlightChunkProperty, null);
1043+
TrySetHighlightChunk(null);
10191044
return;
10201045
}
10211046

10221047
var (startIdx, endIdx) = FindRangeByIndex(lines, lineIdx);
10231048
if (startIdx == -1)
10241049
{
1025-
SetCurrentValue(HighlightChunkProperty, null);
1050+
TrySetHighlightChunk(null);
10261051
return;
10271052
}
10281053

@@ -1036,14 +1061,13 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
10361061
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
10371062
view.Bounds.Height;
10381063

1039-
var hightlight = new TextViewHighlightChunk()
1064+
TrySetHighlightChunk(new TextViewHighlightChunk()
10401065
{
10411066
Y = rectStartY,
10421067
Height = rectEndY - rectStartY,
10431068
StartIdx = textDiff.Lines.IndexOf(lines[startIdx]),
1044-
EndIdx = textDiff.Lines.IndexOf(lines[endIdx]),
1045-
};
1046-
SetCurrentValue(HighlightChunkProperty, hightlight);
1069+
EndIdx = endIdx == lines.Count - 1 ? textDiff.Lines.Count - 1 : textDiff.Lines.IndexOf(lines[endIdx]),
1070+
});
10471071
}
10481072
}
10491073

0 commit comments

Comments
 (0)