diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index dd4143b0fa..7a663b6d77 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -96,7 +96,7 @@ export * from "./types/TestCaseFixture"; export * from "./util/getEnvironmentVariableStrict"; export * from "./util/CompositeKeyDefaultMap"; export * from "./util/toPlainObject"; -export * from "./util/useFallback"; +export * from "./util/clientSupportsFallBack"; export * from "./scopeSupportFacets/scopeSupportFacets.types"; export * from "./scopeSupportFacets/scopeSupportFacetInfos"; export * from "./scopeSupportFacets/textualScopeSupportFacetInfos"; diff --git a/packages/common/src/util/useFallback.ts b/packages/common/src/util/clientSupportsFallBack.ts similarity index 58% rename from packages/common/src/util/useFallback.ts rename to packages/common/src/util/clientSupportsFallBack.ts index a293e756df..beb07f13a9 100644 --- a/packages/common/src/util/useFallback.ts +++ b/packages/common/src/util/clientSupportsFallBack.ts @@ -1,5 +1,5 @@ import type { Command } from "../types/command/command.types"; -export function useFallback(command: Command): boolean { +export function clientSupportsFallback(command: Command): boolean { return command.version >= 7; } diff --git a/packages/cursorless-engine/src/api/CursorlessEngineApi.ts b/packages/cursorless-engine/src/api/CursorlessEngineApi.ts index 84743fbfe5..63e4b8d591 100644 --- a/packages/cursorless-engine/src/api/CursorlessEngineApi.ts +++ b/packages/cursorless-engine/src/api/CursorlessEngineApi.ts @@ -39,7 +39,7 @@ export interface CommandApi { * Runs a command. This is the core of the Cursorless engine. * @param command The command to run */ - runCommand(command: Command): Promise; + runCommand(command: Command): Promise; /** * Designed to run commands that come directly from the user. Ensures that diff --git a/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts b/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts index 672b7b8a4b..9e030348d1 100644 --- a/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts +++ b/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts @@ -5,7 +5,7 @@ import { CommandServerApi, DestinationDescriptor, PartialTargetDescriptor, - useFallback, + clientSupportsFallback, } from "@cursorless/common"; import { CommandRunner } from "../../CommandRunner"; import { ActionRecord, ActionReturnValue } from "../../actions/actions.types"; @@ -53,7 +53,7 @@ export class CommandRunnerImpl implements CommandRunner { * it has one. */ async run(command: CommandComplete): Promise { - if (useFallback(command)) { + if (clientSupportsFallback(command)) { const fallback = await getCommandFallback( this.commandServerApi, this.runAction, diff --git a/packages/cursorless-engine/src/cursorlessEngine.ts b/packages/cursorless-engine/src/cursorlessEngine.ts index b16bea874d..71dd1b1d4e 100644 --- a/packages/cursorless-engine/src/cursorlessEngine.ts +++ b/packages/cursorless-engine/src/cursorlessEngine.ts @@ -1,12 +1,10 @@ import { Command, - CommandResponse, CommandServerApi, FileSystem, Hats, IDE, ScopeProvider, - useFallback, } from "@cursorless/common"; import { CommandRunnerDecorator, @@ -89,8 +87,7 @@ export function createCursorlessEngine( }, async runCommandSafe(...args: unknown[]) { - const command = ensureCommandShape(args); - const response = await runCommand( + return runCommand( treeSitter, commandServerApi, debug, @@ -100,9 +97,8 @@ export function createCursorlessEngine( languageDefinitions, rangeUpdater, commandRunnerDecorators, - command, + ensureCommandShape(args), ); - return unwrapCommandResponse(command, response); }, }, scopeProvider: createScopeProvider( @@ -123,19 +119,6 @@ export function createCursorlessEngine( }; } -async function unwrapCommandResponse( - command: Command, - response: CommandResponse, -): Promise { - if (useFallback(command)) { - return response; - } - if ("returnValue" in response) { - return response.returnValue; - } - return undefined; -} - function createScopeProvider( languageDefinitions: LanguageDefinitions, storedTargets: StoredTargetMap, diff --git a/packages/cursorless-engine/src/runCommand.ts b/packages/cursorless-engine/src/runCommand.ts index ee42c80c10..ac1bcdc22f 100644 --- a/packages/cursorless-engine/src/runCommand.ts +++ b/packages/cursorless-engine/src/runCommand.ts @@ -4,6 +4,7 @@ import { CommandServerApi, HatTokenMap, ReadOnlyHatMap, + clientSupportsFallback, } from "@cursorless/common"; import { CommandRunner } from "./CommandRunner"; import { Actions } from "./actions/Actions"; @@ -43,7 +44,7 @@ export async function runCommand( rangeUpdater: RangeUpdater, commandRunnerDecorators: CommandRunnerDecorator[], command: Command, -): Promise { +): Promise { if (debug.active) { debug.log(`command:`); debug.log(JSON.stringify(command, null, 2)); @@ -70,7 +71,22 @@ export async function runCommand( commandRunner = decorator.wrapCommandRunner(readableHatMap, commandRunner); } - return await commandRunner.run(commandComplete); + const response = await commandRunner.run(commandComplete); + + return await unwrapCommandResponse(command, response); +} + +async function unwrapCommandResponse( + command: Command, + response: CommandResponse, +): Promise { + if (clientSupportsFallback(command)) { + return response; + } + if ("returnValue" in response) { + return response.returnValue; + } + return undefined; } function createCommandRunner( diff --git a/packages/cursorless-vscode-e2e/src/suite/recorded.vscode.test.ts b/packages/cursorless-vscode-e2e/src/suite/recorded.vscode.test.ts index 4ee0d8ff6a..9158290e80 100644 --- a/packages/cursorless-vscode-e2e/src/suite/recorded.vscode.test.ts +++ b/packages/cursorless-vscode-e2e/src/suite/recorded.vscode.test.ts @@ -24,7 +24,7 @@ import { TestCaseFixtureLegacy, TextEditor, TokenHat, - useFallback, + clientSupportsFallback, } from "@cursorless/common"; import { getCursorlessApi, @@ -126,7 +126,7 @@ async function runTest(file: string, spyIde: SpyIDE) { ...fixture.command, usePrePhraseSnapshot, }); - if (useFallback(fixture.command)) { + if (clientSupportsFallback(fixture.command)) { const commandResponse = returnValue as CommandResponse; returnValue = "returnValue" in commandResponse