-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathContiguousScopeHandler.ts
More file actions
169 lines (148 loc) · 4.38 KB
/
ContiguousScopeHandler.ts
File metadata and controls
169 lines (148 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
ContiguousScopeType,
Direction,
Position,
Range,
ScopeType,
TextEditor,
next,
} from "@cursorless/common";
import { ScopeHandlerFactory } from ".";
import { Target } from "../../../typings/target.types";
import { constructScopeRangeTarget } from "../constructScopeRangeTarget";
import { BaseScopeHandler } from "./BaseScopeHandler";
import type { TargetScope } from "./scope.types";
import type {
CustomScopeType,
ScopeHandler,
ScopeIteratorRequirements,
} from "./scopeHandler.types";
export class ContiguousScopeHandler extends BaseScopeHandler {
protected readonly isHierarchical = false;
private readonly scopeHandler: ScopeHandler;
constructor(
private scopeHandlerFactory: ScopeHandlerFactory,
public scopeType: ContiguousScopeType,
languageId: string,
) {
super();
const handler = scopeHandlerFactory.create(scopeType.scopeType, languageId);
if (handler == null) {
throw new Error(
`No available scope handler for '${scopeType.scopeType.type}'`,
);
}
this.scopeHandler = handler;
}
get iterationScopeType(): ScopeType | CustomScopeType {
return this.scopeHandler.iterationScopeType;
}
*generateScopeCandidates(
editor: TextEditor,
position: Position,
direction: Direction,
_hints: ScopeIteratorRequirements,
): Iterable<TargetScope> {
let targetRangeOpposite = next(
generateTargetRangesInDirection(
this.scopeHandler,
editor,
position,
direction === "forward" ? "backward" : "forward",
),
);
const targetRangesIter = generateTargetRangesInDirection(
this.scopeHandler,
editor,
position,
direction,
);
for (const targetRange of targetRangesIter) {
if (
targetRangeOpposite != null &&
isAdjacent(targetRangeOpposite.proximal, targetRange.proximal)
) {
yield combineScopes(targetRangeOpposite.distal, targetRange.distal);
targetRangeOpposite = undefined;
} else {
yield combineScopes(targetRange.proximal, targetRange.distal);
}
}
}
}
function combineScopes(scope1: TargetScope, scope2: TargetScope): TargetScope {
if (scope1.domain.isRangeEqual(scope2.domain)) {
return scope1;
}
return {
editor: scope1.editor,
domain: scope1.domain.union(scope2.domain),
getTargets: (isReversed) => {
return constructScopeRangeTarget(isReversed, scope1, scope2);
},
};
}
function* generateTargetRangesInDirection(
scopeHandler: ScopeHandler,
editor: TextEditor,
position: Position,
direction: Direction,
): Iterable<{ proximal: TargetScope; distal: TargetScope }> {
let proximal, distal: TargetScope | undefined;
const generator = scopeHandler.generateScopes(editor, position, direction, {
allowAdjacentScopes: true,
skipAncestorScopes: true,
});
for (const scope of generator) {
if (proximal == null) {
proximal = scope;
}
if (distal != null) {
if (!isAdjacent(distal, scope)) {
yield { proximal, distal };
proximal = scope;
}
}
distal = scope;
}
if (proximal != null && distal != null) {
yield { proximal, distal };
}
}
function isAdjacent(scope1: TargetScope, scope2: TargetScope): boolean {
if (scope1.domain.isRangeEqual(scope2.domain)) {
return true;
}
const [startTarget, endTarget] = getTargetsInDocumentOrder(
scope1.getTargets(false)[0],
scope2.getTargets(false)[0],
);
const leadingRange =
startTarget.getTrailingDelimiterTarget()?.contentRange ??
startTarget.contentRange;
const trailingRange =
endTarget.getLeadingDelimiterTarget()?.contentRange ??
endTarget.contentRange;
if (leadingRange.intersection(trailingRange) != null) {
return true;
}
// Non line targets are excluded if they are separated by more than one line
if (
!startTarget.isLine &&
trailingRange.start.line - leadingRange.end.line > 1
) {
return false;
}
// Finally targets are excluded if there is non whitespace text between them
const rangeBetween = new Range(leadingRange.end, trailingRange.start);
const text = startTarget.editor.document.getText(rangeBetween);
return /^\s*$/.test(text);
}
function getTargetsInDocumentOrder(
target1: Target,
target2: Target,
): [Target, Target] {
return target1.contentRange.start.isBefore(target2.contentRange.start)
? [target1, target2]
: [target2, target1];
}