Skip to content

Commit 295e872

Browse files
Rename delimiter to separators
1 parent a390a19 commit 295e872

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
ScopeIteratorRequirements,
1616
} from "../scopeHandler.types";
1717
import type { ScopeHandlerFactory } from "../ScopeHandlerFactory";
18-
import { delimiterRegex } from "./getDelimiterOccurrences";
18+
import { separatorRegex } from "./getSeparatorOccurrences";
1919

2020
export class CollectionItemIterationScopeHandler extends BaseScopeHandler {
2121
public scopeType: ScopeType = { type: "collectionItem" };
@@ -59,6 +59,7 @@ export class CollectionItemIterationScopeHandler extends BaseScopeHandler {
5959

6060
yield* scopes;
6161

62+
// TODO: Use a line scope handler and yield only if matching regex. itertools?
6263
const lineScope = makeLineScope(editor, position);
6364

6465
if (lineScope != null) {
@@ -74,7 +75,7 @@ function makeLineScope(
7475
const contentRange = editor.document.lineAt(position.line).range;
7576
const text = editor.document.getText(contentRange);
7677

77-
if (!testRegex(delimiterRegex, text)) {
78+
if (!testRegex(separatorRegex, text)) {
7879
return null;
7980
}
8081

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

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
import type { ScopeHandlerFactory } from "../ScopeHandlerFactory";
1818
import { CollectionItemIterationScopeHandler } from "./CollectionItemIterationScopeHandler";
1919
import { createTargetScope } from "./createTargetScope";
20-
import { getDelimiterOccurrences } from "./getDelimiterOccurrences";
20+
import { getSeparatorOccurrences } from "./getSeparatorOccurrences";
2121

2222
export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
2323
public scopeType: ScopeType = { type: "collectionItem" };
@@ -50,7 +50,7 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
5050
delimiter,
5151
},
5252
this.languageId,
53-
)!;
53+
);
5454
return Array.from(
5555
scopeHandler.generateScopes(editor, new Position(0, 0), "forward", {
5656
containment: undefined,
@@ -69,17 +69,17 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
6969
): Iterable<TargetScope> {
7070
const { document } = editor;
7171
const isEveryScope = hints.containment == null && hints.skipAncestorScopes;
72-
const delimiterRanges = getDelimiterOccurrences(document);
72+
const separatorRanges = getSeparatorOccurrences(document);
7373

7474
const interiorRanges = this.getInteriorRanges(editor, "collectionBoundary");
7575
const stringRanges = this.getInteriorRanges(editor, "string");
7676

7777
const scopes: TargetScope[] = [];
7878
const usedInteriors = new Set<Range>();
79-
const previousIterationRanges: IterationState[] = [];
79+
const iterationStatesStack: IterationState[] = [];
8080

8181
function addScopes(state: IterationState) {
82-
const { delimiters, range: iterationRange } = state;
82+
const { delimiters, iterationRange: iterationRange } = state;
8383

8484
if (delimiters.length === 0) {
8585
return;
@@ -122,59 +122,63 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
122122
}
123123
}
124124

125-
for (const delimiter of delimiterRanges) {
126-
// Delimiters in a string are not considered
127-
if (stringRanges.some((range) => range.contains(delimiter))) {
125+
// TODO: fixed performance on large files
126+
for (const separator of separatorRanges) {
127+
// Separators in a string are not considered
128+
if (stringRanges.some((range) => range.contains(separator))) {
128129
continue;
129130
}
130131

131-
const currentState =
132-
previousIterationRanges[previousIterationRanges.length - 1];
132+
const currentIterationState =
133+
iterationStatesStack[iterationStatesStack.length - 1];
133134

134135
// Get range for smallest containing interior
135136
const containingInteriorRange: Range | undefined = interiorRanges
136-
.filter((range) => range.contains(delimiter))
137+
.filter((range) => range.contains(separator))
137138
.sort((a, b) => (a.contains(b) ? 1 : b.contains(a) ? -1 : 0))[0];
138139

139-
// The contain range is either the interior or the line containing the delimiter
140-
const containingRange =
141-
containingInteriorRange ?? document.lineAt(delimiter.start.line).range;
140+
// The contain range is either the interior or the line containing the separator
141+
const containingIterationRange =
142+
containingInteriorRange ?? document.lineAt(separator.start.line).range;
142143

143-
if (currentState != null) {
144-
// The current containing range is the same as the previous one. Just append delimiter.
145-
if (currentState.range.isRangeEqual(containingRange)) {
146-
currentState.delimiters.push(delimiter);
144+
if (currentIterationState != null) {
145+
// The current containing iteration range is the same as the previous one. Just append delimiter.
146+
if (
147+
currentIterationState.iterationRange.isRangeEqual(
148+
containingIterationRange,
149+
)
150+
) {
151+
currentIterationState.delimiters.push(separator);
147152
continue;
148153
}
149154

150155
// The current containing range does not intersect previous one. Add scopes and remove state.
151-
if (!currentState.range.contains(delimiter)) {
152-
addScopes(currentState);
156+
if (!currentIterationState.iterationRange.contains(separator)) {
157+
addScopes(currentIterationState);
153158
// Remove already added state
154-
previousIterationRanges.pop();
159+
iterationStatesStack.pop();
155160
}
156161
}
157162

158163
// The current containing range is the same as the previous one. Just append delimiter.
159-
if (previousIterationRanges.length > 0) {
160-
const lastState =
161-
previousIterationRanges[previousIterationRanges.length - 1];
162-
if (lastState.range.isRangeEqual(containingRange)) {
163-
lastState.delimiters.push(delimiter);
164+
if (iterationStatesStack.length > 0) {
165+
const lastState = iterationStatesStack[iterationStatesStack.length - 1];
166+
if (lastState.iterationRange.isRangeEqual(containingIterationRange)) {
167+
lastState.delimiters.push(separator);
164168
continue;
165169
}
166170
}
167171

168172
// New containing range. Add it to the list.
169173
usedInteriors.add(containingInteriorRange);
170174

171-
previousIterationRanges.push({
172-
range: containingRange,
173-
delimiters: [delimiter],
175+
iterationStatesStack.push({
176+
iterationRange: containingIterationRange,
177+
delimiters: [separator],
174178
});
175179
}
176180

177-
for (const state of previousIterationRanges) {
181+
for (const state of iterationStatesStack) {
178182
addScopes(state);
179183
}
180184

@@ -195,6 +199,6 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
195199
}
196200

197201
interface IterationState {
198-
range: Range;
202+
iterationRange: Range;
199203
delimiters: Range[];
200204
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { matchAll, Range, type TextDocument } from "@cursorless/common";
22

3-
const delimiter = ",";
3+
const separator = ",";
44

5-
export const delimiterRegex = new RegExp(delimiter, "g");
5+
export const separatorRegex = new RegExp(separator, "g");
66

7-
export function getDelimiterOccurrences(document: TextDocument): Range[] {
7+
export function getSeparatorOccurrences(document: TextDocument): Range[] {
88
const text = document.getText();
99

10-
return matchAll(text, delimiterRegex, (match): Range => {
10+
return matchAll(text, separatorRegex, (match): Range => {
1111
return new Range(
1212
document.positionAt(match.index!),
1313
document.positionAt(match.index! + match[0].length),

0 commit comments

Comments
 (0)