Skip to content

Commit 1ddf2e8

Browse files
Fix git.diff.stageHunk command to work with keyboard shortcuts (microsoft#254145)
The command now falls back to cursor-based hunk staging when no context is provided, enabling keyboard users to stage hunks without manual selection. Fixes microsoft#253895
1 parent 787d09d commit 1ddf2e8

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

extensions/git/src/commands.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,11 @@ export class CommandCenter {
16811681

16821682
@command('git.diff.stageHunk')
16831683
async diffStageHunk(changes: DiffEditorSelectionHunkToolbarContext | undefined): Promise<void> {
1684-
this.diffStageHunkOrSelection(changes);
1684+
if (changes) {
1685+
this.diffStageHunkOrSelection(changes);
1686+
} else {
1687+
await this.stageHunkAtCursor();
1688+
}
16851689
}
16861690

16871691
@command('git.diff.stageSelection')
@@ -1719,6 +1723,36 @@ export class CommandCenter {
17191723
await repository.stage(resource, result, modifiedDocument.encoding));
17201724
}
17211725

1726+
private async stageHunkAtCursor(): Promise<void> {
1727+
const textEditor = window.activeTextEditor;
1728+
1729+
if (!textEditor) {
1730+
return;
1731+
}
1732+
1733+
const workingTreeDiffInformation = getWorkingTreeDiffInformation(textEditor);
1734+
if (!workingTreeDiffInformation) {
1735+
return;
1736+
}
1737+
1738+
const workingTreeLineChanges = toLineChanges(workingTreeDiffInformation);
1739+
const modifiedDocument = textEditor.document;
1740+
const cursorPosition = textEditor.selection.active;
1741+
1742+
// Find the hunk that contains the cursor position
1743+
const hunkAtCursor = workingTreeLineChanges.find(change => {
1744+
const hunkRange = getModifiedRange(modifiedDocument, change);
1745+
return hunkRange.contains(cursorPosition);
1746+
});
1747+
1748+
if (!hunkAtCursor) {
1749+
window.showInformationMessage(l10n.t('No hunk found at cursor position.'));
1750+
return;
1751+
}
1752+
1753+
await this._stageChanges(textEditor, [hunkAtCursor]);
1754+
}
1755+
17221756
@command('git.stageSelectedRanges')
17231757
async stageSelectedChanges(): Promise<void> {
17241758
const textEditor = window.activeTextEditor;

0 commit comments

Comments
 (0)