-
Notifications
You must be signed in to change notification settings - Fork 58
/
context.ts
828 lines (719 loc) · 23.6 KB
/
context.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
import * as vscode from "vscode";
import { SelectionBehavior } from "./types";
import type { CommandDescriptor } from "../commands";
import type { PerEditorState } from "../state/editors";
import type { Extension } from "../state/extension";
import type { Mode } from "../state/modes";
import { EditNotAppliedError, EditorRequiredError } from "../utils/errors";
import { noUndoStops, performDummyEdit } from "../utils/misc";
let currentContext: ContextWithoutActiveEditor | undefined;
const enum ContextFlags {
None = 0,
ShouldInsertUndoStop = 1,
DoNotRecord = 2,
}
/**
* See {@link Context.WithoutActiveEditor} instead.
*
* @internal
*/
export class ContextWithoutActiveEditor {
/**
* Returns the current execution context, or throws an error if called outside
* of an execution context.
*/
public static get current() {
if (currentContext === undefined) {
throw new Error("attempted to access context object outside of execution context");
}
return currentContext;
}
/**
* Returns the current execution context, or `undefined` if called outside of
* an execution context.
*/
public static get currentOrUndefined() {
return currentContext;
}
/**
* Equivalent to calling `wrap` on `Context.current`. If there is no current
* context, it returns the `thenable` directly.
*/
public static wrap<T>(thenable: Thenable<T>) {
return this.currentOrUndefined?.wrap(thenable) ?? thenable;
}
/**
* Equivalent to calling `then` on the current context. If there is no current
* context, it returns the `thenable.then` directly.
*/
public static then<T, R>(
thenable: Thenable<T>,
onFulfilled?: (value: T) => R,
onRejected?: (reason: any) => R,
) {
return this.currentOrUndefined?.then(thenable, onFulfilled, onRejected)
?? thenable.then(onFulfilled, onRejected);
}
/**
* Equivalent to calling `setup` on the current context.
*/
public static setup() {
return this.current.setup();
}
protected _flags = ContextFlags.None;
public constructor(
/**
* The global extension state.
*/
public readonly extension: Extension,
/**
* The token used to cancel an operation running in the current context.
*/
public readonly cancellationToken: vscode.CancellationToken,
/**
* The descriptor of the command that led to the creation of this context.
*/
public readonly commandDescriptor?: CommandDescriptor,
) {
if (currentContext?._flags ?? 0 & ContextFlags.DoNotRecord) {
this._flags |= ContextFlags.DoNotRecord;
}
}
/**
* Returns a new context whose cancellation is controlled by the specified
* cancellation token.
*/
public withCancellationToken(cancellationToken: vscode.CancellationToken) {
return new Context.WithoutActiveEditor(
this.extension, cancellationToken, this.commandDescriptor);
}
/**
* Whether commands executed within this context should be recorded.
*/
public shouldRecord() {
return (this._flags & ContextFlags.DoNotRecord) === 0;
}
/**
* Indicates that commands executed within this context should not be
* recorded.
*/
public doNotRecord() {
this._flags |= ContextFlags.DoNotRecord;
return this;
}
/**
* Creates a new promise that executes within the current context.
*/
public createPromise<T>(
executor: (resolve: (value: T) => void, reject: (error: any) => void) => void,
) {
return this.wrap(new Promise<T>(executor));
}
/**
* Runs the given function within the current context.
*/
public run<T>(f: (context: this) => T) {
const previousContext = currentContext;
if (previousContext === this) {
return f(this) as any;
}
currentContext = this;
try {
return f(this);
} finally {
currentContext = previousContext;
}
}
/**
* Runs the given async function within the current context.
*/
public async runAsync<T>(f: (context: this) => T): Promise<T extends Thenable<infer R> ? R : T> {
const previousContext = currentContext;
if (previousContext === this) {
// Takes care of this situation: context 1 is created, context 1 spawns
// another context 2, context 1 is exited, and then context 2 is exited.
// Context 2 inherited "currentContext" === context 1, so context 2
// restores context 1 instead of exiting context 1 fully.
return f(this) as any;
}
currentContext = this;
try {
return await f(this) as any;
} finally {
currentContext = previousContext;
}
}
/**
* Returns a promise whose continuations will be wrapped in a way that
* preserves the current context.
*
* Await a call to `setup` in an async function to make ensure that all
* subsequent `await` expressions preserve the current context.
*/
public setup() {
return this.wrap(Promise.resolve());
}
/**
* Wraps the given promise in a way that preserves the current context in
* `then`.
*/
public wrap<T>(thenable: Thenable<T>): Thenable<T> {
return {
then: <R>(onFulfilled?: (value: T) => R, onRejected?: (reason: any) => R) => {
return this.then(thenable, onFulfilled, onRejected);
},
};
}
/**
* Wraps the continuation of a promise in order to preserve the current
* context.
*/
public then<T, R>(
thenable: Thenable<T>,
onFulfilled?: (value: T) => R,
onRejected?: (reason: any) => R,
) {
if (onFulfilled !== undefined) {
const f = onFulfilled;
onFulfilled = (value: T) => this.runAsync(() => f(value) as any) as any;
}
if (onRejected !== undefined) {
const f = onRejected;
onRejected = (reason: any) => this.runAsync(() => f(reason) as any) as any;
}
return this.wrap(thenable.then(onFulfilled, onRejected));
}
}
/**
* The context of execution of a script.
*/
export class Context extends ContextWithoutActiveEditor {
/**
* The base {@link Context} class, which does not require an active
* {@link vscode.TextEditor}.
*/
public static readonly WithoutActiveEditor = ContextWithoutActiveEditor;
/**
* Returns the current execution context, or throws an error if called outside
* of an execution context or if the execution context does not have an
* active editor.
*/
public static override get current() {
if (!(currentContext instanceof Context)) {
throw new Error("current context does not have an active text editor");
}
return currentContext;
}
/**
* Returns the current execution context, or `undefined` if called outside of
* an execution context or if the execution context does not have an active
* editor.
*/
public static override get currentOrUndefined() {
if (currentContext === undefined || !(currentContext instanceof Context)) {
return undefined;
}
return currentContext;
}
public static assert(context: Context.WithoutActiveEditor): asserts context is Context {
if (!(context instanceof Context)) {
throw new Error("current context does not have an active text editor");
}
}
/**
* Returns a {@link Context} or {@link Context.WithoutActiveEditor} depending
* on whether there is an active text editor.
*/
public static create(extension: Extension, command: CommandDescriptor) {
const activeEditorState = extension.editors.active,
cancellationToken = extension.cancellationToken;
return activeEditorState === undefined
? new Context.WithoutActiveEditor(extension, cancellationToken, command)
: new Context(activeEditorState, cancellationToken, command);
}
/**
* Returns a {@link Context} or throws an exception if there is no active text
* editor.
*/
public static createWithActiveTextEditor(extension: Extension, command: CommandDescriptor) {
const activeEditorState = extension.editors.active;
EditorRequiredError.throwUnlessAvailable(activeEditorState);
return new Context(activeEditorState, extension.cancellationToken, command);
}
private _document: vscode.TextDocument;
private _editor: vscode.TextEditor;
private _mode: Mode;
/**
* The current `vscode.TextDocument`.
*/
public get document() {
return this._document;
}
/**
* The current `vscode.TextEditor`.
*
* Avoid accessing `editor.selections` -- selections may need to be
* transformed before being returned or updated, which is why
* `context.selections` should be preferred.
*/
public get editor() {
return this._editor as Omit<vscode.TextEditor, "selections">;
}
/**
* The `Mode` associated with the current `vscode.TextEditor`.
*/
public get mode() {
return this._mode;
}
/**
* The selection behavior for this context.
*/
public get selectionBehavior() {
return this._mode.selectionBehavior;
}
/**
* The current selections.
*
* Selections returned by this property **may be different** from the ones
* returned by `editor.selections`. If the current selection behavior is
* `Character`, strictly forward-facing (i.e. `active > anchor`) selections
* will be made longer by one character.
*/
public get selections() {
const editor = this.editor as vscode.TextEditor;
if (this.selectionBehavior === SelectionBehavior.Character) {
return selectionsFromCharacterMode(editor.selections, editor.document);
}
return editor.selections;
}
/**
* Sets the current selections.
*
* If the current selection behavior is `Character`, strictly forward-facing
* (i.e. `active > anchor`) selections will be made shorter by one character.
*/
public set selections(selections: readonly vscode.Selection[]) {
const editor = this.editor as vscode.TextEditor;
if (this.selectionBehavior === SelectionBehavior.Character) {
selections = selectionsToCharacterMode(selections, editor.document);
}
editor.selections = selections as vscode.Selection[];
}
/**
* Equivalent to `selections[0]`.
*
* @see selections
*/
public get mainSelection() {
const editor = this.editor as vscode.TextEditor;
if (this.selectionBehavior === SelectionBehavior.Character) {
return selectionsFromCharacterMode([editor.selection], editor.document)[0];
}
return editor.selection;
}
public constructor(
state: PerEditorState,
cancellationToken: vscode.CancellationToken,
commandDescriptor?: CommandDescriptor,
) {
super(state.extension, cancellationToken, commandDescriptor);
this._document = state.editor.document;
this._editor = state.editor;
this._mode = state.mode;
}
/**
* Returns a new context whose cancellation is controlled by the specified
* cancellation token.
*/
public override withCancellationToken(cancellationToken: vscode.CancellationToken) {
return new Context(this.getState(), cancellationToken, this.commandDescriptor);
}
/**
* Returns the mode-specific state for the current context.
*/
public getState() {
return this.extension.editors.getState(this._editor);
}
/**
* Performs changes on the editor of the context.
*/
public edit<T>(
f: (editBuilder: vscode.TextEditorEdit, selections: readonly vscode.Selection[],
document: vscode.TextDocument) => T,
) {
let value: T;
const document = this.document,
selections = f.length >= 2 ? this.selections : [];
return this.wrap(
this.editor.edit(
(editBuilder) => value = f(editBuilder, selections, document),
noUndoStops,
).then((succeeded) => {
EditNotAppliedError.throwIfNotApplied(succeeded);
this._flags |= ContextFlags.ShouldInsertUndoStop;
return value;
}),
);
}
/**
* Returns whether edits have been performed in this context but not committed
* with `insertUndoStop`.
*/
public hasEditsWithoutUndoStops() {
return (this._flags & ContextFlags.ShouldInsertUndoStop) === ContextFlags.ShouldInsertUndoStop;
}
/**
* Inserts an undo stop if needed.
*/
public insertUndoStop() {
if (!this.hasEditsWithoutUndoStops()) {
return Promise.resolve();
}
return this.wrap(performDummyEdit(this._editor));
}
/**
* Switches the context to the given document.
*/
public async switchToDocument(document: vscode.TextDocument, alsoFocusEditor = false) {
if (this.document === document) {
return;
}
const notebook = (document as { notebook?: vscode.NotebookDocument }).notebook;
let notebookEditor: vscode.TextEditor | undefined;
if (notebook !== undefined) {
const uri = document.uri;
if (uri.scheme === "vscode-notebook-cell" && uri.fragment.startsWith("ch")) {
// Target document is a notebook cell; find its index and attempt to
// focus it.
const cellIndex = parseInt(uri.fragment.slice(2)),
cell = notebook.cellAt(cellIndex);
if (cell.index === cellIndex) {
// `showTextDocument` will force a regular text editor. We use
// `vscode.open` instead, which will focus the right cell.
await vscode.commands.executeCommand("vscode.open", cell.document.uri);
notebookEditor = vscode.window.activeTextEditor;
}
}
}
const editor = notebookEditor
?? await vscode.window.showTextDocument(document, undefined, !alsoFocusEditor);
this._document = document;
this._editor = editor;
this._mode = this.extension.editors.getState(editor).mode;
}
/**
* Switches the mode of the current editor to the given mode.
*/
public async switchToMode(mode: Mode) {
const state = this.extension.editors.getState(this._editor);
await state.setMode(mode);
this._mode = state.mode;
}
}
export declare namespace Context {
export type WithoutActiveEditor = ContextWithoutActiveEditor;
}
/**
* Returns the text of the given range in the current context.
*
* ### Example
* ```js
* const start = new vscode.Position(0, 0),
* end = new vscode.Position(0, 3);
*
* expect(
* text(new vscode.Range(start, end)),
* "to be",
* "foo",
* );
* ```
*
* With:
* ```
* foo bar
* ```
*/
export function text(range: vscode.Range): string;
/**
* Returns the text of all the given ranges in the current context.
*
* ### Example
* ```js
* const start1 = new vscode.Position(0, 0),
* end1 = new vscode.Position(0, 3),
* start2 = new vscode.Position(0, 4),
* end2 = new vscode.Position(0, 7);
*
* expect(
* text([new vscode.Range(start1, end1), new vscode.Range(start2, end2)]),
* "to equal",
* ["foo", "bar"],
* );
* ```
*
* With:
* ```
* foo bar
* ```
*/
export function text(ranges: readonly vscode.Range[]): string[];
export function text(ranges: vscode.Range | readonly vscode.Range[]) {
const document = Context.current.document;
if (Array.isArray(ranges)) {
return ranges.map((range) => document.getText(range));
}
return document.getText(ranges as vscode.Range);
}
/**
* Performs changes on the active editor.
*
* ### Example
* ```js
* await edit((editBuilder) => {
* const start = new vscode.Position(0, 2),
* end = new vscode.Position(0, 4);
*
* editBuilder.delete(new vscode.Range(start, end));
* });
* ```
*
* Before:
* ```
* hello world
* ^^^^^ 0
* ```
*
* After:
* ```
* heo world
* ^^^ 0
* ```
*/
export async function edit<T>(
f: (editBuilder: vscode.TextEditorEdit, selections: readonly vscode.Selection[],
document: vscode.TextDocument) => T,
editor?: vscode.TextEditor,
) {
if (editor !== undefined) {
let value!: T;
const succeeded = await editor.edit(
(editBuilder) => value = f(editBuilder, editor!.selections, editor!.document),
noUndoStops,
);
EditNotAppliedError.throwIfNotApplied(succeeded);
return value;
}
return Context.current.edit(f);
}
/**
* Marks a change, inserting a history undo stop.
*/
export function insertUndoStop(editor?: vscode.TextEditor) {
if (editor !== undefined) {
return performDummyEdit(editor);
}
return Context.current.insertUndoStop();
}
/**
* Transforms a list of caret-mode selections (that is, regular selections as
* manipulated internally) into a list of character-mode selections (that is,
* selections modified to include a block character in them).
*
* This function should be used before setting the selections of a
* `vscode.TextEditor` if the selection behavior is `Character`.
*
* ### Example
* Forward-facing, non-empty selections are reduced by one character.
*
* ```js
* // One-character selection becomes empty.
* expect(Selections.toCharacterMode([Selections.fromAnchorActive(0, 0, 0, 1)]), "to satisfy", [
* expect.it("to be empty at coords", 0, 0),
* ]);
*
* // One-character selection becomes empty (at line break).
* expect(Selections.toCharacterMode([Selections.fromAnchorActive(0, 1, 1, 0)]), "to satisfy", [
* expect.it("to be empty at coords", 0, 1),
* ]);
*
* // Forward-facing selection becomes shorter.
* expect(Selections.toCharacterMode([Selections.fromAnchorActive(0, 0, 1, 1)]), "to satisfy", [
* expect.it("to have anchor at coords", 0, 0).and("to have cursor at coords", 1, 0),
* ]);
*
* // One-character selection becomes empty (reversed).
* expect(Selections.toCharacterMode([Selections.fromAnchorActive(0, 1, 0, 0)]), "to satisfy", [
* expect.it("to be empty at coords", 0, 0),
* ]);
*
* // One-character selection becomes empty (reversed, at line break).
* expect(Selections.toCharacterMode([Selections.fromAnchorActive(1, 0, 0, 1)]), "to satisfy", [
* expect.it("to be empty at coords", 0, 1),
* ]);
*
* // Reversed selection stays as-is.
* expect(Selections.toCharacterMode([Selections.fromAnchorActive(1, 1, 0, 0)]), "to satisfy", [
* expect.it("to have anchor at coords", 1, 1).and("to have cursor at coords", 0, 0),
* ]);
*
* // Empty selection stays as-is.
* expect(Selections.toCharacterMode([Selections.empty(1, 1)]), "to satisfy", [
* expect.it("to be empty at coords", 1, 1),
* ]);
* ```
*
* With:
* ```
* a
* b
* ```
*/
export function selectionsToCharacterMode(
selections: readonly vscode.Selection[],
document?: vscode.TextDocument,
) {
const characterModeSelections = [] as vscode.Selection[];
for (const selection of selections) {
const selectionActive = selection.active,
selectionActiveLine = selectionActive.line,
selectionActiveCharacter = selectionActive.character,
selectionAnchor = selection.anchor,
selectionAnchorLine = selectionAnchor.line,
selectionAnchorCharacter = selectionAnchor.character;
let active = selectionActive,
anchor = selectionAnchor,
changed = false;
if (selectionAnchorLine === selectionActiveLine) {
if (selectionAnchorCharacter + 1 === selectionActiveCharacter) {
// Selection is one-character long: make it empty.
active = selectionAnchor;
changed = true;
} else if (selectionAnchorCharacter - 1 === selectionActiveCharacter) {
// Selection is reversed and one-character long: make it empty.
anchor = selectionActive;
changed = true;
} else if (selectionAnchorCharacter < selectionActiveCharacter) {
// Selection is strictly forward-facing: make it shorter.
active = new vscode.Position(selectionActiveLine, selectionActiveCharacter - 1);
changed = true;
} else {
// Selection is reversed or empty: do nothing.
}
} else if (selectionAnchorLine < selectionActiveLine) {
// Selection is strictly forward-facing: make it shorter.
if (selectionActiveCharacter > 0) {
active = new vscode.Position(selectionActiveLine, selectionActiveCharacter - 1);
changed = true;
} else {
// The active character is the first one, so we have to get some
// information from the document.
if (document === undefined) {
document = Context.current.document;
}
const activePrevLine = selectionActiveLine - 1,
activePrevLineLength = document.lineAt(activePrevLine).text.length;
active = new vscode.Position(activePrevLine, activePrevLineLength);
changed = true;
}
} else if (selectionAnchorLine === selectionActiveLine + 1
&& selectionAnchorCharacter === 0
&& selectionActiveCharacter === (document ?? (document = Context.current.document))
.lineAt(selectionActiveLine).text.length) {
// Selection is reversed and one-character long: make it empty.
anchor = selectionActive;
changed = true;
} else {
// Selection is reversed: do nothing.
}
characterModeSelections.push(changed ? new vscode.Selection(anchor, active) : selection);
}
return characterModeSelections;
}
/**
* Reverses the changes made by `toCharacterMode` by increasing by one the
* length of every empty or forward-facing selection.
*
* This function should be used on the selections of a `vscode.TextEditor` if
* the selection behavior is `Character`.
*
* ### Example
* Selections remain empty in empty documents.
*
* ```js
* expect(Selections.fromCharacterMode([Selections.empty(0, 0)]), "to satisfy", [
* expect.it("to be empty at coords", 0, 0),
* ]);
* ```
*
* With:
* ```
* ```
*
* ### Example
* Empty selections automatically become 1-character selections.
*
* ```js
* expect(Selections.fromCharacterMode([Selections.empty(0, 0)]), "to satisfy", [
* expect.it("to have anchor at coords", 0, 0).and("to have cursor at coords", 0, 1),
* ]);
*
* // At the end of the line, it selects the line ending:
* expect(Selections.fromCharacterMode([Selections.empty(0, 1)]), "to satisfy", [
* expect.it("to have anchor at coords", 0, 1).and("to have cursor at coords", 1, 0),
* ]);
*
* // But it does nothing at the end of the document:
* expect(Selections.fromCharacterMode([Selections.empty(2, 0)]), "to satisfy", [
* expect.it("to be empty at coords", 2, 0),
* ]);
* ```
*
* With:
* ```
* a
* b
*
* ```
*/
export function selectionsFromCharacterMode(
selections: readonly vscode.Selection[],
document?: vscode.TextDocument,
) {
const caretModeSelections = [] as vscode.Selection[];
for (const selection of selections) {
const selectionActive = selection.active,
selectionActiveLine = selectionActive.line,
selectionActiveCharacter = selectionActive.character,
selectionAnchor = selection.anchor,
selectionAnchorLine = selectionAnchor.line,
selectionAnchorCharacter = selectionAnchor.character;
let active = selectionActive,
changed = false;
const isEmptyOrForwardFacing = selectionAnchorLine < selectionActiveLine
|| (selectionAnchorLine === selectionActiveLine
&& selectionAnchorCharacter <= selectionActiveCharacter);
if (isEmptyOrForwardFacing) {
// Selection is empty or forward-facing: extend it if possible.
if (document === undefined) {
document = Context.current.document;
}
const lineLength = document.lineAt(selectionActiveLine).text.length;
if (selectionActiveCharacter === lineLength) {
// Character is at the end of the line.
if (selectionActiveLine + 1 < document.lineCount) {
// This is not the last line: we can extend the selection.
active = new vscode.Position(selectionActiveLine + 1, 0);
changed = true;
} else {
// This is the last line: we cannot do anything.
}
} else {
// Character is not at the end of the line: we can extend the selection.
active = new vscode.Position(selectionActiveLine, selectionActiveCharacter + 1);
changed = true;
}
}
caretModeSelections.push(changed ? new vscode.Selection(selectionAnchor, active) : selection);
}
return caretModeSelections;
}