Skip to content

Commit 54ee838

Browse files
Change complex cope handlers to return undefined if the internal scopes cannot be created
1 parent 0a591a6 commit 54ee838

6 files changed

Lines changed: 64 additions & 38 deletions

File tree

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/CollectionItemScopeHandler/CollectionItemScopeHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class CollectionItemScopeHandler extends BaseScopeHandler {
4646
return textualScopeHandler;
4747
}
4848

49-
return SortedScopeHandler.createFromScopeHandlers([
49+
return new SortedScopeHandler(scopeHandlerFactory, languageId, [
5050
languageScopeHandler,
5151
textualScopeHandler,
5252
]);

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/ConditionalScopeHandler.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { BaseScopeHandler } from "./BaseScopeHandler";
1010
import type { TargetScope } from "./scope.types";
1111
import type {
1212
ConditionalScopeType,
13+
ScopeHandler,
1314
ScopeIteratorRequirements,
1415
} from "./scopeHandler.types";
1516
import type { ScopeHandlerFactory } from "./ScopeHandlerFactory";
@@ -18,10 +19,26 @@ export class ConditionalScopeHandler extends BaseScopeHandler {
1819
public scopeType = undefined;
1920
protected isHierarchical = true;
2021

21-
constructor(
22-
public scopeHandlerFactory: ScopeHandlerFactory,
22+
static maybeCreate(
23+
scopeHandlerFactory: ScopeHandlerFactory,
24+
conditionalScopeType: ConditionalScopeType,
25+
languageId: string,
26+
): ConditionalScopeHandler | undefined {
27+
const scopeHandler = scopeHandlerFactory.maybeCreate(
28+
conditionalScopeType.scopeType,
29+
languageId,
30+
);
31+
32+
if (scopeHandler == null) {
33+
return undefined;
34+
}
35+
36+
return new ConditionalScopeHandler(scopeHandler, conditionalScopeType);
37+
}
38+
39+
private constructor(
40+
private scopeHandler: ScopeHandler,
2341
private conditionalScopeType: ConditionalScopeType,
24-
private languageId: string,
2542
) {
2643
super();
2744
}
@@ -38,13 +55,8 @@ export class ConditionalScopeHandler extends BaseScopeHandler {
3855
direction: Direction,
3956
hints: ScopeIteratorRequirements,
4057
): Iterable<TargetScope> {
41-
const scopeHandler = this.scopeHandlerFactory.create(
42-
this.conditionalScopeType.scopeType,
43-
this.languageId,
44-
);
45-
4658
return ifilter(
47-
scopeHandler.generateScopes(editor, position, direction, hints),
59+
this.scopeHandler.generateScopes(editor, position, direction, hints),
4860
this.conditionalScopeType.predicate,
4961
);
5062
}

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/FallbackScopeHandler.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export class FallbackScopeHandler extends BaseScopeHandler {
1616
public scopeType = undefined;
1717
protected isHierarchical = true;
1818

19-
static create(
19+
static maybeCreate(
2020
scopeHandlerFactory: ScopeHandlerFactory,
2121
scopeType: FallbackScopeType,
2222
languageId: string,
23-
): ScopeHandler {
23+
): ScopeHandler | undefined {
2424
const scopeHandlers = scopeType.scopeTypes
2525
.map((scopeType) =>
2626
scopeHandlerFactory.maybeCreate(scopeType, languageId),
@@ -29,18 +29,18 @@ export class FallbackScopeHandler extends BaseScopeHandler {
2929
(scopeHandler): scopeHandler is ScopeHandler => scopeHandler != null,
3030
);
3131

32+
if (scopeHandlers.length === 0) {
33+
return undefined;
34+
}
35+
3236
if (scopeHandlers.length === 1) {
3337
return scopeHandlers[0];
3438
}
3539

36-
return this.createFromScopeHandlers(scopeHandlers);
37-
}
38-
39-
static createFromScopeHandlers(scopeHandlers: ScopeHandler[]): ScopeHandler {
4040
return new FallbackScopeHandler(scopeHandlers);
4141
}
4242

43-
private constructor(private scopeHandlers: ScopeHandler[]) {
43+
constructor(private scopeHandlers: ScopeHandler[]) {
4444
super();
4545
}
4646

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/NotebookCellScopeHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class NotebookCellScopeHandler extends BaseScopeHandler {
4646
return apiScopeHandler;
4747
}
4848

49-
return SortedScopeHandler.createFromScopeHandlers([
49+
return new SortedScopeHandler(scopeHandlerFactory, languageId, [
5050
languageScopeHandler,
5151
apiScopeHandler,
5252
]);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
136136
case "custom":
137137
return scopeType.scopeHandler;
138138
case "sorted":
139-
return SortedScopeHandler.create(this, scopeType, languageId);
139+
return SortedScopeHandler.maybeCreate(this, scopeType, languageId);
140140
case "fallback":
141-
return FallbackScopeHandler.create(this, scopeType, languageId);
141+
return FallbackScopeHandler.maybeCreate(this, scopeType, languageId);
142142
case "conditional":
143-
return new ConditionalScopeHandler(this, scopeType, languageId);
143+
return ConditionalScopeHandler.maybeCreate(this, scopeType, languageId);
144144
default:
145145
// Pseudoscopes are handled separately in their own modifiers.
146146
if (pseudoScopes.has(scopeType.type)) {

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/SortedScopeHandler.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import type {
2-
Direction,
3-
Position,
4-
ScopeType,
5-
TextEditor,
6-
} from "@cursorless/lib-common";
1+
import type { Direction, Position, TextEditor } from "@cursorless/lib-common";
72
import { BaseScopeHandler } from "./BaseScopeHandler";
83
import { advanceIteratorsUntil, getInitialIteratorInfos } from "./IteratorInfo";
94
import type { ScopeHandlerFactory } from "./ScopeHandlerFactory";
105
import { compareTargetScopes } from "./compareTargetScopes";
116
import type { TargetScope } from "./scope.types";
127
import type {
8+
CustomScopeType,
139
ScopeHandler,
1410
ScopeIteratorRequirements,
1511
SortedScopeType,
@@ -21,11 +17,11 @@ export class SortedScopeHandler extends BaseScopeHandler {
2117
private iterationScopeHandler: SortedScopeHandler | undefined;
2218
private lastYieldedIndex: number | undefined;
2319

24-
static create(
20+
static maybeCreate(
2521
scopeHandlerFactory: ScopeHandlerFactory,
2622
scopeType: SortedScopeType,
2723
languageId: string,
28-
): ScopeHandler {
24+
): ScopeHandler | undefined {
2925
const scopeHandlers = scopeType.scopeTypes
3026
.map((scopeType) =>
3127
scopeHandlerFactory.maybeCreate(scopeType, languageId),
@@ -34,25 +30,43 @@ export class SortedScopeHandler extends BaseScopeHandler {
3430
(scopeHandler): scopeHandler is ScopeHandler => scopeHandler != null,
3531
);
3632

33+
if (scopeHandlers.length === 0) {
34+
return undefined;
35+
}
36+
3737
if (scopeHandlers.length === 1) {
3838
return scopeHandlers[0];
3939
}
4040

41-
return this.createFromScopeHandlers(scopeHandlers);
42-
}
43-
44-
static createFromScopeHandlers(scopeHandlers: ScopeHandler[]): ScopeHandler {
45-
return new SortedScopeHandler(scopeHandlers);
41+
return new SortedScopeHandler(
42+
scopeHandlerFactory,
43+
languageId,
44+
scopeHandlers,
45+
);
4646
}
4747

48-
private constructor(private scopeHandlers: ScopeHandler[]) {
48+
constructor(
49+
private scopeHandlerFactory: ScopeHandlerFactory,
50+
private languageId: string,
51+
private scopeHandlers: ScopeHandler[],
52+
) {
4953
super();
5054
}
5155

52-
get iterationScopeType(): SortedScopeType {
56+
get iterationScopeType(): CustomScopeType {
57+
if (this.iterationScopeHandler == null) {
58+
const iterationScopeHandlers = this.scopeHandlers.map((s) =>
59+
this.scopeHandlerFactory.create(s.iterationScopeType, this.languageId),
60+
);
61+
this.iterationScopeHandler = new SortedScopeHandler(
62+
this.scopeHandlerFactory,
63+
this.languageId,
64+
iterationScopeHandlers,
65+
);
66+
}
5367
return {
54-
type: "sorted",
55-
scopeTypes: this.scopeHandlers.map((s) => s.iterationScopeType),
68+
type: "custom",
69+
scopeHandler: this.iterationScopeHandler,
5670
};
5771
}
5872

0 commit comments

Comments
 (0)