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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { app, BrowserWindow, MessageBoxOptions, WebContents } from 'electron';
import { statSync } from 'fs';
import { hostname, release } from 'os';
import { coalesce, distinct, firstOrDefault } from 'vs/base/common/arrays';
import { CancellationToken } from 'vs/base/common/cancellation';
import { CharCode } from 'vs/base/common/charCode';
import { Emitter, Event } from 'vs/base/common/event';
import { isWindowsDriveLetter, parseLineAndColumnAware, sanitizeFilePath, toSlashes } from 'vs/base/common/extpath';
import { once } from 'vs/base/common/functional';
import { getPathLabel, mnemonicButtonLabel } from 'vs/base/common/labels';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { basename, join, normalize, posix } from 'vs/base/common/path';
import { getMarks, mark } from 'vs/base/common/performance';
import { IProcessEnvironment, isMacintosh, isWindows, OS } from 'vs/base/common/platform';
import { cwd } from 'vs/base/common/process';
import { extUriBiasedIgnorePathCase, isEqualAuthority, normalizePath, originalFSPath, removeTrailingPathSeparator } from 'vs/base/common/resources';
import { assertIsDefined, withNullAsUndefined } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { IBackupMainService } from 'vs/platform/backup/electron-main/backup';
import { IEmptyWindowBackupInfo } from 'vs/platform/backup/node/backup';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogMainService';
import { NativeParsedArgs } from 'vs/platform/environment/common/argv';
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { FileType, IFileService } from 'vs/platform/files/common/files';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
import { ILogService } from 'vs/platform/log/common/log';
import product from 'vs/platform/product/common/product';
import { IProductService } from 'vs/platform/product/common/productService';
import { IProtocolMainService } from 'vs/platform/protocol/electron-main/protocol';
import { getRemoteAuthority } from 'vs/platform/remote/common/remoteHosts';
import { IStateMainService } from 'vs/platform/state/electron-main/state';
import { IAddFoldersRequest, INativeOpenFileRequest, INativeWindowConfiguration, IOpenEmptyWindowOptions, IPath, IPathsToWaitFor, isFileToOpen, isFolderToOpen, isWorkspaceToOpen, IWindowOpenable, IWindowSettings } from 'vs/platform/window/common/window';
import { CodeWindow } from 'vs/platform/windows/electron-main/window';
import { IOpenConfiguration, IOpenEmptyConfiguration, IWindowsCountChangedEvent, IWindowsMainService, OpenContext } from 'vs/platform/windows/electron-main/windows';
import { findWindowOnExtensionDevelopmentPath, findWindowOnFile, findWindowOnWorkspaceOrFolder } from 'vs/platform/windows/electron-main/windowsFinder';
import { IWindowState, WindowsStateHandler } from 'vs/platform/windows/electron-main/windowsStateHandler';
import { IRecent } from 'vs/platform/workspaces/common/workspaces';
import { hasWorkspaceFileExtension, ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, isWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
import { getSingleFolderWorkspaceIdentifier, getWorkspaceIdentifier } from 'vs/platform/workspaces/electron-main/workspaces';
import { IWorkspacesHistoryMainService } from 'vs/platform/workspaces/electron-main/workspacesHistoryMainService';
import { IWorkspacesManagementMainService } from 'vs/platform/workspaces/electron-main/workspacesManagementMainService';
import { ICodeWindow, UnloadReason } from 'vs/platform/window/electron-main/window';
import { IThemeMainService } from 'vs/platform/theme/electron-main/themeMainService';
import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
import { IUserDataProfilesService } from 'vs/platform/userDataProfile/common/userDataProfile';
import { IPolicyService } from 'vs/platform/policy/common/policy';
//#region Helper Interfaces
type RestoreWindowsSetting = 'preserve' | 'all' | 'folders' | 'one' | 'none';
interface IOpenBrowserWindowOptions {
readonly userEnv?: IProcessEnvironment;
readonly cli?: NativeParsedArgs;
readonly workspace?: IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier;
readonly remoteAuthority?: string;
readonly initialStartup?: boolean;
readonly filesToOpen?: IFilesToOpen;
readonly forceNewWindow?: boolean;
readonly forceNewTabbedWindow?: boolean;
readonly windowToUse?: ICodeWindow;
readonly emptyWindowBackupInfo?: IEmptyWindowBackupInfo;
}
interface IPathResolveOptions {
/**
* By default, resolving a path will check
* if the path exists. This can be disabled
* with this flag.
*/
readonly ignoreFileNotFound?: boolean;
/**
* Will reject a path if it points to a transient
* workspace as indicated by a `transient: true`
* property in the workspace file.
*/
readonly rejectTransientWorkspaces?: boolean;
/**
* If enabled, will resolve the path line/column
* aware and properly remove this information
* from the resulting file path.
*/
readonly gotoLineMode?: boolean;
/**
* Forces to resolve the provided path as workspace
* file instead of opening it as a file.
*/
readonly forceOpenWorkspaceAsFile?: boolean;
/**
* The remoteAuthority to use if the URL to open is
* neither `file` nor `vscode-remote`.
*/
readonly remoteAuthority?: string;
}
interface IFilesToOpen {
readonly remoteAuthority?: string;
filesToOpenOrCreate: IPath[];
filesToDiff: IPath[];
filesToMerge: IPath[];
filesToWait?: IPathsToWaitFor;
}
interface IPathToOpen<T = IEditorOptions> extends IPath<T> {
/**
* The workspace to open
*/
readonly workspace?: IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier;
/**
* Whether the path is considered to be transient or not
* for example, a transient workspace should not add to
* the workspaces history and should never restore.
*/
readonly transient?: boolean;
/**
* The backup path to use
*/
readonly backupPath?: string;
/**
* The remote authority for the Code instance to open. Undefined if not remote.
*/
readonly remoteAuthority?: string;
/**
* Optional label for the recent history
*/
label?: string;
}
interface IWorkspacePathToOpen extends IPathToOpen {
readonly workspace: IWorkspaceIdentifier;
}
interface ISingleFolderWorkspacePathToOpen extends IPathToOpen {
readonly workspace: ISingleFolderWorkspaceIdentifier;
}
function isWorkspacePathToOpen(path: IPathToOpen | undefined): path is IWorkspacePathToOpen {
return isWorkspaceIdentifier(path?.workspace);
}
function isSingleFolderWorkspacePathToOpen(path: IPathToOpen | undefined): path is ISingleFolderWorkspacePathToOpen {
return isSingleFolderWorkspaceIdentifier(path?.workspace);
}
//#endregion
export class WindowsMainService extends Disposable implements IWindowsMainService {
declare readonly _serviceBrand: undefined;
private static readonly WINDOWS: ICodeWindow[] = [];
private readonly _onDidOpenWindow = this._register(new Emitter<ICodeWindow>());
readonly onDidOpenWindow = this._onDidOpenWindow.event;
private readonly _onDidSignalReadyWindow = this._register(new Emitter<ICodeWindow>());
readonly onDidSignalReadyWindow = this._onDidSignalReadyWindow.event;
private readonly _onDidDestroyWindow = this._register(new Emitter<ICodeWindow>());
readonly onDidDestroyWindow = this._onDidDestroyWindow.event;
private readonly _onDidChangeWindowsCount = this._register(new Emitter<IWindowsCountChangedEvent>());
readonly onDidChangeWindowsCount = this._onDidChangeWindowsCount.event;
private readonly _onDidTriggerSystemContextMenu = this._register(new Emitter<{ window: ICodeWindow; x: number; y: number }>());
readonly onDidTriggerSystemContextMenu = this._onDidTriggerSystemContextMenu.event;
private readonly windowsStateHandler = this._register(new WindowsStateHandler(this, this.stateMainService, this.lifecycleMainService, this.logService, this.configurationService));
constructor(
private readonly machineId: string,
private readonly initialUserEnv: IProcessEnvironment,
@ILogService private readonly logService: ILogService,
@IStateMainService private readonly stateMainService: IStateMainService,
@IPolicyService private readonly policyService: IPolicyService,
@IEnvironmentMainService private readonly environmentMainService: IEnvironmentMainService,
@IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService,
@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
@IBackupMainService private readonly backupMainService: IBackupMainService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IWorkspacesHistoryMainService private readonly workspacesHistoryMainService: IWorkspacesHistoryMainService,
@IWorkspacesManagementMainService private readonly workspacesManagementMainService: IWorkspacesManagementMainService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IDialogMainService private readonly dialogMainService: IDialogMainService,
@IFileService private readonly fileService: IFileService,
@IProductService private readonly productService: IProductService,
@IProtocolMainService private readonly protocolMainService: IProtocolMainService,
@IThemeMainService private readonly themeMainService: IThemeMainService
) {
super();
this.registerListeners();
}
private registerListeners(): void {
// Signal a window is ready after having entered a workspace
this._register(this.workspacesManagementMainService.onDidEnterWorkspace(event => this._onDidSignalReadyWindow.fire(event.window)));
// Update valid roots in protocol service for extension dev windows
this._register(this.onDidSignalReadyWindow(window => {
if (window.config?.extensionDevelopmentPath || window.config?.extensionTestsPath) {
const disposables = new DisposableStore();
disposables.add(Event.any(window.onDidClose, window.onDidDestroy)(() => disposables.dispose()));
// Allow access to extension development path
if (window.config.extensionDevelopmentPath) {
for (const extensionDevelopmentPath of window.config.extensionDevelopmentPath) {
disposables.add(this.protocolMainService.addValidFileRoot(extensionDevelopmentPath));
}
}
// Allow access to extension tests path
if (window.config.extensionTestsPath) {
disposables.add(this.protocolMainService.addValidFileRoot(window.config.extensionTestsPath));
}
}
}));
}
openEmptyWindow(openConfig: IOpenEmptyConfiguration, options?: IOpenEmptyWindowOptions): ICodeWindow[] {
const cli = this.environmentMainService.args;
const remoteAuthority = options?.remoteAuthority || undefined;
const forceEmpty = true;
const forceReuseWindow = options?.forceReuseWindow;
const forceNewWindow = !forceReuseWindow;
return this.open({ ...openConfig, cli, forceEmpty, forceNewWindow, forceReuseWindow, remoteAuthority });
}
openExistingWindow(window: ICodeWindow, openConfig: IOpenConfiguration): void {
// Bring window to front
window.focus();
// Handle --wait
this.handleWaitMarkerFile(openConfig, [window]);
}
open(openConfig: IOpenConfiguration): ICodeWindow[] {
this.logService.trace('windowsManager#open');
if (openConfig.addMode && (openConfig.initialStartup || !this.getLastActiveWindow())) {
openConfig.addMode = false; // Make sure addMode is only enabled if we have an active window
}
const foldersToAdd: ISingleFolderWorkspacePathToOpen[] = [];
const foldersToOpen: ISingleFolderWorkspacePathToOpen[] = [];
const workspacesToOpen: IWorkspacePathToOpen[] = [];
const untitledWorkspacesToRestore: IWorkspacePathToOpen[] = [];
const emptyWindowsWithBackupsToRestore: IEmptyWindowBackupInfo[] = [];
let filesToOpen: IFilesToOpen | undefined;
let emptyToOpen = 0;
// Identify things to open from open config
const pathsToOpen = this.getPathsToOpen(openConfig);
this.logService.trace('windowsManager#open pathsToOpen', pathsToOpen);
for (const path of pathsToOpen) {
if (isSingleFolderWorkspacePathToOpen(path)) {
if (openConfig.addMode) {
// When run with --add, take the folders that are to be opened as
// folders that should be added to the currently active window.
foldersToAdd.push(path);
} else {
foldersToOpen.push(path);
}
} else if (isWorkspacePathToOpen(path)) {
workspacesToOpen.push(path);
} else if (path.fileUri) {
if (!filesToOpen) {
filesToOpen = { filesToOpenOrCreate: [], filesToDiff: [], filesToMerge: [], remoteAuthority: path.remoteAuthority };
}
filesToOpen.filesToOpenOrCreate.push(path);
} else if (path.backupPath) {
emptyWindowsWithBackupsToRestore.push({ backupFolder: basename(path.backupPath), remoteAuthority: path.remoteAuthority });
} else {
emptyToOpen++;
}
}
// When run with --diff, take the first 2 files to open as files to diff
if (openConfig.diffMode && filesToOpen && filesToOpen.filesToOpenOrCreate.length >= 2) {
filesToOpen.filesToDiff = filesToOpen.filesToOpenOrCreate.slice(0, 2);
filesToOpen.filesToOpenOrCreate = [];
}
// When run with --merge, take the first 4 files to open as files to merge
if (openConfig.mergeMode && filesToOpen && filesToOpen.filesToOpenOrCreate.length === 4) {
filesToOpen.filesToMerge = filesToOpen.filesToOpenOrCreate.slice(0, 4);
filesToOpen.filesToOpenOrCreate = [];
filesToOpen.filesToDiff = [];
}
// When run with --wait, make sure we keep the paths to wait for
if (filesToOpen && openConfig.waitMarkerFileURI) {
filesToOpen.filesToWait = { paths: coalesce([...filesToOpen.filesToDiff, filesToOpen.filesToMerge[3] /* [3] is the resulting merge file */, ...filesToOpen.filesToOpenOrCreate]), waitMarkerFileUri: openConfig.waitMarkerFileURI };
}
// These are windows to restore because of hot-exit or from previous session (only performed once on startup!)
if (openConfig.initialStartup) {
// Untitled workspaces are always restored
untitledWorkspacesToRestore.push(...this.workspacesManagementMainService.getUntitledWorkspacesSync());
workspacesToOpen.push(...untitledWorkspacesToRestore);
// Empty windows with backups are always restored
emptyWindowsWithBackupsToRestore.push(...this.backupMainService.getEmptyWindowBackupPaths());
} else {
emptyWindowsWithBackupsToRestore.length = 0;
}
// Open based on config
const { windows: usedWindows, filesOpenedInWindow } = this.doOpen(openConfig, workspacesToOpen, foldersToOpen, emptyWindowsWithBackupsToRestore, emptyToOpen, filesToOpen, foldersToAdd);
this.logService.trace(`windowsManager#open used window count ${usedWindows.length} (workspacesToOpen: ${workspacesToOpen.length}, foldersToOpen: ${foldersToOpen.length}, emptyToRestore: ${emptyWindowsWithBackupsToRestore.length}, emptyToOpen: ${emptyToOpen})`);
// Make sure to pass focus to the most relevant of the windows if we open multiple
if (usedWindows.length > 1) {
// 1.) focus window we opened files in always with highest priority
if (filesOpenedInWindow) {
filesOpenedInWindow.focus();
}
// Otherwise, find a good window based on open params
else {
const focusLastActive = this.windowsStateHandler.state.lastActiveWindow && !openConfig.forceEmpty && !openConfig.cli._.length && !openConfig.cli['file-uri'] && !openConfig.cli['folder-uri'] && !(openConfig.urisToOpen && openConfig.urisToOpen.length);
let focusLastOpened = true;
let focusLastWindow = true;
// 2.) focus last active window if we are not instructed to open any paths
if (focusLastActive) {
const lastActiveWindow = usedWindows.filter(window => this.windowsStateHandler.state.lastActiveWindow && window.backupPath === this.windowsStateHandler.state.lastActiveWindow.backupPath);
if (lastActiveWindow.length) {
lastActiveWindow[0].focus();
focusLastOpened = false;
focusLastWindow = false;
}
}
// 3.) if instructed to open paths, focus last window which is not restored
if (focusLastOpened) {
for (let i = usedWindows.length - 1; i >= 0; i--) {
const usedWindow = usedWindows[i];
if (
(usedWindow.openedWorkspace && untitledWorkspacesToRestore.some(workspace => usedWindow.openedWorkspace && workspace.workspace.id === usedWindow.openedWorkspace.id)) || // skip over restored workspace
(usedWindow.backupPath && emptyWindowsWithBackupsToRestore.some(empty => usedWindow.backupPath && empty.backupFolder === basename(usedWindow.backupPath))) // skip over restored empty window
) {
continue;
}
usedWindow.focus();
focusLastWindow = false;
break;
}
}
// 4.) finally, always ensure to have at least last used window focused
if (focusLastWindow) {
usedWindows[usedWindows.length - 1].focus();
}
}
}
// Remember in recent document list (unless this opens for extension development)
// Also do not add paths when files are opened for diffing or merging, only if opened individually
const isDiff = filesToOpen && filesToOpen.filesToDiff.length > 0;
const isMerge = filesToOpen && filesToOpen.filesToMerge.length > 0;
if (!usedWindows.some(window => window.isExtensionDevelopmentHost) && !isDiff && !isMerge && !openConfig.noRecentEntry) {
const recents: IRecent[] = [];
for (const pathToOpen of pathsToOpen) {
if (isWorkspacePathToOpen(pathToOpen) && !pathToOpen.transient /* never add transient workspaces to history */) {
recents.push({ label: pathToOpen.label, workspace: pathToOpen.workspace, remoteAuthority: pathToOpen.remoteAuthority });
} else if (isSingleFolderWorkspacePathToOpen(pathToOpen)) {
recents.push({ label: pathToOpen.label, folderUri: pathToOpen.workspace.uri, remoteAuthority: pathToOpen.remoteAuthority });
} else if (pathToOpen.fileUri) {
recents.push({ label: pathToOpen.label, fileUri: pathToOpen.fileUri, remoteAuthority: pathToOpen.remoteAuthority });
}
}
this.workspacesHistoryMainService.addRecentlyOpened(recents);
}
// Handle --wait
this.handleWaitMarkerFile(openConfig, usedWindows);
return usedWindows;
}
private handleWaitMarkerFile(openConfig: IOpenConfiguration, usedWindows: ICodeWindow[]): void {
// If we got started with --wait from the CLI, we need to signal to the outside when the window
// used for the edit operation is closed or loaded to a different folder so that the waiting
// process can continue. We do this by deleting the waitMarkerFilePath.
const waitMarkerFileURI = openConfig.waitMarkerFileURI;
if (openConfig.context === OpenContext.CLI && waitMarkerFileURI && usedWindows.length === 1 && usedWindows[0]) {
(async () => {
await usedWindows[0].whenClosedOrLoaded;
try {
await this.fileService.del(waitMarkerFileURI);
} catch (error) {
// ignore - could have been deleted from the window already
}
})();
}
}
private doOpen(
openConfig: IOpenConfiguration,
workspacesToOpen: IWorkspacePathToOpen[],
foldersToOpen: ISingleFolderWorkspacePathToOpen[],
emptyToRestore: IEmptyWindowBackupInfo[],
emptyToOpen: number,
filesToOpen: IFilesToOpen | undefined,
foldersToAdd: ISingleFolderWorkspacePathToOpen[]
): { windows: ICodeWindow[]; filesOpenedInWindow: ICodeWindow | undefined } {
// Keep track of used windows and remember
// if files have been opened in one of them
const usedWindows: ICodeWindow[] = [];
let filesOpenedInWindow: ICodeWindow | undefined = undefined;
function addUsedWindow(window: ICodeWindow, openedFiles?: boolean): void {
usedWindows.push(window);
if (openedFiles) {
filesOpenedInWindow = window;
filesToOpen = undefined; // reset `filesToOpen` since files have been opened
}
}
// Settings can decide if files/folders open in new window or not
let { openFolderInNewWindow, openFilesInNewWindow } = this.shouldOpenNewWindow(openConfig);
// Handle folders to add by looking for the last active workspace (not on initial startup)
if (!openConfig.initialStartup && foldersToAdd.length > 0) {
const authority = foldersToAdd[0].remoteAuthority;
const lastActiveWindow = this.getLastActiveWindowForAuthority(authority);
if (lastActiveWindow) {
addUsedWindow(this.doAddFoldersToExistingWindow(lastActiveWindow, foldersToAdd.map(folderToAdd => folderToAdd.workspace.uri)));
}
}
// Handle files to open/diff/merge or to create when we dont open a folder and we do not restore any
// folder/untitled from hot-exit by trying to open them in the window that fits best
const potentialNewWindowsCount = foldersToOpen.length + workspacesToOpen.length + emptyToRestore.length;
if (filesToOpen && potentialNewWindowsCount === 0) {
// Find suitable window or folder path to open files in
const fileToCheck: IPath<IEditorOptions> | undefined = filesToOpen.filesToOpenOrCreate[0] || filesToOpen.filesToDiff[0] || filesToOpen.filesToMerge[3] /* [3] is the resulting merge file */;
// only look at the windows with correct authority
const windows = this.getWindows().filter(window => filesToOpen && isEqualAuthority(window.remoteAuthority, filesToOpen.remoteAuthority));
// figure out a good window to open the files in if any
// with a fallback to the last active window.
//
// in case `openFilesInNewWindow` is enforced, we skip
// this step.
let windowToUseForFiles: ICodeWindow | undefined = undefined;
if (fileToCheck?.fileUri && !openFilesInNewWindow) {
if (openConfig.context === OpenContext.DESKTOP || openConfig.context === OpenContext.CLI || openConfig.context === OpenContext.DOCK) {
windowToUseForFiles = findWindowOnFile(windows, fileToCheck.fileUri, workspace => workspace.configPath.scheme === Schemas.file ? this.workspacesManagementMainService.resolveLocalWorkspaceSync(workspace.configPath) : undefined);
}
if (!windowToUseForFiles) {
windowToUseForFiles = this.doGetLastActiveWindow(windows);
}
}
// We found a window to open the files in
if (windowToUseForFiles) {
// Window is workspace
if (isWorkspaceIdentifier(windowToUseForFiles.openedWorkspace)) {
workspacesToOpen.push({ workspace: windowToUseForFiles.openedWorkspace, remoteAuthority: windowToUseForFiles.remoteAuthority });
}
// Window is single folder
else if (isSingleFolderWorkspaceIdentifier(windowToUseForFiles.openedWorkspace)) {
foldersToOpen.push({ workspace: windowToUseForFiles.openedWorkspace, remoteAuthority: windowToUseForFiles.remoteAuthority });
}
// Window is empty
else {
addUsedWindow(this.doOpenFilesInExistingWindow(openConfig, windowToUseForFiles, filesToOpen), true);
}
}
// Finally, if no window or folder is found, just open the files in an empty window
else {
addUsedWindow(this.openInBrowserWindow({
userEnv: openConfig.userEnv,
cli: openConfig.cli,
initialStartup: openConfig.initialStartup,
filesToOpen,
forceNewWindow: true,
remoteAuthority: filesToOpen.remoteAuthority,
forceNewTabbedWindow: openConfig.forceNewTabbedWindow
}), true);
}
}
// Handle workspaces to open (instructed and to restore)
const allWorkspacesToOpen = distinct(workspacesToOpen, workspace => workspace.workspace.id); // prevent duplicates
if (allWorkspacesToOpen.length > 0) {
// Check for existing instances
const windowsOnWorkspace = coalesce(allWorkspacesToOpen.map(workspaceToOpen => findWindowOnWorkspaceOrFolder(this.getWindows(), workspaceToOpen.workspace.configPath)));
if (windowsOnWorkspace.length > 0) {
const windowOnWorkspace = windowsOnWorkspace[0];
const filesToOpenInWindow = isEqualAuthority(filesToOpen?.remoteAuthority, windowOnWorkspace.remoteAuthority) ? filesToOpen : undefined;
// Do open files
addUsedWindow(this.doOpenFilesInExistingWindow(openConfig, windowOnWorkspace, filesToOpenInWindow), !!filesToOpenInWindow);
openFolderInNewWindow = true; // any other folders to open must open in new window then
}
// Open remaining ones
allWorkspacesToOpen.forEach(workspaceToOpen => {
if (windowsOnWorkspace.some(window => window.openedWorkspace && window.openedWorkspace.id === workspaceToOpen.workspace.id)) {
return; // ignore folders that are already open
}
const remoteAuthority = workspaceToOpen.remoteAuthority;
const filesToOpenInWindow = isEqualAuthority(filesToOpen?.remoteAuthority, remoteAuthority) ? filesToOpen : undefined;
// Do open folder
addUsedWindow(this.doOpenFolderOrWorkspace(openConfig, workspaceToOpen, openFolderInNewWindow, filesToOpenInWindow), !!filesToOpenInWindow);
openFolderInNewWindow = true; // any other folders to open must open in new window then
});
}
// Handle folders to open (instructed and to restore)
const allFoldersToOpen = distinct(foldersToOpen, folder => extUriBiasedIgnorePathCase.getComparisonKey(folder.workspace.uri)); // prevent duplicates
if (allFoldersToOpen.length > 0) {
// Check for existing instances
const windowsOnFolderPath = coalesce(allFoldersToOpen.map(folderToOpen => findWindowOnWorkspaceOrFolder(this.getWindows(), folderToOpen.workspace.uri)));
if (windowsOnFolderPath.length > 0) {
const windowOnFolderPath = windowsOnFolderPath[0];
const filesToOpenInWindow = isEqualAuthority(filesToOpen?.remoteAuthority, windowOnFolderPath.remoteAuthority) ? filesToOpen : undefined;
// Do open files
addUsedWindow(this.doOpenFilesInExistingWindow(openConfig, windowOnFolderPath, filesToOpenInWindow), !!filesToOpenInWindow);
openFolderInNewWindow = true; // any other folders to open must open in new window then
}
// Open remaining ones
allFoldersToOpen.forEach(folderToOpen => {
if (windowsOnFolderPath.some(window => isSingleFolderWorkspaceIdentifier(window.openedWorkspace) && extUriBiasedIgnorePathCase.isEqual(window.openedWorkspace.uri, folderToOpen.workspace.uri))) {
return; // ignore folders that are already open
}
const remoteAuthority = folderToOpen.remoteAuthority;
const filesToOpenInWindow = isEqualAuthority(filesToOpen?.remoteAuthority, remoteAuthority) ? filesToOpen : undefined;
// Do open folder
addUsedWindow(this.doOpenFolderOrWorkspace(openConfig, folderToOpen, openFolderInNewWindow, filesToOpenInWindow), !!filesToOpenInWindow);
openFolderInNewWindow = true; // any other folders to open must open in new window then
});
}
// Handle empty to restore
const allEmptyToRestore = distinct(emptyToRestore, info => info.backupFolder); // prevent duplicates
if (allEmptyToRestore.length > 0) {
allEmptyToRestore.forEach(emptyWindowBackupInfo => {
const remoteAuthority = emptyWindowBackupInfo.remoteAuthority;
const filesToOpenInWindow = isEqualAuthority(filesToOpen?.remoteAuthority, remoteAuthority) ? filesToOpen : undefined;
addUsedWindow(this.doOpenEmpty(openConfig, true, remoteAuthority, filesToOpenInWindow, emptyWindowBackupInfo), !!filesToOpenInWindow);
openFolderInNewWindow = true; // any other folders to open must open in new window then
});
}
// Handle empty to open (only if no other window opened)
if (usedWindows.length === 0 || filesToOpen) {
if (filesToOpen && !emptyToOpen) {
emptyToOpen++;
}
const remoteAuthority = filesToOpen ? filesToOpen.remoteAuthority : openConfig.remoteAuthority;
for (let i = 0; i < emptyToOpen; i++) {
addUsedWindow(this.doOpenEmpty(openConfig, openFolderInNewWindow, remoteAuthority, filesToOpen), !!filesToOpen);
// any other window to open must open in new window then
openFolderInNewWindow = true;
}
}
return { windows: distinct(usedWindows), filesOpenedInWindow };
}
private doOpenFilesInExistingWindow(configuration: IOpenConfiguration, window: ICodeWindow, filesToOpen?: IFilesToOpen): ICodeWindow {
this.logService.trace('windowsManager#doOpenFilesInExistingWindow', { filesToOpen });
window.focus(); // make sure window has focus
const params: INativeOpenFileRequest = {
filesToOpenOrCreate: filesToOpen?.filesToOpenOrCreate,
filesToDiff: filesToOpen?.filesToDiff,
filesToMerge: filesToOpen?.filesToMerge,
filesToWait: filesToOpen?.filesToWait,
termProgram: configuration?.userEnv?.['TERM_PROGRAM']
};
window.sendWhenReady('vscode:openFiles', CancellationToken.None, params);
return window;
}
private doAddFoldersToExistingWindow(window: ICodeWindow, foldersToAdd: URI[]): ICodeWindow {
this.logService.trace('windowsManager#doAddFoldersToExistingWindow', { foldersToAdd });
window.focus(); // make sure window has focus
const request: IAddFoldersRequest = { foldersToAdd };
window.sendWhenReady('vscode:addFolders', CancellationToken.None, request);
return window;
}
private doOpenEmpty(openConfig: IOpenConfiguration, forceNewWindow: boolean, remoteAuthority: string | undefined, filesToOpen: IFilesToOpen | undefined, emptyWindowBackupInfo?: IEmptyWindowBackupInfo): ICodeWindow {
this.logService.trace('windowsManager#doOpenEmpty', { restore: !!emptyWindowBackupInfo, remoteAuthority, filesToOpen, forceNewWindow });
let windowToUse: ICodeWindow | undefined;
if (!forceNewWindow && typeof openConfig.contextWindowId === 'number') {
windowToUse = this.getWindowById(openConfig.contextWindowId); // fix for https://github.com/microsoft/vscode/issues/97172
}
return this.openInBrowserWindow({
userEnv: openConfig.userEnv,
cli: openConfig.cli,
initialStartup: openConfig.initialStartup,
remoteAuthority,
forceNewWindow,
forceNewTabbedWindow: openConfig.forceNewTabbedWindow,
filesToOpen,
windowToUse,
emptyWindowBackupInfo
});
}
private doOpenFolderOrWorkspace(openConfig: IOpenConfiguration, folderOrWorkspace: IWorkspacePathToOpen | ISingleFolderWorkspacePathToOpen, forceNewWindow: boolean, filesToOpen: IFilesToOpen | undefined, windowToUse?: ICodeWindow): ICodeWindow {
this.logService.trace('windowsManager#doOpenFolderOrWorkspace', { folderOrWorkspace, filesToOpen });
if (!forceNewWindow && !windowToUse && typeof openConfig.contextWindowId === 'number') {
windowToUse = this.getWindowById(openConfig.contextWindowId); // fix for https://github.com/microsoft/vscode/issues/49587
}
return this.openInBrowserWindow({
workspace: folderOrWorkspace.workspace,
userEnv: openConfig.userEnv,
cli: openConfig.cli,
initialStartup: openConfig.initialStartup,
remoteAuthority: folderOrWorkspace.remoteAuthority,
forceNewWindow,
forceNewTabbedWindow: openConfig.forceNewTabbedWindow,
filesToOpen,
windowToUse
});
}
private getPathsToOpen(openConfig: IOpenConfiguration): IPathToOpen[] {
let pathsToOpen: IPathToOpen[];
let isCommandLineOrAPICall = false;
let restoredWindows = false;
// Extract paths: from API
if (openConfig.urisToOpen && openConfig.urisToOpen.length > 0) {
pathsToOpen = this.doExtractPathsFromAPI(openConfig);
isCommandLineOrAPICall = true;
}
// Check for force empty
else if (openConfig.forceEmpty) {
pathsToOpen = [Object.create(null)];
}
// Extract paths: from CLI
else if (openConfig.cli._.length || openConfig.cli['folder-uri'] || openConfig.cli['file-uri']) {
pathsToOpen = this.doExtractPathsFromCLI(openConfig.cli);
if (pathsToOpen.length === 0) {
pathsToOpen.push(Object.create(null)); // add an empty window if we did not have windows to open from command line
}
isCommandLineOrAPICall = true;
}
// Extract paths: from previous session
else {
pathsToOpen = this.doGetPathsFromLastSession();
if (pathsToOpen.length === 0) {
pathsToOpen.push(Object.create(null)); // add an empty window if we did not have windows to restore
}
restoredWindows = true;
}
// Convert multiple folders into workspace (if opened via API or CLI)
// This will ensure to open these folders in one window instead of multiple
// If we are in `addMode`, we should not do this because in that case all
// folders should be added to the existing window.
if (!openConfig.addMode && isCommandLineOrAPICall) {
const foldersToOpen = pathsToOpen.filter(path => isSingleFolderWorkspacePathToOpen(path)) as ISingleFolderWorkspacePathToOpen[];
if (foldersToOpen.length > 1) {
const remoteAuthority = foldersToOpen[0].remoteAuthority;
if (foldersToOpen.every(folderToOpen => isEqualAuthority(folderToOpen.remoteAuthority, remoteAuthority))) { // only if all folder have the same authority
const workspace = this.workspacesManagementMainService.createUntitledWorkspaceSync(foldersToOpen.map(folder => ({ uri: folder.workspace.uri })));
// Add workspace and remove folders thereby
pathsToOpen.push({ workspace, remoteAuthority });
pathsToOpen = pathsToOpen.filter(path => !isSingleFolderWorkspacePathToOpen(path));
}
}
}
// Check for `window.startup` setting to include all windows
// from the previous session if this is the initial startup and we have
// not restored windows already otherwise.
// Use `unshift` to ensure any new window to open comes last
// for proper focus treatment.
if (openConfig.initialStartup && !restoredWindows && this.configurationService.getValue<IWindowSettings | undefined>('window')?.restoreWindows === 'preserve') {
pathsToOpen.unshift(...this.doGetPathsFromLastSession().filter(path => isWorkspacePathToOpen(path) || isSingleFolderWorkspacePathToOpen(path) || path.backupPath));
}
return pathsToOpen;
}
private doExtractPathsFromAPI(openConfig: IOpenConfiguration): IPathToOpen[] {
const pathsToOpen: IPathToOpen[] = [];
const pathResolveOptions: IPathResolveOptions = { gotoLineMode: openConfig.gotoLineMode, remoteAuthority: openConfig.remoteAuthority };
for (const pathToOpen of coalesce(openConfig.urisToOpen || [])) {
const path = this.resolveOpenable(pathToOpen, pathResolveOptions);
// Path exists
if (path) {
path.label = pathToOpen.label;
pathsToOpen.push(path);
}
// Path does not exist: show a warning box
else {
const uri = this.resourceFromOpenable(pathToOpen);
const options: MessageBoxOptions = {
title: this.productService.nameLong,
type: 'info',
buttons: [mnemonicButtonLabel(localize({ key: 'ok', comment: ['&& denotes a mnemonic'] }, "&&OK"))],
defaultId: 0,
message: uri.scheme === Schemas.file ? localize('pathNotExistTitle', "Path does not exist") : localize('uriInvalidTitle', "URI can not be opened"),
detail: uri.scheme === Schemas.file ?
localize('pathNotExistDetail', "The path '{0}' does not exist on this computer.", getPathLabel(uri, { os: OS, tildify: this.environmentMainService })) :
localize('uriInvalidDetail', "The URI '{0}' is not valid and can not be opened.", uri.toString(true)),
noLink: true
};
this.dialogMainService.showMessageBox(options, withNullAsUndefined(BrowserWindow.getFocusedWindow()));
}
}
return pathsToOpen;
}
private doExtractPathsFromCLI(cli: NativeParsedArgs): IPath[] {
const pathsToOpen: IPathToOpen[] = [];
const pathResolveOptions: IPathResolveOptions = {
ignoreFileNotFound: true,
gotoLineMode: cli.goto,
remoteAuthority: cli.remote || undefined,
forceOpenWorkspaceAsFile:
// special case diff / merge mode to force open
// workspace as file
// https://github.com/microsoft/vscode/issues/149731
cli.diff && cli._.length === 2 ||
cli.merge && cli._.length === 4
};
// folder uris
const folderUris = cli['folder-uri'];
if (folderUris) {
for (const rawFolderUri of folderUris) {
const folderUri = this.cliArgToUri(rawFolderUri);
if (folderUri) {
const path = this.resolveOpenable({ folderUri }, pathResolveOptions);
if (path) {
pathsToOpen.push(path);
}
}
}
}
// file uris
const fileUris = cli['file-uri'];
if (fileUris) {
for (const rawFileUri of fileUris) {
const fileUri = this.cliArgToUri(rawFileUri);
if (fileUri) {
const path = this.resolveOpenable(hasWorkspaceFileExtension(rawFileUri) ? { workspaceUri: fileUri } : { fileUri }, pathResolveOptions);
if (path) {
pathsToOpen.push(path);
}
}
}
}
// folder or file paths
const cliPaths = cli._;
for (const cliPath of cliPaths) {
const path = pathResolveOptions.remoteAuthority ? this.doResolvePathRemote(cliPath, pathResolveOptions) : this.doResolveFilePath(cliPath, pathResolveOptions);
if (path) {
pathsToOpen.push(path);
}
}
return pathsToOpen;
}
private cliArgToUri(arg: string): URI | undefined {
try {
const uri = URI.parse(arg);
if (!uri.scheme) {
this.logService.error(`Invalid URI input string, scheme missing: ${arg}`);
return undefined;
}
return uri;
} catch (e) {
this.logService.error(`Invalid URI input string: ${arg}, ${e.message}`);
}
return undefined;
}
private doGetPathsFromLastSession(): IPathToOpen[] {
const restoreWindowsSetting = this.getRestoreWindowsSetting();
switch (restoreWindowsSetting) {
// none: no window to restore
case 'none':
return [];
// one: restore last opened workspace/folder or empty window
// all: restore all windows
// folders: restore last opened folders only
case 'one':
case 'all':
case 'preserve':
case 'folders': {
// Collect previously opened windows
const lastSessionWindows: IWindowState[] = [];
if (restoreWindowsSetting !== 'one') {
lastSessionWindows.push(...this.windowsStateHandler.state.openedWindows);
}
if (this.windowsStateHandler.state.lastActiveWindow) {
lastSessionWindows.push(this.windowsStateHandler.state.lastActiveWindow);
}
const pathsToOpen: IPathToOpen[] = [];
for (const lastSessionWindow of lastSessionWindows) {
// Workspaces
if (lastSessionWindow.workspace) {
const pathToOpen = this.resolveOpenable({ workspaceUri: lastSessionWindow.workspace.configPath }, { remoteAuthority: lastSessionWindow.remoteAuthority, rejectTransientWorkspaces: true /* https://github.com/microsoft/vscode/issues/119695 */ });
if (isWorkspacePathToOpen(pathToOpen)) {
pathsToOpen.push(pathToOpen);
}
}
// Folders
else if (lastSessionWindow.folderUri) {
const pathToOpen = this.resolveOpenable({ folderUri: lastSessionWindow.folderUri }, { remoteAuthority: lastSessionWindow.remoteAuthority });
if (isSingleFolderWorkspacePathToOpen(pathToOpen)) {
pathsToOpen.push(pathToOpen);
}
}
// Empty window, potentially editors open to be restored
else if (restoreWindowsSetting !== 'folders' && lastSessionWindow.backupPath) {
pathsToOpen.push({ backupPath: lastSessionWindow.backupPath, remoteAuthority: lastSessionWindow.remoteAuthority });
}
}
return pathsToOpen;
}
}
}
private getRestoreWindowsSetting(): RestoreWindowsSetting {
let restoreWindows: RestoreWindowsSetting;
if (this.lifecycleMainService.wasRestarted) {
restoreWindows = 'all'; // always reopen all windows when an update was applied
} else {
const windowConfig = this.configurationService.getValue<IWindowSettings | undefined>('window');
restoreWindows = windowConfig?.restoreWindows || 'all'; // by default restore all windows
if (!['preserve', 'all', 'folders', 'one', 'none'].includes(restoreWindows)) {
restoreWindows = 'all'; // by default restore all windows
}
}
return restoreWindows;
}
private resolveOpenable(openable: IWindowOpenable, options: IPathResolveOptions = Object.create(null)): IPathToOpen | undefined {
// handle file:// openables with some extra validation
const uri = this.resourceFromOpenable(openable);
if (uri.scheme === Schemas.file) {
if (isFileToOpen(openable)) {
options = { ...options, forceOpenWorkspaceAsFile: true };
}
return this.doResolveFilePath(uri.fsPath, options);
}
// handle non file:// openables
return this.doResolveRemoteOpenable(openable, options);
}
private doResolveRemoteOpenable(openable: IWindowOpenable, options: IPathResolveOptions): IPathToOpen<ITextEditorOptions> | undefined {
let uri = this.resourceFromOpenable(openable);
// use remote authority from vscode
const remoteAuthority = getRemoteAuthority(uri) || options.remoteAuthority;
// normalize URI
uri = removeTrailingPathSeparator(normalizePath(uri));
// File
if (isFileToOpen(openable)) {
if (options.gotoLineMode) {
const { path, line, column } = parseLineAndColumnAware(uri.path);
return {
fileUri: uri.with({ path }),
options: {
selection: line ? { startLineNumber: line, startColumn: column || 1 } : undefined
},
remoteAuthority
};
}
return { fileUri: uri, remoteAuthority };
}
// Workspace
else if (isWorkspaceToOpen(openable)) {
return { workspace: getWorkspaceIdentifier(uri), remoteAuthority };
}
// Folder
return { workspace: getSingleFolderWorkspaceIdentifier(uri), remoteAuthority };
}
private resourceFromOpenable(openable: IWindowOpenable): URI {
if (isWorkspaceToOpen(openable)) {
return openable.workspaceUri;
}
if (isFolderToOpen(openable)) {
return openable.folderUri;
}
return openable.fileUri;
}
private doResolveFilePath(path: string, options: IPathResolveOptions): IPathToOpen<ITextEditorOptions> | undefined {
// Extract line/col information from path
let lineNumber: number | undefined;
let columnNumber: number | undefined;
if (options.gotoLineMode) {
({ path, line: lineNumber, column: columnNumber } = parseLineAndColumnAware(path));
}
// Ensure the path is normalized and absolute
path = sanitizeFilePath(normalize(path), cwd());
try {
const pathStat = statSync(path);
// File
if (pathStat.isFile()) {
// Workspace (unless disabled via flag)
if (!options.forceOpenWorkspaceAsFile) {
const workspace = this.workspacesManagementMainService.resolveLocalWorkspaceSync(URI.file(path));
if (workspace) {
// If the workspace is transient and we are to ignore
// transient workspaces, reject it.
if (workspace.transient && options.rejectTransientWorkspaces) {
return undefined;
}
return {
workspace: { id: workspace.id, configPath: workspace.configPath },
type: FileType.File,
exists: true,
remoteAuthority: workspace.remoteAuthority,
transient: workspace.transient
};
}
}
return {
fileUri: URI.file(path),
type: FileType.File,
exists: true,
options: {
selection: lineNumber ? { startLineNumber: lineNumber, startColumn: columnNumber || 1 } : undefined
}
};
}
// Folder
else if (pathStat.isDirectory()) {
return {
workspace: getSingleFolderWorkspaceIdentifier(URI.file(path), pathStat),
type: FileType.Directory,
exists: true
};
}
// Special device: in POSIX environments, we may get /dev/null passed
// in (for example git uses it to signal one side of a diff does not
// exist). In that special case, treat it like a file to support this
// scenario ()
else if (!isWindows && path === '/dev/null') {
return {
fileUri: URI.file(path),
type: FileType.File,
exists: true
};
}
} catch (error) {
const fileUri = URI.file(path);
// since file does not seem to exist anymore, remove from recent
this.workspacesHistoryMainService.removeRecentlyOpened([fileUri]);
// assume this is a file that does not yet exist
if (options.ignoreFileNotFound) {
return {
fileUri,
type: FileType.File,
exists: false
};
}
}
return undefined;
}
private doResolvePathRemote(path: string, options: IPathResolveOptions): IPathToOpen<ITextEditorOptions> | undefined {
const first = path.charCodeAt(0);
const remoteAuthority = options.remoteAuthority;
// Extract line/col information from path
let lineNumber: number | undefined;
let columnNumber: number | undefined;
if (options.gotoLineMode) {
({ path, line: lineNumber, column: columnNumber } = parseLineAndColumnAware(path));
}
// make absolute
if (first !== CharCode.Slash) {
if (isWindowsDriveLetter(first) && path.charCodeAt(path.charCodeAt(1)) === CharCode.Colon) {
path = toSlashes(path);
}
path = `/${path}`;
}
const uri = URI.from({ scheme: Schemas.vscodeRemote, authority: remoteAuthority, path: path });
// guess the file type:
// - if it ends with a slash it's a folder
// - if in goto line mode or if it has a file extension, it's a file or a workspace
// - by defaults it's a folder
if (path.charCodeAt(path.length - 1) !== CharCode.Slash) {
// file name ends with .code-workspace
if (hasWorkspaceFileExtension(path)) {
if (options.forceOpenWorkspaceAsFile) {
return {
fileUri: uri,
options: {
selection: lineNumber ? { startLineNumber: lineNumber, startColumn: columnNumber || 1 } : undefined
},
remoteAuthority: options.remoteAuthority
};
}
return { workspace: getWorkspaceIdentifier(uri), remoteAuthority };
}
// file name starts with a dot or has an file extension
else if (options.gotoLineMode || posix.basename(path).indexOf('.') !== -1) {
return {
fileUri: uri,
options: {
selection: lineNumber ? { startLineNumber: lineNumber, startColumn: columnNumber || 1 } : undefined
},
remoteAuthority
};
}
}
return { workspace: getSingleFolderWorkspaceIdentifier(uri), remoteAuthority };
}
private shouldOpenNewWindow(openConfig: IOpenConfiguration): { openFolderInNewWindow: boolean; openFilesInNewWindow: boolean } {
// let the user settings override how folders are open in a new window or same window unless we are forced
const windowConfig = this.configurationService.getValue<IWindowSettings | undefined>('window');
const openFolderInNewWindowConfig = windowConfig?.openFoldersInNewWindow || 'default' /* default */;
const openFilesInNewWindowConfig = windowConfig?.openFilesInNewWindow || 'off' /* default */;
let openFolderInNewWindow = (openConfig.preferNewWindow || openConfig.forceNewWindow) && !openConfig.forceReuseWindow;
if (!openConfig.forceNewWindow && !openConfig.forceReuseWindow && (openFolderInNewWindowConfig === 'on' || openFolderInNewWindowConfig === 'off')) {
openFolderInNewWindow = (openFolderInNewWindowConfig === 'on');
}
// let the user settings override how files are open in a new window or same window unless we are forced (not for extension development though)
let openFilesInNewWindow: boolean = false;
if (openConfig.forceNewWindow || openConfig.forceReuseWindow) {
openFilesInNewWindow = !!openConfig.forceNewWindow && !openConfig.forceReuseWindow;
} else {
// macOS: by default we open files in a new window if this is triggered via DOCK context
if (isMacintosh) {
if (openConfig.context === OpenContext.DOCK) {
openFilesInNewWindow = true;
}
}
// Linux/Windows: by default we open files in the new window unless triggered via DIALOG / MENU context
// or from the integrated terminal where we assume the user prefers to open in the current window
else {
if (openConfig.context !== OpenContext.DIALOG && openConfig.context !== OpenContext.MENU && !(openConfig.userEnv && openConfig.userEnv['TERM_PROGRAM'] === 'vscode')) {
openFilesInNewWindow = true;
}
}
// finally check for overrides of default
if (!openConfig.cli.extensionDevelopmentPath && (openFilesInNewWindowConfig === 'on' || openFilesInNewWindowConfig === 'off')) {
openFilesInNewWindow = (openFilesInNewWindowConfig === 'on');
}
}
return { openFolderInNewWindow: !!openFolderInNewWindow, openFilesInNewWindow };
}
openExtensionDevelopmentHostWindow(extensionDevelopmentPaths: string[], openConfig: IOpenConfiguration): ICodeWindow[] {
// Reload an existing extension development host window on the same path
// We currently do not allow more than one extension development window
// on the same extension path.
const existingWindow = findWindowOnExtensionDevelopmentPath(this.getWindows(), extensionDevelopmentPaths);
if (existingWindow) {
this.lifecycleMainService.reload(existingWindow, openConfig.cli);
existingWindow.focus(); // make sure it gets focus and is restored
return [existingWindow];
}
let folderUris = openConfig.cli['folder-uri'] || [];
let fileUris = openConfig.cli['file-uri'] || [];
let cliArgs = openConfig.cli._;
// Fill in previously opened workspace unless an explicit path is provided and we are not unit testing
if (!cliArgs.length && !folderUris.length && !fileUris.length && !openConfig.cli.extensionTestsPath) {
const extensionDevelopmentWindowState = this.windowsStateHandler.state.lastPluginDevelopmentHostWindow;
const workspaceToOpen = extensionDevelopmentWindowState && (extensionDevelopmentWindowState.workspace || extensionDevelopmentWindowState.folderUri);
if (workspaceToOpen) {
if (URI.isUri(workspaceToOpen)) {
if (workspaceToOpen.scheme === Schemas.file) {
cliArgs = [workspaceToOpen.fsPath];
} else {
folderUris = [workspaceToOpen.toString()];
}
} else {
if (workspaceToOpen.configPath.scheme === Schemas.file) {
cliArgs = [originalFSPath(workspaceToOpen.configPath)];
} else {
fileUris = [workspaceToOpen.configPath.toString()];
}
}
}
}
let remoteAuthority = openConfig.remoteAuthority;
for (const extensionDevelopmentPath of extensionDevelopmentPaths) {
if (extensionDevelopmentPath.match(/^[a-zA-Z][a-zA-Z0-9\+\-\.]+:/)) {
const url = URI.parse(extensionDevelopmentPath);
const extensionDevelopmentPathRemoteAuthority = getRemoteAuthority(url);
if (extensionDevelopmentPathRemoteAuthority) {
if (remoteAuthority) {
if (!isEqualAuthority(extensionDevelopmentPathRemoteAuthority, remoteAuthority)) {
this.logService.error('more than one extension development path authority');
}
} else {
remoteAuthority = extensionDevelopmentPathRemoteAuthority;
}
}
}
}
// Make sure that we do not try to open:
// - a workspace or folder that is already opened
// - a workspace or file that has a different authority as the extension development.
cliArgs = cliArgs.filter(path => {
const uri = URI.file(path);
if (!!findWindowOnWorkspaceOrFolder(this.getWindows(), uri)) {
return false;
}
return isEqualAuthority(getRemoteAuthority(uri), remoteAuthority);
});
folderUris = folderUris.filter(folderUriStr => {
const folderUri = this.cliArgToUri(folderUriStr);
if (folderUri && !!findWindowOnWorkspaceOrFolder(this.getWindows(), folderUri)) {
return false;
}
return folderUri ? isEqualAuthority(getRemoteAuthority(folderUri), remoteAuthority) : false;
});
fileUris = fileUris.filter(fileUriStr => {
const fileUri = this.cliArgToUri(fileUriStr);
if (fileUri && !!findWindowOnWorkspaceOrFolder(this.getWindows(), fileUri)) {
return false;
}
return fileUri ? isEqualAuthority(getRemoteAuthority(fileUri), remoteAuthority) : false;
});
openConfig.cli._ = cliArgs;
openConfig.cli['folder-uri'] = folderUris;
openConfig.cli['file-uri'] = fileUris;
const noFilesOrFolders = !cliArgs.length && !folderUris.length && !fileUris.length;
// Open it
const openArgs: IOpenConfiguration = {
context: openConfig.context,
cli: openConfig.cli,
forceNewWindow: true,
forceEmpty: noFilesOrFolders,
userEnv: openConfig.userEnv,
noRecentEntry: true,
waitMarkerFileURI: openConfig.waitMarkerFileURI,
remoteAuthority
};
return this.open(openArgs);
}
private openInBrowserWindow(options: IOpenBrowserWindowOptions): ICodeWindow {
const windowConfig = this.configurationService.getValue<IWindowSettings | undefined>('window');
// Build up the window configuration from provided options, config and environment
const configuration: INativeWindowConfiguration = {
// Inherit CLI arguments from environment and/or
// the specific properties from this launch if provided
...this.environmentMainService.args,
...options.cli,
machineId: this.machineId,
windowId: -1, // Will be filled in by the window once loaded later
mainPid: process.pid,
appRoot: this.environmentMainService.appRoot,
execPath: process.execPath,
codeCachePath: this.environmentMainService.codeCachePath,
// If we know the backup folder upfront (for empty windows to restore), we can set it
// directly here which helps for restoring UI state associated with that window.
// For all other cases we first call into registerEmptyWindowBackupSync() to set it before
// loading the window.
backupPath: options.emptyWindowBackupInfo ? join(this.environmentMainService.backupHome, options.emptyWindowBackupInfo.backupFolder) : undefined,
profiles: {
all: this.userDataProfilesService.profiles,
current: this.userDataProfilesService.getProfile(options.workspace ?? 'empty-window'),
},
homeDir: this.environmentMainService.userHome.fsPath,
tmpDir: this.environmentMainService.tmpDir.fsPath,
userDataDir: this.environmentMainService.userDataPath,
remoteAuthority: options.remoteAuthority,
workspace: options.workspace,
userEnv: { ...this.initialUserEnv, ...options.userEnv },
filesToOpenOrCreate: options.filesToOpen?.filesToOpenOrCreate,
filesToDiff: options.filesToOpen?.filesToDiff,
filesToMerge: options.filesToOpen?.filesToMerge,
filesToWait: options.filesToOpen?.filesToWait,
logLevel: this.logService.getLevel(),
logsPath: this.environmentMainService.logsPath,
product,
isInitialStartup: options.initialStartup,
perfMarks: getMarks(),
os: { release: release(), hostname: hostname() },
zoomLevel: typeof windowConfig?.zoomLevel === 'number' ? windowConfig.zoomLevel : undefined,
autoDetectHighContrast: windowConfig?.autoDetectHighContrast ?? true,
autoDetectColorScheme: windowConfig?.autoDetectColorScheme ?? false,
accessibilitySupport: app.accessibilitySupportEnabled,
colorScheme: this.themeMainService.getColorScheme(),
policiesData: this.policyService.serialize(),
editSessionId: this.environmentMainService.editSessionId,
};
let window: ICodeWindow | undefined;
if (!options.forceNewWindow && !options.forceNewTabbedWindow) {
window = options.windowToUse || this.getLastActiveWindow();
if (window) {
window.focus();
}
}
// New window
if (!window) {
const state = this.windowsStateHandler.getNewWindowState(configuration);
// Create the window
mark('code/willCreateCodeWindow');
const createdWindow = window = this.instantiationService.createInstance(CodeWindow, {
state,
extensionDevelopmentPath: configuration.extensionDevelopmentPath,
isExtensionTestHost: !!configuration.extensionTestsPath
});
mark('code/didCreateCodeWindow');
// Add as window tab if configured (macOS only)
if (options.forceNewTabbedWindow) {
const activeWindow = this.getLastActiveWindow();
activeWindow?.addTabbedWindow(createdWindow);
}
// Add to our list of windows
WindowsMainService.WINDOWS.push(createdWindow);
// Indicate new window via event
this._onDidOpenWindow.fire(createdWindow);
// Indicate number change via event
this._onDidChangeWindowsCount.fire({ oldCount: this.getWindowCount() - 1, newCount: this.getWindowCount() });
// Window Events
once(createdWindow.onDidSignalReady)(() => this._onDidSignalReadyWindow.fire(createdWindow));
once(createdWindow.onDidClose)(() => this.onWindowClosed(createdWindow));
once(createdWindow.onDidDestroy)(() => this._onDidDestroyWindow.fire(createdWindow));
createdWindow.onDidTriggerSystemContextMenu(({ x, y }) => this._onDidTriggerSystemContextMenu.fire({ window: createdWindow, x, y }));
const webContents = assertIsDefined(createdWindow.win?.webContents);
webContents.removeAllListeners('devtools-reload-page'); // remove built in listener so we can handle this on our own
webContents.on('devtools-reload-page', () => this.lifecycleMainService.reload(createdWindow));
// Lifecycle
this.lifecycleMainService.registerWindow(createdWindow);
}
// Existing window
else {
// Some configuration things get inherited if the window is being reused and we are
// in extension development host mode. These options are all development related.
const currentWindowConfig = window.config;
if (!configuration.extensionDevelopmentPath && currentWindowConfig && !!currentWindowConfig.extensionDevelopmentPath) {
configuration.extensionDevelopmentPath = currentWindowConfig.extensionDevelopmentPath;
configuration.verbose = currentWindowConfig.verbose;
configuration['inspect-brk-extensions'] = currentWindowConfig['inspect-brk-extensions'];
configuration.debugId = currentWindowConfig.debugId;
configuration.extensionEnvironment = currentWindowConfig.extensionEnvironment;
configuration['inspect-extensions'] = currentWindowConfig['inspect-extensions'];
configuration['extensions-dir'] = currentWindowConfig['extensions-dir'];
}
}
// Update window identifier and session now
// that we have the window object in hand.
configuration.windowId = window.id;
// If the window was already loaded, make sure to unload it
// first and only load the new configuration if that was
// not vetoed
if (window.isReady) {
this.lifecycleMainService.unload(window, UnloadReason.LOAD).then(veto => {
if (!veto) {
this.doOpenInBrowserWindow(window!, configuration, options);
}
});
} else {
this.doOpenInBrowserWindow(window, configuration, options);
}
return window;
}
private doOpenInBrowserWindow(window: ICodeWindow, configuration: INativeWindowConfiguration, options: IOpenBrowserWindowOptions): void {
// Register window for backups
if (!configuration.extensionDevelopmentPath) {
if (isWorkspaceIdentifier(configuration.workspace)) {
configuration.backupPath = this.backupMainService.registerWorkspaceBackupSync({ workspace: configuration.workspace, remoteAuthority: configuration.remoteAuthority });
} else if (isSingleFolderWorkspaceIdentifier(configuration.workspace)) {
configuration.backupPath = this.backupMainService.registerFolderBackupSync({ folderUri: configuration.workspace.uri, remoteAuthority: configuration.remoteAuthority });
} else {
const backupFolder = options.emptyWindowBackupInfo && options.emptyWindowBackupInfo.backupFolder;
configuration.backupPath = this.backupMainService.registerEmptyWindowBackupSync(backupFolder, configuration.remoteAuthority);
}
}
// Load it
window.load(configuration);
}
private onWindowClosed(window: ICodeWindow): void {
// Remove from our list so that Electron can clean it up
const index = WindowsMainService.WINDOWS.indexOf(window);
WindowsMainService.WINDOWS.splice(index, 1);
// Emit
this._onDidChangeWindowsCount.fire({ oldCount: this.getWindowCount() + 1, newCount: this.getWindowCount() });
}
getFocusedWindow(): ICodeWindow | undefined {
const window = BrowserWindow.getFocusedWindow();
if (window) {
return this.getWindowById(window.id);
}
return undefined;
}
getLastActiveWindow(): ICodeWindow | undefined {
return this.doGetLastActiveWindow(this.getWindows());
}
private getLastActiveWindowForAuthority(remoteAuthority: string | undefined): ICodeWindow | undefined {
return this.doGetLastActiveWindow(this.getWindows().filter(window => isEqualAuthority(window.remoteAuthority, remoteAuthority)));
}
private doGetLastActiveWindow(windows: ICodeWindow[]): ICodeWindow | undefined {
const lastFocusedDate = Math.max.apply(Math, windows.map(window => window.lastFocusTime));
return windows.find(window => window.lastFocusTime === lastFocusedDate);
}
sendToFocused(channel: string, ...args: any[]): void {
const focusedWindow = this.getFocusedWindow() || this.getLastActiveWindow();
focusedWindow?.sendWhenReady(channel, CancellationToken.None, ...args);
}
sendToAll(channel: string, payload?: any, windowIdsToIgnore?: number[]): void {
for (const window of this.getWindows()) {
if (windowIdsToIgnore && windowIdsToIgnore.indexOf(window.id) >= 0) {
continue; // do not send if we are instructed to ignore it
}
window.sendWhenReady(channel, CancellationToken.None, payload);
}
}
getWindows(): ICodeWindow[] {
return WindowsMainService.WINDOWS;
}
getWindowCount(): number {
return WindowsMainService.WINDOWS.length;
}
getWindowById(windowId: number): ICodeWindow | undefined {
const windows = this.getWindows().filter(window => window.id === windowId);
return firstOrDefault(windows);
}
getWindowByWebContents(webContents: WebContents): ICodeWindow | undefined {
const browserWindow = BrowserWindow.fromWebContents(webContents);
if (!browserWindow) {
return undefined;
}
return this.getWindowById(browserWindow.id);
}
}
|