Skip to content

Commit ebc8d27

Browse files
committed
Bonus: Cleanup and simplify the use of Block Navigation
* Added private helper method GotoChange(), to remove redundant code. * Made `SyncScrollOffset()` virtual, so the `Goto<...>Change()` methods could be removed. * In `DiffContext.CheckSettings()`, setting the Context is now made on a single (less redundant) line, just like in the `DiffContext.UseSideBySide` setter.
1 parent 3b5babb commit ebc8d27

File tree

2 files changed

+22
-53
lines changed

2 files changed

+22
-53
lines changed

src/ViewModels/DiffContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ public void CheckSettings()
136136

137137
if (ctx.IsSideBySide() != UseSideBySide)
138138
{
139-
ctx = ctx.SwitchMode();
140-
Content = ctx;
139+
Content = ctx.SwitchMode();
141140
}
142141
}
143142
}

src/Views/TextDiffView.axaml.cs

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -487,44 +487,38 @@ public virtual void UpdateSelectedChunk(double y)
487487
{
488488
}
489489

490-
public virtual void GotoFirstChange()
490+
protected virtual void SyncScrollOffset()
491491
{
492-
var first = BlockNavigation.GotoFirst();
493-
if (first != null)
494-
{
495-
TextArea.Caret.Line = first.Start;
496-
ScrollToLine(first.Start);
497-
}
498492
}
499493

500-
public virtual void GotoPrevChange()
494+
private void GotoChange(ViewModels.BlockNavigation.Block block)
501495
{
502-
var prev = BlockNavigation.GotoPrev();
503-
if (prev != null)
496+
if (block != null)
504497
{
505-
TextArea.Caret.Line = prev.Start;
506-
ScrollToLine(prev.Start);
498+
TextArea.Caret.Line = block.Start;
499+
ScrollToLine(block.Start);
500+
SyncScrollOffset();
507501
}
508502
}
509503

510-
public virtual void GotoNextChange()
504+
public void GotoFirstChange()
511505
{
512-
var next = BlockNavigation.GotoNext();
513-
if (next != null)
514-
{
515-
TextArea.Caret.Line = next.Start;
516-
ScrollToLine(next.Start);
517-
}
506+
GotoChange(BlockNavigation.GotoFirst());
518507
}
519508

520-
public virtual void GotoLastChange()
509+
public void GotoPrevChange()
521510
{
522-
var next = BlockNavigation.GotoLast();
523-
if (next != null)
524-
{
525-
TextArea.Caret.Line = next.Start;
526-
ScrollToLine(next.Start);
527-
}
511+
GotoChange(BlockNavigation.GotoPrev());
512+
}
513+
514+
public void GotoNextChange()
515+
{
516+
GotoChange(BlockNavigation.GotoNext());
517+
}
518+
519+
public void GotoLastChange()
520+
{
521+
GotoChange(BlockNavigation.GotoLast());
528522
}
529523

530524
public override void Render(DrawingContext context)
@@ -1095,30 +1089,6 @@ public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
10951089
return [];
10961090
}
10971091

1098-
public override void GotoFirstChange()
1099-
{
1100-
base.GotoFirstChange();
1101-
SyncScrollOffset();
1102-
}
1103-
1104-
public override void GotoPrevChange()
1105-
{
1106-
base.GotoPrevChange();
1107-
SyncScrollOffset();
1108-
}
1109-
1110-
public override void GotoNextChange()
1111-
{
1112-
base.GotoNextChange();
1113-
SyncScrollOffset();
1114-
}
1115-
1116-
public override void GotoLastChange()
1117-
{
1118-
base.GotoLastChange();
1119-
SyncScrollOffset();
1120-
}
1121-
11221092
public override void UpdateSelectedChunk(double y)
11231093
{
11241094
if (DataContext is not ViewModels.TwoSideTextDiff diff)
@@ -1315,7 +1285,7 @@ private void OnBlockNavigationPropertyChanged(object sender, PropertyChangedEven
13151285
TextArea?.TextView?.Redraw();
13161286
}
13171287

1318-
private void SyncScrollOffset()
1288+
protected override void SyncScrollOffset()
13191289
{
13201290
if (_scrollViewer is not null && DataContext is ViewModels.TwoSideTextDiff diff)
13211291
diff.ScrollOffset = _scrollViewer.Offset;

0 commit comments

Comments
 (0)