diff --git a/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts index 6b22f1c783..1cfa2b39be 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts @@ -34,7 +34,7 @@ export class ContainingScopeStage implements ModifierStage { run(target: Target): Target[] { const { scopeType, ancestorIndex = 0 } = this.modifier; - const scopeHandler = this.scopeHandlerFactory.create( + const scopeHandler = this.scopeHandlerFactory.tryCreate( scopeType, target.editor.document.languageId, ); diff --git a/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts index 8d8f00df85..32252be996 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts @@ -39,7 +39,7 @@ export class EveryScopeStage implements ModifierStage { const { scopeType } = this.modifier; const { editor, isReversed } = target; - const scopeHandler = this.scopeHandlerFactory.create( + const scopeHandler = this.scopeHandlerFactory.tryCreate( scopeType, editor.document.languageId, ); @@ -108,7 +108,7 @@ export class EveryScopeStage implements ModifierStage { scopeHandlerFactory: ScopeHandlerFactory, target: Target, ): Range[] { - const iterationScopeHandler = scopeHandlerFactory.create( + const iterationScopeHandler = scopeHandlerFactory.tryCreate( scopeHandler.iterationScopeType, target.editor.document.languageId, ); diff --git a/packages/cursorless-engine/src/processTargets/modifiers/PreferredScopeStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/PreferredScopeStage.ts index d04fd6d86d..03e9fb7ca3 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/PreferredScopeStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/PreferredScopeStage.ts @@ -41,7 +41,7 @@ export class PreferredScopeStage implements ModifierStage { } } - const scopeHandler = this.scopeHandlerFactory.createWithAssert( + const scopeHandler = this.scopeHandlerFactory.create( this.modifier.scopeType, target.editor.document.languageId, ); diff --git a/packages/cursorless-engine/src/processTargets/modifiers/RelativeScopeStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/RelativeScopeStage.ts index c43c62a006..2ad109e072 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/RelativeScopeStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/RelativeScopeStage.ts @@ -31,7 +31,7 @@ export class RelativeScopeStage implements ModifierStage { ) {} run(target: Target): Target[] { - const scopeHandler = this.scopeHandlerFactory.create( + const scopeHandler = this.scopeHandlerFactory.tryCreate( this.modifier.scopeType, target.editor.document.languageId, ); diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/BoundedScopeHandler.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/BoundedScopeHandler.ts index cfe5b23745..56d0f6bbf2 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/BoundedScopeHandler.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/BoundedScopeHandler.ts @@ -32,18 +32,17 @@ abstract class BoundedBaseScopeHandler extends BaseScopeHandler { ) { super(); - this.targetScopeHandler = this.scopeHandlerFactory.createWithAssert( + this.targetScopeHandler = this.scopeHandlerFactory.create( this.targetScopeType, this.languageId, ); - this.surroundingPairInteriorScopeHandler = - this.scopeHandlerFactory.createWithAssert( - { - type: "surroundingPairInterior", - delimiter: "any", - }, - this.languageId, - ); + this.surroundingPairInteriorScopeHandler = this.scopeHandlerFactory.create( + { + type: "surroundingPairInterior", + delimiter: "any", + }, + this.languageId, + ); } get iterationScopeType(): ScopeType { diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/NestedScopeHandler.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/NestedScopeHandler.ts index d8284b2744..95af8c8675 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/NestedScopeHandler.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/NestedScopeHandler.ts @@ -60,7 +60,7 @@ export abstract class NestedScopeHandler extends BaseScopeHandler { private get searchScopeHandler(): ScopeHandler { if (this._searchScopeHandler == null) { - this._searchScopeHandler = this.scopeHandlerFactory.createWithAssert( + this._searchScopeHandler = this.scopeHandlerFactory.create( this.searchScopeType, this.languageId, ); diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/OneOfScopeHandler.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/OneOfScopeHandler.ts index 6a8f91d28e..c484ef76a9 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/OneOfScopeHandler.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/OneOfScopeHandler.ts @@ -24,8 +24,7 @@ export class OneOfScopeHandler extends BaseScopeHandler { languageId: string, ): ScopeHandler { const scopeHandlers: ScopeHandler[] = scopeType.scopeTypes.map( - (scopeType) => - scopeHandlerFactory.createWithAssert(scopeType, languageId), + (scopeType) => scopeHandlerFactory.create(scopeType, languageId), ); const iterationScopeType = (): CustomScopeType => ({ @@ -33,7 +32,7 @@ export class OneOfScopeHandler extends BaseScopeHandler { scopeHandler: new OneOfScopeHandler( undefined, scopeHandlers.map((scopeHandler) => - scopeHandlerFactory.createWithAssert( + scopeHandlerFactory.create( scopeHandler.iterationScopeType, languageId, ), diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactory.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactory.ts index 74229e5759..977332839a 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactory.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactory.ts @@ -2,12 +2,12 @@ import type { ScopeType } from "@cursorless/common"; import type { CustomScopeType, ScopeHandler } from "./scopeHandler.types"; export interface ScopeHandlerFactory { - create( + tryCreate( scopeType: ScopeType | CustomScopeType, languageId: string, ): ScopeHandler | undefined; - createWithAssert( + create( scopeType: ScopeType | CustomScopeType, languageId: string, ): ScopeHandler; diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts index 149990b23c..b4b071c3a6 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts @@ -45,10 +45,10 @@ import type { CustomScopeType, ScopeHandler } from "./scopeHandler.types"; */ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory { constructor(private languageDefinitions: LanguageDefinitions) { - this.create = this.create.bind(this); + this.tryCreate = this.tryCreate.bind(this); } - create( + tryCreate( scopeType: ScopeType | CustomScopeType, languageId: string, ): ScopeHandler | undefined { @@ -115,11 +115,11 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory { } } - createWithAssert( + create( scopeType: ScopeType | CustomScopeType, languageId: string, ): ScopeHandler { - const handler = this.create(scopeType, languageId); + const handler = this.tryCreate(scopeType, languageId); if (handler == null) { throw new Error(`Couldn't create scope handler for '${scopeType.type}'`); } diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/SurroundingPairInteriorScopeHandler.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/SurroundingPairInteriorScopeHandler.ts index 289423a7c5..feb91a40a5 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/SurroundingPairInteriorScopeHandler.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/SurroundingPairInteriorScopeHandler.ts @@ -17,15 +17,14 @@ export class SurroundingPairInteriorScopeHandler extends BaseScopeHandler { ) { super(); - this.surroundingPairScopeHandler = - this.scopeHandlerFactory.createWithAssert( - { - type: "surroundingPair", - delimiter: this.scopeType.delimiter, - requireStrongContainment: true, - }, - this.languageId, - ); + this.surroundingPairScopeHandler = this.scopeHandlerFactory.create( + { + type: "surroundingPair", + delimiter: this.scopeType.delimiter, + requireStrongContainment: true, + }, + this.languageId, + ); } get iterationScopeType() { diff --git a/packages/cursorless-engine/src/scopeProviders/ScopeRangeProvider.ts b/packages/cursorless-engine/src/scopeProviders/ScopeRangeProvider.ts index c69fe4c860..15002defc3 100644 --- a/packages/cursorless-engine/src/scopeProviders/ScopeRangeProvider.ts +++ b/packages/cursorless-engine/src/scopeProviders/ScopeRangeProvider.ts @@ -29,7 +29,7 @@ export class ScopeRangeProvider { editor: TextEditor, { scopeType, visibleOnly }: ScopeRangeConfig, ): ScopeRanges[] { - const scopeHandler = this.scopeHandlerFactory.create( + const scopeHandler = this.scopeHandlerFactory.tryCreate( scopeType, editor.document.languageId, ); @@ -50,13 +50,16 @@ export class ScopeRangeProvider { { scopeType, visibleOnly, includeNestedTargets }: IterationScopeRangeConfig, ): IterationScopeRanges[] { const { languageId } = editor.document; - const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId); + const scopeHandler = this.scopeHandlerFactory.tryCreate( + scopeType, + languageId, + ); if (scopeHandler == null) { return []; } - const iterationScopeHandler = this.scopeHandlerFactory.create( + const iterationScopeHandler = this.scopeHandlerFactory.tryCreate( scopeHandler.iterationScopeType, languageId, ); diff --git a/packages/cursorless-engine/src/scopeProviders/ScopeSupportChecker.ts b/packages/cursorless-engine/src/scopeProviders/ScopeSupportChecker.ts index a3844346eb..c1d812948d 100644 --- a/packages/cursorless-engine/src/scopeProviders/ScopeSupportChecker.ts +++ b/packages/cursorless-engine/src/scopeProviders/ScopeSupportChecker.ts @@ -29,7 +29,10 @@ export class ScopeSupportChecker { */ getScopeSupport(editor: TextEditor, scopeType: ScopeType): ScopeSupport { const { languageId } = editor.document; - const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId); + const scopeHandler = this.scopeHandlerFactory.tryCreate( + scopeType, + languageId, + ); if (scopeHandler == null) { return getLegacyScopeSupport(languageId, scopeType); @@ -53,13 +56,16 @@ export class ScopeSupportChecker { scopeType: ScopeType, ): ScopeSupport { const { languageId } = editor.document; - const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId); + const scopeHandler = this.scopeHandlerFactory.tryCreate( + scopeType, + languageId, + ); if (scopeHandler == null) { return getLegacyScopeSupport(languageId, scopeType); } - const iterationScopeHandler = this.scopeHandlerFactory.create( + const iterationScopeHandler = this.scopeHandlerFactory.tryCreate( scopeHandler.iterationScopeType, languageId, );