This repository was archived by the owner on Nov 20, 2023. It is now read-only.
forked from microsoft/vscode-extension-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.ts
More file actions
343 lines (270 loc) · 9.27 KB
/
operators.ts
File metadata and controls
343 lines (270 loc) · 9.27 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Position, Selection, Range, TextDocument, TextEditor, TextEditorRevealType } from 'vscode';
import { Motion, Motions } from './motions';
import { Mode, IController, DeleteRegister } from './common';
export abstract class Operator {
public abstract runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean;
public abstract runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean;
protected doc(ed: TextEditor): TextDocument {
return ed.document;
}
protected pos(ed: TextEditor): Position {
return ed.selection.active;
}
protected sel(ed: TextEditor): Selection {
return ed.selection;
}
protected setPosReveal(ed: TextEditor, line: number, char: number): void {
ed.selection = new Selection(new Position(line, char), new Position(line, char));
ed.revealRange(ed.selection, TextEditorRevealType.Default);
}
protected delete(ctrl: IController, ed: TextEditor, isWholeLine: boolean, range: Range): void {
ctrl.setDeleteRegister(new DeleteRegister(isWholeLine, ed.document.getText(range)));
ed.edit((builder) => {
builder.delete(range);
});
}
}
abstract class OperatorWithNoArgs extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
this._run(ctrl, ed);
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
this._run(ctrl, ed);
return true;
}
protected abstract _run(ctrl: IController, ed: TextEditor): void;
}
class InsertOperator extends OperatorWithNoArgs {
protected _run(ctrl: IController, ed: TextEditor): void {
ctrl.setMode(Mode.INSERT);
}
}
class AppendOperator extends OperatorWithNoArgs {
protected _run(ctrl: IController, ed: TextEditor): void {
const newPos = Motions.RightMotion.run(this.doc(ed), this.pos(ed), ctrl.motionState);
this.setPosReveal(ed, newPos.line, newPos.character);
ctrl.setMode(Mode.INSERT);
}
}
class AppendEndOfLineOperator extends OperatorWithNoArgs {
protected _run(ctrl: IController, ed: TextEditor): void {
const newPos = Motions.EndOfLine.run(this.doc(ed), this.pos(ed), ctrl.motionState);
this.setPosReveal(ed, newPos.line, newPos.character);
ctrl.setMode(Mode.INSERT);
}
}
class VisualOperator extends OperatorWithNoArgs {
protected _run(ctrl: IController, ed: TextEditor): void {
ctrl.motionState.anchor = this.pos(ed);
ctrl.setVisual(true);
}
}
class DeleteCharUnderCursorOperator extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
const to = Motions.NextCharacter.repeat(repeatCount > 1, repeatCount).run(this.doc(ed), this.pos(ed), ctrl.motionState);
const from = this.pos(ed);
this.delete(ctrl, ed, false, new Range(from.line, from.character, to.line, to.character));
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
const sel = this.sel(ed);
this.delete(ctrl, ed, false, sel);
return true;
}
}
class DeleteLineOperator extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
const pos = this.pos(ed);
const doc = this.doc(ed);
let fromLine = pos.line;
let fromCharacter = 0;
let toLine = fromLine + repeatCount;
let toCharacter = 0;
if (toLine >= doc.lineCount - 1) {
// Deleting last line
toLine = doc.lineCount - 1;
toCharacter = doc.lineAt(toLine).text.length;
if (fromLine > 0) {
fromLine = fromLine - 1;
fromCharacter = doc.lineAt(fromLine).text.length;
}
}
this.delete(ctrl, ed, true, new Range(fromLine, fromCharacter, toLine, toCharacter));
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
const sel = this.sel(ed);
this.delete(ctrl, ed, false, sel);
return true;
}
}
abstract class OperatorWithMotion extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
const motion = ctrl.findMotion(args);
if (!motion) {
// is it motion building
if (ctrl.isMotionPrefix(args)) {
return false;
}
// INVALID INPUT - beep!!
return true;
}
return this._runNormalMode(ctrl, ed, motion.repeat(repeatCount > 1, repeatCount));
}
protected abstract _runNormalMode(ctrl: IController, ed: TextEditor, motion: Motion): boolean;
}
class DeleteToOperator extends OperatorWithMotion {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
if (args === 'd') {
// dd
return Operators.DeleteLine.runNormalMode(ctrl, ed, repeatCount, args);
}
return super.runNormalMode(ctrl, ed, repeatCount, args);
}
protected _runNormalMode(ctrl: IController, ed: TextEditor, motion: Motion): boolean {
const to = motion.run(this.doc(ed), this.pos(ed), ctrl.motionState);
const from = this.pos(ed);
this.delete(ctrl, ed, false, new Range(from.line, from.character, to.line, to.character));
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
const sel = this.sel(ed);
this.delete(ctrl, ed, false, sel);
return true;
}
}
class PutOperator extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
const register = ctrl.getDeleteRegister();
if (!register) {
// No delete register - beep!!
return true;
}
let str = repeatString(register.content, repeatCount);
const pos = this.pos(ed);
if (!register.isWholeLine) {
ed.edit((builder) => {
builder.insert(new Position(pos.line, pos.character + 1), str);
});
return true;
}
const doc = this.doc(ed);
let insertLine = pos.line + 1;
let insertCharacter = 0;
if (insertLine >= doc.lineCount) {
// on last line
insertLine = doc.lineCount - 1;
insertCharacter = doc.lineAt(insertLine).text.length;
str = '\n' + str;
}
ed.edit((builder) => {
builder.insert(new Position(insertLine, insertCharacter), str);
});
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
const register = ctrl.getDeleteRegister();
if (!register) {
// No delete register - beep!!
return false;
}
const str = register.content;
const sel = this.sel(ed);
ed.edit((builder) => {
builder.replace(sel, str);
});
return true;
}
}
class ReplaceOperator extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
if (args.length === 0) {
// input not ready
return false;
}
const doc = this.doc(ed);
const pos = this.pos(ed);
const toCharacter = pos.character + repeatCount;
if (toCharacter > doc.lineAt(pos).text.length) {
// invalid replace (beep!)
return true;
}
ed.edit((builder) => {
builder.replace(new Range(pos.line, pos.character, pos.line, toCharacter), repeatString(args, repeatCount));
});
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
if (args.length === 0) {
// input not ready
return false;
}
const doc = this.doc(ed);
const sel = this.sel(ed);
const srcString = doc.getText(sel);
let dstString = '';
for (let i = 0; i < srcString.length; i++) {
const ch = srcString.charAt(i);
if (ch === '\r' || ch === '\n') {
dstString += ch;
} else {
dstString += args;
}
}
ed.edit((builder) => {
builder.replace(sel, dstString);
});
return true;
}
}
class ReplaceModeOperator extends Operator {
public runNormalMode(ctrl: IController, ed: TextEditor, repeatCount: number, args: string): boolean {
ctrl.setMode(Mode.REPLACE);
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
this.delete(ctrl, ed, false, this.sel(ed));
ctrl.setMode(Mode.INSERT);
return true;
}
}
class ChangeOperator extends OperatorWithMotion {
protected _runNormalMode(ctrl: IController, ed: TextEditor, motion: Motion): boolean {
const to = motion.run(this.doc(ed), this.pos(ed), ctrl.motionState);
const from = this.pos(ed);
this.delete(ctrl, ed, false, new Range(from.line, from.character, to.line, to.character));
ctrl.setMode(Mode.INSERT);
return true;
}
public runVisualMode(ctrl: IController, ed: TextEditor, args: string): boolean {
const sel = this.sel(ed);
this.delete(ctrl, ed, false, sel);
ctrl.setMode(Mode.INSERT);
return true;
}
}
function repeatString(str: string, repeatCount: number): string {
let result = '';
for (let i = 0; i < repeatCount; i++) {
result += str;
}
return result;
}
export const Operators = {
Insert: new InsertOperator(),
Visual: new VisualOperator(),
Append: new AppendOperator(),
AppendEndOfLine: new AppendEndOfLineOperator(),
DeleteCharUnderCursor: new DeleteCharUnderCursorOperator(),
DeleteTo: new DeleteToOperator(),
DeleteLine: new DeleteLineOperator(),
Put: new PutOperator(),
Replace: new ReplaceOperator(),
Change: new ChangeOperator(),
ReplaceMode: new ReplaceModeOperator(),
};