Skip to content

Commit

Permalink
Fix type annotations around EmacsCommand class to handle the promise …
Browse files Browse the repository at this point in the history
…returned in afterCommand
  • Loading branch information
whitphx committed Oct 12, 2020
1 parent 37ecf83 commit aa81909
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
13 changes: 6 additions & 7 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class EmacsCommand {
public abstract readonly id: string;

protected emacsController: IMarkModeController & IEmacsCommandRunner;
private afterExecute: () => void;
private afterExecute: () => void | Promise<unknown>;

public constructor(afterExecute: () => void, markModeController: IMarkModeController & IEmacsCommandRunner) {
this.afterExecute = afterExecute;
Expand All @@ -20,21 +20,20 @@ export abstract class EmacsCommand {
textEditor: TextEditor,
isInMarkMode: boolean,
prefixArgument: number | undefined
): undefined | Thenable<unknown> {
): void | Thenable<unknown> {
const ret = this.execute(textEditor, isInMarkMode, prefixArgument);
if (ret !== undefined && (ret as Thenable<any>).then !== undefined) {
return (ret as Thenable<any>).then(() => this.afterExecute());
if (ret != null) {
return ret.then(this.afterExecute);
} else {
this.afterExecute();
return;
return this.afterExecute();
}
}

public abstract execute(
textEditor: TextEditor,
isInMarkMode: boolean,
prefixArgument: number | undefined
): void | undefined | Thenable<unknown> | Promise<void>;
): void | Thenable<unknown>;
}

export interface IEmacsCommandInterrupted {
Expand Down
4 changes: 2 additions & 2 deletions src/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Configuration } from "./configuration/configuration";
import { MarkRing } from "./mark-ring";

export interface IEmacsCommandRunner {
runCommand(commandName: string): undefined | Thenable<unknown>;
runCommand(commandName: string): void | Thenable<unknown>;
}

export interface IMarkModeController {
Expand Down Expand Up @@ -343,7 +343,7 @@ export class EmacsEmulator implements IEmacsCommandRunner, IMarkModeController {
}

private afterCommand() {
this.prefixArgumentHandler.cancel();
return this.prefixArgumentHandler.cancel();
}

private onDidInterruptTextEditor() {
Expand Down
7 changes: 4 additions & 3 deletions src/test/suite/commands/kill-yank/kill-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ abcdefghij
});

// Test kill appending is not enabled after cursorMoves, editing, or some other ops
const moves = moveCommandIds.map((commandName): [string, () => Thenable<unknown> | undefined] => [
const moves = moveCommandIds.map((commandName): [string, () => Thenable<unknown> | void] => [
commandName,
() => emulator.runCommand(commandName),
]);
Expand All @@ -172,9 +172,10 @@ abcdefghij
),
],
];
const otherOps: Array<[string, () => Thenable<unknown>]> = [["cancel", async () => await emulator.cancel()]];

const ops: Array<[string, () => Thenable<unknown> | undefined]> = [...moves, ...edits, ...otherOps];
const otherOps: Array<[string, () => Thenable<void>]> = [["cancel", async () => await emulator.cancel()]];

const ops: Array<[string, () => Thenable<unknown> | void]> = [...moves, ...edits, ...otherOps];
ops.forEach(([label, op]) => {
test(`it does not append the killed text after ${label}`, async () => {
setEmptyCursors(activeTextEditor, [1, 5]);
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/commands/kill-yank/kill-ring-yank.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ ABCDEFGHIJ`
],
];

const moves = moveCommandIds.map((commandName): [string, () => Thenable<unknown> | undefined] => [
const moves = moveCommandIds.map((commandName): [string, () => Thenable<unknown> | void] => [
commandName,
() => emulator.runCommand(commandName),
]);
Expand Down

0 comments on commit aa81909

Please sign in to comment.