Skip to content

Commit 7630ea1

Browse files
Added last yielded index
1 parent 4298d59 commit 7630ea1

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
interface IteratorInfo<T> {
66
iterator: Iterator<T>;
77
value: T;
8+
index: number;
89
}
910

1011
/**
@@ -19,14 +20,15 @@ interface IteratorInfo<T> {
1920
export function getInitialIteratorInfos<T>(
2021
iterators: Iterator<T>[],
2122
): IteratorInfo<T>[] {
22-
return iterators.flatMap((iterator) => {
23+
return iterators.flatMap((iterator, i) => {
2324
const { value, done } = iterator.next();
2425
return done
2526
? []
2627
: [
2728
{
2829
iterator,
2930
value,
31+
index: i,
3032
},
3133
];
3234
});
@@ -47,10 +49,10 @@ export function advanceIteratorsUntil<T>(
4749
criterion: (arg: T) => boolean,
4850
): IteratorInfo<T>[] {
4951
return iteratorInfos.flatMap((iteratorInfo) => {
50-
const { iterator } = iteratorInfo;
52+
const { iterator, index } = iteratorInfo;
5153
let { value } = iteratorInfo;
52-
5354
let done: boolean | undefined = false;
55+
5456
while (!done && !criterion(value)) {
5557
({ value, done } = iterator.next());
5658
}
@@ -59,6 +61,6 @@ export function advanceIteratorsUntil<T>(
5961
return [];
6062
}
6163

62-
return [{ iterator, value }];
64+
return [{ iterator, value, index }];
6365
});
6466
}

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import type {
1717

1818
export class OneOfScopeHandler extends BaseScopeHandler {
1919
protected isHierarchical = true;
20+
private iterationScopeHandler: OneOfScopeHandler | undefined;
21+
private lastYieldedIndex: number | undefined;
2022

2123
static create(
2224
scopeHandlerFactory: ScopeHandlerFactory,
@@ -47,9 +49,8 @@ export class OneOfScopeHandler extends BaseScopeHandler {
4749
scopeHandlers: ScopeHandler[],
4850
languageId: string,
4951
): ScopeHandler {
50-
const iterationScopeType = (): CustomScopeType => ({
51-
type: "custom",
52-
scopeHandler: new OneOfScopeHandler(
52+
const getIterationScopeHandler = () =>
53+
new OneOfScopeHandler(
5354
undefined,
5455
scopeHandlers.map(
5556
(scopeHandler) =>
@@ -61,20 +62,29 @@ export class OneOfScopeHandler extends BaseScopeHandler {
6162
() => {
6263
throw new Error("Not implemented");
6364
},
64-
),
65-
});
65+
);
6666

67-
return new OneOfScopeHandler(scopeType, scopeHandlers, iterationScopeType);
67+
return new OneOfScopeHandler(
68+
scopeType,
69+
scopeHandlers,
70+
getIterationScopeHandler,
71+
);
6872
}
6973

7074
get iterationScopeType(): CustomScopeType {
71-
return this.getIterationScopeType();
75+
if (this.iterationScopeHandler == null) {
76+
this.iterationScopeHandler = this.getIterationScopeHandler();
77+
}
78+
return {
79+
type: "custom",
80+
scopeHandler: this.iterationScopeHandler,
81+
};
7282
}
7383

7484
private constructor(
7585
public readonly scopeType: OneOfScopeType | undefined,
7686
private scopeHandlers: ScopeHandler[],
77-
private getIterationScopeType: () => CustomScopeType,
87+
private getIterationScopeHandler: () => OneOfScopeHandler,
7888
) {
7989
super();
8090
}
@@ -85,6 +95,14 @@ export class OneOfScopeHandler extends BaseScopeHandler {
8595
direction: Direction,
8696
hints: ScopeIteratorRequirements,
8797
): Iterable<TargetScope> {
98+
// If we have used the iteration scope handler we only want to yield from it's handler
99+
if (this.iterationScopeHandler?.lastYieldedIndex != null) {
100+
const handlerIndex = this.iterationScopeHandler.lastYieldedIndex;
101+
const handler = this.scopeHandlers[handlerIndex];
102+
yield* handler.generateScopes(editor, position, direction, hints);
103+
return;
104+
}
105+
88106
const iterators = this.scopeHandlers.map((scopeHandler) =>
89107
scopeHandler
90108
.generateScopes(editor, position, direction, hints)
@@ -99,7 +117,9 @@ export class OneOfScopeHandler extends BaseScopeHandler {
99117
);
100118

101119
// Pick minimum scope according to canonical scope ordering
102-
const currentScope = iteratorInfos[0].value;
120+
const iteratorInfo = iteratorInfos[0];
121+
const currentScope = iteratorInfo.value;
122+
this.lastYieldedIndex = iteratorInfo.index;
103123

104124
yield currentScope;
105125

0 commit comments

Comments
 (0)