From af3b243d83d4ddfa15190f0442f6115d54f30720 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 13 Jul 2025 23:54:18 +0200 Subject: [PATCH] Rename cascadingModifier to fallbackModifier --- packages/common/src/types/SpokenFormType.ts | 2 +- .../command/PartialTargetDescriptor.types.ts | 6 ++--- .../common/src/types/command/command.types.ts | 16 ++++++++---- .../src/actions/InsertSnippet.ts | 2 +- .../snippetsLegacy/InsertSnippetLegacy.ts | 2 +- .../src/core/getCommandFallback.ts | 6 ++--- .../primitiveTargetToSpokenForm.ts | 2 +- .../ModifierStageFactoryImpl.ts | 6 ++--- .../{CascadingStage.ts => FallbackStage.ts} | 26 +++++++------------ .../scopeHandlers/FallbackScopeHandler.ts | 4 +-- .../src/util/getScopeType.ts | 2 +- 11 files changed, 36 insertions(+), 38 deletions(-) rename packages/cursorless-engine/src/processTargets/modifiers/{CascadingStage.ts => FallbackStage.ts} (55%) diff --git a/packages/common/src/types/SpokenFormType.ts b/packages/common/src/types/SpokenFormType.ts index 9032d40171..ab6613d378 100644 --- a/packages/common/src/types/SpokenFormType.ts +++ b/packages/common/src/types/SpokenFormType.ts @@ -65,7 +65,7 @@ type SimpleModifierType = Exclude< | "ordinalScope" | "relativeScope" | "modifyIfUntyped" - | "cascading" + | "fallback" | "range" >; diff --git a/packages/common/src/types/command/PartialTargetDescriptor.types.ts b/packages/common/src/types/command/PartialTargetDescriptor.types.ts index b64d3f8425..425ab13d9a 100644 --- a/packages/common/src/types/command/PartialTargetDescriptor.types.ts +++ b/packages/common/src/types/command/PartialTargetDescriptor.types.ts @@ -442,8 +442,8 @@ export interface ModifyIfUntypedModifier { * doesn't throw an error, returning the output from the first modifier not * throwing an error. */ -export interface CascadingModifier { - type: "cascading"; +export interface FallbackModifier { + type: "fallback"; /** * The modifiers to try in turn @@ -480,7 +480,7 @@ export type Modifier = | TrailingModifier | RawSelectionModifier | ModifyIfUntypedModifier - | CascadingModifier + | FallbackModifier | RangeModifier | KeepContentFilterModifier | KeepEmptyFilterModifier diff --git a/packages/common/src/types/command/command.types.ts b/packages/common/src/types/command/command.types.ts index baf934734f..c521123a9e 100644 --- a/packages/common/src/types/command/command.types.ts +++ b/packages/common/src/types/command/command.types.ts @@ -30,15 +30,21 @@ export type Command = export type CommandResponse = { returnValue: unknown } | { fallback: Fallback }; -export type FallbackModifier = Modifier | { type: "containingTokenIfEmpty" }; +export type FallbackCommandModifier = + | Modifier + | { type: "containingTokenIfEmpty" }; export type Fallback = - | { action: ActionDescriptor["name"]; modifiers: FallbackModifier[] } - | { action: "insert"; modifiers: FallbackModifier[]; text: string } - | { action: "callAsFunction"; modifiers: FallbackModifier[]; callee: string } + | { action: ActionDescriptor["name"]; modifiers: FallbackCommandModifier[] } + | { action: "insert"; modifiers: FallbackCommandModifier[]; text: string } + | { + action: "callAsFunction"; + modifiers: FallbackCommandModifier[]; + callee: string; + } | { action: "wrapWithPairedDelimiter" | "rewrapWithPairedDelimiter"; - modifiers: FallbackModifier[]; + modifiers: FallbackCommandModifier[]; left: string; right: string; }; diff --git a/packages/cursorless-engine/src/actions/InsertSnippet.ts b/packages/cursorless-engine/src/actions/InsertSnippet.ts index 87ed8dea6e..391e0ce74e 100644 --- a/packages/cursorless-engine/src/actions/InsertSnippet.ts +++ b/packages/cursorless-engine/src/actions/InsertSnippet.ts @@ -46,7 +46,7 @@ export default class InsertSnippet { ? [] : [ new ModifyIfUntypedExplicitStage(this.modifierStageFactory, { - type: "cascading", + type: "fallback", modifiers: defaultScopeTypes.map((scopeType) => ({ type: "containingScope", scopeType, diff --git a/packages/cursorless-engine/src/actions/snippetsLegacy/InsertSnippetLegacy.ts b/packages/cursorless-engine/src/actions/snippetsLegacy/InsertSnippetLegacy.ts index d4b134d06e..4accd9c0e0 100644 --- a/packages/cursorless-engine/src/actions/snippetsLegacy/InsertSnippetLegacy.ts +++ b/packages/cursorless-engine/src/actions/snippetsLegacy/InsertSnippetLegacy.ts @@ -43,7 +43,7 @@ export default class InsertSnippetLegacy { ? [] : [ new ModifyIfUntypedExplicitStage(this.modifierStageFactory, { - type: "cascading", + type: "fallback", modifiers: defaultScopeTypes.map((scopeType) => ({ type: "containingScope", scopeType, diff --git a/packages/cursorless-engine/src/core/getCommandFallback.ts b/packages/cursorless-engine/src/core/getCommandFallback.ts index 2ad510a80e..720547428a 100644 --- a/packages/cursorless-engine/src/core/getCommandFallback.ts +++ b/packages/cursorless-engine/src/core/getCommandFallback.ts @@ -4,7 +4,7 @@ import type { CommandServerApi, DestinationDescriptor, Fallback, - FallbackModifier, + FallbackCommandModifier, PartialTargetDescriptor, } from "@cursorless/common"; import type { ActionReturnValue } from "../actions/actions.types"; @@ -127,7 +127,7 @@ function targetIsSelection(target: PartialTargetDescriptor): boolean { function getModifiersFromDestination( destination: DestinationDescriptor, -): FallbackModifier[] { +): FallbackCommandModifier[] { if (destination.type === "primitive") { return getModifiersFromTarget(destination.target); } @@ -136,7 +136,7 @@ function getModifiersFromDestination( function getModifiersFromTarget( target: PartialTargetDescriptor, -): FallbackModifier[] { +): FallbackCommandModifier[] { if (target.type === "primitive") { if (target.modifiers != null && target.modifiers.length > 0) { return target.modifiers; diff --git a/packages/cursorless-engine/src/generateSpokenForm/primitiveTargetToSpokenForm.ts b/packages/cursorless-engine/src/generateSpokenForm/primitiveTargetToSpokenForm.ts index 9b657aedb8..a12ccb72d0 100644 --- a/packages/cursorless-engine/src/generateSpokenForm/primitiveTargetToSpokenForm.ts +++ b/packages/cursorless-engine/src/generateSpokenForm/primitiveTargetToSpokenForm.ts @@ -43,7 +43,7 @@ export class PrimitiveTargetSpokenFormGenerator { private handleModifier(modifier: Modifier): SpokenFormComponent { switch (modifier.type) { - case "cascading": + case "fallback": case "modifyIfUntyped": case "preferredScope": throw new NoSpokenFormError(`Modifier '${modifier.type}'`); diff --git a/packages/cursorless-engine/src/processTargets/ModifierStageFactoryImpl.ts b/packages/cursorless-engine/src/processTargets/ModifierStageFactoryImpl.ts index 34ae25fe18..5ad4c56a5a 100644 --- a/packages/cursorless-engine/src/processTargets/ModifierStageFactoryImpl.ts +++ b/packages/cursorless-engine/src/processTargets/ModifierStageFactoryImpl.ts @@ -3,11 +3,11 @@ import type { StoredTargetMap } from "../core/StoredTargets"; import type { LanguageDefinitions } from "../languages/LanguageDefinitions"; import type { ModifierStageFactory } from "./ModifierStageFactory"; import type { ModifierStage } from "./PipelineStages.types"; -import { CascadingStage } from "./modifiers/CascadingStage"; import { ClassFunctionNameStage } from "./modifiers/ClassFunctionNameStage"; import { ModifyIfUntypedStage } from "./modifiers/ConditionalModifierStages"; import { ContainingScopeStage } from "./modifiers/ContainingScopeStage"; import { EveryScopeStage } from "./modifiers/EveryScopeStage"; +import { FallbackStage } from "./modifiers/FallbackStage"; import { KeepContentFilterStage, KeepEmptyFilterStage, @@ -111,8 +111,8 @@ export class ModifierStageFactoryImpl implements ModifierStageFactory { return new KeepContentFilterStage(modifier); case "keepEmptyFilter": return new KeepEmptyFilterStage(modifier); - case "cascading": - return new CascadingStage(this, modifier); + case "fallback": + return new FallbackStage(this, modifier); case "modifyIfUntyped": return new ModifyIfUntypedStage(this, modifier); case "range": diff --git a/packages/cursorless-engine/src/processTargets/modifiers/CascadingStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/FallbackStage.ts similarity index 55% rename from packages/cursorless-engine/src/processTargets/modifiers/CascadingStage.ts rename to packages/cursorless-engine/src/processTargets/modifiers/FallbackStage.ts index 698fe9ee15..aae9266d6b 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/CascadingStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/FallbackStage.ts @@ -1,4 +1,4 @@ -import type { CascadingModifier } from "@cursorless/common"; +import type { FallbackModifier } from "@cursorless/common"; import type { Target } from "../../typings/target.types"; import type { ModifierStageFactory } from "../ModifierStageFactory"; import type { @@ -10,28 +10,20 @@ import type { * Tries each of the given modifiers in turn until one of them doesn't throw an * error, returning the output from the first modifier not throwing an error. */ -export class CascadingStage implements ModifierStage { - private nestedStages_?: ModifierStage[]; - +export class FallbackStage implements ModifierStage { constructor( private modifierStageFactory: ModifierStageFactory, - private modifier: CascadingModifier, + private modifier: FallbackModifier, ) {} - private get nestedStages() { - if (this.nestedStages_ == null) { - this.nestedStages_ = this.modifier.modifiers.map( - this.modifierStageFactory.create, - ); - } - - return this.nestedStages_; - } - run(target: Target, options: ModifierStateOptions): Target[] { - for (const nestedStage of this.nestedStages) { + const stages = this.modifier.modifiers.map( + this.modifierStageFactory.create, + ); + + for (const stage of stages) { try { - return nestedStage.run(target, options); + return stage.run(target, options); } catch (_error) { continue; } diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/FallbackScopeHandler.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/FallbackScopeHandler.ts index 911bfaafe9..daabcd8dc0 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/FallbackScopeHandler.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/FallbackScopeHandler.ts @@ -23,8 +23,8 @@ export class FallbackScopeHandler extends BaseScopeHandler { scopeType: FallbackScopeType, languageId: string, ): ScopeHandler { - const scopeHandlers: ScopeHandler[] = scopeType.scopeTypes.map( - (scopeType) => scopeHandlerFactory.create(scopeType, languageId), + const scopeHandlers = scopeType.scopeTypes.map((scopeType) => + scopeHandlerFactory.create(scopeType, languageId), ); return this.createFromScopeHandlers(scopeHandlers); diff --git a/packages/cursorless-engine/src/util/getScopeType.ts b/packages/cursorless-engine/src/util/getScopeType.ts index 0e9ee2e27c..1d6ad3b7cb 100644 --- a/packages/cursorless-engine/src/util/getScopeType.ts +++ b/packages/cursorless-engine/src/util/getScopeType.ts @@ -22,7 +22,7 @@ export function getScopeType(modifier: Modifier): ScopeType | undefined { case "endOf": case "extendThroughStartOf": case "extendThroughEndOf": - case "cascading": + case "fallback": case "range": case "modifyIfUntyped": return undefined;