Skip to content

Commit ed7591e

Browse files
Assert creation of scope handlers
1 parent 8cf3464 commit ed7591e

File tree

7 files changed

+45
-37
lines changed

7 files changed

+45
-37
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ export class PreferredScopeStage implements ModifierStage {
4141
}
4242
}
4343

44-
const scopeHandler = this.scopeHandlerFactory.create(
44+
const scopeHandler = this.scopeHandlerFactory.createWithAssert(
4545
this.modifier.scopeType,
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/scopeHandlers/BoundedScopeHandler.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ abstract class BoundedBaseScopeHandler extends BaseScopeHandler {
3232
) {
3333
super();
3434

35-
this.targetScopeHandler = this.scopeHandlerFactory.create(
35+
this.targetScopeHandler = this.scopeHandlerFactory.createWithAssert(
3636
this.targetScopeType,
3737
this.languageId,
38-
)!;
39-
this.surroundingPairInteriorScopeHandler = this.scopeHandlerFactory.create(
40-
{
41-
type: "surroundingPairInterior",
42-
delimiter: "any",
43-
},
44-
this.languageId,
45-
)!;
38+
);
39+
this.surroundingPairInteriorScopeHandler =
40+
this.scopeHandlerFactory.createWithAssert(
41+
{
42+
type: "surroundingPairInterior",
43+
delimiter: "any",
44+
},
45+
this.languageId,
46+
);
4647
}
4748

4849
get iterationScopeType(): ScopeType {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export abstract class NestedScopeHandler extends BaseScopeHandler {
6060

6161
private get searchScopeHandler(): ScopeHandler {
6262
if (this._searchScopeHandler == null) {
63-
this._searchScopeHandler = this.scopeHandlerFactory.create(
63+
this._searchScopeHandler = this.scopeHandlerFactory.createWithAssert(
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: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,19 @@ 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) =>
28+
scopeHandlerFactory.createWithAssert(scopeType, languageId),
3429
);
3530

3631
const iterationScopeType = (): CustomScopeType => ({
3732
type: "custom",
3833
scopeHandler: new OneOfScopeHandler(
3934
undefined,
40-
scopeHandlers.map(
41-
(scopeHandler) =>
42-
scopeHandlerFactory.create(
43-
scopeHandler.iterationScopeType,
44-
languageId,
45-
)!,
35+
scopeHandlers.map((scopeHandler) =>
36+
scopeHandlerFactory.createWithAssert(
37+
scopeHandler.iterationScopeType,
38+
languageId,
39+
),
4640
),
4741
() => {
4842
throw new Error("Not implemented");

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ export interface ScopeHandlerFactory {
66
scopeType: ScopeType | CustomScopeType,
77
languageId: string,
88
): ScopeHandler | undefined;
9+
10+
createWithAssert(
11+
scopeType: ScopeType | CustomScopeType,
12+
languageId: string,
13+
): ScopeHandler;
914
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,15 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
114114
?.getScopeHandler(scopeType);
115115
}
116116
}
117+
118+
createWithAssert(
119+
scopeType: ScopeType | CustomScopeType,
120+
languageId: string,
121+
): ScopeHandler {
122+
const handler = this.create(scopeType, languageId);
123+
if (handler == null) {
124+
throw new Error(`Couldn't create scope handler for '${scopeType.type}'`);
125+
}
126+
return handler;
127+
}
117128
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ export class SurroundingPairInteriorScopeHandler extends BaseScopeHandler {
1717
) {
1818
super();
1919

20-
this.surroundingPairScopeHandler = this.scopeHandlerFactory.create(
21-
{
22-
type: "surroundingPair",
23-
delimiter: this.scopeType.delimiter,
24-
requireStrongContainment: true,
25-
},
26-
this.languageId,
27-
)!;
20+
this.surroundingPairScopeHandler =
21+
this.scopeHandlerFactory.createWithAssert(
22+
{
23+
type: "surroundingPair",
24+
delimiter: this.scopeType.delimiter,
25+
requireStrongContainment: true,
26+
},
27+
this.languageId,
28+
);
2829
}
2930

3031
get iterationScopeType() {

0 commit comments

Comments
 (0)