Skip to content

Commit

Permalink
Create executeCommandWithPrefixArgument command
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed Dec 29, 2021
1 parent 9af5c1b commit 0d4db90
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,16 @@ export class EmacsEmulator implements IEmacsCommandRunner, IMarkModeController,
vscode.commands.executeCommand("setContext", "emacs-mcx.inMarkMode", false);
}

public async executeCommandWithPrefixArgument(
command: string,
args: any = null,
prefixArgumentKey = "prefixArgument"
): Promise<unknown> {
const prefixArgument = this.prefixArgumentHandler.getPrefixArgument();

return vscode.commands.executeCommand(command, { ...args, [prefixArgumentKey]: prefixArgument });
}

private makeSelectionsEmpty() {
const srcSelections = this.rectMode ? this.nonRectSelections : this.textEditor.selections;
this.textEditor.selections = srcSelections.map((selection) => new Selection(selection.active, selection.active));
Expand Down
10 changes: 10 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ export function activate(context: vscode.ExtensionContext): void {
executeCommands(args[0]);
}
});

registerEmulatorCommand("emacs-mcx.executeCommandWithPrefixArgument", (emulator, args) => {
if (typeof args === "object" && args != null) {
if ("command" in args) {
emulator.executeCommandWithPrefixArgument(args["command"], args["args"], args["prefixArgumentKey"]);
}
} else if (Array.isArray(args) && args.length >= 1) {
emulator.executeCommandWithPrefixArgument(args[0], args[1], args[2]);
}
});
}

// this method is called when your extension is deactivated
Expand Down
62 changes: 62 additions & 0 deletions src/test/suite/execute-command-with-prefix-argument.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as vscode from "vscode";
import sinon from "sinon";
import assert from "assert";
import { EmacsEmulator } from "../../emulator";
import { cleanUpWorkspace, setupWorkspace } from "./utils";

suite("executeCommandWithPrefixArgument", () => {
let commandMock: sinon.SinonExpectation;
let commandDisposable: vscode.Disposable;

let activeTextEditor: vscode.TextEditor;
let emulator: EmacsEmulator;

setup(async () => {
commandMock = sinon.mock();
commandDisposable = vscode.commands.registerCommand("emacs-mcx-test.foo", commandMock);

activeTextEditor = await setupWorkspace();
emulator = new EmacsEmulator(activeTextEditor);
});

teardown(async () => {
commandDisposable.dispose();
await cleanUpWorkspace();
});

test("executing another command", async () => {
await emulator.executeCommandWithPrefixArgument("emacs-mcx-test.foo");

assert.ok(commandMock.calledWith({ prefixArgument: undefined }));
});

test("executing another command with the prefix argument", async () => {
await emulator.universalArgument();

await emulator.executeCommandWithPrefixArgument("emacs-mcx-test.foo");

assert.ok(commandMock.calledWith({ prefixArgument: 4 }));
});

test("executing another command with arguments", async () => {
await emulator.executeCommandWithPrefixArgument("emacs-mcx-test.foo", { foo: "bar", baz: 42 });

assert.ok(commandMock.calledWith({ foo: "bar", baz: 42, prefixArgument: undefined }));
});

test("executing another command with the passed arguments and the prefix argument", async () => {
await emulator.universalArgument();

await emulator.executeCommandWithPrefixArgument("emacs-mcx-test.foo", { foo: "bar", baz: 42 });

assert.ok(commandMock.calledWith({ foo: "bar", baz: 42, prefixArgument: 4 }));
});

test("executing another command with the customized prefix argument key", async () => {
await emulator.universalArgument();

await emulator.executeCommandWithPrefixArgument("emacs-mcx-test.foo", {}, "repeat");

assert.ok(commandMock.calledWith({ repeat: 4 }));
});
});

0 comments on commit 0d4db90

Please sign in to comment.