Skip to content

Commit 646b080

Browse files
committed
feature: remember last navigated block after toggling Show All Lines (#1952)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 291361d commit 646b080

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/ViewModels/BlockNavigation.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using CommunityToolkit.Mvvm.ComponentModel;
34

@@ -35,13 +36,15 @@ public string Indicator
3536
}
3637
}
3738

38-
public BlockNavigation(List<Models.TextDiffLine> lines, bool gotoFirst)
39+
public BlockNavigation(List<Models.TextDiffLine> lines, int cur)
3940
{
4041
_blocks.Clear();
41-
_current = -1;
4242

4343
if (lines.Count == 0)
44+
{
45+
_current = -1;
4446
return;
47+
}
4548

4649
var lineIdx = 0;
4750
var blockStartIdx = 0;
@@ -73,9 +76,12 @@ public BlockNavigation(List<Models.TextDiffLine> lines, bool gotoFirst)
7376
blocks.Add(new Block(blockStartIdx, lines.Count));
7477

7578
_blocks.AddRange(blocks);
79+
_current = Math.Min(_blocks.Count - 1, cur);
80+
}
7681

77-
if (gotoFirst)
78-
Goto(BlockNavigationDirection.First);
82+
public int GetCurrentBlockIndex()
83+
{
84+
return _current;
7985
}
8086

8187
public Block GetCurrentBlock()

src/ViewModels/DiffContext.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ private void LoadContent()
150150

151151
Task.Run(async () =>
152152
{
153-
var showEntireFile = Preferences.Instance.UseFullTextDiff;
154-
var numLines = showEntireFile ? _entireFileLine : _unifiedLines;
153+
var numLines = Preferences.Instance.UseFullTextDiff ? _entireFileLine : _unifiedLines;
155154
var ignoreWhitespace = Preferences.Instance.IgnoreWhitespaceChangesInDiff;
156155

157156
var latest = await new Commands.Diff(_repo, _option, numLines, ignoreWhitespace)
@@ -274,9 +273,9 @@ private void LoadContent()
274273
IsTextDiff = true;
275274

276275
if (Preferences.Instance.UseSideBySideDiff)
277-
Content = new TwoSideTextDiff(_option, showEntireFile, cur, _content as TwoSideTextDiff);
276+
Content = new TwoSideTextDiff(_option, cur, _content as TwoSideTextDiff);
278277
else
279-
Content = new CombinedTextDiff(_option, showEntireFile, cur, _content as CombinedTextDiff);
278+
Content = new CombinedTextDiff(_option, cur, _content as CombinedTextDiff);
280279
}
281280
else
282281
{

src/ViewModels/TextDiffContext.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,33 @@ public virtual TextDiffContext SwitchMode()
130130
return null;
131131
}
132132

133-
protected bool TryKeepPrevScrollOffset(TextDiffContext prev)
133+
protected void TryKeepPrevState(TextDiffContext prev, List<Models.TextDiffLine> lines)
134134
{
135135
var fastTest = prev != null &&
136-
prev._showEntireFile == _showEntireFile &&
137136
prev._option.IsUnstaged == _option.IsUnstaged &&
138137
prev._option.Path.Equals(_option.Path, StringComparison.Ordinal) &&
139138
prev._option.OrgPath.Equals(_option.OrgPath, StringComparison.Ordinal) &&
140139
prev._option.Revisions.Count == _option.Revisions.Count;
141140

142141
if (!fastTest)
143-
return false;
142+
{
143+
_blockNavigation = new BlockNavigation(lines, 0);
144+
return;
145+
}
144146

145147
for (int i = 0; i < _option.Revisions.Count; i++)
146148
{
147149
if (!prev._option.Revisions[i].Equals(_option.Revisions[i], StringComparison.Ordinal))
148-
return false;
150+
{
151+
_blockNavigation = new BlockNavigation(lines, 0);
152+
return;
153+
}
149154
}
150155

151-
_scrollOffset = prev._scrollOffset;
152-
return true;
156+
_blockNavigation = new BlockNavigation(lines, prev._blockNavigation.GetCurrentBlockIndex());
153157
}
154158

155159
protected Models.DiffOption _option = null;
156-
protected bool _showEntireFile = false;
157160
protected Models.TextDiff _data = null;
158161
protected Vector _scrollOffset = Vector.Zero;
159162
protected BlockNavigation _blockNavigation = null;
@@ -164,19 +167,17 @@ protected bool TryKeepPrevScrollOffset(TextDiffContext prev)
164167

165168
public class CombinedTextDiff : TextDiffContext
166169
{
167-
public CombinedTextDiff(Models.DiffOption option, bool showEntireFile, Models.TextDiff diff, CombinedTextDiff previous = null)
170+
public CombinedTextDiff(Models.DiffOption option, Models.TextDiff diff, CombinedTextDiff previous = null)
168171
{
169172
_option = option;
170-
_showEntireFile = showEntireFile;
171173
_data = diff;
172174

173-
var keep = TryKeepPrevScrollOffset(previous);
174-
_blockNavigation = new BlockNavigation(_data.Lines, !keep);
175+
TryKeepPrevState(previous, _data.Lines);
175176
}
176177

177178
public override TextDiffContext SwitchMode()
178179
{
179-
return new TwoSideTextDiff(_option, _showEntireFile, _data);
180+
return new TwoSideTextDiff(_option, _data);
180181
}
181182
}
182183

@@ -185,10 +186,9 @@ public class TwoSideTextDiff : TextDiffContext
185186
public List<Models.TextDiffLine> Old { get; } = [];
186187
public List<Models.TextDiffLine> New { get; } = [];
187188

188-
public TwoSideTextDiff(Models.DiffOption option, bool showEntireFile, Models.TextDiff diff, TwoSideTextDiff previous = null)
189+
public TwoSideTextDiff(Models.DiffOption option, Models.TextDiff diff, TwoSideTextDiff previous = null)
189190
{
190191
_option = option;
191-
_showEntireFile = showEntireFile;
192192
_data = diff;
193193

194194
foreach (var line in diff.Lines)
@@ -210,9 +210,7 @@ public TwoSideTextDiff(Models.DiffOption option, bool showEntireFile, Models.Tex
210210
}
211211

212212
FillEmptyLines();
213-
214-
var keep = TryKeepPrevScrollOffset(previous);
215-
_blockNavigation = new BlockNavigation(Old, !keep);
213+
TryKeepPrevState(previous, Old);
216214
}
217215

218216
public override bool IsSideBySide()
@@ -222,7 +220,7 @@ public override bool IsSideBySide()
222220

223221
public override TextDiffContext SwitchMode()
224222
{
225-
return new CombinedTextDiff(_option, _showEntireFile, _data);
223+
return new CombinedTextDiff(_option, _data);
226224
}
227225

228226
public void ConvertsToCombinedRange(ref int startLine, ref int endLine, bool isOldSide)

0 commit comments

Comments
 (0)