-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathinferFullTargets.ts
More file actions
235 lines (214 loc) · 7 KB
/
Copy pathinferFullTargets.ts
File metadata and controls
235 lines (214 loc) · 7 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import {
Mark,
Modifier,
PartialListTargetDescriptor,
PartialPrimitiveTargetDescriptor,
PartialRangeTargetDescriptor,
PartialTargetDescriptor,
PositionModifier,
PrimitiveTargetDescriptor,
RangeTargetDescriptor,
TargetDescriptor,
} from "../typings/targetDescriptor.types";
/**
* Performs inference on the partial targets provided by the user, using
* previous targets, global defaults, and action-specific defaults to fill out
* any details that may have been omitted in the spoken form.
* For example, we would automatically infer that `"take funk air and bat"` is
* equivalent to `"take funk air and funk bat"`.
* @param targets The partial targets which need to be completed by inference.
* @returns Target objects fully filled out and ready to be processed by {@link processTargets}.
*/
export default function inferFullTargets(
targets: PartialTargetDescriptor[]
): TargetDescriptor[] {
return targets.map((target, index) =>
inferTarget(target, targets.slice(0, index))
);
}
function inferTarget(
target: PartialTargetDescriptor,
previousTargets: PartialTargetDescriptor[]
): TargetDescriptor {
switch (target.type) {
case "list":
return inferListTarget(target, previousTargets);
case "range":
case "primitive":
return inferNonListTarget(target, previousTargets);
}
}
function inferListTarget(
target: PartialListTargetDescriptor,
previousTargets: PartialTargetDescriptor[]
): TargetDescriptor {
return {
...target,
elements: target.elements.map((element, index) =>
inferNonListTarget(
element,
previousTargets.concat(target.elements.slice(0, index))
)
),
};
}
function inferNonListTarget(
target: PartialPrimitiveTargetDescriptor | PartialRangeTargetDescriptor,
previousTargets: PartialTargetDescriptor[]
): PrimitiveTargetDescriptor | RangeTargetDescriptor {
switch (target.type) {
case "primitive":
return inferPrimitiveTarget(target, previousTargets);
case "range":
return inferRangeTarget(target, previousTargets);
}
}
function inferRangeTarget(
target: PartialRangeTargetDescriptor,
previousTargets: PartialTargetDescriptor[]
): RangeTargetDescriptor {
return {
type: "range",
excludeAnchor: target.excludeAnchor ?? false,
excludeActive: target.excludeActive ?? false,
rangeType: target.rangeType ?? "continuous",
anchor: inferPrimitiveTarget(target.anchor, previousTargets),
active: inferPrimitiveTarget(
target.active,
previousTargets.concat(target.anchor)
),
};
}
function inferPrimitiveTarget(
target: PartialPrimitiveTargetDescriptor,
previousTargets: PartialTargetDescriptor[]
): PrimitiveTargetDescriptor {
if (target.isImplicit) {
return {
type: "primitive",
mark: { type: "cursor" },
modifiers: [{ type: "toRawSelection" }],
};
}
const ownPositionalModifier = getPositionalModifier(target);
const ownNonPositionalModifiers = getNonPositionalModifiers(target);
// Position without a mark can be something like "take air past end of line"
// We will remove this case when we implement #736
const mark = target.mark ??
(ownPositionalModifier == null
? null
: getPreviousMark(previousTargets)) ?? {
type: "cursor",
};
const nonPositionalModifiers =
ownNonPositionalModifiers ??
getPreviousNonPositionalModifiers(previousTargets) ??
[];
const positionalModifier =
ownPositionalModifier ?? getPreviousPositionalModifier(previousTargets);
const modifiers = [
...(positionalModifier == null ? [] : [positionalModifier]),
...nonPositionalModifiers,
];
return {
type: target.type,
mark,
modifiers,
};
}
function getPositionalModifier(
target: PartialPrimitiveTargetDescriptor
): PositionModifier | undefined {
if (target.modifiers == null) {
return undefined;
}
const positionModifierIndex = target.modifiers.findIndex(
(modifier) => modifier.type === "position"
);
if (positionModifierIndex > 0) {
throw Error("Position modifiers must be at the start of a modifier chain");
}
return positionModifierIndex === -1
? undefined
: (target.modifiers[positionModifierIndex] as PositionModifier);
}
/**
* Return a list of non-positional modifiers on the given target. We return
* undefined if there are none. Note that we will never return an empty list; we
* will always return `undefined` if there are no non-positional modifiers.
* @param target The target from which to get the non-positional modifiers
* @returns A list of non-positional modifiers or `undefined` if there are none
*/
function getNonPositionalModifiers(
target: PartialPrimitiveTargetDescriptor
): Modifier[] | undefined {
const nonPositionalModifiers = target.modifiers?.filter(
(modifier) => modifier.type !== "position"
);
return nonPositionalModifiers == null || nonPositionalModifiers.length === 0
? undefined
: nonPositionalModifiers;
}
function getPreviousMark(
previousTargets: PartialTargetDescriptor[]
): Mark | undefined {
return getPreviousTargetAttribute(
previousTargets,
(target: PartialPrimitiveTargetDescriptor) => target.mark
);
}
function getPreviousNonPositionalModifiers(
previousTargets: PartialTargetDescriptor[]
): Modifier[] | undefined {
return getPreviousTargetAttribute(previousTargets, getNonPositionalModifiers);
}
function getPreviousPositionalModifier(
previousTargets: PartialTargetDescriptor[]
): PositionModifier | undefined {
return getPreviousTargetAttribute(previousTargets, getPositionalModifier);
}
/**
* Walks backward through the given targets and their descendants trying to find
* the first target for which the given attribute extractor returns a
* non-nullish value. Returns `undefined` if none could be found
* @param previousTargets The targets that precede the target we are trying to
* infer. We look in these targets and their descendants for the given attribute
* @param getAttribute The function used to extract the attribute from a
* primitive target
* @returns The extracted attribute or undefined if one could not be found
*/
function getPreviousTargetAttribute<T>(
previousTargets: PartialTargetDescriptor[],
getAttribute: (target: PartialPrimitiveTargetDescriptor) => T | undefined
): T | undefined {
// Search from back(last) to front(first)
for (let i = previousTargets.length - 1; i > -1; --i) {
const target = previousTargets[i];
switch (target.type) {
case "primitive": {
const attributeValue = getAttribute(target);
if (attributeValue != null) {
return attributeValue;
}
break;
}
case "range": {
const attributeValue = getAttribute(target.anchor);
if (attributeValue != null) {
return attributeValue;
}
break;
}
case "list":
const attributeValue = getPreviousTargetAttribute(
target.elements,
getAttribute
);
if (attributeValue != null) {
return attributeValue;
}
break;
}
}
return undefined;
}