-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
transformations.ts
276 lines (233 loc) · 6.54 KB
/
transformations.ts
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { Position, PositionDiff } from "./../motion/position";
import { Range } from "./../motion/range";
/**
* This file contains definitions of objects that represent text
* additions/deletions/replacements on the document. You'll add them
* to vimState.recordedState.transformations and then they will be applied
* later on.
*
* We do it in this way so they can all be processed in parallel and merged
* if necessary.
*/
/**
* Represents inserting text at a position in the document.
*/
export interface InsertTextTransformation {
/**
* Type of this insertion (used for type checking with discriminated
* union types).
*/
type : "insertText";
/**
* Text content of this insertion.
*/
text : string;
/**
* The location to insert the text.
*/
position: Position;
/**
* The index of the cursor that this transformation applies to.
*/
cursorIndex?: number;
/**
* A position diff that will be added to the position of the cursor after
* the replace transformation has been applied.
*
* If you don't know what this is, just ignore it. You probably don't need it.
*/
diff?: PositionDiff;
}
export interface ReplaceTextTransformation {
type: "replaceText";
/**
* Text to insert.
*/
text: string;
/**
* Start of location to replace.
*/
start: Position;
/**
* End of location to replace.
*/
end: Position;
/**
* The index of the cursor that this transformation applies to.
*/
cursorIndex?: number;
/**
* A position diff that will be added to the position of the cursor after
* the replace transformation has been applied.
*
* If you don't know what this is, just ignore it. You probably don't need it.
*/
diff?: PositionDiff;
}
/**
* Represents inserting a character and allowing visual studio to do
* its post-character stuff if it wants. (e.g., if you type "(" this
* will automatically add the closing ")").
*/
export interface InsertTextVSCodeTransformation {
type : "insertTextVSCode";
/**
* Text to insert.
*/
text : string;
/**
* The index of the cursor that this transformation applies to.
*/
cursorIndex?: number;
/**
* A position diff that will be added to the position of the cursor after
* the replace transformation has been applied.
*
* If you don't know what this is, just ignore it. You probably don't need it.
*/
diff?: PositionDiff;
}
/**
* Represents deleting a character at a position in the document.
*/
export interface DeleteTextTransformation {
type : "deleteText";
/**
* Position at which to delete a character.
*/
position : Position;
/**
* The index of the cursor that this transformation applies to.
*/
cursorIndex?: number;
/**
* A position diff that will be added to the position of the cursor after
* the replace transformation has been applied.
*
* If you don't know what this is, just ignore it. You probably don't need it.
*/
diff?: PositionDiff;
}
/**
* Represents deleting a range of characters.
*/
export interface DeleteTextRangeTransformation {
type : "deleteRange";
/**
* Range of characters to delete.
*/
range: Range;
/**
* A position diff that will be added to the position of the cursor after
* the replace transformation has been applied.
*
* If you don't know what this is, just ignore it. You probably don't need it.
*/
diff?: PositionDiff;
collapseRange?: boolean;
/**
* The index of the cursor that this transformation applies to.
*/
cursorIndex?: number;
/**
* Please don't use this! It's a hack.
*/
manuallySetCursorPositions?: boolean;
}
export interface MoveCursorTransformation {
type: "moveCursor";
cursorIndex?: number;
/**
* Move the cursor this much.
*/
diff: PositionDiff;
}
/**
* Represents pressing ':'
*/
export interface ShowCommandLine {
type: "showCommandLine";
}
/**
* Represents pressing '.'
*/
export interface Dot {
type: "dot";
}
/**
* Represents macro
*/
export interface Macro {
type: "macro";
register: string;
replay: "contentChange" | "keystrokes";
}
export type Transformation
= InsertTextTransformation
| InsertTextVSCodeTransformation
| ReplaceTextTransformation
| DeleteTextRangeTransformation
| DeleteTextTransformation
| MoveCursorTransformation
| ShowCommandLine
| Dot
| Macro
| DeleteTextTransformation;
/**
* Text Transformations
*
* Using these indicates that you want Visual Studio Code to execute your text
* actions as a batch operation. It's a bit tricky because we defer cursor updating
* behavior to whatever the batch operation returns, so if you update the cursor in your
* Action, VSCode will override whatever you did.
*
* If your cursor isn't ending up in the right place, you can adjust it by passing along
* a PositionDiff.
*
* (There are a LOT of weird edge cases with cursor behavior that we don't want to have to reimplement. Trust
* me... I tried.)
*/
export type TextTransformations
= InsertTextTransformation
| InsertTextVSCodeTransformation
| DeleteTextRangeTransformation
| MoveCursorTransformation
| DeleteTextTransformation
| ReplaceTextTransformation;
export const isTextTransformation = (x: Transformation): x is TextTransformations => {
return x.type === 'insertText' ||
x.type === 'replaceText' ||
x.type === 'deleteText' ||
x.type === 'moveCursor' ||
x.type === 'deleteRange';
};
const getRangeFromTextTransformation = (transformation: TextTransformations): Range | undefined => {
switch (transformation.type) {
case 'insertText':
return new Range(transformation.position, transformation.position);
case 'replaceText':
return new Range(transformation.start, transformation.end);
case 'deleteText':
return new Range(transformation.position, transformation.position);
case 'deleteRange':
return transformation.range;
case 'moveCursor':
return undefined;
}
throw new Error("This should never happen!");
};
export const areAnyTransformationsOverlapping = (transformations: TextTransformations[]) => {
for (let i = 0; i < transformations.length; i++) {
for (let j = i + 1; j < transformations.length; j++) {
const first = transformations[i];
const second = transformations[j];
const firstRange = getRangeFromTextTransformation(first);
const secondRange = getRangeFromTextTransformation(second);
if (!firstRange || !secondRange) { continue; }
if (firstRange.overlaps(secondRange)) {
return true;
}
}
}
return false;
};