Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/common/src/types/SpokenFormType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type SimpleModifierType = Exclude<
| "ordinalScope"
| "relativeScope"
| "modifyIfUntyped"
| "cascading"
| "fallback"
| "range"
>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -480,7 +480,7 @@ export type Modifier =
| TrailingModifier
| RawSelectionModifier
| ModifyIfUntypedModifier
| CascadingModifier
| FallbackModifier
| RangeModifier
| KeepContentFilterModifier
| KeepEmptyFilterModifier
Expand Down
16 changes: 11 additions & 5 deletions packages/common/src/types/command/command.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/actions/InsertSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class InsertSnippet {
? []
: [
new ModifyIfUntypedExplicitStage(this.modifierStageFactory, {
type: "cascading",
type: "fallback",
modifiers: defaultScopeTypes.map((scopeType) => ({
type: "containingScope",
scopeType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class InsertSnippetLegacy {
? []
: [
new ModifyIfUntypedExplicitStage(this.modifierStageFactory, {
type: "cascading",
type: "fallback",
modifiers: defaultScopeTypes.map((scopeType) => ({
type: "containingScope",
scopeType,
Expand Down
6 changes: 3 additions & 3 deletions packages/cursorless-engine/src/core/getCommandFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
CommandServerApi,
DestinationDescriptor,
Fallback,
FallbackModifier,
FallbackCommandModifier,
PartialTargetDescriptor,
} from "@cursorless/common";
import type { ActionReturnValue } from "../actions/actions.types";
Expand Down Expand Up @@ -127,7 +127,7 @@ function targetIsSelection(target: PartialTargetDescriptor): boolean {

function getModifiersFromDestination(
destination: DestinationDescriptor,
): FallbackModifier[] {
): FallbackCommandModifier[] {
if (destination.type === "primitive") {
return getModifiersFromTarget(destination.target);
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/util/getScopeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading