-
Notifications
You must be signed in to change notification settings - Fork 29.3k
/
editor.ts
1065 lines (858 loc) · 27.8 KB
/
editor.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.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import { Event, Emitter, once } from 'vs/base/common/event';
import * as objects from 'vs/base/common/objects';
import * as types from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
import { IEditor as ICodeEditor, IEditorViewState, ScrollType, IDiffEditor } from 'vs/editor/common/editorCommon';
import { IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput } from 'vs/platform/editor/common/editor';
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { Registry } from 'vs/platform/registry/common/platform';
import { ITextModel } from 'vs/editor/common/model';
import { Schemas } from 'vs/base/common/network';
import { IEditorGroup } from 'vs/workbench/services/group/common/editorGroupsService';
import { ICompositeControl } from 'vs/workbench/common/composite';
import { ActionRunner, IAction } from 'vs/base/common/actions';
export const ActiveEditorContext = new RawContextKey<string>('activeEditor', null);
export const EditorsVisibleContext = new RawContextKey<boolean>('editorIsOpen', false);
export const EditorGroupActiveEditorDirtyContext = new RawContextKey<boolean>('groupActiveEditorDirty', false);
export const NoEditorsVisibleContext: ContextKeyExpr = EditorsVisibleContext.toNegated();
export const TextCompareEditorVisibleContext = new RawContextKey<boolean>('textCompareEditorVisible', false);
export const TextCompareEditorActiveContext = new RawContextKey<boolean>('textCompareEditorActive', false);
export const ActiveEditorGroupEmptyContext = new RawContextKey<boolean>('activeEditorGroupEmpty', false);
export const MultipleEditorGroupsContext = new RawContextKey<boolean>('multipleEditorGroups', false);
export const SingleEditorGroupsContext = MultipleEditorGroupsContext.toNegated();
export const InEditorZenModeContext = new RawContextKey<boolean>('inZenMode', false);
export const SplitEditorsVertically = new RawContextKey<boolean>('splitEditorsVertically', false);
/**
* Text diff editor id.
*/
export const TEXT_DIFF_EDITOR_ID = 'workbench.editors.textDiffEditor';
/**
* Binary diff editor id.
*/
export const BINARY_DIFF_EDITOR_ID = 'workbench.editors.binaryResourceDiffEditor';
export interface IEditor {
/**
* The assigned input of this editor.
*/
input: IEditorInput;
/**
* The assigned options of this editor.
*/
options: IEditorOptions;
/**
* The assigned group this editor is showing in.
*/
group: IEditorGroup;
/**
* The minimum width of this editor.
*/
readonly minimumWidth: number;
/**
* The maximum width of this editor.
*/
readonly maximumWidth: number;
/**
* The minimum height of this editor.
*/
readonly minimumHeight: number;
/**
* The maximum height of this editor.
*/
readonly maximumHeight: number;
/**
* An event to notify whenever minimum/maximum width/height changes.
*/
readonly onDidSizeConstraintsChange: Event<{ width: number; height: number; }>;
/**
* Returns the unique identifier of this editor.
*/
getId(): string;
/**
* Returns the underlying control of this editor.
*/
getControl(): IEditorControl;
/**
* Asks the underlying control to focus.
*/
focus(): void;
/**
* Finds out if this editor is visible or not.
*/
isVisible(): boolean;
}
export interface ITextEditor extends IEditor {
/**
* Returns the underlying text editor widget of this editor.
*/
getControl(): ICodeEditor;
}
export interface ITextDiffEditor extends IEditor {
/**
* Returns the underlying text editor widget of this editor.
*/
getControl(): IDiffEditor;
}
export interface ITextSideBySideEditor extends IEditor {
/**
* Returns the underlying text editor widget of the master side
* of this side-by-side editor.
*/
getMasterEditor(): ITextEditor;
/**
* Returns the underlying text editor widget of the details side
* of this side-by-side editor.
*/
getDetailsEditor(): ITextEditor;
}
/**
* Marker interface for the base editor control
*/
export interface IEditorControl extends ICompositeControl { }
export interface IFileInputFactory {
createFileInput(resource: URI, encoding: string, instantiationService: IInstantiationService): IFileEditorInput;
isFileInput(obj: any): obj is IFileEditorInput;
}
export interface IEditorInputFactoryRegistry {
/**
* Registers the file input factory to use for file inputs.
*/
registerFileInputFactory(factory: IFileInputFactory): void;
/**
* Returns the file input factory to use for file inputs.
*/
getFileInputFactory(): IFileInputFactory;
/**
* Registers a editor input factory for the given editor input to the registry. An editor input factory
* is capable of serializing and deserializing editor inputs from string data.
*
* @param editorInputId the identifier of the editor input
* @param factory the editor input factory for serialization/deserialization
*/
registerEditorInputFactory(editorInputId: string, ctor: IConstructorSignature0<IEditorInputFactory>): void;
/**
* Returns the editor input factory for the given editor input.
*
* @param editorInputId the identifier of the editor input
*/
getEditorInputFactory(editorInputId: string): IEditorInputFactory;
setInstantiationService(service: IInstantiationService): void;
}
export interface IEditorInputFactory {
/**
* Returns a string representation of the provided editor input that contains enough information
* to deserialize back to the original editor input from the deserialize() method.
*/
serialize(editorInput: EditorInput): string;
/**
* Returns an editor input from the provided serialized form of the editor input. This form matches
* the value returned from the serialize() method.
*/
deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput;
}
export interface IUntitledResourceInput extends IBaseResourceInput {
/**
* Optional resource. If the resource is not provided a new untitled file is created.
*/
resource?: URI;
/**
* Optional file path. Using the file resource will associate the file to the untitled resource.
*/
filePath?: string;
/**
* Optional language of the untitled resource.
*/
language?: string;
/**
* Optional contents of the untitled resource.
*/
contents?: string;
/**
* Optional encoding of the untitled resource.
*/
encoding?: string;
}
export interface IResourceDiffInput extends IBaseResourceInput {
/**
* The left hand side URI to open inside a diff editor.
*/
leftResource: URI;
/**
* The right hand side URI to open inside a diff editor.
*/
rightResource: URI;
}
export interface IResourceSideBySideInput extends IBaseResourceInput {
/**
* The right hand side URI to open inside a side by side editor.
*/
masterResource: URI;
/**
* The left hand side URI to open inside a side by side editor.
*/
detailResource: URI;
}
export const enum Verbosity {
SHORT,
MEDIUM,
LONG
}
export interface IRevertOptions {
/**
* Forces to load the contents of the editor again even if the editor is not dirty.
*/
force?: boolean;
/**
* A soft revert will clear dirty state of an editor but will not attempt to load it.
*/
soft?: boolean;
}
export interface IEditorInput extends IDisposable {
/**
* Triggered when this input is disposed.
*/
onDispose: Event<void>;
/**
* Returns the associated resource of this input.
*/
getResource(): URI;
/**
* Unique type identifier for this inpput.
*/
getTypeId(): string;
/**
* Returns the display name of this input.
*/
getName(): string;
/**
* Returns the display description of this input.
*/
getDescription(verbosity?: Verbosity): string;
/**
* Returns the display title of this input.
*/
getTitle(verbosity?: Verbosity): string;
/**
* Resolves the input.
*/
resolve(): TPromise<IEditorModel>;
/**
* Returns if this input is dirty or not.
*/
isDirty(): boolean;
/**
* Reverts this input.
*/
revert(options?: IRevertOptions): TPromise<boolean>;
/**
* Returns if the other object matches this input.
*/
matches(other: any): boolean;
}
/**
* Editor inputs are lightweight objects that can be passed to the workbench API to open inside the editor part.
* Each editor input is mapped to an editor that is capable of opening it through the Platform facade.
*/
export abstract class EditorInput extends Disposable implements IEditorInput {
protected readonly _onDidChangeDirty: Emitter<void> = this._register(new Emitter<void>());
get onDidChangeDirty(): Event<void> { return this._onDidChangeDirty.event; }
protected readonly _onDidChangeLabel: Emitter<void> = this._register(new Emitter<void>());
get onDidChangeLabel(): Event<void> { return this._onDidChangeLabel.event; }
private readonly _onDispose: Emitter<void> = this._register(new Emitter<void>());
get onDispose(): Event<void> { return this._onDispose.event; }
private disposed: boolean = false;
/**
* Returns the unique type identifier of this input.
*/
abstract getTypeId(): string;
/**
* Returns the associated resource of this input if any.
*/
getResource(): URI {
return null;
}
/**
* Returns the name of this input that can be shown to the user. Examples include showing the name of the input
* above the editor area when the input is shown.
*/
getName(): string {
return null;
}
/**
* Returns the description of this input that can be shown to the user. Examples include showing the description of
* the input above the editor area to the side of the name of the input.
*/
getDescription(verbosity?: Verbosity): string {
return null;
}
/**
* Returns the title of this input that can be shown to the user. Examples include showing the title of
* the input above the editor area as hover over the input label.
*/
getTitle(verbosity?: Verbosity): string {
return this.getName();
}
/**
* Returns the preferred editor for this input. A list of candidate editors is passed in that whee registered
* for the input. This allows subclasses to decide late which editor to use for the input on a case by case basis.
*/
getPreferredEditorId(candidates: string[]): string {
if (candidates && candidates.length > 0) {
return candidates[0];
}
return null;
}
/**
* Returns a descriptor suitable for telemetry events or null if none is available.
*
* Subclasses should extend if they can contribute.
*/
getTelemetryDescriptor(): object {
/* __GDPR__FRAGMENT__
"EditorTelemetryDescriptor" : {
"typeId" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
return { typeId: this.getTypeId() };
}
/**
* Returns a type of EditorModel that represents the resolved input. Subclasses should
* override to provide a meaningful model.
*/
abstract resolve(): TPromise<IEditorModel>;
/**
* An editor that is dirty will be asked to be saved once it closes.
*/
isDirty(): boolean {
return false;
}
/**
* Subclasses should bring up a proper dialog for the user if the editor is dirty and return the result.
*/
confirmSave(): TPromise<ConfirmResult> {
return TPromise.wrap(ConfirmResult.DONT_SAVE);
}
/**
* Saves the editor if it is dirty. Subclasses return a promise with a boolean indicating the success of the operation.
*/
save(): TPromise<boolean> {
return TPromise.as(true);
}
/**
* Reverts the editor if it is dirty. Subclasses return a promise with a boolean indicating the success of the operation.
*/
revert(options?: IRevertOptions): TPromise<boolean> {
return TPromise.as(true);
}
/**
* Called when this input is no longer opened in any editor. Subclasses can free resources as needed.
*/
close(): void {
this.dispose();
}
/**
* Subclasses can set this to false if it does not make sense to split the editor input.
*/
supportsSplitEditor(): boolean {
return true;
}
/**
* Returns true if this input is identical to the otherInput.
*/
matches(otherInput: any): boolean {
return this === otherInput;
}
/**
* Returns whether this input was disposed or not.
*/
isDisposed(): boolean {
return this.disposed;
}
/**
* Called when an editor input is no longer needed. Allows to free up any resources taken by
* resolving the editor input.
*/
dispose(): void {
this.disposed = true;
this._onDispose.fire();
super.dispose();
}
}
export const enum ConfirmResult {
SAVE,
DONT_SAVE,
CANCEL
}
export const enum EncodingMode {
/**
* Instructs the encoding support to encode the current input with the provided encoding
*/
Encode,
/**
* Instructs the encoding support to decode the current input with the provided encoding
*/
Decode
}
export interface IEncodingSupport {
/**
* Gets the encoding of the input if known.
*/
getEncoding(): string;
/**
* Sets the encoding for the input for saving.
*/
setEncoding(encoding: string, mode: EncodingMode): void;
}
/**
* This is a tagging interface to declare an editor input being capable of dealing with files. It is only used in the editor registry
* to register this kind of input to the platform.
*/
export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
/**
* Sets the preferred encodingt to use for this input.
*/
setPreferredEncoding(encoding: string): void;
/**
* Forces this file input to open as binary instead of text.
*/
setForceOpenAsBinary(): void;
}
/**
* Side by side editor inputs that have a master and details side.
*/
export class SideBySideEditorInput extends EditorInput {
static readonly ID: string = 'workbench.editorinputs.sidebysideEditorInput';
constructor(private name: string, private description: string, private _details: EditorInput, private _master: EditorInput) {
super();
this.registerListeners();
}
get master(): EditorInput {
return this._master;
}
get details(): EditorInput {
return this._details;
}
isDirty(): boolean {
return this.master.isDirty();
}
confirmSave(): TPromise<ConfirmResult> {
return this.master.confirmSave();
}
save(): TPromise<boolean> {
return this.master.save();
}
revert(): TPromise<boolean> {
return this.master.revert();
}
getTelemetryDescriptor(): object {
const descriptor = this.master.getTelemetryDescriptor();
return objects.assign(descriptor, super.getTelemetryDescriptor());
}
private registerListeners(): void {
// When the details or master input gets disposed, dispose this diff editor input
const onceDetailsDisposed = once(this.details.onDispose);
this._register(onceDetailsDisposed(() => {
if (!this.isDisposed()) {
this.dispose();
}
}));
const onceMasterDisposed = once(this.master.onDispose);
this._register(onceMasterDisposed(() => {
if (!this.isDisposed()) {
this.dispose();
}
}));
// Reemit some events from the master side to the outside
this._register(this.master.onDidChangeDirty(() => this._onDidChangeDirty.fire()));
this._register(this.master.onDidChangeLabel(() => this._onDidChangeLabel.fire()));
}
resolve(): TPromise<EditorModel> {
return TPromise.as(null);
}
getTypeId(): string {
return SideBySideEditorInput.ID;
}
getName(): string {
return this.name;
}
getDescription(): string {
return this.description;
}
matches(otherInput: any): boolean {
if (super.matches(otherInput) === true) {
return true;
}
if (otherInput) {
if (!(otherInput instanceof SideBySideEditorInput)) {
return false;
}
const otherDiffInput = <SideBySideEditorInput>otherInput;
return this.details.matches(otherDiffInput.details) && this.master.matches(otherDiffInput.master);
}
return false;
}
}
export interface ITextEditorModel extends IEditorModel {
textEditorModel: ITextModel;
}
/**
* The editor model is the heavyweight counterpart of editor input. Depending on the editor input, it
* connects to the disk to retrieve content and may allow for saving it back or reverting it. Editor models
* are typically cached for some while because they are expensive to construct.
*/
export class EditorModel extends Disposable implements IEditorModel {
private readonly _onDispose: Emitter<void> = this._register(new Emitter<void>());
get onDispose(): Event<void> { return this._onDispose.event; }
/**
* Causes this model to load returning a promise when loading is completed.
*/
load(): TPromise<EditorModel> {
return TPromise.as(this);
}
/**
* Returns whether this model was loaded or not.
*/
isResolved(): boolean {
return true;
}
/**
* Subclasses should implement to free resources that have been claimed through loading.
*/
dispose(): void {
this._onDispose.fire();
super.dispose();
}
}
export interface IEditorInputWithOptions {
editor: IEditorInput;
options?: IEditorOptions | ITextEditorOptions;
}
export function isEditorInputWithOptions(obj: any): obj is IEditorInputWithOptions {
const editorInputWithOptions = obj as IEditorInputWithOptions;
return !!editorInputWithOptions && !!editorInputWithOptions.editor;
}
/**
* The editor options is the base class of options that can be passed in when opening an editor.
*/
export class EditorOptions implements IEditorOptions {
/**
* Helper to create EditorOptions inline.
*/
static create(settings: IEditorOptions): EditorOptions {
const options = new EditorOptions();
options.preserveFocus = settings.preserveFocus;
options.forceReload = settings.forceReload;
options.revealIfVisible = settings.revealIfVisible;
options.revealIfOpened = settings.revealIfOpened;
options.pinned = settings.pinned;
options.index = settings.index;
options.inactive = settings.inactive;
return options;
}
/**
* Tells the editor to not receive keyboard focus when the editor is being opened. By default,
* the editor will receive keyboard focus on open.
*/
preserveFocus: boolean;
/**
* Tells the editor to reload the editor input in the editor even if it is identical to the one
* already showing. By default, the editor will not reload the input if it is identical to the
* one showing.
*/
forceReload: boolean;
/**
* Will reveal the editor if it is already opened and visible in any of the opened editor groups.
*/
revealIfVisible: boolean;
/**
* Will reveal the editor if it is already opened (even when not visible) in any of the opened editor groups.
*/
revealIfOpened: boolean;
/**
* An editor that is pinned remains in the editor stack even when another editor is being opened.
* An editor that is not pinned will always get replaced by another editor that is not pinned.
*/
pinned: boolean;
/**
* The index in the document stack where to insert the editor into when opening.
*/
index: number;
/**
* An active editor that is opened will show its contents directly. Set to true to open an editor
* in the background.
*/
inactive: boolean;
}
/**
* Base Text Editor Options.
*/
export class TextEditorOptions extends EditorOptions {
private startLineNumber: number;
private startColumn: number;
private endLineNumber: number;
private endColumn: number;
private revealInCenterIfOutsideViewport: boolean;
private editorViewState: IEditorViewState;
static from(input?: IBaseResourceInput): TextEditorOptions {
if (!input || !input.options) {
return null;
}
return TextEditorOptions.create(input.options);
}
/**
* Helper to convert options bag to real class
*/
static create(options: ITextEditorOptions = Object.create(null)): TextEditorOptions {
const textEditorOptions = new TextEditorOptions();
if (options.selection) {
const selection = options.selection;
textEditorOptions.selection(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
}
if (options.viewState) {
textEditorOptions.editorViewState = options.viewState as IEditorViewState;
}
if (options.forceReload) {
textEditorOptions.forceReload = true;
}
if (options.revealIfVisible) {
textEditorOptions.revealIfVisible = true;
}
if (options.revealIfOpened) {
textEditorOptions.revealIfOpened = true;
}
if (options.preserveFocus) {
textEditorOptions.preserveFocus = true;
}
if (options.revealInCenterIfOutsideViewport) {
textEditorOptions.revealInCenterIfOutsideViewport = true;
}
if (options.pinned) {
textEditorOptions.pinned = true;
}
if (options.inactive) {
textEditorOptions.inactive = true;
}
if (typeof options.index === 'number') {
textEditorOptions.index = options.index;
}
return textEditorOptions;
}
/**
* Returns if this options object has objects defined for the editor.
*/
hasOptionsDefined(): boolean {
return !!this.editorViewState || (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn));
}
/**
* Tells the editor to set show the given selection when the editor is being opened.
*/
selection(startLineNumber: number, startColumn: number, endLineNumber: number = startLineNumber, endColumn: number = startColumn): EditorOptions {
this.startLineNumber = startLineNumber;
this.startColumn = startColumn;
this.endLineNumber = endLineNumber;
this.endColumn = endColumn;
return this;
}
/**
* Create a TextEditorOptions inline to be used when the editor is opening.
*/
static fromEditor(editor: ICodeEditor, settings?: IEditorOptions): TextEditorOptions {
const options = TextEditorOptions.create(settings);
// View state
options.editorViewState = editor.saveViewState();
return options;
}
/**
* Apply the view state or selection to the given editor.
*
* @return if something was applied
*/
apply(editor: ICodeEditor, scrollType: ScrollType): boolean {
// View state
return this.applyViewState(editor, scrollType);
}
private applyViewState(editor: ICodeEditor, scrollType: ScrollType): boolean {
let gotApplied = false;
// First try viewstate
if (this.editorViewState) {
editor.restoreViewState(this.editorViewState);
gotApplied = true;
}
// Otherwise check for selection
else if (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn)) {
// Select
if (!types.isUndefinedOrNull(this.endLineNumber) && !types.isUndefinedOrNull(this.endColumn)) {
const range = {
startLineNumber: this.startLineNumber,
startColumn: this.startColumn,
endLineNumber: this.endLineNumber,
endColumn: this.endColumn
};
editor.setSelection(range);
if (this.revealInCenterIfOutsideViewport) {
editor.revealRangeInCenterIfOutsideViewport(range, scrollType);
} else {
editor.revealRangeInCenter(range, scrollType);
}
}
// Reveal
else {
const pos = {
lineNumber: this.startLineNumber,
column: this.startColumn
};
editor.setPosition(pos);
if (this.revealInCenterIfOutsideViewport) {
editor.revealPositionInCenterIfOutsideViewport(pos, scrollType);
} else {
editor.revealPositionInCenter(pos, scrollType);
}
}
gotApplied = true;
}
return gotApplied;
}
}
export interface IEditorIdentifier {
groupId: GroupIdentifier;
editor: IEditorInput;
}
/**
* The editor commands context is used for editor commands (e.g. in the editor title)
* and we must ensure that the context is serializable because it potentially travels
* to the extension host!
*/
export interface IEditorCommandsContext {
groupId: GroupIdentifier;
editorIndex?: number;
}
export class EditorCommandsContextActionRunner extends ActionRunner {
constructor(
private context: IEditorCommandsContext
) {
super();
}
run(action: IAction, context?: any): TPromise<void> {
return super.run(action, this.context);
}
}
export interface IEditorCloseEvent extends IEditorIdentifier {
replaced: boolean;
index: number;
}
export type GroupIdentifier = number;
export interface IWorkbenchEditorConfiguration {
workbench: {
editor: IWorkbenchEditorPartConfiguration,
iconTheme: string;
};
}
export interface IWorkbenchEditorPartConfiguration {
showTabs?: boolean;
tabCloseButton?: 'left' | 'right' | 'off';
tabSizing?: 'fit' | 'shrink';
showIcons?: boolean;
enablePreview?: boolean;
enablePreviewFromQuickOpen?: boolean;
closeOnFileDelete?: boolean;
openPositioning?: 'left' | 'right' | 'first' | 'last';
openSideBySideDirection?: 'right' | 'down';
closeEmptyGroups?: boolean;
revealIfOpen?: boolean;
swipeToNavigate?: boolean;
labelFormat?: 'default' | 'short' | 'medium' | 'long';
restoreViewState?: boolean;
}
export interface IResourceOptions {
supportSideBySide?: boolean;
filter?: string | string[];
}
export function toResource(editor: IEditorInput, options?: IResourceOptions): URI {
if (!editor) {
return null;
}
// Check for side by side if we are asked to
if (options && options.supportSideBySide && editor instanceof SideBySideEditorInput) {
editor = editor.master;
}
const resource = editor.getResource();
if (!options || !options.filter) {
return resource; // return early if no filter is specified
}
if (!resource) {
return null;
}
let includeFiles: boolean;
let includeUntitled: boolean;
if (Array.isArray(options.filter)) {
includeFiles = (options.filter.indexOf(Schemas.file) >= 0);
includeUntitled = (options.filter.indexOf(Schemas.untitled) >= 0);
} else {
includeFiles = (options.filter === Schemas.file);
includeUntitled = (options.filter === Schemas.untitled);
}
if (includeFiles && resource.scheme === Schemas.file) {
return resource;
}
if (includeUntitled && resource.scheme === Schemas.untitled) {
return resource;
}
return null;
}