Skip to content

Commit c39ca7e

Browse files
Optimized performance in bounded scope handler
1 parent 845cd19 commit c39ca7e

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,18 @@ abstract class BoundedBaseScopeHandler extends BaseScopeHandler {
100100
),
101101
);
102102

103+
if (interiorScopes.length === 0) {
104+
yield* targetScopes;
105+
return;
106+
}
107+
103108
for (const targetScope of targetScopes) {
104-
const allScopes: TargetScope[] = [];
109+
let allScopes: TargetScope[] | undefined;
105110

106111
for (const interiorScope of interiorScopes) {
107112
const domain = targetScope.domain.intersection(interiorScope.domain);
108113
if (domain != null && !domain.isEmpty) {
109-
allScopes.push({
114+
(allScopes ??= []).push({
110115
editor,
111116
domain,
112117
getTargets: (isReversed) => {
@@ -121,6 +126,30 @@ abstract class BoundedBaseScopeHandler extends BaseScopeHandler {
121126
}
122127
}
123128

129+
if (allScopes == null) {
130+
yield targetScope;
131+
continue;
132+
}
133+
134+
if (allScopes.length === 1) {
135+
const intersectionScope = allScopes[0];
136+
if (
137+
compareTargetScopes(
138+
direction,
139+
position,
140+
intersectionScope,
141+
targetScope,
142+
) <= 0
143+
) {
144+
yield intersectionScope;
145+
yield targetScope;
146+
} else {
147+
yield targetScope;
148+
yield intersectionScope;
149+
}
150+
continue;
151+
}
152+
124153
// NB: We add the target scope last so that if it is identical to
125154
// one of our intersection scopes, we prefer that one so that rich ranges
126155
// function properly
@@ -162,7 +191,7 @@ export class BoundedNonWhitespaceSequenceScopeHandler extends BoundedBaseScopeHa
162191
);
163192

164193
if (contentRange == null || contentRange.isEmpty) {
165-
throw Error("Expected non empty intersection");
194+
throw Error("Expected non-empty intersection");
166195
}
167196

168197
return new TokenTarget({

0 commit comments

Comments
 (0)