From 658c9f363af9e67c9363d527451e6e05a62dad64 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Mon, 14 Jul 2025 00:09:27 +0200 Subject: [PATCH 1/3] Use create instead of maybeCreate for scope handlers --- packages/common/src/errors.ts | 10 ++++++++++ .../processTargets/modifiers/ContainingScopeStage.ts | 6 +----- .../src/processTargets/modifiers/EveryScopeStage.ts | 12 ++---------- .../scopeHandlers/ScopeHandlerFactoryImpl.ts | 8 ++++++-- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index 3236fc5ed6..dcab1dca69 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -1,4 +1,5 @@ import { DOCS_URL } from "./constants"; +import type { ScopeTypeType } from "./types/command/PartialTargetDescriptor.types"; export class UnsupportedLanguageError extends Error { constructor(languageId: string) { @@ -9,6 +10,15 @@ export class UnsupportedLanguageError extends Error { } } +export class UnsupportedScopeError extends Error { + constructor(scopeType: string) { + super( + `Scope '${scopeType}' is not implemented yet; See ${DOCS_URL}/contributing/adding-a-new-scope`, + ); + this.name = "UnsupportedScopeError"; + } +} + export class UnsupportedError extends Error { constructor(message: string) { super(message); diff --git a/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts index fdbe17ab1b..23c23bf2f0 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts @@ -34,15 +34,11 @@ export class ContainingScopeStage implements ModifierStage { run(target: Target): Target[] { const { scopeType, ancestorIndex = 0 } = this.modifier; - const scopeHandler = this.scopeHandlerFactory.maybeCreate( + const scopeHandler = this.scopeHandlerFactory.create( scopeType, target.editor.document.languageId, ); - if (scopeHandler == null) { - throw new NoContainingScopeError(scopeType.type); - } - const containingScopes = getContainingScopeTarget( target, scopeHandler, diff --git a/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts b/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts index f9b3f43c43..6033a39001 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts @@ -42,15 +42,11 @@ export class EveryScopeStage implements ModifierStage { const { scopeType } = this.modifier; const { editor, isReversed } = target; - const scopeHandler = this.scopeHandlerFactory.maybeCreate( + const scopeHandler = this.scopeHandlerFactory.create( scopeType, editor.document.languageId, ); - if (scopeHandler == null) { - throw new NoContainingScopeError(scopeType.type); - } - let scopes: TargetScope[] | undefined; if (target.hasExplicitRange) { @@ -97,15 +93,11 @@ export class EveryScopeStage implements ModifierStage { scopeHandlerFactory: ScopeHandlerFactory, target: Target, ): Range[] { - const iterationScopeHandler = scopeHandlerFactory.maybeCreate( + const iterationScopeHandler = scopeHandlerFactory.create( scopeHandler.iterationScopeType, target.editor.document.languageId, ); - if (iterationScopeHandler == null) { - throw Error("Could not find iteration scope handler"); - } - const iterationScopeTarget = getContainingScopeTarget( target, iterationScopeHandler, diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts index 4002f883fe..b23314d897 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts @@ -1,4 +1,8 @@ -import { pseudoScopes, type ScopeType } from "@cursorless/common"; +import { + pseudoScopes, + UnsupportedScopeError, + type ScopeType, +} from "@cursorless/common"; import type { LanguageDefinitions } from "../../../languages/LanguageDefinitions"; import { BoundedNonWhitespaceSequenceScopeHandler, @@ -145,7 +149,7 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory { ): ScopeHandler { const handler = this.maybeCreate(scopeType, languageId); if (handler == null) { - throw new Error(`Couldn't create scope handler for '${scopeType.type}'`); + throw new UnsupportedScopeError(scopeType.type); } return handler; } From a4cfef0b7843bce6599008c1e9022a9bf57b2cce Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Mon, 14 Jul 2025 00:13:47 +0200 Subject: [PATCH 2/3] Remove unused import --- packages/common/src/errors.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index dcab1dca69..0bf54cec94 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -1,5 +1,4 @@ import { DOCS_URL } from "./constants"; -import type { ScopeTypeType } from "./types/command/PartialTargetDescriptor.types"; export class UnsupportedLanguageError extends Error { constructor(languageId: string) { From b9f532ba1a9dccb27be551fe483568d893aa19fc Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Mon, 14 Jul 2025 00:14:28 +0200 Subject: [PATCH 3/3] Remove unused error --- packages/common/src/errors.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index 0bf54cec94..90ee2b4c81 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -18,13 +18,6 @@ export class UnsupportedScopeError extends Error { } } -export class UnsupportedError extends Error { - constructor(message: string) { - super(message); - this.name = "UnsupportedError"; - } -} - export class OutdatedExtensionError extends Error { constructor() { super(