@@ -29,6 +29,17 @@ public class TextViewHighlightChunk
29
29
public double Height { get ; set ; } = 0.0 ;
30
30
public int StartIdx { get ; set ; } = 0 ;
31
31
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
+ }
32
43
}
33
44
34
45
public class ThemedTextDiffPresenter : TextEditor
@@ -214,6 +225,21 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
214
225
}
215
226
}
216
227
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
+
217
243
protected ( int , int ) FindRangeByIndex ( List < Models . TextDiffLine > lines , int lineIdx )
218
244
{
219
245
var startIdx = - 1 ;
@@ -598,7 +624,7 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
598
624
599
625
if ( ! string . IsNullOrEmpty ( SelectedText ) )
600
626
{
601
- SetCurrentValue ( HighlightChunkProperty , null ) ;
627
+ TrySetHighlightChunk ( null ) ;
602
628
return ;
603
629
}
604
630
@@ -622,14 +648,14 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
622
648
623
649
if ( lineIdx == - 1 )
624
650
{
625
- SetCurrentValue ( HighlightChunkProperty , null ) ;
651
+ TrySetHighlightChunk ( null ) ;
626
652
return ;
627
653
}
628
654
629
655
var ( startIdx , endIdx ) = FindRangeByIndex ( DiffData . Lines , lineIdx ) ;
630
656
if ( startIdx == - 1 )
631
657
{
632
- SetCurrentValue ( HighlightChunkProperty , null ) ;
658
+ TrySetHighlightChunk ( null ) ;
633
659
return ;
634
660
}
635
661
@@ -643,14 +669,13 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
643
669
endLine . GetTextLineVisualYPosition ( endLine . TextLines [ ^ 1 ] , VisualYPosition . TextBottom ) - view . VerticalOffset :
644
670
view . Bounds . Height ;
645
671
646
- var hightlight = new TextViewHighlightChunk ( )
672
+ TrySetHighlightChunk ( new TextViewHighlightChunk ( )
647
673
{
648
674
Y = rectStartY ,
649
675
Height = rectEndY - rectStartY ,
650
676
StartIdx = startIdx ,
651
677
EndIdx = endIdx ,
652
- } ;
653
- SetCurrentValue ( HighlightChunkProperty , hightlight ) ;
678
+ } ) ;
654
679
}
655
680
}
656
681
@@ -982,14 +1007,14 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
982
1007
983
1008
if ( ! string . IsNullOrEmpty ( SelectedText ) )
984
1009
{
985
- SetCurrentValue ( HighlightChunkProperty , null ) ;
1010
+ TrySetHighlightChunk ( null ) ;
986
1011
return ;
987
1012
}
988
1013
989
1014
var parentView = this . FindAncestorOfType < TextDiffView > ( ) ;
990
1015
if ( parentView == null || parentView . DataContext == null )
991
1016
{
992
- SetCurrentValue ( HighlightChunkProperty , null ) ;
1017
+ TrySetHighlightChunk ( null ) ;
993
1018
return ;
994
1019
}
995
1020
@@ -1015,14 +1040,14 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
1015
1040
1016
1041
if ( lineIdx == - 1 )
1017
1042
{
1018
- SetCurrentValue ( HighlightChunkProperty , null ) ;
1043
+ TrySetHighlightChunk ( null ) ;
1019
1044
return ;
1020
1045
}
1021
1046
1022
1047
var ( startIdx , endIdx ) = FindRangeByIndex ( lines , lineIdx ) ;
1023
1048
if ( startIdx == - 1 )
1024
1049
{
1025
- SetCurrentValue ( HighlightChunkProperty , null ) ;
1050
+ TrySetHighlightChunk ( null ) ;
1026
1051
return ;
1027
1052
}
1028
1053
@@ -1036,14 +1061,13 @@ private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
1036
1061
endLine . GetTextLineVisualYPosition ( endLine . TextLines [ ^ 1 ] , VisualYPosition . TextBottom ) - view . VerticalOffset :
1037
1062
view . Bounds . Height ;
1038
1063
1039
- var hightlight = new TextViewHighlightChunk ( )
1064
+ TrySetHighlightChunk ( new TextViewHighlightChunk ( )
1040
1065
{
1041
1066
Y = rectStartY ,
1042
1067
Height = rectEndY - rectStartY ,
1043
1068
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
+ } ) ;
1047
1071
}
1048
1072
}
1049
1073
0 commit comments