Skip to content

Commit

Permalink
move and rename fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Feb 19, 2024
1 parent 98e9529 commit 5b7371e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/api/CursorlessEngineApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandResponse>;
runCommand(command: Command): Promise<CommandResponse | unknown>;

/**
* Designed to run commands that come directly from the user. Ensures that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CommandServerApi,
DestinationDescriptor,
PartialTargetDescriptor,
useFallback,
clientSupportsFallback,
} from "@cursorless/common";
import { CommandRunner } from "../../CommandRunner";
import { ActionRecord, ActionReturnValue } from "../../actions/actions.types";
Expand Down Expand Up @@ -53,7 +53,7 @@ export class CommandRunnerImpl implements CommandRunner {
* it has one.
*/
async run(command: CommandComplete): Promise<CommandResponse> {
if (useFallback(command)) {
if (clientSupportsFallback(command)) {
const fallback = await getCommandFallback(
this.commandServerApi,
this.runAction,
Expand Down
21 changes: 2 additions & 19 deletions packages/cursorless-engine/src/cursorlessEngine.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
Command,
CommandResponse,
CommandServerApi,
FileSystem,
Hats,
IDE,
ScopeProvider,
useFallback,
} from "@cursorless/common";
import {
CommandRunnerDecorator,
Expand Down Expand Up @@ -89,8 +87,7 @@ export function createCursorlessEngine(
},

async runCommandSafe(...args: unknown[]) {
const command = ensureCommandShape(args);
const response = await runCommand(
return runCommand(
treeSitter,
commandServerApi,
debug,
Expand All @@ -100,9 +97,8 @@ export function createCursorlessEngine(
languageDefinitions,
rangeUpdater,
commandRunnerDecorators,
command,
ensureCommandShape(args),
);
return unwrapCommandResponse(command, response);
},
},
scopeProvider: createScopeProvider(
Expand All @@ -123,19 +119,6 @@ export function createCursorlessEngine(
};
}

async function unwrapCommandResponse(
command: Command,
response: CommandResponse,
): Promise<CommandResponse | unknown> {
if (useFallback(command)) {
return response;
}
if ("returnValue" in response) {
return response.returnValue;
}
return undefined;
}

function createScopeProvider(
languageDefinitions: LanguageDefinitions,
storedTargets: StoredTargetMap,
Expand Down
20 changes: 18 additions & 2 deletions packages/cursorless-engine/src/runCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CommandServerApi,
HatTokenMap,
ReadOnlyHatMap,
clientSupportsFallback,
} from "@cursorless/common";
import { CommandRunner } from "./CommandRunner";
import { Actions } from "./actions/Actions";
Expand Down Expand Up @@ -43,7 +44,7 @@ export async function runCommand(
rangeUpdater: RangeUpdater,
commandRunnerDecorators: CommandRunnerDecorator[],
command: Command,
): Promise<CommandResponse> {
): Promise<CommandResponse | unknown> {
if (debug.active) {
debug.log(`command:`);
debug.log(JSON.stringify(command, null, 2));
Expand All @@ -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<CommandResponse | unknown> {
if (clientSupportsFallback(command)) {
return response;
}
if ("returnValue" in response) {
return response.returnValue;
}
return undefined;
}

function createCommandRunner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
TestCaseFixtureLegacy,
TextEditor,
TokenHat,
useFallback,
clientSupportsFallback,
} from "@cursorless/common";
import {
getCursorlessApi,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5b7371e

Please sign in to comment.