Skip to content

Commit 2b08c14

Browse files
Added stage and unstage file as separate functions in the editor interface
1 parent 44df877 commit 2b08c14

5 files changed

Lines changed: 67 additions & 31 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: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ abstract class SimpleIdeCommandAction {
4646
callback: (editor, targets) =>
4747
callback(
4848
editor,
49-
acceptsLocation ? targets.map((t) => t.contentRange) : undefined,
49+
targets.map((t) => t.contentRange),
50+
acceptsLocation,
5051
this.command,
5152
),
5253
setSelection: !acceptsLocation,
@@ -150,9 +151,12 @@ export class GitUnstage extends SimpleIdeCommandAction {
150151

151152
function callback(
152153
editor: EditableTextEditor,
153-
ranges: Range[] | undefined,
154+
targetRanges: Range[],
155+
acceptsLocation: boolean,
154156
command: CommandId,
155157
): Promise<void> {
158+
const ranges = acceptsLocation ? targetRanges : undefined;
159+
156160
switch (command) {
157161
// Multi target actions
158162
case "toggleLineComment":
@@ -192,12 +196,25 @@ function callback(
192196
case "gitRevert":
193197
return editor.gitRevert(ranges?.[0]);
194198
case "gitStage":
195-
return editor.gitStage(ranges?.[0]);
199+
if (rangeIsEntireDocument(editor, targetRanges)) {
200+
return editor.gitStageFile();
201+
}
202+
return editor.gitStageRange(ranges?.[0]);
196203
case "gitUnstage":
197-
return editor.gitUnstage(ranges?.[0]);
204+
if (rangeIsEntireDocument(editor, targetRanges)) {
205+
return editor.gitUnstageFile();
206+
}
207+
return editor.gitUnstageRange(ranges?.[0]);
198208

199209
// Unsupported as simple action
200210
case "highlight":
201211
throw Error("Highlight command not supported as simple action");
202212
}
203213
}
214+
215+
function rangeIsEntireDocument(
216+
editor: EditableTextEditor,
217+
ranges: Range[],
218+
): boolean {
219+
return ranges.length === 1 && ranges[0].isRangeEqual(editor.document.range);
220+
}

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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,19 @@ export class VscodeTextEditorImpl implements EditableTextEditor {
251251
await vscode.commands.executeCommand("git.revertSelectedRanges");
252252
}
253253

254-
public async gitStage(_range?: Range): Promise<void> {
255-
if (this.selectionIsEntireDocument()) {
256-
await vscode.commands.executeCommand("git.stage");
257-
} else {
258-
await vscode.commands.executeCommand("git.stageSelectedRanges");
259-
}
254+
public async gitStageFile(): Promise<void> {
255+
await vscode.commands.executeCommand("git.stage");
260256
}
261257

262-
public async gitUnstage(_range?: Range): Promise<void> {
263-
if (this.selectionIsEntireDocument()) {
264-
await vscode.commands.executeCommand("git.unstage");
265-
} else {
266-
await vscode.commands.executeCommand("git.unstageSelectedRanges");
267-
}
258+
public async gitUnstageFile(): Promise<void> {
259+
await vscode.commands.executeCommand("git.unstage");
260+
}
261+
262+
public async gitStageRange(_range?: Range): Promise<void> {
263+
await vscode.commands.executeCommand("git.stageSelectedRanges");
268264
}
269265

270-
private selectionIsEntireDocument(): boolean {
271-
return (
272-
this.selections.length === 1 &&
273-
this.selections[0].isRangeEqual(this.document.range)
274-
);
266+
public async gitUnstageRange(_range?: Range): Promise<void> {
267+
await vscode.commands.executeCommand("git.unstageSelectedRanges");
275268
}
276269
}

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)