Skip to content

Commit ff8d1d4

Browse files
Refactor
1 parent 23ecc35 commit ff8d1d4

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/CollectionItemScopeHandler/CollectionItemTextualScopeHandler.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,18 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
5858
const usedInteriors = new Set<Range>();
5959
const iterationStatesStack: IterationState[] = [];
6060

61-
// TODO: fixed performance on large files
6261
for (const separator of separatorRanges) {
6362
// Separators in a string are not considered
64-
if (stringRanges.some((range) => range.contains(separator))) {
63+
if (stringRanges.contains(separator)) {
6564
continue;
6665
}
6766

6867
const currentIterationState =
6968
iterationStatesStack[iterationStatesStack.length - 1];
7069

7170
// Get range for smallest containing interior
72-
const containingInteriorRange: Range | undefined = interiorRanges
73-
.filter((range) => range.contains(separator))
74-
.sort((a, b) => (a.contains(b) ? 1 : b.contains(a) ? -1 : 0))[0];
71+
const containingInteriorRange =
72+
interiorRanges.getsSmallestContaining(separator);
7573

7674
// The contain range is either the interior or the line containing the separator
7775
const containingIterationRange =
@@ -107,7 +105,9 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
107105
}
108106

109107
// New containing range. Add it to the list.
110-
usedInteriors.add(containingInteriorRange);
108+
if (containingInteriorRange != null) {
109+
usedInteriors.add(containingInteriorRange);
110+
}
111111

112112
iterationStatesStack.push({
113113
editor,
@@ -122,7 +122,7 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
122122
}
123123

124124
// Add interior ranges without a delimiter in them. eg: `[foo]`
125-
for (const interior of interiorRanges) {
125+
for (const interior of interiorRanges.ranges) {
126126
if (!usedInteriors.has(interior)) {
127127
const range = shrinkRangeToFitContent(editor, interior);
128128
if (!range.isEmpty) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { Range } from "@cursorless/common";
2+
3+
export class RangeIterator {
4+
private index = 0;
5+
6+
constructor(public ranges: Range[]) {}
7+
8+
contains(separator: Range): boolean {
9+
while (this.index < this.ranges.length) {
10+
const range = this.ranges[this.index];
11+
12+
if (range.contains(separator)) {
13+
return true;
14+
}
15+
16+
// Separator is after the range. Since the ranges are sorted, we can stop here.
17+
if (separator.start.isAfter(range.end)) {
18+
return false;
19+
}
20+
21+
this.index++;
22+
}
23+
24+
return false;
25+
}
26+
27+
getsSmallestContaining(separator: Range): Range | undefined {
28+
// TODO: fixed performance on large files
29+
return this.ranges
30+
.filter((range) => range.contains(separator))
31+
.sort((a, b) => (a.contains(b) ? 1 : b.contains(a) ? -1 : 0))[0];
32+
}
33+
}
34+
35+
// const containingInteriorRange: Range | undefined = interiorRanges
36+
// .filter((range) => range.contains(separator))
37+
// .sort((a, b) => (a.contains(b) ? 1 : b.contains(a) ? -1 : 0))[0];
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
import {
2-
type Range,
32
type SurroundingPairName,
43
type TextEditor,
54
Position,
65
} from "@cursorless/common";
76
import type { ScopeHandlerFactory } from "../ScopeHandlerFactory";
7+
import { RangeIterator } from "./RangeIterator";
88

99
export function getInteriorRanges(
1010
scopeHandlerFactory: ScopeHandlerFactory,
1111
languageId: string,
1212
editor: TextEditor,
1313
delimiter: SurroundingPairName,
14-
): Range[] {
14+
): RangeIterator {
1515
const scopeHandler = scopeHandlerFactory.create(
1616
{
1717
type: "surroundingPairInterior",
1818
delimiter,
1919
},
2020
languageId,
2121
);
22-
return Array.from(
22+
23+
const ranges = Array.from(
2324
scopeHandler.generateScopes(editor, new Position(0, 0), "forward", {
2425
containment: undefined,
2526
skipAncestorScopes: false,
2627
includeDescendantScopes: true,
2728
}),
2829
(scope) => scope.domain,
2930
);
31+
32+
return new RangeIterator(ranges);
3033
}

0 commit comments

Comments
 (0)