Skip to content

Commit bd86a64

Browse files
Continues target
1 parent 8b5a95f commit bd86a64

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
next,
99
} from "@cursorless/common";
1010
import { ScopeHandlerFactory } from ".";
11-
import type { Target } from "../../../typings/target.types";
11+
import { createContinuousRangeTarget } from "../../createContinuousRangeTarget";
1212
import { BaseScopeHandler } from "./BaseScopeHandler";
1313
import type { TargetScope } from "./scope.types";
1414
import type {
@@ -67,32 +67,32 @@ export class ContiguousScopeHandler extends BaseScopeHandler {
6767
targetRangeOpposite != null &&
6868
isAdjacent(targetRangeOpposite.proximal, targetRange.proximal)
6969
) {
70-
yield targetsToScope(targetRangeOpposite.distal, targetRange.distal);
70+
yield combineScopes(targetRangeOpposite.distal, targetRange.distal);
7171
targetRangeOpposite = undefined;
7272
} else {
73-
yield targetsToScope(targetRange.proximal, targetRange.distal);
73+
yield combineScopes(targetRange.proximal, targetRange.distal);
7474
}
7575
}
7676
}
7777
}
7878

79-
function targetsToScope(
80-
leadingTarget: Target,
81-
trailingTarget: Target,
82-
): TargetScope {
83-
if (leadingTarget.contentRange.isRangeEqual(trailingTarget.contentRange)) {
84-
return {
85-
editor: leadingTarget.editor,
86-
domain: leadingTarget.contentRange,
87-
getTargets: () => [leadingTarget],
88-
};
79+
function combineScopes(scope1: TargetScope, scope2: TargetScope): TargetScope {
80+
if (scope1.domain.isRangeEqual(scope2.domain)) {
81+
return scope1;
8982
}
9083

91-
const range = leadingTarget.contentRange.union(trailingTarget.contentRange);
9284
return {
93-
editor: leadingTarget.editor,
94-
domain: range,
95-
getTargets: () => [leadingTarget.withContentRange(range)],
85+
editor: scope1.editor,
86+
domain: scope1.domain.union(scope2.domain),
87+
getTargets: (isReversed) => [
88+
createContinuousRangeTarget(
89+
isReversed,
90+
scope1.getTargets(false)[0],
91+
scope2.getTargets(false)[0],
92+
true,
93+
true,
94+
),
95+
],
9696
};
9797
}
9898

@@ -101,41 +101,42 @@ function* generateTargetRangesInDirection(
101101
editor: TextEditor,
102102
position: Position,
103103
direction: Direction,
104-
): Iterable<{ proximal: Target; distal: Target }> {
105-
let proximal, distal: Target | undefined;
104+
): Iterable<{ proximal: TargetScope; distal: TargetScope }> {
105+
let proximal, distal: TargetScope | undefined;
106106

107107
const generator = scopeHandler.generateScopes(editor, position, direction, {
108108
allowAdjacentScopes: true,
109109
skipAncestorScopes: true,
110110
});
111111

112112
for (const scope of generator) {
113-
for (const target of scope.getTargets(false)) {
114-
if (proximal == null) {
115-
proximal = target;
116-
}
113+
if (proximal == null) {
114+
proximal = scope;
115+
}
117116

118-
if (distal != null) {
119-
if (!isAdjacent(distal, target)) {
120-
yield { proximal, distal };
121-
proximal = target;
122-
}
117+
if (distal != null) {
118+
if (!isAdjacent(distal, scope)) {
119+
yield { proximal, distal };
120+
proximal = scope;
123121
}
124-
125-
distal = target;
126122
}
123+
124+
distal = scope;
127125
}
128126

129127
if (proximal != null && distal != null) {
130128
yield { proximal, distal };
131129
}
132130
}
133131

134-
function isAdjacent(target1: Target, target2: Target): boolean {
135-
if (target1.contentRange.isRangeEqual(target2.contentRange)) {
132+
function isAdjacent(scope1: TargetScope, scope2: TargetScope): boolean {
133+
if (scope1.domain.isRangeEqual(scope2.domain)) {
136134
return true;
137135
}
138136

137+
const target1 = scope1.getTargets(false)[0];
138+
const target2 = scope2.getTargets(false)[0];
139+
139140
const [leadingTarget, trailingTarget] = target1.contentRange.start.isBefore(
140141
target2.contentRange.start,
141142
)

0 commit comments

Comments
 (0)