Skip to content

Commit fac40c0

Browse files
Support staging and unstaging entire files with git actions (#3209)
1 parent 3e84a12 commit fac40c0

5 files changed

Lines changed: 63 additions & 14 deletions

File tree

‎packages/common/src/types/TextEditor.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,25 @@ export interface EditableTextEditor extends TextEditor {
250250
*/
251251
gitRevert(range?: Range): Promise<void>;
252252

253+
/**
254+
* Git stage file
255+
*/
256+
gitStageFile(): Promise<void>;
257+
258+
/**
259+
* Git unstage file
260+
*/
261+
gitUnstageFile(): Promise<void>;
262+
253263
/**
254264
* Git stage range
255265
* @param range A {@link Range range}
256266
*/
257-
gitStage(range?: Range): Promise<void>;
267+
gitStageRange(range?: Range): Promise<void>;
258268

259269
/**
260270
* Git unstage range
261271
* @param range A {@link Range range}
262272
*/
263-
gitUnstage(range?: Range): Promise<void>;
273+
gitUnstageRange(range?: Range): Promise<void>;
264274
}

‎packages/cursorless-engine/src/actions/SimpleIdeCommandActions.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ abstract class SimpleIdeCommandAction {
4747
callback(
4848
editor,
4949
acceptsLocation ? targets.map((t) => t.contentRange) : undefined,
50+
rangeIsEntireDocument(targets),
5051
this.command,
5152
),
5253
setSelection: !acceptsLocation,
@@ -151,6 +152,7 @@ export class GitUnstage extends SimpleIdeCommandAction {
151152
function callback(
152153
editor: EditableTextEditor,
153154
ranges: Range[] | undefined,
155+
rangeIsEntireDocument: boolean,
154156
command: CommandId,
155157
): Promise<void> {
156158
switch (command) {
@@ -192,12 +194,25 @@ function callback(
192194
case "gitRevert":
193195
return editor.gitRevert(ranges?.[0]);
194196
case "gitStage":
195-
return editor.gitStage(ranges?.[0]);
197+
if (rangeIsEntireDocument) {
198+
return editor.gitStageFile();
199+
}
200+
return editor.gitStageRange(ranges?.[0]);
196201
case "gitUnstage":
197-
return editor.gitUnstage(ranges?.[0]);
202+
if (rangeIsEntireDocument) {
203+
return editor.gitUnstageFile();
204+
}
205+
return editor.gitUnstageRange(ranges?.[0]);
198206

199207
// Unsupported as simple action
200208
case "highlight":
201209
throw Error("Highlight command not supported as simple action");
202210
}
203211
}
212+
213+
function rangeIsEntireDocument(targets: Target[]): boolean {
214+
return (
215+
targets.length === 1 &&
216+
targets[0].contentRange.isRangeEqual(targets[0].editor.document.range)
217+
);
218+
}

‎packages/cursorless-everywhere-talon-core/src/ide/TalonJsEditor.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,19 @@ export class TalonJsEditor implements EditableTextEditor {
163163
throw Error("gitRevert not implemented");
164164
}
165165

166-
public async gitStage(_range?: Range): Promise<void> {
167-
throw Error("gitStage not implemented");
166+
public async gitStageFile(): Promise<void> {
167+
throw Error("gitStageFile not implemented");
168168
}
169169

170-
public async gitUnstage(_range?: Range): Promise<void> {
171-
throw Error("gitUnstage not implemented");
170+
public async gitUnstageFile(): Promise<void> {
171+
throw Error("gitUnstageFile not implemented");
172+
}
173+
174+
public async gitStageRange(_range?: Range): Promise<void> {
175+
throw Error("gitStageRange not implemented");
176+
}
177+
178+
public async gitUnstageRange(_range?: Range): Promise<void> {
179+
throw Error("gitUnstageRange not implemented");
172180
}
173181
}

‎packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,19 @@ export class VscodeTextEditorImpl implements EditableTextEditor {
251251
await vscode.commands.executeCommand("git.revertSelectedRanges");
252252
}
253253

254-
public async gitStage(_range?: Range): Promise<void> {
254+
public async gitStageFile(): Promise<void> {
255+
await vscode.commands.executeCommand("git.stage");
256+
}
257+
258+
public async gitUnstageFile(): Promise<void> {
259+
await vscode.commands.executeCommand("git.unstage");
260+
}
261+
262+
public async gitStageRange(_range?: Range): Promise<void> {
255263
await vscode.commands.executeCommand("git.stageSelectedRanges");
256264
}
257265

258-
public async gitUnstage(_range?: Range): Promise<void> {
266+
public async gitUnstageRange(_range?: Range): Promise<void> {
259267
await vscode.commands.executeCommand("git.unstageSelectedRanges");
260268
}
261269
}

‎packages/neovim-common/src/ide/neovim/NeovimTextEditorImpl.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,19 @@ export class NeovimTextEditorImpl implements EditableTextEditor {
199199
throw Error("gitRevert Not implemented");
200200
}
201201

202-
public async gitStage(_range?: Range): Promise<void> {
203-
throw Error("gitStage Not implemented");
202+
public async gitStageFile(): Promise<void> {
203+
throw Error("gitStageFile not implemented");
204204
}
205205

206-
public async gitUnstage(_range?: Range): Promise<void> {
207-
throw Error("gitUnstage Not implemented");
206+
public async gitUnstageFile(): Promise<void> {
207+
throw Error("gitUnstageFile not implemented");
208+
}
209+
210+
public async gitStageRange(_range?: Range): Promise<void> {
211+
throw Error("gitStageRange not implemented");
212+
}
213+
214+
public async gitUnstageRange(_range?: Range): Promise<void> {
215+
throw Error("gitUnstageRange not implemented");
208216
}
209217
}

0 commit comments

Comments
 (0)