enhance: keep scroll after staging/unstaging/discarding hunks (#2072) (#2083)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-01-29 19:14:46 +08:00
parent 25b56fb18a
commit 2006fa8040
2 changed files with 29 additions and 3 deletions

View File

@@ -110,6 +110,29 @@ namespace SourceGit.ViewModels
return _blocks[_current];
}
public void UpdateByChunk(TextDiffSelectedChunk chunk)
{
_current = -1;
var chunkStart = chunk.StartIdx + 1;
var chunkEnd = chunk.EndIdx + 1;
for (var i = 0; i < _blocks.Count; i++)
{
var block = _blocks[i];
if (chunkStart > block.End)
continue;
if (chunkEnd < block.Start)
{
_current = i - 1;
break;
}
_current = i;
}
}
public void UpdateByCaretPosition(int caretLine)
{
if (_current >= 0 && _current < _blocks.Count)

View File

@@ -1464,7 +1464,7 @@ namespace SourceGit.Views
private async void OnStageChunk(object _1, RoutedEventArgs _2)
{
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } })
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } } vm)
return;
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
@@ -1496,12 +1496,13 @@ namespace SourceGit.Views
await new Commands.Apply(repo.FullPath, tmpFile, true, "nowarn", "--cache --index").ExecAsync();
File.Delete(tmpFile);
vm.BlockNavigation.UpdateByChunk(chunk);
repo.MarkWorkingCopyDirtyManually();
}
private async void OnUnstageChunk(object _1, RoutedEventArgs _2)
{
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: false, WorkingCopyChange: { } change } })
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: false, WorkingCopyChange: { } change } } vm)
return;
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
@@ -1526,12 +1527,13 @@ namespace SourceGit.Views
await new Commands.Apply(repo.FullPath, tmpFile, true, "nowarn", "--cache --index --reverse").ExecAsync();
File.Delete(tmpFile);
vm.BlockNavigation.UpdateByChunk(chunk);
repo.MarkWorkingCopyDirtyManually();
}
private async void OnDiscardChunk(object _1, RoutedEventArgs _2)
{
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } })
if (DataContext is not ViewModels.TextDiffContext { SelectedChunk: { } chunk, Data: { } diff, Option: { IsUnstaged: true, WorkingCopyChange: { } change } } vm)
return;
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
@@ -1563,6 +1565,7 @@ namespace SourceGit.Views
await new Commands.Apply(repo.FullPath, tmpFile, true, "nowarn", "--reverse").ExecAsync();
File.Delete(tmpFile);
vm.BlockNavigation.UpdateByChunk(chunk);
repo.MarkWorkingCopyDirtyManually();
}
}