Skip to content

Commit f4a966a

Browse files
Made sorted scope handler a custom scope handler
1 parent 5c8023b commit f4a966a

4 files changed

Lines changed: 35 additions & 59 deletions

File tree

packages/lib-common/src/types/command/PartialTargetDescriptor.types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,6 @@ export interface SurroundingPairInteriorScopeType {
260260
delimiter: SurroundingPairName;
261261
}
262262

263-
export interface OneOfScopeType {
264-
type: "oneOf";
265-
scopeTypes: ScopeType[];
266-
}
267-
268263
export interface GlyphScopeType {
269264
type: "glyph";
270265
character: string;
@@ -275,7 +270,6 @@ export type ScopeType =
275270
| SurroundingPairScopeType
276271
| SurroundingPairInteriorScopeType
277272
| CustomRegexScopeType
278-
| OneOfScopeType
279273
| GlyphScopeType;
280274

281275
export interface ContainingSurroundingPairModifier extends ContainingScopeModifier {

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
2-
NoContainingScopeError,
32
type Direction,
43
type Position,
5-
type ScopeType,
64
type TextEditor,
75
} from "@cursorless/lib-common";
86
import { BaseScopeHandler } from "./BaseScopeHandler";
@@ -23,9 +21,17 @@ export class FallbackScopeHandler extends BaseScopeHandler {
2321
scopeType: FallbackScopeType,
2422
languageId: string,
2523
): ScopeHandler {
26-
const scopeHandlers = scopeType.scopeTypes.map((scopeType) =>
27-
scopeHandlerFactory.create(scopeType, languageId),
28-
);
24+
const scopeHandlers = scopeType.scopeTypes
25+
.map((scopeType) =>
26+
scopeHandlerFactory.maybeCreate(scopeType, languageId),
27+
)
28+
.filter(
29+
(scopeHandler): scopeHandler is ScopeHandler => scopeHandler != null,
30+
);
31+
32+
if (scopeHandlers.length === 1) {
33+
return scopeHandlers[0];
34+
}
2935

3036
return this.createFromScopeHandlers(scopeHandlers);
3137
}
@@ -38,10 +44,11 @@ export class FallbackScopeHandler extends BaseScopeHandler {
3844
super();
3945
}
4046

41-
get iterationScopeType(): ScopeType {
42-
throw new NoContainingScopeError(
43-
"Iteration scope for FallbackScopeHandler",
44-
);
47+
get iterationScopeType(): FallbackScopeType {
48+
return {
49+
type: "fallback",
50+
scopeTypes: this.scopeHandlers.map((s) => s.iterationScopeType),
51+
};
4552
}
4653

4754
*generateScopeCandidates(

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

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import type {
2-
Direction,
3-
OneOfScopeType,
4-
Position,
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 {
13-
CustomScopeType,
148
ScopeHandler,
159
ScopeIteratorRequirements,
10+
SortedScopeType,
1611
} from "./scopeHandler.types";
1712

1813
export class SortedScopeHandler extends BaseScopeHandler {
@@ -23,7 +18,7 @@ export class SortedScopeHandler extends BaseScopeHandler {
2318

2419
static create(
2520
scopeHandlerFactory: ScopeHandlerFactory,
26-
scopeType: OneOfScopeType,
21+
scopeType: SortedScopeType,
2722
languageId: string,
2823
): ScopeHandler {
2924
const scopeHandlers = scopeType.scopeTypes
@@ -38,50 +33,21 @@ export class SortedScopeHandler extends BaseScopeHandler {
3833
return scopeHandlers[0];
3934
}
4035

41-
return this.createFromScopeHandlers(
42-
scopeHandlerFactory,
43-
languageId,
44-
scopeHandlers,
45-
);
36+
return this.createFromScopeHandlers(scopeHandlers);
4637
}
4738

48-
static createFromScopeHandlers(
49-
scopeHandlerFactory: ScopeHandlerFactory,
50-
languageId: string,
51-
scopeHandlers: ScopeHandler[],
52-
): ScopeHandler {
53-
const getIterationScopeHandler = () =>
54-
new SortedScopeHandler(
55-
scopeHandlers.map((scopeHandler) =>
56-
scopeHandlerFactory.create(
57-
scopeHandler.iterationScopeType,
58-
languageId,
59-
),
60-
),
61-
() => {
62-
throw new Error(
63-
"SortedScopeHandler: Iteration scope is not implemented",
64-
);
65-
},
66-
);
67-
68-
return new SortedScopeHandler(scopeHandlers, getIterationScopeHandler);
39+
static createFromScopeHandlers(scopeHandlers: ScopeHandler[]): ScopeHandler {
40+
return new SortedScopeHandler(scopeHandlers);
6941
}
7042

71-
private constructor(
72-
private scopeHandlers: ScopeHandler[],
73-
private getIterationScopeHandler: () => SortedScopeHandler,
74-
) {
43+
private constructor(private scopeHandlers: ScopeHandler[]) {
7544
super();
7645
}
7746

78-
get iterationScopeType(): CustomScopeType {
79-
if (this.iterationScopeHandler == null) {
80-
this.iterationScopeHandler = this.getIterationScopeHandler();
81-
}
47+
get iterationScopeType(): SortedScopeType {
8248
return {
83-
type: "custom",
84-
scopeHandler: this.iterationScopeHandler,
49+
type: "sorted",
50+
scopeTypes: this.scopeHandlers.map((s) => s.iterationScopeType),
8551
};
8652
}
8753

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/scopeHandler.types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export interface FallbackScopeType {
2525
scopeTypes: (ScopeType | ComplexScopeType)[];
2626
}
2727

28+
/**
29+
* Used to handle sorted scope types. The scope types are yielded in sorted order.
30+
*/
31+
export interface SortedScopeType {
32+
type: "sorted";
33+
scopeTypes: (ScopeType | ComplexScopeType)[];
34+
}
35+
2836
/**
2937
* Used to handle conditional scope types. The predicate determines if the
3038
* scope should be yielded or not.
@@ -38,6 +46,7 @@ export interface ConditionalScopeType {
3846
export type ComplexScopeType =
3947
| CustomScopeType
4048
| FallbackScopeType
49+
| SortedScopeType
4150
| ConditionalScopeType;
4251

4352
/**

0 commit comments

Comments
 (0)