-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
editorActions.ts
2661 lines (2194 loc) · 82.2 KB
/
editorActions.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.
*--------------------------------------------------------------------------------------------*/
import { localize, localize2 } from 'vs/nls';
import { Action } from 'vs/base/common/actions';
import { firstOrDefault } from 'vs/base/common/arrays';
import { IEditorIdentifier, IEditorCommandsContext, CloseDirection, SaveReason, EditorsOrder, EditorInputCapabilities, DEFAULT_EDITOR_ASSOCIATION, GroupIdentifier, EditorResourceAccessor } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { SideBySideEditorInput } from 'vs/workbench/common/editor/sideBySideEditorInput';
import { IWorkbenchLayoutService, Parts } from 'vs/workbench/services/layout/browser/layoutService';
import { GoFilter, IHistoryService } from 'vs/workbench/services/history/common/history';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { CLOSE_EDITOR_COMMAND_ID, MOVE_ACTIVE_EDITOR_COMMAND_ID, ActiveEditorMoveCopyArguments, SPLIT_EDITOR_LEFT, SPLIT_EDITOR_RIGHT, SPLIT_EDITOR_UP, SPLIT_EDITOR_DOWN, splitEditor, LAYOUT_EDITOR_GROUPS_COMMAND_ID, UNPIN_EDITOR_COMMAND_ID, COPY_ACTIVE_EDITOR_COMMAND_ID, SPLIT_EDITOR, resolveCommandsContext, getCommandsContext, TOGGLE_MAXIMIZE_EDITOR_GROUP, MOVE_EDITOR_INTO_NEW_WINDOW_COMMAND_ID, COPY_EDITOR_INTO_NEW_WINDOW_COMMAND_ID, MOVE_EDITOR_GROUP_INTO_NEW_WINDOW_COMMAND_ID, COPY_EDITOR_GROUP_INTO_NEW_WINDOW_COMMAND_ID, NEW_EMPTY_EDITOR_WINDOW_COMMAND_ID as NEW_EMPTY_EDITOR_WINDOW_COMMAND_ID, resolveEditorsContext, getEditorsContext } from 'vs/workbench/browser/parts/editor/editorCommands';
import { IEditorGroupsService, IEditorGroup, GroupsArrangement, GroupLocation, GroupDirection, preferredSideBySideGroupDirection, IFindGroupScope, GroupOrientation, EditorGroupLayout, GroupsOrder, MergeGroupMode } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
import { IFileDialogService, ConfirmResult, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { ItemActivation, IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { AllEditorsByMostRecentlyUsedQuickAccess, ActiveGroupEditorsByMostRecentlyUsedQuickAccess, AllEditorsByAppearanceQuickAccess } from 'vs/workbench/browser/parts/editor/editorQuickAccess';
import { Codicon } from 'vs/base/common/codicons';
import { ThemeIcon } from 'vs/base/common/themables';
import { IFilesConfigurationService, AutoSaveMode } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { IEditorResolverService } from 'vs/workbench/services/editor/common/editorResolverService';
import { isLinux, isNative, isWindows } from 'vs/base/common/platform';
import { Action2, IAction2Options, MenuId } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { KeyChord, KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { IKeybindingRule, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ILogService } from 'vs/platform/log/common/log';
import { Categories } from 'vs/platform/action/common/actionCommonCategories';
import { ActiveEditorAvailableEditorIdsContext, ActiveEditorContext, ActiveEditorGroupEmptyContext, AuxiliaryBarVisibleContext, EditorPartMaximizedEditorGroupContext, EditorPartMultipleEditorGroupsContext, IsAuxiliaryWindowFocusedContext, MultipleEditorGroupsContext, SideBarVisibleContext } from 'vs/workbench/common/contextkeys';
import { URI } from 'vs/base/common/uri';
import { getActiveDocument } from 'vs/base/browser/dom';
import { ICommandActionTitle } from 'vs/platform/action/common/action';
class ExecuteCommandAction extends Action2 {
constructor(
desc: Readonly<IAction2Options>,
private readonly commandId: string,
private readonly commandArgs?: unknown
) {
super(desc);
}
override run(accessor: ServicesAccessor): Promise<void> {
const commandService = accessor.get(ICommandService);
return commandService.executeCommand(this.commandId, this.commandArgs);
}
}
abstract class AbstractSplitEditorAction extends Action2 {
protected getDirection(configurationService: IConfigurationService): GroupDirection {
return preferredSideBySideGroupDirection(configurationService);
}
override async run(accessor: ServicesAccessor, resourceOrContext?: URI | IEditorCommandsContext, context?: IEditorCommandsContext): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
const configurationService = accessor.get(IConfigurationService);
const commandContext = getCommandsContext(accessor, resourceOrContext, context);
splitEditor(editorGroupService, this.getDirection(configurationService), commandContext ? [commandContext] : undefined);
}
}
export class SplitEditorAction extends AbstractSplitEditorAction {
static readonly ID = SPLIT_EDITOR;
constructor() {
super({
id: SplitEditorAction.ID,
title: localize2('splitEditor', 'Split Editor'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyMod.CtrlCmd | KeyCode.Backslash
},
category: Categories.View
});
}
}
export class SplitEditorOrthogonalAction extends AbstractSplitEditorAction {
constructor() {
super({
id: 'workbench.action.splitEditorOrthogonal',
title: localize2('splitEditorOrthogonal', 'Split Editor Orthogonal'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.Backslash)
},
category: Categories.View
});
}
protected override getDirection(configurationService: IConfigurationService): GroupDirection {
const direction = preferredSideBySideGroupDirection(configurationService);
return direction === GroupDirection.RIGHT ? GroupDirection.DOWN : GroupDirection.RIGHT;
}
}
export class SplitEditorLeftAction extends ExecuteCommandAction {
constructor() {
super({
id: SPLIT_EDITOR_LEFT,
title: localize2('splitEditorGroupLeft', 'Split Editor Left'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.Backslash)
},
category: Categories.View
}, SPLIT_EDITOR_LEFT);
}
}
export class SplitEditorRightAction extends ExecuteCommandAction {
constructor() {
super({
id: SPLIT_EDITOR_RIGHT,
title: localize2('splitEditorGroupRight', 'Split Editor Right'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.Backslash)
},
category: Categories.View
}, SPLIT_EDITOR_RIGHT);
}
}
export class SplitEditorUpAction extends ExecuteCommandAction {
static readonly LABEL = localize('splitEditorGroupUp', "Split Editor Up");
constructor() {
super({
id: SPLIT_EDITOR_UP,
title: localize2('splitEditorGroupUp', "Split Editor Up"),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.Backslash)
},
category: Categories.View
}, SPLIT_EDITOR_UP);
}
}
export class SplitEditorDownAction extends ExecuteCommandAction {
static readonly LABEL = localize('splitEditorGroupDown', "Split Editor Down");
constructor() {
super({
id: SPLIT_EDITOR_DOWN,
title: localize2('splitEditorGroupDown', "Split Editor Down"),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.Backslash)
},
category: Categories.View
}, SPLIT_EDITOR_DOWN);
}
}
export class JoinTwoGroupsAction extends Action2 {
constructor() {
super({
id: 'workbench.action.joinTwoGroups',
title: localize2('joinTwoGroups', 'Join Editor Group with Next Group'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor, context?: IEditorIdentifier): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
let sourceGroup: IEditorGroup | undefined;
if (context && typeof context.groupId === 'number') {
sourceGroup = editorGroupService.getGroup(context.groupId);
} else {
sourceGroup = editorGroupService.activeGroup;
}
if (sourceGroup) {
const targetGroupDirections = [GroupDirection.RIGHT, GroupDirection.DOWN, GroupDirection.LEFT, GroupDirection.UP];
for (const targetGroupDirection of targetGroupDirections) {
const targetGroup = editorGroupService.findGroup({ direction: targetGroupDirection }, sourceGroup);
if (targetGroup && sourceGroup !== targetGroup) {
editorGroupService.mergeGroup(sourceGroup, targetGroup);
break;
}
}
}
}
}
export class JoinAllGroupsAction extends Action2 {
constructor() {
super({
id: 'workbench.action.joinAllGroups',
title: localize2('joinAllGroups', 'Join All Editor Groups'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
editorGroupService.mergeAllGroups(editorGroupService.activeGroup);
}
}
export class NavigateBetweenGroupsAction extends Action2 {
constructor() {
super({
id: 'workbench.action.navigateEditorGroups',
title: localize2('navigateEditorGroups', 'Navigate Between Editor Groups'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
const nextGroup = editorGroupService.findGroup({ location: GroupLocation.NEXT }, editorGroupService.activeGroup, true);
nextGroup?.focus();
}
}
export class FocusActiveGroupAction extends Action2 {
constructor() {
super({
id: 'workbench.action.focusActiveEditorGroup',
title: localize2('focusActiveEditorGroup', 'Focus Active Editor Group'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
editorGroupService.activeGroup.focus();
}
}
abstract class AbstractFocusGroupAction extends Action2 {
constructor(
desc: Readonly<IAction2Options>,
private readonly scope: IFindGroupScope
) {
super(desc);
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
const group = editorGroupService.findGroup(this.scope, editorGroupService.activeGroup, true);
group?.focus();
}
}
export class FocusFirstGroupAction extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusFirstEditorGroup',
title: localize2('focusFirstEditorGroup', 'Focus First Editor Group'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyMod.CtrlCmd | KeyCode.Digit1
},
category: Categories.View
}, { location: GroupLocation.FIRST });
}
}
export class FocusLastGroupAction extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusLastEditorGroup',
title: localize2('focusLastEditorGroup', 'Focus Last Editor Group'),
f1: true,
category: Categories.View
}, { location: GroupLocation.LAST });
}
}
export class FocusNextGroup extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusNextGroup',
title: localize2('focusNextGroup', 'Focus Next Editor Group'),
f1: true,
category: Categories.View
}, { location: GroupLocation.NEXT });
}
}
export class FocusPreviousGroup extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusPreviousGroup',
title: localize2('focusPreviousGroup', 'Focus Previous Editor Group'),
f1: true,
category: Categories.View
}, { location: GroupLocation.PREVIOUS });
}
}
export class FocusLeftGroup extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusLeftGroup',
title: localize2('focusLeftGroup', 'Focus Left Editor Group'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.LeftArrow)
},
category: Categories.View
}, { direction: GroupDirection.LEFT });
}
}
export class FocusRightGroup extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusRightGroup',
title: localize2('focusRightGroup', 'Focus Right Editor Group'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.RightArrow)
},
category: Categories.View
}, { direction: GroupDirection.RIGHT });
}
}
export class FocusAboveGroup extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusAboveGroup',
title: localize2('focusAboveGroup', 'Focus Editor Group Above'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.UpArrow)
},
category: Categories.View
}, { direction: GroupDirection.UP });
}
}
export class FocusBelowGroup extends AbstractFocusGroupAction {
constructor() {
super({
id: 'workbench.action.focusBelowGroup',
title: localize2('focusBelowGroup', 'Focus Editor Group Below'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.DownArrow)
},
category: Categories.View
}, { direction: GroupDirection.DOWN });
}
}
export class CloseEditorAction extends Action {
static readonly ID = 'workbench.action.closeActiveEditor';
static readonly LABEL = localize('closeEditor', "Close Editor");
constructor(
id: string,
label: string,
@ICommandService private readonly commandService: ICommandService
) {
super(id, label, ThemeIcon.asClassName(Codicon.close));
}
override run(context?: IEditorCommandsContext): Promise<void> {
return this.commandService.executeCommand(CLOSE_EDITOR_COMMAND_ID, undefined, context);
}
}
export class UnpinEditorAction extends Action {
static readonly ID = 'workbench.action.unpinActiveEditor';
static readonly LABEL = localize('unpinEditor', "Unpin Editor");
constructor(
id: string,
label: string,
@ICommandService private readonly commandService: ICommandService
) {
super(id, label, ThemeIcon.asClassName(Codicon.pinned));
}
override run(context?: IEditorCommandsContext): Promise<void> {
return this.commandService.executeCommand(UNPIN_EDITOR_COMMAND_ID, undefined, context);
}
}
export class CloseEditorTabAction extends Action {
static readonly ID = 'workbench.action.closeActiveEditor';
static readonly LABEL = localize('closeOneEditor', "Close");
constructor(
id: string,
label: string,
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService
) {
super(id, label, ThemeIcon.asClassName(Codicon.close));
}
override async run(context?: IEditorCommandsContext): Promise<void> {
const group = context ? this.editorGroupService.getGroup(context.groupId) : this.editorGroupService.activeGroup;
if (!group) {
// group mentioned in context does not exist
return;
}
const targetEditor = context?.editorIndex !== undefined ? group.getEditorByIndex(context.editorIndex) : group.activeEditor;
if (!targetEditor) {
// No editor open or editor at index does not exist
return;
}
const editors: EditorInput[] = [];
if (group.isSelected(targetEditor)) {
editors.push(...group.selectedEditors);
} else {
editors.push(targetEditor);
}
// Close specific editors in group
for (const editor of editors) {
await group.closeEditor(editor, { preserveFocus: context?.preserveFocus });
}
}
}
export class RevertAndCloseEditorAction extends Action2 {
constructor() {
super({
id: 'workbench.action.revertAndCloseActiveEditor',
title: localize2('revertAndCloseActiveEditor', 'Revert and Close Editor'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorService = accessor.get(IEditorService);
const logService = accessor.get(ILogService);
const activeEditorPane = editorService.activeEditorPane;
if (activeEditorPane) {
const editor = activeEditorPane.input;
const group = activeEditorPane.group;
// first try a normal revert where the contents of the editor are restored
try {
await editorService.revert({ editor, groupId: group.id });
} catch (error) {
logService.error(error);
// if that fails, since we are about to close the editor, we accept that
// the editor cannot be reverted and instead do a soft revert that just
// enables us to close the editor. With this, a user can always close a
// dirty editor even when reverting fails.
await editorService.revert({ editor, groupId: group.id }, { soft: true });
}
await group.closeEditor(editor);
}
}
}
export class CloseLeftEditorsInGroupAction extends Action2 {
constructor() {
super({
id: 'workbench.action.closeEditorsToTheLeft',
title: localize2('closeEditorsToTheLeft', 'Close Editors to the Left in Group'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor, context?: IEditorIdentifier): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
const { group, editor } = this.getTarget(editorGroupService, context);
if (group && editor) {
await group.closeEditors({ direction: CloseDirection.LEFT, except: editor, excludeSticky: true });
}
}
private getTarget(editorGroupService: IEditorGroupsService, context?: IEditorIdentifier): { editor: EditorInput | null; group: IEditorGroup | undefined } {
if (context) {
return { editor: context.editor, group: editorGroupService.getGroup(context.groupId) };
}
// Fallback to active group
return { group: editorGroupService.activeGroup, editor: editorGroupService.activeGroup.activeEditor };
}
}
abstract class AbstractCloseAllAction extends Action2 {
protected groupsToClose(editorGroupService: IEditorGroupsService): IEditorGroup[] {
const groupsToClose: IEditorGroup[] = [];
// Close editors in reverse order of their grid appearance so that the editor
// group that is the first (top-left) remains. This helps to keep view state
// for editors around that have been opened in this visually first group.
const groups = editorGroupService.getGroups(GroupsOrder.GRID_APPEARANCE);
for (let i = groups.length - 1; i >= 0; i--) {
groupsToClose.push(groups[i]);
}
return groupsToClose;
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorService = accessor.get(IEditorService);
const editorGroupService = accessor.get(IEditorGroupsService);
const filesConfigurationService = accessor.get(IFilesConfigurationService);
const fileDialogService = accessor.get(IFileDialogService);
// Depending on the editor and auto save configuration,
// split editors into buckets for handling confirmation
const dirtyEditorsWithDefaultConfirm = new Set<IEditorIdentifier>();
const dirtyAutoSaveOnFocusChangeEditors = new Set<IEditorIdentifier>();
const dirtyAutoSaveOnWindowChangeEditors = new Set<IEditorIdentifier>();
const editorsWithCustomConfirm = new Map<string /* typeId */, Set<IEditorIdentifier>>();
for (const { editor, groupId } of editorService.getEditors(EditorsOrder.SEQUENTIAL, { excludeSticky: this.excludeSticky })) {
let confirmClose = false;
if (editor.closeHandler) {
confirmClose = editor.closeHandler.showConfirm(); // custom handling of confirmation on close
} else {
confirmClose = editor.isDirty() && !editor.isSaving(); // default confirm only when dirty and not saving
}
if (!confirmClose) {
continue;
}
// Editor has custom confirm implementation
if (typeof editor.closeHandler?.confirm === 'function') {
let customEditorsToConfirm = editorsWithCustomConfirm.get(editor.typeId);
if (!customEditorsToConfirm) {
customEditorsToConfirm = new Set();
editorsWithCustomConfirm.set(editor.typeId, customEditorsToConfirm);
}
customEditorsToConfirm.add({ editor, groupId });
}
// Editor will be saved on focus change when a
// dialog appears, so just track that separate
else if (!editor.hasCapability(EditorInputCapabilities.Untitled) && filesConfigurationService.getAutoSaveMode(editor).mode === AutoSaveMode.ON_FOCUS_CHANGE) {
dirtyAutoSaveOnFocusChangeEditors.add({ editor, groupId });
}
// Windows, Linux: editor will be saved on window change
// when a native dialog appears, so just track that separate
// (see https://github.com/microsoft/vscode/issues/134250)
else if ((isNative && (isWindows || isLinux)) && !editor.hasCapability(EditorInputCapabilities.Untitled) && filesConfigurationService.getAutoSaveMode(editor).mode === AutoSaveMode.ON_WINDOW_CHANGE) {
dirtyAutoSaveOnWindowChangeEditors.add({ editor, groupId });
}
// Editor will show in generic file based dialog
else {
dirtyEditorsWithDefaultConfirm.add({ editor, groupId });
}
}
// 1.) Show default file based dialog
if (dirtyEditorsWithDefaultConfirm.size > 0) {
const editors = Array.from(dirtyEditorsWithDefaultConfirm.values());
await this.revealEditorsToConfirm(editors, editorGroupService); // help user make a decision by revealing editors
const confirmation = await fileDialogService.showSaveConfirm(editors.map(({ editor }) => {
if (editor instanceof SideBySideEditorInput) {
return editor.primary.getName(); // prefer shorter names by using primary's name in this case
}
return editor.getName();
}));
switch (confirmation) {
case ConfirmResult.CANCEL:
return;
case ConfirmResult.DONT_SAVE:
await editorService.revert(editors, { soft: true });
break;
case ConfirmResult.SAVE:
await editorService.save(editors, { reason: SaveReason.EXPLICIT });
break;
}
}
// 2.) Show custom confirm based dialog
for (const [, editorIdentifiers] of editorsWithCustomConfirm) {
const editors = Array.from(editorIdentifiers.values());
await this.revealEditorsToConfirm(editors, editorGroupService); // help user make a decision by revealing editors
const confirmation = await firstOrDefault(editors)?.editor.closeHandler?.confirm?.(editors);
if (typeof confirmation === 'number') {
switch (confirmation) {
case ConfirmResult.CANCEL:
return;
case ConfirmResult.DONT_SAVE:
await editorService.revert(editors, { soft: true });
break;
case ConfirmResult.SAVE:
await editorService.save(editors, { reason: SaveReason.EXPLICIT });
break;
}
}
}
// 3.) Save autosaveable editors (focus change)
if (dirtyAutoSaveOnFocusChangeEditors.size > 0) {
const editors = Array.from(dirtyAutoSaveOnFocusChangeEditors.values());
await editorService.save(editors, { reason: SaveReason.FOCUS_CHANGE });
}
// 4.) Save autosaveable editors (window change)
if (dirtyAutoSaveOnWindowChangeEditors.size > 0) {
const editors = Array.from(dirtyAutoSaveOnWindowChangeEditors.values());
await editorService.save(editors, { reason: SaveReason.WINDOW_CHANGE });
}
// 5.) Finally close all editors: even if an editor failed to
// save or revert and still reports dirty, the editor part makes
// sure to bring up another confirm dialog for those editors
// specifically.
return this.doCloseAll(editorGroupService);
}
private async revealEditorsToConfirm(editors: ReadonlyArray<IEditorIdentifier>, editorGroupService: IEditorGroupsService): Promise<void> {
try {
const handledGroups = new Set<GroupIdentifier>();
for (const { editor, groupId } of editors) {
if (handledGroups.has(groupId)) {
continue;
}
handledGroups.add(groupId);
const group = editorGroupService.getGroup(groupId);
await group?.openEditor(editor);
}
} catch (error) {
// ignore any error as the revealing is just convinience
}
}
protected abstract get excludeSticky(): boolean;
protected async doCloseAll(editorGroupService: IEditorGroupsService): Promise<void> {
await Promise.all(this.groupsToClose(editorGroupService).map(group => group.closeAllEditors({ excludeSticky: this.excludeSticky })));
}
}
export class CloseAllEditorsAction extends AbstractCloseAllAction {
static readonly ID = 'workbench.action.closeAllEditors';
static readonly LABEL = localize2('closeAllEditors', 'Close All Editors');
constructor() {
super({
id: CloseAllEditorsAction.ID,
title: CloseAllEditorsAction.LABEL,
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.KeyW)
},
icon: Codicon.closeAll,
category: Categories.View
});
}
protected get excludeSticky(): boolean {
return true; // exclude sticky from this mass-closing operation
}
}
export class CloseAllEditorGroupsAction extends AbstractCloseAllAction {
constructor() {
super({
id: 'workbench.action.closeAllGroups',
title: localize2('closeAllGroups', 'Close All Editor Groups'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyW)
},
category: Categories.View
});
}
protected get excludeSticky(): boolean {
return false; // the intent to close groups means, even sticky are included
}
protected override async doCloseAll(editorGroupService: IEditorGroupsService): Promise<void> {
await super.doCloseAll(editorGroupService);
for (const groupToClose of this.groupsToClose(editorGroupService)) {
editorGroupService.removeGroup(groupToClose);
}
}
}
export class CloseEditorsInOtherGroupsAction extends Action2 {
constructor() {
super({
id: 'workbench.action.closeEditorsInOtherGroups',
title: localize2('closeEditorsInOtherGroups', 'Close Editors in Other Groups'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor, context?: IEditorIdentifier): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
const groupToSkip = context ? editorGroupService.getGroup(context.groupId) : editorGroupService.activeGroup;
await Promise.all(editorGroupService.getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE).map(async group => {
if (groupToSkip && group.id === groupToSkip.id) {
return;
}
return group.closeAllEditors({ excludeSticky: true });
}));
}
}
export class CloseEditorInAllGroupsAction extends Action2 {
constructor() {
super({
id: 'workbench.action.closeEditorInAllGroups',
title: localize2('closeEditorInAllGroups', 'Close Editor in All Groups'),
f1: true,
category: Categories.View
});
}
override async run(accessor: ServicesAccessor): Promise<void> {
const editorService = accessor.get(IEditorService);
const editorGroupService = accessor.get(IEditorGroupsService);
const activeEditor = editorService.activeEditor;
if (activeEditor) {
await Promise.all(editorGroupService.getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE).map(group => group.closeEditor(activeEditor)));
}
}
}
abstract class AbstractMoveCopyGroupAction extends Action2 {
constructor(
desc: Readonly<IAction2Options>,
private readonly direction: GroupDirection,
private readonly isMove: boolean
) {
super(desc);
}
override async run(accessor: ServicesAccessor, context?: IEditorIdentifier): Promise<void> {
const editorGroupService = accessor.get(IEditorGroupsService);
let sourceGroup: IEditorGroup | undefined;
if (context && typeof context.groupId === 'number') {
sourceGroup = editorGroupService.getGroup(context.groupId);
} else {
sourceGroup = editorGroupService.activeGroup;
}
if (sourceGroup) {
let resultGroup: IEditorGroup | undefined = undefined;
if (this.isMove) {
const targetGroup = this.findTargetGroup(editorGroupService, sourceGroup);
if (targetGroup) {
resultGroup = editorGroupService.moveGroup(sourceGroup, targetGroup, this.direction);
}
} else {
resultGroup = editorGroupService.copyGroup(sourceGroup, sourceGroup, this.direction);
}
if (resultGroup) {
editorGroupService.activateGroup(resultGroup);
}
}
}
private findTargetGroup(editorGroupService: IEditorGroupsService, sourceGroup: IEditorGroup): IEditorGroup | undefined {
const targetNeighbours: GroupDirection[] = [this.direction];
// Allow the target group to be in alternative locations to support more
// scenarios of moving the group to the taret location.
// Helps for https://github.com/microsoft/vscode/issues/50741
switch (this.direction) {
case GroupDirection.LEFT:
case GroupDirection.RIGHT:
targetNeighbours.push(GroupDirection.UP, GroupDirection.DOWN);
break;
case GroupDirection.UP:
case GroupDirection.DOWN:
targetNeighbours.push(GroupDirection.LEFT, GroupDirection.RIGHT);
break;
}
for (const targetNeighbour of targetNeighbours) {
const targetNeighbourGroup = editorGroupService.findGroup({ direction: targetNeighbour }, sourceGroup);
if (targetNeighbourGroup) {
return targetNeighbourGroup;
}
}
return undefined;
}
}
abstract class AbstractMoveGroupAction extends AbstractMoveCopyGroupAction {
constructor(
desc: Readonly<IAction2Options>,
direction: GroupDirection
) {
super(desc, direction, true);
}
}
export class MoveGroupLeftAction extends AbstractMoveGroupAction {
constructor() {
super({
id: 'workbench.action.moveActiveEditorGroupLeft',
title: localize2('moveActiveGroupLeft', 'Move Editor Group Left'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyCode.LeftArrow)
},
category: Categories.View
}, GroupDirection.LEFT);
}
}
export class MoveGroupRightAction extends AbstractMoveGroupAction {
constructor() {
super({
id: 'workbench.action.moveActiveEditorGroupRight',
title: localize2('moveActiveGroupRight', 'Move Editor Group Right'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyCode.RightArrow)
},
category: Categories.View
}, GroupDirection.RIGHT);
}
}
export class MoveGroupUpAction extends AbstractMoveGroupAction {
constructor() {
super({
id: 'workbench.action.moveActiveEditorGroupUp',
title: localize2('moveActiveGroupUp', 'Move Editor Group Up'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyCode.UpArrow)
},
category: Categories.View
}, GroupDirection.UP);
}
}
export class MoveGroupDownAction extends AbstractMoveGroupAction {
constructor() {
super({
id: 'workbench.action.moveActiveEditorGroupDown',
title: localize2('moveActiveGroupDown', 'Move Editor Group Down'),
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyCode.DownArrow)
},
category: Categories.View
}, GroupDirection.DOWN);
}
}
abstract class AbstractDuplicateGroupAction extends AbstractMoveCopyGroupAction {
constructor(
desc: Readonly<IAction2Options>,
direction: GroupDirection
) {
super(desc, direction, false);
}
}
export class DuplicateGroupLeftAction extends AbstractDuplicateGroupAction {
constructor() {
super({
id: 'workbench.action.duplicateActiveEditorGroupLeft',
title: localize2('duplicateActiveGroupLeft', 'Duplicate Editor Group Left'),
f1: true,
category: Categories.View
}, GroupDirection.LEFT);
}
}
export class DuplicateGroupRightAction extends AbstractDuplicateGroupAction {
constructor() {
super({
id: 'workbench.action.duplicateActiveEditorGroupRight',
title: localize2('duplicateActiveGroupRight', 'Duplicate Editor Group Right'),
f1: true,
category: Categories.View
}, GroupDirection.RIGHT);
}
}
export class DuplicateGroupUpAction extends AbstractDuplicateGroupAction {
constructor() {
super({
id: 'workbench.action.duplicateActiveEditorGroupUp',
title: localize2('duplicateActiveGroupUp', 'Duplicate Editor Group Up'),
f1: true,
category: Categories.View
}, GroupDirection.UP);
}
}
export class DuplicateGroupDownAction extends AbstractDuplicateGroupAction {
constructor() {