Skip to content

Commit 84d803e

Browse files
Assert creation of scope handlers (#2684)
## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet
1 parent 6c86092 commit 84d803e

File tree

12 files changed

+48
-33
lines changed

12 files changed

+48
-33
lines changed

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

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class EveryScopeStage implements ModifierStage {
3939
const { scopeType } = this.modifier;
4040
const { editor, isReversed } = target;
4141

42-
const scopeHandler = this.scopeHandlerFactory.create(
42+
const scopeHandler = this.scopeHandlerFactory.maybeCreate(
4343
scopeType,
4444
editor.document.languageId,
4545
);
@@ -108,7 +108,7 @@ export class EveryScopeStage implements ModifierStage {
108108
scopeHandlerFactory: ScopeHandlerFactory,
109109
target: Target,
110110
): Range[] {
111-
const iterationScopeHandler = scopeHandlerFactory.create(
111+
const iterationScopeHandler = scopeHandlerFactory.maybeCreate(
112112
scopeHandler.iterationScopeType,
113113
target.editor.document.languageId,
114114
);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ export class PreferredScopeStage implements ModifierStage {
4646
target.editor.document.languageId,
4747
);
4848

49-
if (scopeHandler == null) {
50-
throw Error(`Couldn't create scope handler for: ${scopeType.type}`);
51-
}
52-
5349
const closestTargets = getClosestScopeTargets(target, scopeHandler);
5450

5551
if (closestTargets == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class RelativeScopeStage implements ModifierStage {
3131
) {}
3232

3333
run(target: Target): Target[] {
34-
const scopeHandler = this.scopeHandlerFactory.create(
34+
const scopeHandler = this.scopeHandlerFactory.maybeCreate(
3535
this.modifier.scopeType,
3636
target.editor.document.languageId,
3737
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ abstract class BoundedBaseScopeHandler extends BaseScopeHandler {
3535
this.targetScopeHandler = this.scopeHandlerFactory.create(
3636
this.targetScopeType,
3737
this.languageId,
38-
)!;
38+
);
3939
this.surroundingPairInteriorScopeHandler = this.scopeHandlerFactory.create(
4040
{
4141
type: "surroundingPairInterior",
4242
delimiter: "any",
4343
},
4444
this.languageId,
45-
)!;
45+
);
4646
}
4747

4848
get iterationScopeType(): ScopeType {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export abstract class NestedScopeHandler extends BaseScopeHandler {
6363
this._searchScopeHandler = this.scopeHandlerFactory.create(
6464
this.searchScopeType,
6565
this.languageId,
66-
)!;
66+
);
6767
}
6868

6969
return this._searchScopeHandler;

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,18 @@ export class OneOfScopeHandler extends BaseScopeHandler {
2424
languageId: string,
2525
): ScopeHandler {
2626
const scopeHandlers: ScopeHandler[] = scopeType.scopeTypes.map(
27-
(scopeType) => {
28-
const handler = scopeHandlerFactory.create(scopeType, languageId);
29-
if (handler == null) {
30-
throw new Error(`No available scope handler for '${scopeType.type}'`);
31-
}
32-
return handler;
33-
},
27+
(scopeType) => scopeHandlerFactory.create(scopeType, languageId),
3428
);
3529

3630
const iterationScopeType = (): CustomScopeType => ({
3731
type: "custom",
3832
scopeHandler: new OneOfScopeHandler(
3933
undefined,
40-
scopeHandlers.map(
41-
(scopeHandler) =>
42-
scopeHandlerFactory.create(
43-
scopeHandler.iterationScopeType,
44-
languageId,
45-
)!,
34+
scopeHandlers.map((scopeHandler) =>
35+
scopeHandlerFactory.create(
36+
scopeHandler.iterationScopeType,
37+
languageId,
38+
),
4639
),
4740
() => {
4841
throw new Error("Not implemented");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import type { ScopeType } from "@cursorless/common";
22
import type { CustomScopeType, ScopeHandler } from "./scopeHandler.types";
33

44
export interface ScopeHandlerFactory {
5-
create(
5+
maybeCreate(
66
scopeType: ScopeType | CustomScopeType,
77
languageId: string,
88
): ScopeHandler | undefined;
9+
10+
create(
11+
scopeType: ScopeType | CustomScopeType,
12+
languageId: string,
13+
): ScopeHandler;
914
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ import type { CustomScopeType, ScopeHandler } from "./scopeHandler.types";
4545
*/
4646
export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
4747
constructor(private languageDefinitions: LanguageDefinitions) {
48+
this.maybeCreate = this.maybeCreate.bind(this);
4849
this.create = this.create.bind(this);
4950
}
5051

51-
create(
52+
maybeCreate(
5253
scopeType: ScopeType | CustomScopeType,
5354
languageId: string,
5455
): ScopeHandler | undefined {
@@ -114,4 +115,15 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
114115
?.getScopeHandler(scopeType);
115116
}
116117
}
118+
119+
create(
120+
scopeType: ScopeType | CustomScopeType,
121+
languageId: string,
122+
): ScopeHandler {
123+
const handler = this.maybeCreate(scopeType, languageId);
124+
if (handler == null) {
125+
throw new Error(`Couldn't create scope handler for '${scopeType.type}'`);
126+
}
127+
return handler;
128+
}
117129
}

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/SurroundingPairInteriorScopeHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class SurroundingPairInteriorScopeHandler extends BaseScopeHandler {
2424
requireStrongContainment: true,
2525
},
2626
this.languageId,
27-
)!;
27+
);
2828
}
2929

3030
get iterationScopeType() {

0 commit comments

Comments
 (0)