-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
extHostTypes.ts
4221 lines (3505 loc) · 105 KB
/
extHostTypes.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable local/code-no-native-private */
import { asArray, coalesceInPlace, equals } from 'vs/base/common/arrays';
import { illegalArgument } from 'vs/base/common/errors';
import { IRelativePattern } from 'vs/base/common/glob';
import { MarkdownString as BaseMarkdownString, MarkdownStringTrustedOptions } from 'vs/base/common/htmlContent';
import { ResourceMap } from 'vs/base/common/map';
import { Mimes, normalizeMimeType } from 'vs/base/common/mime';
import { nextCharLength } from 'vs/base/common/strings';
import { isNumber, isObject, isString, isStringArray } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { generateUuid } from 'vs/base/common/uuid';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { FileSystemProviderErrorCode, markAsFileSystemProviderError } from 'vs/platform/files/common/files';
import { RemoteAuthorityResolverErrorCode } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { IRelativePatternDto } from 'vs/workbench/api/common/extHost.protocol';
import { CellEditType, ICellMetadataEdit, IDocumentMetadataEdit, isTextStreamMime } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import type * as vscode from 'vscode';
/**
* @deprecated
*
* This utility ensures that old JS code that uses functions for classes still works. Existing usages cannot be removed
* but new ones must not be added
* */
function es5ClassCompat(target: Function): any {
const interceptFunctions = {
apply: function (...args: any[]): any {
if (args.length === 0) {
return Reflect.construct(target, []);
} else {
const argsList = args.length === 1 ? [] : args[1];
return Reflect.construct(target, argsList, args[0].constructor);
}
},
call: function (...args: any[]): any {
if (args.length === 0) {
return Reflect.construct(target, []);
} else {
const [thisArg, ...restArgs] = args;
return Reflect.construct(target, restArgs, thisArg.constructor);
}
}
};
return Object.assign(target, interceptFunctions);
}
export enum TerminalOutputAnchor {
Top = 0,
Bottom = 1
}
export enum TerminalQuickFixType {
TerminalCommand = 0,
Opener = 1,
Command = 3
}
@es5ClassCompat
export class Disposable {
static from(...inDisposables: { dispose(): any }[]): Disposable {
let disposables: ReadonlyArray<{ dispose(): any }> | undefined = inDisposables;
return new Disposable(function () {
if (disposables) {
for (const disposable of disposables) {
if (disposable && typeof disposable.dispose === 'function') {
disposable.dispose();
}
}
disposables = undefined;
}
});
}
#callOnDispose?: () => any;
constructor(callOnDispose: () => any) {
this.#callOnDispose = callOnDispose;
}
dispose(): any {
if (typeof this.#callOnDispose === 'function') {
this.#callOnDispose();
this.#callOnDispose = undefined;
}
}
}
@es5ClassCompat
export class Position {
static Min(...positions: Position[]): Position {
if (positions.length === 0) {
throw new TypeError();
}
let result = positions[0];
for (let i = 1; i < positions.length; i++) {
const p = positions[i];
if (p.isBefore(result)) {
result = p;
}
}
return result;
}
static Max(...positions: Position[]): Position {
if (positions.length === 0) {
throw new TypeError();
}
let result = positions[0];
for (let i = 1; i < positions.length; i++) {
const p = positions[i];
if (p.isAfter(result)) {
result = p;
}
}
return result;
}
static isPosition(other: any): other is Position {
if (!other) {
return false;
}
if (other instanceof Position) {
return true;
}
const { line, character } = <Position>other;
if (typeof line === 'number' && typeof character === 'number') {
return true;
}
return false;
}
static of(obj: vscode.Position): Position {
if (obj instanceof Position) {
return obj;
} else if (this.isPosition(obj)) {
return new Position(obj.line, obj.character);
}
throw new Error('Invalid argument, is NOT a position-like object');
}
private _line: number;
private _character: number;
get line(): number {
return this._line;
}
get character(): number {
return this._character;
}
constructor(line: number, character: number) {
if (line < 0) {
throw illegalArgument('line must be non-negative');
}
if (character < 0) {
throw illegalArgument('character must be non-negative');
}
this._line = line;
this._character = character;
}
isBefore(other: Position): boolean {
if (this._line < other._line) {
return true;
}
if (other._line < this._line) {
return false;
}
return this._character < other._character;
}
isBeforeOrEqual(other: Position): boolean {
if (this._line < other._line) {
return true;
}
if (other._line < this._line) {
return false;
}
return this._character <= other._character;
}
isAfter(other: Position): boolean {
return !this.isBeforeOrEqual(other);
}
isAfterOrEqual(other: Position): boolean {
return !this.isBefore(other);
}
isEqual(other: Position): boolean {
return this._line === other._line && this._character === other._character;
}
compareTo(other: Position): number {
if (this._line < other._line) {
return -1;
} else if (this._line > other.line) {
return 1;
} else {
// equal line
if (this._character < other._character) {
return -1;
} else if (this._character > other._character) {
return 1;
} else {
// equal line and character
return 0;
}
}
}
translate(change: { lineDelta?: number; characterDelta?: number }): Position;
translate(lineDelta?: number, characterDelta?: number): Position;
translate(lineDeltaOrChange: number | undefined | { lineDelta?: number; characterDelta?: number }, characterDelta: number = 0): Position {
if (lineDeltaOrChange === null || characterDelta === null) {
throw illegalArgument();
}
let lineDelta: number;
if (typeof lineDeltaOrChange === 'undefined') {
lineDelta = 0;
} else if (typeof lineDeltaOrChange === 'number') {
lineDelta = lineDeltaOrChange;
} else {
lineDelta = typeof lineDeltaOrChange.lineDelta === 'number' ? lineDeltaOrChange.lineDelta : 0;
characterDelta = typeof lineDeltaOrChange.characterDelta === 'number' ? lineDeltaOrChange.characterDelta : 0;
}
if (lineDelta === 0 && characterDelta === 0) {
return this;
}
return new Position(this.line + lineDelta, this.character + characterDelta);
}
with(change: { line?: number; character?: number }): Position;
with(line?: number, character?: number): Position;
with(lineOrChange: number | undefined | { line?: number; character?: number }, character: number = this.character): Position {
if (lineOrChange === null || character === null) {
throw illegalArgument();
}
let line: number;
if (typeof lineOrChange === 'undefined') {
line = this.line;
} else if (typeof lineOrChange === 'number') {
line = lineOrChange;
} else {
line = typeof lineOrChange.line === 'number' ? lineOrChange.line : this.line;
character = typeof lineOrChange.character === 'number' ? lineOrChange.character : this.character;
}
if (line === this.line && character === this.character) {
return this;
}
return new Position(line, character);
}
toJSON(): any {
return { line: this.line, character: this.character };
}
}
@es5ClassCompat
export class Range {
static isRange(thing: any): thing is vscode.Range {
if (thing instanceof Range) {
return true;
}
if (!thing) {
return false;
}
return Position.isPosition((<Range>thing).start)
&& Position.isPosition((<Range>thing.end));
}
static of(obj: vscode.Range): Range {
if (obj instanceof Range) {
return obj;
}
if (this.isRange(obj)) {
return new Range(obj.start, obj.end);
}
throw new Error('Invalid argument, is NOT a range-like object');
}
protected _start: Position;
protected _end: Position;
get start(): Position {
return this._start;
}
get end(): Position {
return this._end;
}
constructor(start: vscode.Position, end: vscode.Position);
constructor(start: Position, end: Position);
constructor(startLine: number, startColumn: number, endLine: number, endColumn: number);
constructor(startLineOrStart: number | Position | vscode.Position, startColumnOrEnd: number | Position | vscode.Position, endLine?: number, endColumn?: number) {
let start: Position | undefined;
let end: Position | undefined;
if (typeof startLineOrStart === 'number' && typeof startColumnOrEnd === 'number' && typeof endLine === 'number' && typeof endColumn === 'number') {
start = new Position(startLineOrStart, startColumnOrEnd);
end = new Position(endLine, endColumn);
} else if (Position.isPosition(startLineOrStart) && Position.isPosition(startColumnOrEnd)) {
start = Position.of(startLineOrStart);
end = Position.of(startColumnOrEnd);
}
if (!start || !end) {
throw new Error('Invalid arguments');
}
if (start.isBefore(end)) {
this._start = start;
this._end = end;
} else {
this._start = end;
this._end = start;
}
}
contains(positionOrRange: Position | Range): boolean {
if (Range.isRange(positionOrRange)) {
return this.contains(positionOrRange.start)
&& this.contains(positionOrRange.end);
} else if (Position.isPosition(positionOrRange)) {
if (Position.of(positionOrRange).isBefore(this._start)) {
return false;
}
if (this._end.isBefore(positionOrRange)) {
return false;
}
return true;
}
return false;
}
isEqual(other: Range): boolean {
return this._start.isEqual(other._start) && this._end.isEqual(other._end);
}
intersection(other: Range): Range | undefined {
const start = Position.Max(other.start, this._start);
const end = Position.Min(other.end, this._end);
if (start.isAfter(end)) {
// this happens when there is no overlap:
// |-----|
// |----|
return undefined;
}
return new Range(start, end);
}
union(other: Range): Range {
if (this.contains(other)) {
return this;
} else if (other.contains(this)) {
return other;
}
const start = Position.Min(other.start, this._start);
const end = Position.Max(other.end, this.end);
return new Range(start, end);
}
get isEmpty(): boolean {
return this._start.isEqual(this._end);
}
get isSingleLine(): boolean {
return this._start.line === this._end.line;
}
with(change: { start?: Position; end?: Position }): Range;
with(start?: Position, end?: Position): Range;
with(startOrChange: Position | undefined | { start?: Position; end?: Position }, end: Position = this.end): Range {
if (startOrChange === null || end === null) {
throw illegalArgument();
}
let start: Position;
if (!startOrChange) {
start = this.start;
} else if (Position.isPosition(startOrChange)) {
start = startOrChange;
} else {
start = startOrChange.start || this.start;
end = startOrChange.end || this.end;
}
if (start.isEqual(this._start) && end.isEqual(this.end)) {
return this;
}
return new Range(start, end);
}
toJSON(): any {
return [this.start, this.end];
}
}
@es5ClassCompat
export class Selection extends Range {
static isSelection(thing: any): thing is Selection {
if (thing instanceof Selection) {
return true;
}
if (!thing) {
return false;
}
return Range.isRange(thing)
&& Position.isPosition((<Selection>thing).anchor)
&& Position.isPosition((<Selection>thing).active)
&& typeof (<Selection>thing).isReversed === 'boolean';
}
private _anchor: Position;
public get anchor(): Position {
return this._anchor;
}
private _active: Position;
public get active(): Position {
return this._active;
}
constructor(anchor: Position, active: Position);
constructor(anchorLine: number, anchorColumn: number, activeLine: number, activeColumn: number);
constructor(anchorLineOrAnchor: number | Position, anchorColumnOrActive: number | Position, activeLine?: number, activeColumn?: number) {
let anchor: Position | undefined;
let active: Position | undefined;
if (typeof anchorLineOrAnchor === 'number' && typeof anchorColumnOrActive === 'number' && typeof activeLine === 'number' && typeof activeColumn === 'number') {
anchor = new Position(anchorLineOrAnchor, anchorColumnOrActive);
active = new Position(activeLine, activeColumn);
} else if (Position.isPosition(anchorLineOrAnchor) && Position.isPosition(anchorColumnOrActive)) {
anchor = Position.of(anchorLineOrAnchor);
active = Position.of(anchorColumnOrActive);
}
if (!anchor || !active) {
throw new Error('Invalid arguments');
}
super(anchor, active);
this._anchor = anchor;
this._active = active;
}
get isReversed(): boolean {
return this._anchor === this._end;
}
override toJSON() {
return {
start: this.start,
end: this.end,
active: this.active,
anchor: this.anchor
};
}
}
const validateConnectionToken = (connectionToken: string) => {
if (typeof connectionToken !== 'string' || connectionToken.length === 0 || !/^[0-9A-Za-z_\-]+$/.test(connectionToken)) {
throw illegalArgument('connectionToken');
}
};
export class ResolvedAuthority {
public static isResolvedAuthority(resolvedAuthority: any): resolvedAuthority is ResolvedAuthority {
return resolvedAuthority
&& typeof resolvedAuthority === 'object'
&& typeof resolvedAuthority.host === 'string'
&& typeof resolvedAuthority.port === 'number'
&& (resolvedAuthority.connectionToken === undefined || typeof resolvedAuthority.connectionToken === 'string');
}
readonly host: string;
readonly port: number;
readonly connectionToken: string | undefined;
constructor(host: string, port: number, connectionToken?: string) {
if (typeof host !== 'string' || host.length === 0) {
throw illegalArgument('host');
}
if (typeof port !== 'number' || port === 0 || Math.round(port) !== port) {
throw illegalArgument('port');
}
if (typeof connectionToken !== 'undefined') {
validateConnectionToken(connectionToken);
}
this.host = host;
this.port = Math.round(port);
this.connectionToken = connectionToken;
}
}
export class ManagedResolvedAuthority {
public static isManagedResolvedAuthority(resolvedAuthority: any): resolvedAuthority is ManagedResolvedAuthority {
return resolvedAuthority
&& typeof resolvedAuthority === 'object'
&& typeof resolvedAuthority.makeConnection === 'function'
&& (resolvedAuthority.connectionToken === undefined || typeof resolvedAuthority.connectionToken === 'string');
}
constructor(public readonly makeConnection: () => Thenable<vscode.ManagedMessagePassing>, public readonly connectionToken?: string) {
if (typeof connectionToken !== 'undefined') {
validateConnectionToken(connectionToken);
}
}
}
export class RemoteAuthorityResolverError extends Error {
static NotAvailable(message?: string, handled?: boolean): RemoteAuthorityResolverError {
return new RemoteAuthorityResolverError(message, RemoteAuthorityResolverErrorCode.NotAvailable, handled);
}
static TemporarilyNotAvailable(message?: string): RemoteAuthorityResolverError {
return new RemoteAuthorityResolverError(message, RemoteAuthorityResolverErrorCode.TemporarilyNotAvailable);
}
public readonly _message: string | undefined;
public readonly _code: RemoteAuthorityResolverErrorCode;
public readonly _detail: any;
constructor(message?: string, code: RemoteAuthorityResolverErrorCode = RemoteAuthorityResolverErrorCode.Unknown, detail?: any) {
super(message);
this._message = message;
this._code = code;
this._detail = detail;
// workaround when extending builtin objects and when compiling to ES5, see:
// https://github.com/microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, RemoteAuthorityResolverError.prototype);
}
}
export enum EndOfLine {
LF = 1,
CRLF = 2
}
export enum EnvironmentVariableMutatorType {
Replace = 1,
Append = 2,
Prepend = 3
}
@es5ClassCompat
export class TextEdit {
static isTextEdit(thing: any): thing is TextEdit {
if (thing instanceof TextEdit) {
return true;
}
if (!thing) {
return false;
}
return Range.isRange((<TextEdit>thing))
&& typeof (<TextEdit>thing).newText === 'string';
}
static replace(range: Range, newText: string): TextEdit {
return new TextEdit(range, newText);
}
static insert(position: Position, newText: string): TextEdit {
return TextEdit.replace(new Range(position, position), newText);
}
static delete(range: Range): TextEdit {
return TextEdit.replace(range, '');
}
static setEndOfLine(eol: EndOfLine): TextEdit {
const ret = new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), '');
ret.newEol = eol;
return ret;
}
protected _range: Range;
protected _newText: string | null;
protected _newEol?: EndOfLine;
get range(): Range {
return this._range;
}
set range(value: Range) {
if (value && !Range.isRange(value)) {
throw illegalArgument('range');
}
this._range = value;
}
get newText(): string {
return this._newText || '';
}
set newText(value: string) {
if (value && typeof value !== 'string') {
throw illegalArgument('newText');
}
this._newText = value;
}
get newEol(): EndOfLine | undefined {
return this._newEol;
}
set newEol(value: EndOfLine | undefined) {
if (value && typeof value !== 'number') {
throw illegalArgument('newEol');
}
this._newEol = value;
}
constructor(range: Range, newText: string | null) {
this._range = range;
this._newText = newText;
}
toJSON(): any {
return {
range: this.range,
newText: this.newText,
newEol: this._newEol
};
}
}
@es5ClassCompat
export class NotebookEdit implements vscode.NotebookEdit {
static isNotebookCellEdit(thing: any): thing is NotebookEdit {
if (thing instanceof NotebookEdit) {
return true;
}
if (!thing) {
return false;
}
return NotebookRange.isNotebookRange((<NotebookEdit>thing))
&& Array.isArray((<NotebookEdit>thing).newCells);
}
static replaceCells(range: NotebookRange, newCells: NotebookCellData[]): NotebookEdit {
return new NotebookEdit(range, newCells);
}
static insertCells(index: number, newCells: vscode.NotebookCellData[]): vscode.NotebookEdit {
return new NotebookEdit(new NotebookRange(index, index), newCells);
}
static deleteCells(range: NotebookRange): NotebookEdit {
return new NotebookEdit(range, []);
}
static updateCellMetadata(index: number, newMetadata: { [key: string]: any }): NotebookEdit {
const edit = new NotebookEdit(new NotebookRange(index, index), []);
edit.newCellMetadata = newMetadata;
return edit;
}
static updateNotebookMetadata(newMetadata: { [key: string]: any }): NotebookEdit {
const edit = new NotebookEdit(new NotebookRange(0, 0), []);
edit.newNotebookMetadata = newMetadata;
return edit;
}
range: NotebookRange;
newCells: NotebookCellData[];
newCellMetadata?: { [key: string]: any };
newNotebookMetadata?: { [key: string]: any };
constructor(range: NotebookRange, newCells: NotebookCellData[]) {
this.range = range;
this.newCells = newCells;
}
}
export class SnippetTextEdit implements vscode.SnippetTextEdit {
static isSnippetTextEdit(thing: any): thing is SnippetTextEdit {
if (thing instanceof SnippetTextEdit) {
return true;
}
if (!thing) {
return false;
}
return Range.isRange((<SnippetTextEdit>thing).range)
&& SnippetString.isSnippetString((<SnippetTextEdit>thing).snippet);
}
static replace(range: Range, snippet: SnippetString): SnippetTextEdit {
return new SnippetTextEdit(range, snippet);
}
static insert(position: Position, snippet: SnippetString): SnippetTextEdit {
return SnippetTextEdit.replace(new Range(position, position), snippet);
}
range: Range;
snippet: SnippetString;
constructor(range: Range, snippet: SnippetString) {
this.range = range;
this.snippet = snippet;
}
}
export interface IFileOperationOptions {
readonly overwrite?: boolean;
readonly ignoreIfExists?: boolean;
readonly ignoreIfNotExists?: boolean;
readonly recursive?: boolean;
readonly contents?: Uint8Array | vscode.DataTransferFile;
}
export const enum FileEditType {
File = 1,
Text = 2,
Cell = 3,
CellReplace = 5,
Snippet = 6,
}
export interface IFileOperation {
readonly _type: FileEditType.File;
readonly from?: URI;
readonly to?: URI;
readonly options?: IFileOperationOptions;
readonly metadata?: vscode.WorkspaceEditEntryMetadata;
}
export interface IFileTextEdit {
readonly _type: FileEditType.Text;
readonly uri: URI;
readonly edit: TextEdit;
readonly metadata?: vscode.WorkspaceEditEntryMetadata;
}
export interface IFileSnippetTextEdit {
readonly _type: FileEditType.Snippet;
readonly uri: URI;
readonly range: vscode.Range;
readonly edit: vscode.SnippetString;
readonly metadata?: vscode.WorkspaceEditEntryMetadata;
}
export interface IFileCellEdit {
readonly _type: FileEditType.Cell;
readonly uri: URI;
readonly edit?: ICellMetadataEdit | IDocumentMetadataEdit;
readonly notebookMetadata?: Record<string, any>;
readonly metadata?: vscode.WorkspaceEditEntryMetadata;
}
export interface ICellEdit {
readonly _type: FileEditType.CellReplace;
readonly metadata?: vscode.WorkspaceEditEntryMetadata;
readonly uri: URI;
readonly index: number;
readonly count: number;
readonly cells: vscode.NotebookCellData[];
}
type WorkspaceEditEntry = IFileOperation | IFileTextEdit | IFileSnippetTextEdit | IFileCellEdit | ICellEdit;
@es5ClassCompat
export class WorkspaceEdit implements vscode.WorkspaceEdit {
private readonly _edits: WorkspaceEditEntry[] = [];
_allEntries(): ReadonlyArray<WorkspaceEditEntry> {
return this._edits;
}
// --- file
renameFile(from: vscode.Uri, to: vscode.Uri, options?: { readonly overwrite?: boolean; readonly ignoreIfExists?: boolean }, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.File, from, to, options, metadata });
}
createFile(uri: vscode.Uri, options?: { readonly overwrite?: boolean; readonly ignoreIfExists?: boolean; readonly contents?: Uint8Array | vscode.DataTransferFile }, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.File, from: undefined, to: uri, options, metadata });
}
deleteFile(uri: vscode.Uri, options?: { readonly recursive?: boolean; readonly ignoreIfNotExists?: boolean }, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.File, from: uri, to: undefined, options, metadata });
}
// --- notebook
private replaceNotebookMetadata(uri: URI, value: Record<string, any>, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.Cell, metadata, uri, edit: { editType: CellEditType.DocumentMetadata, metadata: value }, notebookMetadata: value });
}
private replaceNotebookCells(uri: URI, startOrRange: vscode.NotebookRange, cellData: vscode.NotebookCellData[], metadata?: vscode.WorkspaceEditEntryMetadata): void {
const start = startOrRange.start;
const end = startOrRange.end;
if (start !== end || cellData.length > 0) {
this._edits.push({ _type: FileEditType.CellReplace, uri, index: start, count: end - start, cells: cellData, metadata });
}
}
private replaceNotebookCellMetadata(uri: URI, index: number, cellMetadata: Record<string, any>, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.Cell, metadata, uri, edit: { editType: CellEditType.Metadata, index, metadata: cellMetadata } });
}
// --- text
replace(uri: URI, range: Range, newText: string, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.Text, uri, edit: new TextEdit(range, newText), metadata });
}
insert(resource: URI, position: Position, newText: string, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this.replace(resource, new Range(position, position), newText, metadata);
}
delete(resource: URI, range: Range, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this.replace(resource, range, '', metadata);
}
// --- text (Maplike)
has(uri: URI): boolean {
return this._edits.some(edit => edit._type === FileEditType.Text && edit.uri.toString() === uri.toString());
}
set(uri: URI, edits: ReadonlyArray<TextEdit | SnippetTextEdit>): void;
set(uri: URI, edits: ReadonlyArray<[TextEdit | SnippetTextEdit, vscode.WorkspaceEditEntryMetadata | undefined]>): void;
set(uri: URI, edits: readonly NotebookEdit[]): void;
set(uri: URI, edits: ReadonlyArray<[NotebookEdit, vscode.WorkspaceEditEntryMetadata | undefined]>): void;
set(uri: URI, edits: null | undefined | ReadonlyArray<TextEdit | SnippetTextEdit | NotebookEdit | [NotebookEdit, vscode.WorkspaceEditEntryMetadata | undefined] | [TextEdit | SnippetTextEdit, vscode.WorkspaceEditEntryMetadata | undefined]>): void {
if (!edits) {
// remove all text, snippet, or notebook edits for `uri`
for (let i = 0; i < this._edits.length; i++) {
const element = this._edits[i];
switch (element._type) {
case FileEditType.Text:
case FileEditType.Snippet:
case FileEditType.Cell:
case FileEditType.CellReplace:
if (element.uri.toString() === uri.toString()) {
this._edits[i] = undefined!; // will be coalesced down below
}
break;
}
}
coalesceInPlace(this._edits);
} else {
// append edit to the end
for (const editOrTuple of edits) {
if (!editOrTuple) {
continue;
}
let edit: TextEdit | SnippetTextEdit | NotebookEdit;
let metadata: vscode.WorkspaceEditEntryMetadata | undefined;
if (Array.isArray(editOrTuple)) {
edit = editOrTuple[0];
metadata = editOrTuple[1];
} else {
edit = editOrTuple;
}
if (NotebookEdit.isNotebookCellEdit(edit)) {
if (edit.newCellMetadata) {
this.replaceNotebookCellMetadata(uri, edit.range.start, edit.newCellMetadata, metadata);
} else if (edit.newNotebookMetadata) {
this.replaceNotebookMetadata(uri, edit.newNotebookMetadata, metadata);
} else {
this.replaceNotebookCells(uri, edit.range, edit.newCells, metadata);
}
} else if (SnippetTextEdit.isSnippetTextEdit(edit)) {
this._edits.push({ _type: FileEditType.Snippet, uri, range: edit.range, edit: edit.snippet, metadata });
} else {
this._edits.push({ _type: FileEditType.Text, uri, edit, metadata });
}
}
}
}
get(uri: URI): TextEdit[] {
const res: TextEdit[] = [];
for (const candidate of this._edits) {
if (candidate._type === FileEditType.Text && candidate.uri.toString() === uri.toString()) {
res.push(candidate.edit);
}
}
return res;
}
entries(): [URI, TextEdit[]][] {
const textEdits = new ResourceMap<[URI, TextEdit[]]>();
for (const candidate of this._edits) {
if (candidate._type === FileEditType.Text) {
let textEdit = textEdits.get(candidate.uri);
if (!textEdit) {
textEdit = [candidate.uri, []];
textEdits.set(candidate.uri, textEdit);
}
textEdit[1].push(candidate.edit);
}
}
return [...textEdits.values()];
}
get size(): number {
return this.entries().length;
}
toJSON(): any {
return this.entries();
}
}
@es5ClassCompat
export class SnippetString {
static isSnippetString(thing: any): thing is SnippetString {
if (thing instanceof SnippetString) {
return true;
}
if (!thing) {
return false;
}
return typeof (<SnippetString>thing).value === 'string';
}
private static _escape(value: string): string {
return value.replace(/\$|}|\\/g, '\\$&');
}
private _tabstop: number = 1;
value: string;
constructor(value?: string) {
this.value = value || '';
}
appendText(string: string): SnippetString {
this.value += SnippetString._escape(string);
return this;
}
appendTabstop(number: number = this._tabstop++): SnippetString {
this.value += '$';
this.value += number;
return this;
}
appendPlaceholder(value: string | ((snippet: SnippetString) => any), number: number = this._tabstop++): SnippetString {
if (typeof value === 'function') {
const nested = new SnippetString();
nested._tabstop = this._tabstop;
value(nested);
this._tabstop = nested._tabstop;
value = nested.value;
} else {
value = SnippetString._escape(value);
}
this.value += '${';