Skip to content

Commit c8bf916

Browse files
committed
Add cursor change after revert or stage changes
1 parent 154f2dd commit c8bf916

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

extensions/git/src/commands.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { lstat, Stats } from 'fs';
77
import * as os from 'os';
88
import * as path from 'path';
9-
import { commands, Disposable, LineChange, MessageOptions, OutputChannel, Position, ProgressLocation, QuickPickItem, Range, SourceControlResourceState, TextDocumentShowOptions, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceEdit, WorkspaceFolder } from 'vscode';
9+
import { Selection, commands, Disposable, LineChange, MessageOptions, OutputChannel, Position, ProgressLocation, QuickPickItem, Range, SourceControlResourceState, TextDocumentShowOptions, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceEdit, WorkspaceFolder } from 'vscode';
1010
import TelemetryReporter from 'vscode-extension-telemetry';
1111
import * as nls from 'vscode-nls';
1212
import { Branch, GitErrorCodes, Ref, RefType, Status } from './api/git';
@@ -980,7 +980,10 @@ export class CommandCenter {
980980
return;
981981
}
982982

983-
await this._stageChanges(textEditor, [changes[index]]);
983+
await this._stageChanges(textEditor, [changes[index]]).then(() => {
984+
const firstStagedLine = changes[index].modifiedStartLineNumber - 1;
985+
textEditor.selections = [new Selection(firstStagedLine, 0, firstStagedLine, 0)];
986+
});
984987
}
985988

986989
@command('git.stageSelectedRanges', { diff: true })
@@ -1027,7 +1030,10 @@ export class CommandCenter {
10271030
return;
10281031
}
10291032

1030-
await this._revertChanges(textEditor, [...changes.slice(0, index), ...changes.slice(index + 1)]);
1033+
await this._revertChanges(textEditor, [...changes.slice(0, index), ...changes.slice(index + 1)]).then(() => {
1034+
const firstStagedLine = changes[index].modifiedStartLineNumber - 1;
1035+
textEditor.selections = [new Selection(firstStagedLine, 0, firstStagedLine, 0)];
1036+
});
10311037
}
10321038

10331039
@command('git.revertSelectedRanges', { diff: true })
@@ -1049,7 +1055,10 @@ export class CommandCenter {
10491055
return;
10501056
}
10511057

1052-
await this._revertChanges(textEditor, selectedChanges);
1058+
const selectionsBeforeRevert = textEditor.selections;
1059+
await this._revertChanges(textEditor, selectedChanges).then(() => {
1060+
textEditor.selections = selectionsBeforeRevert;
1061+
});
10531062
}
10541063

10551064
private async _revertChanges(textEditor: TextEditor, changes: LineChange[]): Promise<void> {
@@ -1062,7 +1071,6 @@ export class CommandCenter {
10621071

10631072
const originalUri = toGitUri(modifiedUri, '~');
10641073
const originalDocument = await workspace.openTextDocument(originalUri);
1065-
const selectionsBeforeRevert = textEditor.selections;
10661074
const visibleRangesBeforeRevert = textEditor.visibleRanges;
10671075
const result = applyLineChanges(originalDocument, modifiedDocument, changes);
10681076

@@ -1072,7 +1080,6 @@ export class CommandCenter {
10721080

10731081
await modifiedDocument.save();
10741082

1075-
textEditor.selections = selectionsBeforeRevert;
10761083
textEditor.revealRange(visibleRangesBeforeRevert[0]);
10771084
}
10781085

0 commit comments

Comments
 (0)