Fix count issue; make test debugging slightly easier for error cases.

This commit is contained in:
Grégoire Geis 2021-03-20 19:21:59 +01:00
parent b4f98a4167
commit c6ea85dbfc
3 changed files with 30 additions and 5 deletions

View File

@ -134,6 +134,11 @@ export interface InputDescrMap {
* Defines a command's behavior, as well as its inputs.
*/
export class CommandDescriptor<Input extends InputKind = InputKind> {
/**
* Whether errors in command executions should lead to hard exceptions.
*/
public static throwOnError = false;
public constructor(
public readonly command: Command,
public readonly flags: CommandFlags,
@ -269,6 +274,10 @@ export class CommandDescriptor<Input extends InputKind = InputKind> {
argument,
);
if (!this.command.startsWith("dance.count.")) {
extension.currentCount = 0;
}
editorState.ignoreSelectionChanges = true;
let result;
@ -283,6 +292,11 @@ export class CommandDescriptor<Input extends InputKind = InputKind> {
}
}
} catch (e) {
if (CommandDescriptor.throwOnError) {
console.error(e);
throw e;
}
let message = e;
if (typeof e === "object" && e !== null && e.constructor === Error) {
@ -336,10 +350,6 @@ export class CommandDescriptor<Input extends InputKind = InputKind> {
editor.revealRange(new vscode.Range(position, position));
}
if (!this.command.startsWith("dance.count.")) {
extension.currentCount = 0;
}
if (remainingNormalCommands === 1) {
remainingNormalCommands = 0;

View File

@ -8,6 +8,7 @@ import * as path from "path";
import * as vscode from "vscode";
import { Command } from "../../commands";
import { CommandDescriptor } from "../../src/commands";
import { extensionState } from "../../src/extension";
import { SelectionBehavior } from "../../src/state/extension";
@ -21,6 +22,7 @@ export namespace testCommands {
readonly initialContent: string;
readonly mutations: readonly Mutation[];
readonly selectionBehavior: SelectionBehavior;
readonly allowErrors: boolean;
}
}
@ -97,10 +99,11 @@ function stringifySelection(document: vscode.TextDocument, selection: vscode.Sel
async function testCommands(
editor: vscode.TextEditor,
{ initialContent, mutations, selectionBehavior }: testCommands.Options,
{ initialContent, mutations, selectionBehavior, allowErrors }: testCommands.Options,
) {
// @ts-ignore
extensionState._selectionBehavior = selectionBehavior;
CommandDescriptor.throwOnError = !allowErrors;
const content = getPlainContent(initialContent);
const document = editor.document;
@ -214,6 +217,7 @@ suite("Running commands", function () {
initialContent: `{0}f|{0}oo`,
mutations: [{ contentAfterMutation: `{0}fo|{0}o`, commands: [Command.rightExtend] }],
selectionBehavior: SelectionBehavior.Character,
allowErrors: false,
});
});
@ -223,6 +227,7 @@ suite("Running commands", function () {
initialContent: `|{0}foo`,
mutations: [{ contentAfterMutation: `|{0}foo`, commands: [Command.rightExtend] }],
selectionBehavior: SelectionBehavior.Character,
allowErrors: false,
});
} catch (err) {
if (
@ -325,6 +330,7 @@ suite("Running commands", function () {
initialContent,
mutations: [{ contentAfterMutation, commands }],
selectionBehavior,
allowErrors: true,
});
success = true;

View File

@ -0,0 +1,9 @@
he|{0}l{0}lo world
//== 0 > 1
//= {"command": "dance.objects.performSelection", "args": [{"object": "word", "action": "select", "inner": true}]}
{0}hello|{0} world
//== 1 > 2
//= {"command": "dance.objects.performSelection", "args": [{"object": "word", "action": "select", "inner": true}]}
{0}hello|{0} world