Skip to content

Commit 8b5a95f

Browse files
Use proximal and distal
1 parent 34cbbb1 commit 8b5a95f

File tree

1 file changed

+30
-73
lines changed

1 file changed

+30
-73
lines changed

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

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -40,79 +40,37 @@ export class ContiguousScopeHandler extends BaseScopeHandler {
4040
return this.scopeHandler.iterationScopeType;
4141
}
4242

43-
generateScopeCandidates(
43+
*generateScopeCandidates(
4444
editor: TextEditor,
4545
position: Position,
4646
direction: Direction,
4747
_hints: ScopeIteratorRequirements,
4848
): Iterable<TargetScope> {
49-
return direction === "backward"
50-
? this.generateScopeCandidatesBackward(editor, position)
51-
: this.generateScopeCandidatesForward(editor, position);
52-
}
53-
54-
private *generateScopeCandidatesBackward(
55-
editor: TextEditor,
56-
position: Position,
57-
): Iterable<TargetScope> {
58-
let targetRangeForward = next(
49+
let targetRangeOpposite = next(
5950
generateTargetRangesInDirection(
6051
this.scopeHandler,
6152
editor,
6253
position,
63-
"forward",
54+
direction === "forward" ? "backward" : "forward",
6455
),
6556
);
6657

67-
const targetRangesBackwardIter = generateTargetRangesInDirection(
58+
const targetRangesIter = generateTargetRangesInDirection(
6859
this.scopeHandler,
6960
editor,
7061
position,
71-
"backward",
62+
direction,
7263
);
7364

74-
for (const targetRange of targetRangesBackwardIter) {
65+
for (const targetRange of targetRangesIter) {
7566
if (
76-
targetRangeForward != null &&
77-
isAdjacent(targetRange[1], targetRangeForward[0])
67+
targetRangeOpposite != null &&
68+
isAdjacent(targetRangeOpposite.proximal, targetRange.proximal)
7869
) {
79-
yield targetsToScope(targetRange[0], targetRangeForward[1]);
80-
targetRangeForward = undefined;
70+
yield targetsToScope(targetRangeOpposite.distal, targetRange.distal);
71+
targetRangeOpposite = undefined;
8172
} else {
82-
yield targetsToScope(...targetRange);
83-
}
84-
}
85-
}
86-
87-
private *generateScopeCandidatesForward(
88-
editor: TextEditor,
89-
position: Position,
90-
): Iterable<TargetScope> {
91-
let targetRangeBackward = next(
92-
generateTargetRangesInDirection(
93-
this.scopeHandler,
94-
editor,
95-
position,
96-
"backward",
97-
),
98-
);
99-
100-
const targetRangesForwardIter = generateTargetRangesInDirection(
101-
this.scopeHandler,
102-
editor,
103-
position,
104-
"forward",
105-
);
106-
107-
for (const targetRange of targetRangesForwardIter) {
108-
if (
109-
targetRangeBackward != null &&
110-
isAdjacent(targetRangeBackward[1], targetRange[0])
111-
) {
112-
yield targetsToScope(targetRangeBackward[0], targetRange[1]);
113-
targetRangeBackward = undefined;
114-
} else {
115-
yield targetsToScope(...targetRange);
73+
yield targetsToScope(targetRange.proximal, targetRange.distal);
11674
}
11775
}
11876
}
@@ -143,9 +101,8 @@ function* generateTargetRangesInDirection(
143101
editor: TextEditor,
144102
position: Position,
145103
direction: Direction,
146-
): Iterable<[Target, Target]> {
147-
const isForward = direction === "forward";
148-
let first, last: Target | undefined;
104+
): Iterable<{ proximal: Target; distal: Target }> {
105+
let proximal, distal: Target | undefined;
149106

150107
const generator = scopeHandler.generateScopes(editor, position, direction, {
151108
allowAdjacentScopes: true,
@@ -154,37 +111,37 @@ function* generateTargetRangesInDirection(
154111

155112
for (const scope of generator) {
156113
for (const target of scope.getTargets(false)) {
157-
if (first == null) {
158-
first = target;
114+
if (proximal == null) {
115+
proximal = target;
159116
}
160117

161-
if (last != null) {
162-
const [leadingTarget, trailingTarget] = isForward
163-
? [last, target]
164-
: [target, last];
165-
166-
if (!isAdjacent(leadingTarget, trailingTarget)) {
167-
yield isForward ? [first, last] : [last, first];
168-
first = target;
118+
if (distal != null) {
119+
if (!isAdjacent(distal, target)) {
120+
yield { proximal, distal };
121+
proximal = target;
169122
}
170123
}
171124

172-
last = target;
125+
distal = target;
173126
}
174127
}
175128

176-
if (first != null && last != null) {
177-
yield isForward ? [first, last] : [last, first];
129+
if (proximal != null && distal != null) {
130+
yield { proximal, distal };
178131
}
179132
}
180133

181-
function isAdjacent(leadingTarget: Target, trailingTarget: Target): boolean {
182-
if (
183-
leadingTarget.contentRange.intersection(trailingTarget.contentRange) != null
184-
) {
134+
function isAdjacent(target1: Target, target2: Target): boolean {
135+
if (target1.contentRange.isRangeEqual(target2.contentRange)) {
185136
return true;
186137
}
187138

139+
const [leadingTarget, trailingTarget] = target1.contentRange.start.isBefore(
140+
target2.contentRange.start,
141+
)
142+
? [target1, target2]
143+
: [target2, target1];
144+
188145
const leadingRange =
189146
leadingTarget.getTrailingDelimiterTarget()?.contentRange ??
190147
leadingTarget.contentRange;

0 commit comments

Comments
 (0)