Skip to content

Commit 56f199b

Browse files
Use create instead of maybeCreate for scope handlers (#3043)
1 parent e90710d commit 56f199b

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

packages/common/src/errors.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export class UnsupportedLanguageError extends Error {
99
}
1010
}
1111

12-
export class UnsupportedError extends Error {
13-
constructor(message: string) {
14-
super(message);
15-
this.name = "UnsupportedError";
12+
export class UnsupportedScopeError extends Error {
13+
constructor(scopeType: string) {
14+
super(
15+
`Scope '${scopeType}' is not implemented yet; See ${DOCS_URL}/contributing/adding-a-new-scope`,
16+
);
17+
this.name = "UnsupportedScopeError";
1618
}
1719
}
1820

packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ export class ContainingScopeStage implements ModifierStage {
3434
run(target: Target): Target[] {
3535
const { scopeType, ancestorIndex = 0 } = this.modifier;
3636

37-
const scopeHandler = this.scopeHandlerFactory.maybeCreate(
37+
const scopeHandler = this.scopeHandlerFactory.create(
3838
scopeType,
3939
target.editor.document.languageId,
4040
);
4141

42-
if (scopeHandler == null) {
43-
throw new NoContainingScopeError(scopeType.type);
44-
}
45-
4642
const containingScopes = getContainingScopeTarget(
4743
target,
4844
scopeHandler,

packages/cursorless-engine/src/processTargets/modifiers/EveryScopeStage.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ export class EveryScopeStage implements ModifierStage {
4242
const { scopeType } = this.modifier;
4343
const { editor, isReversed } = target;
4444

45-
const scopeHandler = this.scopeHandlerFactory.maybeCreate(
45+
const scopeHandler = this.scopeHandlerFactory.create(
4646
scopeType,
4747
editor.document.languageId,
4848
);
4949

50-
if (scopeHandler == null) {
51-
throw new NoContainingScopeError(scopeType.type);
52-
}
53-
5450
let scopes: TargetScope[] | undefined;
5551

5652
if (target.hasExplicitRange) {
@@ -97,15 +93,11 @@ export class EveryScopeStage implements ModifierStage {
9793
scopeHandlerFactory: ScopeHandlerFactory,
9894
target: Target,
9995
): Range[] {
100-
const iterationScopeHandler = scopeHandlerFactory.maybeCreate(
96+
const iterationScopeHandler = scopeHandlerFactory.create(
10197
scopeHandler.iterationScopeType,
10298
target.editor.document.languageId,
10399
);
104100

105-
if (iterationScopeHandler == null) {
106-
throw Error("Could not find iteration scope handler");
107-
}
108-
109101
const iterationScopeTarget = getContainingScopeTarget(
110102
target,
111103
iterationScopeHandler,

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { pseudoScopes, type ScopeType } from "@cursorless/common";
1+
import {
2+
pseudoScopes,
3+
UnsupportedScopeError,
4+
type ScopeType,
5+
} from "@cursorless/common";
26
import type { LanguageDefinitions } from "../../../languages/LanguageDefinitions";
37
import {
48
BoundedNonWhitespaceSequenceScopeHandler,
@@ -145,7 +149,7 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
145149
): ScopeHandler {
146150
const handler = this.maybeCreate(scopeType, languageId);
147151
if (handler == null) {
148-
throw new Error(`Couldn't create scope handler for '${scopeType.type}'`);
152+
throw new UnsupportedScopeError(scopeType.type);
149153
}
150154
return handler;
151155
}

0 commit comments

Comments
 (0)