Skip to content

Commit 2006fa8

Browse files
committed
enhance: keep scroll after staging/unstaging/discarding hunks (#2072) (#2083)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 25b56fb commit 2006fa8

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/ViewModels/BlockNavigation.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,29 @@ public Block Goto(BlockNavigationDirection direction)
110110
return _blocks[_current];
111111
}
112112

113+
public void UpdateByChunk(TextDiffSelectedChunk chunk)
114+
{
115+
_current = -1;
116+
117+
var chunkStart = chunk.StartIdx + 1;
118+
var chunkEnd = chunk.EndIdx + 1;
119+
120+
for (var i = 0; i < _blocks.Count; i++)
121+
{
122+
var block = _blocks[i];
123+
if (chunkStart > block.End)
124+
continue;
125+
126+
if (chunkEnd < block.Start)
127+
{
128+
_current = i - 1;
129+
break;
130+
}
131+
132+
_current = i;
133+
}
134+
}
135+
113136
public void UpdateByCaretPosition(int caretLine)
114137
{
115138
if (_current >= 0 && _current < _blocks.Count)

src/Views/TextDiffView.axaml.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ protected override void OnPointerExited(PointerEventArgs e)
14641464

14651465
private async void OnStageChunk(object _1, RoutedEventArgs _2)
14661466
{
1467-
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } })
1467+
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } } vm)
14681468
return;
14691469

14701470
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
@@ -1496,12 +1496,13 @@ private async void OnStageChunk(object _1, RoutedEventArgs _2)
14961496
await new Commands.Apply(repo.FullPath, tmpFile, true, "nowarn", "--cache --index").ExecAsync();
14971497
File.Delete(tmpFile);
14981498

1499+
vm.BlockNavigation.UpdateByChunk(chunk);
14991500
repo.MarkWorkingCopyDirtyManually();
15001501
}
15011502

15021503
private async void OnUnstageChunk(object _1, RoutedEventArgs _2)
15031504
{
1504-
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: false, WorkingCopyChange: { } change } })
1505+
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: false, WorkingCopyChange: { } change } } vm)
15051506
return;
15061507

15071508
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
@@ -1526,12 +1527,13 @@ private async void OnUnstageChunk(object _1, RoutedEventArgs _2)
15261527
await new Commands.Apply(repo.FullPath, tmpFile, true, "nowarn", "--cache --index --reverse").ExecAsync();
15271528
File.Delete(tmpFile);
15281529

1530+
vm.BlockNavigation.UpdateByChunk(chunk);
15291531
repo.MarkWorkingCopyDirtyManually();
15301532
}
15311533

15321534
private async void OnDiscardChunk(object _1, RoutedEventArgs _2)
15331535
{
1534-
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } })
1536+
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } } vm)
15351537
return;
15361538

15371539
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
@@ -1563,6 +1565,7 @@ private async void OnDiscardChunk(object _1, RoutedEventArgs _2)
15631565
await new Commands.Apply(repo.FullPath, tmpFile, true, "nowarn", "--reverse").ExecAsync();
15641566
File.Delete(tmpFile);
15651567

1568+
vm.BlockNavigation.UpdateByChunk(chunk);
15661569
repo.MarkWorkingCopyDirtyManually();
15671570
}
15681571
}

0 commit comments

Comments
 (0)