forked from VSCodeVim/Vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
2350 lines (1842 loc) · 69.2 KB
/
actions.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { VimSpecialCommands, VimState, SearchState } from './../mode/modeHandler';
import { ModeName } from './../mode/mode';
import { TextEditor } from './../textEditor';
import { Register, RegisterMode } from './../register/register';
import { Position } from './../motion/position';
import * as vscode from 'vscode';
const controlKeys: string[] = [
"ctrl",
"alt",
"shift",
"esc",
"delete",
"left",
"right",
"up",
"down"
];
const compareKeypressSequence = function (one: string[], two: string[]): boolean {
const containsControlKey = (s: string): boolean => {
for (const controlKey of controlKeys) {
if (s.indexOf(controlKey!) !== -1) {
return true;
}
}
return false;
};
const isSingleNumber = (s: string): boolean => {
return s.length === 1 && "1234567890".indexOf(s) > -1;
};
if (one.length !== two.length) {
return false;
}
for (let i = 0, j = 0; i < one.length; i++, j++) {
const left = one[i], right = two[j];
if (left === "<any>") { continue; }
if (right === "<any>") { continue; }
if (left === "<number>" && isSingleNumber(right)) { continue; }
if (right === "<number>" && isSingleNumber(left) ) { continue; }
if (left === "<character>" && !containsControlKey(right)) { continue; }
if (right === "<character>" && !containsControlKey(left)) { continue; }
if (left !== right) { return false; }
}
return true;
};
/**
* The result of a (more sophisticated) Movement.
*/
export interface IMovement {
start : Position;
stop : Position;
// It /so/ annoys me that I have to put this here.
registerMode?: RegisterMode;
}
export function isIMovement(o: IMovement | Position): o is IMovement {
return (o as IMovement).start !== undefined &&
(o as IMovement).stop !== undefined;
}
export class BaseAction {
/**
* Can this action be paired with an operator (is it like w in dw)? All
* BaseMovements can be, and some more sophisticated commands also can be.
*/
isMotion = false;
canBeRepeatedWithDot = false;
/**
* Modes that this action can be run in.
*/
public modes: ModeName[];
/**
* The sequence of keys you use to trigger the action.
*/
public keys: string[];
public mustBeFirstKey = false;
/**
* The keys pressed at the time that this action was triggered.
*/
public keysPressed: string[] = [];
/**
* Is this action valid in the current Vim state?
*/
public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
if (this.modes.indexOf(vimState.currentMode) === -1) { return false; }
if (!compareKeypressSequence(this.keys, keysPressed)) { return false; }
if (vimState.recordedState.actionsRun.length > 0 &&
this.mustBeFirstKey) { return false; }
if (this instanceof BaseOperator && vimState.recordedState.operator) { return false; }
return true;
}
/**
* Could the user be in the process of doing this action.
*/
public couldActionApply(vimState: VimState, keysPressed: string[]): boolean {
if (this.modes.indexOf(vimState.currentMode) === -1) { return false; }
if (!compareKeypressSequence(this.keys.slice(0, keysPressed.length), keysPressed)) { return false; }
if (vimState.recordedState.actionsRun.length > 0 &&
this.mustBeFirstKey) { return false; }
if (this instanceof BaseOperator && vimState.recordedState.operator) { return false; }
return true;
}
public toString(): string {
return this.keys.join("");
}
}
/**
* A movement is something like 'h', 'k', 'w', 'b', 'gg', etc.
*/
export abstract class BaseMovement extends BaseAction {
isMotion = true;
canBePrefixedWithCount = false;
/**
* Whether we should change desiredColumn in VimState.
*/
public doesntChangeDesiredColumn = false;
/**
* This is for commands like $ which force the desired column to be at
* the end of even the longest line.
*/
public setsDesiredColumnToEOL = false;
/**
* Run the movement a single time.
*
* Generally returns a new Position. If necessary, it can return an IMovement instead.
*/
public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
throw new Error("Not implemented!");
}
/**
* Run the movement in an operator context a single time.
*
* Some movements operate over different ranges when used for operators.
*/
public async execActionForOperator(position: Position, vimState: VimState): Promise<Position | IMovement> {
return await this.execAction(position, vimState);
}
/**
* Run a movement count times.
*
* count: the number prefix the user entered, or 0 if they didn't enter one.
*/
public async execActionWithCount(position: Position, vimState: VimState, count: number): Promise<Position | IMovement> {
let recordedState = vimState.recordedState;
let result: Position | IMovement = new Position(0, 0); // bogus init to satisfy typechecker
if (count < 1) {
count = 1;
} else if (count > 99999) {
count = 99999;
}
for (let i = 0; i < count; i++) {
const lastIteration = (i === count - 1);
const temporaryResult = (recordedState.operator && lastIteration) ?
await this.execActionForOperator(position, vimState) :
await this.execAction (position, vimState);
result = temporaryResult;
if (result instanceof Position) {
position = result;
} else if (isIMovement(result)) {
position = result.stop;
}
}
return result;
}
}
/**
* A command is something like <esc>, :, v, i, etc.
*/
export abstract class BaseCommand extends BaseAction {
/**
* If isCompleteAction is true, then triggering this command is a complete action -
* that means that we'll go and try to run it.
*/
isCompleteAction = true;
canBePrefixedWithCount = false;
canBeRepeatedWithDot = false;
/**
* Run the command a single time.
*/
public async exec(position: Position, vimState: VimState): Promise<VimState> {
throw new Error("Not implemented!");
}
/**
* Run the command the number of times VimState wants us to.
*/
public async execCount(position: Position, vimState: VimState): Promise<VimState> {
let timesToRepeat = this.canBePrefixedWithCount ? vimState.recordedState.count || 1 : 1;
for (let i = 0; i < timesToRepeat; i++) {
vimState = await this.exec(position, vimState);
}
return vimState;
}
}
export class BaseOperator extends BaseAction {
canBeRepeatedWithDot = true;
/**
* Run this operator on a range, returning the new location of the cursor.
*/
run(vimState: VimState, start: Position, stop: Position): Promise<VimState> {
throw new Error("You need to override this!");
}
}
export enum KeypressState {
WaitingOnKeys,
NoPossibleMatch
}
export class Actions {
/**
* Every Vim action will be added here with the @RegisterAction decorator.
*/
public static allActions: { type: typeof BaseAction, action: BaseAction }[] = [];
/**
* Gets the action that should be triggered given a key
* sequence.
*
* If there is a definitive action that matched, returns that action.
*
* If an action could potentially match if more keys were to be pressed, returns true. (e.g.
* you pressed "g" and are about to press "g" action to make the full action "gg".)
*
* If no action could ever match, returns false.
*/
public static getRelevantAction(keysPressed: string[], vimState: VimState): BaseAction | KeypressState {
let couldPotentiallyHaveMatch = false;
for (const thing of Actions.allActions) {
const { type, action } = thing!;
if (action.doesActionApply(vimState, keysPressed)) {
const result = new type();
result.keysPressed = vimState.recordedState.actionKeys.slice(0);
return result;
}
if (action.couldActionApply(vimState, keysPressed)) {
couldPotentiallyHaveMatch = true;
}
}
return couldPotentiallyHaveMatch ? KeypressState.WaitingOnKeys : KeypressState.NoPossibleMatch;
}
}
export function RegisterAction(action: typeof BaseAction): void {
Actions.allActions.push({ type: action, action: new action() });
}
// begin actions
@RegisterAction
class CommandNumber extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["<number>"];
isCompleteAction = false;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const number = parseInt(this.keysPressed[0], 10);
vimState.recordedState.count = vimState.recordedState.count * 10 + number;
return vimState;
}
public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
const isZero = keysPressed[0] === "0";
return super.doesActionApply(vimState, keysPressed) &&
((isZero && vimState.recordedState.count > 0) || !isZero);
}
public couldActionApply(vimState: VimState, keysPressed: string[]): boolean {
const isZero = keysPressed[0] === "0";
return super.couldActionApply(vimState, keysPressed) &&
((isZero && vimState.recordedState.count > 0) || !isZero);
}
}
@RegisterAction
class CommandEsc extends BaseCommand {
modes = [ModeName.Insert, ModeName.Visual, ModeName.VisualLine];
keys = ["<esc>"];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
if (vimState.currentMode !== ModeName.Visual &&
vimState.currentMode !== ModeName.VisualLine) {
vimState.cursorPosition = position.getLeft();
}
vimState.currentMode = ModeName.Normal;
return vimState;
}
}
@RegisterAction
class CommandCtrlC extends CommandEsc {
modes = [ModeName.Insert, ModeName.Visual, ModeName.VisualLine];
keys = ["ctrl+c"];
}
@RegisterAction
class CommandInsertAtCursor extends BaseCommand {
modes = [ModeName.Normal];
keys = ["i"];
mustBeFirstKey = true;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.currentMode = ModeName.Insert;
return vimState;
}
}
@RegisterAction
class CommandInsertInSearchMode extends BaseCommand {
modes = [ModeName.SearchInProgressMode];
keys = ["<any>"];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const key = this.keysPressed[0];
const searchState = vimState.searchState!;
// handle special keys first
if (key === "<backspace>") {
searchState.searchString = searchState.searchString.slice(0, -1);
} else if (key === "\n") {
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = searchState.getNextSearchMatchPosition(searchState.searchCursorStartPosition).pos;
return vimState;
} else if (key === "<esc>") {
vimState.currentMode = ModeName.Normal;
vimState.searchState = undefined;
return vimState;
} else {
searchState.searchString += this.keysPressed[0];
}
// console.log(vimState.searchString); (TODO: Show somewhere!)
vimState.cursorPosition = searchState.getNextSearchMatchPosition(searchState.searchCursorStartPosition).pos;
return vimState;
}
}
@RegisterAction
class CommandNextSearchMatch extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["n"];
public async execAction(position: Position, vimState: VimState): Promise<Position> {
const searchState = vimState.searchState;
if (!searchState || searchState.searchString === "") {
return position;
}
return searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
}
}
@RegisterAction
class CommandStar extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["*"];
isMotion = true;
canBePrefixedWithCount = true;
public static GetWordAtPosition(position: Position): string {
const start = position.getWordLeft(true);
const end = position.getCurrentWordEnd(true).getRight();
return TextEditor.getText(new vscode.Range(start, end));
}
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const currentWord = CommandStar.GetWordAtPosition(position);
vimState.searchState = new SearchState(+1, vimState.cursorPosition, currentWord);
do {
vimState.cursorPosition = vimState.searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
} while (CommandStar.GetWordAtPosition(vimState.cursorPosition) !== currentWord);
return vimState;
}
}
@RegisterAction
class CommandHash extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["#"];
isMotion = true;
canBePrefixedWithCount = true;
public static GetWordAtPosition(position: Position): string {
const start = position.getWordLeft(true);
const end = position.getCurrentWordEnd(true).getRight();
return TextEditor.getText(new vscode.Range(start, end));
}
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const currentWord = CommandStar.GetWordAtPosition(position);
vimState.searchState = new SearchState(-1, vimState.cursorPosition, currentWord);
do {
vimState.cursorPosition = vimState.searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
} while (CommandStar.GetWordAtPosition(vimState.cursorPosition) !== currentWord);
return vimState;
}
}
@RegisterAction
class CommandPreviousSearchMatch extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["N"];
public async execAction(position: Position, vimState: VimState): Promise<Position> {
const searchState = vimState.searchState;
if (!searchState || searchState.searchString === "") {
return position;
}
return searchState.getNextSearchMatchPosition(vimState.cursorPosition, -1).pos;
}
}
@RegisterAction
class CommandInsertInInsertMode extends BaseCommand {
modes = [ModeName.Insert];
keys = ["<character>"];
// TODO - I am sure this can be improved.
// The hard case is . where we have to track cursor pos since we don't
// update the view
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const char = this.keysPressed[this.keysPressed.length - 1];
if (char === "<backspace>") {
if (position.character === 0) {
if (position.line > 0) {
await TextEditor.delete(new vscode.Range(
position.getPreviousLineBegin().getLineEnd(),
position.getLineBegin()
));
vimState.cursorPosition = position.getPreviousLineBegin().getLineEnd();
vimState.cursorStartPosition = position.getPreviousLineBegin().getLineEnd();
}
} else {
await TextEditor.delete(new vscode.Range(position, position.getLeft()));
vimState.cursorPosition = position.getLeft();
vimState.cursorStartPosition = position.getLeft();
}
} else {
await TextEditor.insert(char, vimState.cursorPosition);
vimState.cursorStartPosition = Position.FromVSCodePosition(vscode.window.activeTextEditor.selection.start);
vimState.cursorPosition = Position.FromVSCodePosition(vscode.window.activeTextEditor.selection.start);
}
return vimState;
}
public toString(): string {
return this.keysPressed[this.keysPressed.length - 1];
}
}
@RegisterAction
export class CommandSearchForwards extends BaseCommand {
modes = [ModeName.Normal];
keys = ["/"];
isMotion = true;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.searchState = new SearchState(+1, vimState.cursorPosition);
vimState.currentMode = ModeName.SearchInProgressMode;
return vimState;
}
}
@RegisterAction
export class CommandSearchBackwards extends BaseCommand {
modes = [ModeName.Normal];
keys = ["?"];
isMotion = true;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.searchState = new SearchState(-1, vimState.cursorPosition);
vimState.currentMode = ModeName.SearchInProgressMode;
return vimState;
}
}
@RegisterAction
class CommandFormatCode extends BaseCommand {
modes = [ModeName.Visual, ModeName.VisualLine];
keys = ["="];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("editor.action.format");
vimState.currentMode = ModeName.Normal;
return vimState;
}
}
@RegisterAction
export class DeleteOperator extends BaseOperator {
public keys = ["d"];
public modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
/**
* Deletes from the position of start to 1 past the position of end.
*/
public async run(vimState: VimState, start: Position, end: Position, yank = true): Promise<VimState> {
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
start = start.getLineBegin();
end = end.getLineEnd();
}
end = new Position(end.line, end.character + 1);
const isOnLastLine = end.line === TextEditor.getLineCount() - 1;
// Vim does this weird thing where it allows you to select and delete
// the newline character, which it places 1 past the last character
// in the line. Here we interpret a character position 1 past the end
// as selecting the newline character.
if (end.character === TextEditor.getLineAt(end).text.length + 1) {
end = end.getDown(0);
}
// If we delete linewise to the final line of the document, we expect the line
// to be removed. This is actually a special case because the newline
// character we've selected to delete is the newline on the end of the document,
// but we actually delete the newline on the second to last line.
// Just writing about this is making me more confused. -_-
if (isOnLastLine &&
start.line !== 0 &&
vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
start = start.getPreviousLineBegin().getLineEnd();
}
let text = vscode.window.activeTextEditor.document.getText(new vscode.Range(start, end));
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
text = text.slice(0, -1); // slice final newline in linewise mode - linewise put will add it back.
}
if (yank) {
Register.put(text, vimState);
}
await TextEditor.delete(new vscode.Range(start, end));
if (vimState.currentMode === ModeName.Visual) {
vimState.cursorPosition = Position.EarlierOf(start, end);
}
if (start.character >= TextEditor.getLineAt(start).text.length) {
vimState.cursorPosition = start.getLeft();
} else {
vimState.cursorPosition = start;
}
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
vimState.cursorPosition = vimState.cursorPosition.getLineBegin();
}
vimState.currentMode = ModeName.Normal;
return vimState;
}
}
@RegisterAction
export class DeleteOperatorVisual extends BaseOperator {
public keys = ["D"];
public modes = [ModeName.Visual, ModeName.VisualLine];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
return await new DeleteOperator().run(vimState, start, end);
}
}
@RegisterAction
export class YankOperator extends BaseOperator {
public keys = ["y"];
public modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
canBeRepeatedWithDot = false;
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
if (start.compareTo(end) <= 0) {
end = new Position(end.line, end.character + 1);
} else {
const tmp = start;
start = end;
end = tmp;
end = new Position(end.line, end.character + 1);
}
let text = TextEditor.getText(new vscode.Range(start, end));
// If we selected the newline character, add it as well.
if (vimState.currentMode === ModeName.Visual &&
end.character === TextEditor.getLineAt(end).text.length + 1) {
text = text + "\n";
}
Register.put(text, vimState);
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = start;
return vimState;
}
}
@RegisterAction
export class DeleteOperatorXVisual extends BaseOperator {
public keys = ["x"];
public modes = [ModeName.Visual, ModeName.VisualLine];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
return await new DeleteOperator().run(vimState, start, end);
}
}
@RegisterAction
export class ChangeOperatorSVisual extends BaseOperator {
public keys = ["s"];
public modes = [ModeName.Visual, ModeName.VisualLine];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
return await new ChangeOperator().run(vimState, start, end);
}
}
@RegisterAction
export class UpperCaseOperator extends BaseOperator {
public keys = ["U"];
public modes = [ModeName.Visual, ModeName.VisualLine];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
const range = new vscode.Range(start, new Position(end.line, end.character + 1));
let text = vscode.window.activeTextEditor.document.getText(range);
await TextEditor.replace(range, text.toUpperCase());
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = start;
return vimState;
}
}
@RegisterAction
export class LowerCaseOperator extends BaseOperator {
public keys = ["u"];
public modes = [ModeName.Visual, ModeName.VisualLine];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
const range = new vscode.Range(start, new Position(end.line, end.character + 1));
let text = vscode.window.activeTextEditor.document.getText(range);
await TextEditor.replace(range, text.toLowerCase());
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = start;
return vimState;
}
}
@RegisterAction
export class MarkCommand extends BaseCommand {
keys = ["m", "<character>"];
modes = [ModeName.Normal];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const markName = this.keysPressed[1];
vimState.historyTracker.addMark(position, markName);
return vimState;
}
}
@RegisterAction
export class MarkMovementBOL extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["'", "<character>"];
public async execAction(position: Position, vimState: VimState): Promise<Position> {
const markName = this.keysPressed[1];
const mark = vimState.historyTracker.getMark(markName);
return mark.position.getFirstLineNonBlankChar();
}
}
@RegisterAction
export class MarkMovement extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["`", "<character>"];
public async execAction(position: Position, vimState: VimState): Promise<Position> {
const markName = this.keysPressed[1];
const mark = vimState.historyTracker.getMark(markName);
return mark.position;
}
}
@RegisterAction
export class ChangeOperator extends BaseOperator {
public keys = ["c"];
public modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
const isEndOfLine = end.character === TextEditor.getLineAt(end).text.length - 1;
const state = await new DeleteOperator().run(vimState, start, end);
state.currentMode = ModeName.Insert;
// If we delete to EOL, the block cursor would end on the final character,
// which means the insert cursor would be one to the left of the end of
// the line.
if (isEndOfLine) {
state.cursorPosition = state.cursorPosition.getRight();
}
return state;
}
}
@RegisterAction
export class PutCommand extends BaseCommand {
keys = ["p"];
modes = [ModeName.Normal];
canBePrefixedWithCount = true;
canBeRepeatedWithDot = true;
public async exec(position: Position, vimState: VimState, before: boolean = false, adjustIndent: boolean = false): Promise<VimState> {
const register = Register.get(vimState);
const dest = before ? position : position.getRight();
let text = register.text;
if (register.registerMode === RegisterMode.CharacterWise) {
await TextEditor.insertAt(text, dest);
} else {
if (adjustIndent) {
// Adjust indent to current line
let indentationWidth = TextEditor.getIndentationLevel(TextEditor.getLineAt(position).text);
let firstLineIdentationWidth = TextEditor.getIndentationLevel(text.split('\n')[0]);
text = text.split('\n').map(line => {
let currentIdentationWidth = TextEditor.getIndentationLevel(line);
let newIndentationWidth = currentIdentationWidth - firstLineIdentationWidth + indentationWidth;
return TextEditor.setIndentationLevel(line, newIndentationWidth);
}).join('\n');
}
if (before) {
await TextEditor.insertAt(text + "\n", dest.getLineBegin());
} else {
await TextEditor.insertAt("\n" + text, dest.getLineEnd());
}
}
// More vim weirdness: If the thing you're pasting has a newline, the cursor
// stays in the same place. Otherwise, it moves to the end of what you pasted.
if (register.registerMode === RegisterMode.LineWise) {
vimState.cursorPosition = new Position(dest.line + 1, 0);
} else {
if (text.indexOf("\n") === -1) {
vimState.cursorPosition = new Position(dest.line, Math.max(dest.character + text.length - 1, 0));
} else {
vimState.cursorPosition = dest;
}
}
vimState.currentRegisterMode = register.registerMode;
return vimState;
}
public async execCount(position: Position, vimState: VimState): Promise<VimState> {
const result = await super.execCount(position, vimState);
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
result.cursorPosition = new Position(position.line + 1, 0).getFirstLineNonBlankChar();
}
return result;
}
}
@RegisterAction
export class GPutCommand extends BaseCommand {
keys = ["g", "p"];
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
canBePrefixedWithCount = true;
canBeRepeatedWithDot = true;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const result = await new PutCommand().exec(position, vimState);
return result;
}
public async execCount(position: Position, vimState: VimState): Promise<VimState> {
const register = Register.get(vimState);
const addedLinesCount = register.text.split('\n').length;
const result = await super.execCount(position, vimState);
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
let lastAddedLine = new Position(position.line + addedLinesCount, 0);
if (TextEditor.isLastLine(lastAddedLine)) {
result.cursorPosition = lastAddedLine.getLineBegin();
} else {
result.cursorPosition = lastAddedLine.getLineEnd().getRightThroughLineBreaks();
}
}
return result;
}
}
@RegisterAction
export class PutWithIndentCommand extends BaseCommand {
keys = ["]", "p"];
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
canBePrefixedWithCount = true;
canBeRepeatedWithDot = true;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const result = await new PutCommand().exec(position, vimState, false, true);
return result;
}
public async execCount(position: Position, vimState: VimState): Promise<VimState> {
const result = await super.execCount(position, vimState);
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
result.cursorPosition = new Position(position.line + 1, 0).getFirstLineNonBlankChar();
}
return result;
}
}
@RegisterAction
export class PutCommandVisual extends BaseCommand {
keys = ["p"];
modes = [ModeName.Visual, ModeName.VisualLine];
canBePrefixedWithCount = true;
canBePrefixedWithDot = true;
public async exec(position: Position, vimState: VimState, before: boolean = false): Promise<VimState> {
const result = await new DeleteOperator().run(vimState, vimState.cursorStartPosition, vimState.cursorPosition, false);
return await new PutCommand().exec(result.cursorPosition, result, true);
}
// TODO - execWithCount
}
@RegisterAction
export class PutCommandVisualCapitalP extends PutCommandVisual {
keys = ["P"];
}
@RegisterAction
class IndentOperator extends BaseOperator {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = [">"];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
vscode.window.activeTextEditor.selection = new vscode.Selection(start, end);
await vscode.commands.executeCommand("editor.action.indentLines");
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = start.getFirstLineNonBlankChar();
return vimState;
}
}
@RegisterAction
class OutdentOperator extends BaseOperator {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["<"];
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
vscode.window.activeTextEditor.selection = new vscode.Selection(start, end);
await vscode.commands.executeCommand("editor.action.outdentLines");
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = vimState.cursorStartPosition;
return vimState;
}
}
@RegisterAction
export class PutBeforeCommand extends BaseCommand {
public keys = ["P"];
public modes = [ModeName.Normal];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const result = await new PutCommand().exec(position, vimState, true);
if (vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
result.cursorPosition = result.cursorPosition.getPreviousLineBegin();
}
return result;
}
}
@RegisterAction
export class GPutBeforeCommand extends BaseCommand {