-
Notifications
You must be signed in to change notification settings - Fork 31.3k
/
Copy patheditSessions.contribution.ts
919 lines (801 loc) · 40.1 KB
/
editSessions.contribution.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions, IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { Registry } from 'vs/platform/registry/common/platform';
import { ILifecycleService, LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { Action2, IAction2Options, MenuRegistry, registerAction2 } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { localize } from 'vs/nls';
import { IEditSessionsStorageService, Change, ChangeType, Folder, EditSession, FileType, EDIT_SESSION_SYNC_CATEGORY, EDIT_SESSIONS_CONTAINER_ID, EditSessionSchemaVersion, IEditSessionsLogService, EDIT_SESSIONS_VIEW_ICON, EDIT_SESSIONS_TITLE, EDIT_SESSIONS_SHOW_VIEW, EDIT_SESSIONS_DATA_VIEW_ID, decodeEditSessionFileContent } from 'vs/workbench/contrib/editSessions/common/editSessions';
import { ISCMRepository, ISCMService } from 'vs/workbench/contrib/scm/common/scm';
import { IFileService } from 'vs/platform/files/common/files';
import { IWorkspaceContextService, IWorkspaceFolder, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { URI } from 'vs/base/common/uri';
import { basename, joinPath, relativePath } from 'vs/base/common/resources';
import { encodeBase64 } from 'vs/base/common/buffer';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
import { EditSessionsWorkbenchService } from 'vs/workbench/contrib/editSessions/browser/editSessionsStorageService';
import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { UserDataSyncErrorCode, UserDataSyncStoreError } from 'vs/platform/userDataSync/common/userDataSync';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { getFileNamesMessage, IDialogService, IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IProductService } from 'vs/platform/product/common/productService';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { workbenchConfigurationNodeBase } from 'vs/workbench/common/configuration';
import { Extensions as ConfigurationExtensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { ExtensionsRegistry } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { ContextKeyExpr, ContextKeyExpression, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { getVirtualWorkspaceLocation } from 'vs/platform/workspace/common/virtualWorkspace';
import { Schemas } from 'vs/base/common/network';
import { IsWebContext } from 'vs/platform/contextkey/common/contextkeys';
import { isProposedApiEnabled } from 'vs/workbench/services/extensions/common/extensions';
import { EditSessionsLogService } from 'vs/workbench/contrib/editSessions/common/editSessionsLogService';
import { IViewContainersRegistry, Extensions as ViewExtensions, ViewContainerLocation, IViewsService } from 'vs/workbench/common/views';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { ViewPaneContainer } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { EditSessionsDataViews } from 'vs/workbench/contrib/editSessions/browser/editSessionsViews';
import { EditSessionsFileSystemProvider } from 'vs/workbench/contrib/editSessions/browser/editSessionsFileSystemProvider';
import { isNative } from 'vs/base/common/platform';
import { WorkspaceFolderCountContext } from 'vs/workbench/common/contextkeys';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { equals } from 'vs/base/common/objects';
import { EditSessionIdentityMatch, IEditSessionIdentityService } from 'vs/platform/workspace/common/editSessions';
import { ThemeIcon } from 'vs/platform/theme/common/themeService';
import { IOutputService } from 'vs/workbench/services/output/common/output';
import * as Constants from 'vs/workbench/contrib/logs/common/logConstants';
import { sha1Hex } from 'vs/base/browser/hash';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { IActivityService, NumberBadge } from 'vs/workbench/services/activity/common/activity';
registerSingleton(IEditSessionsLogService, EditSessionsLogService, InstantiationType.Delayed);
registerSingleton(IEditSessionsStorageService, EditSessionsWorkbenchService, InstantiationType.Delayed);
const continueWorkingOnCommand: IAction2Options = {
id: '_workbench.editSessions.actions.continueEditSession',
title: { value: localize('continue working on', "Continue Working On..."), original: 'Continue Working On...' },
precondition: WorkspaceFolderCountContext.notEqualsTo('0'),
f1: true
};
const openLocalFolderCommand: IAction2Options = {
id: '_workbench.editSessions.actions.continueEditSession.openLocalFolder',
title: { value: localize('continue edit session in local folder', "Open In Local Folder"), original: 'Open In Local Folder' },
category: EDIT_SESSION_SYNC_CATEGORY,
precondition: IsWebContext
};
const showOutputChannelCommand: IAction2Options = {
id: 'workbench.editSessions.actions.showOutputChannel',
title: { value: localize('show log', 'Show Log'), original: 'Show Log' },
category: EDIT_SESSION_SYNC_CATEGORY
};
const resumingProgressOptions = {
location: ProgressLocation.Window,
type: 'syncing',
title: `[${localize('resuming edit session window', 'Resuming edit session...')}](command:${showOutputChannelCommand.id})`
};
const queryParamName = 'editSessionId';
const useEditSessionsWithContinueOn = 'workbench.editSessions.continueOn';
export class EditSessionsContribution extends Disposable implements IWorkbenchContribution {
private continueEditSessionOptions: ContinueEditSessionItem[] = [];
private readonly shouldShowViewsContext: IContextKey<boolean>;
private static APPLICATION_LAUNCHED_VIA_CONTINUE_ON_STORAGE_KEY = 'applicationLaunchedViaContinueOn';
private accountsMenuBadgeDisposable = this._register(new MutableDisposable());
constructor(
@IEditSessionsStorageService private readonly editSessionsStorageService: IEditSessionsStorageService,
@IFileService private readonly fileService: IFileService,
@IProgressService private readonly progressService: IProgressService,
@IOpenerService private readonly openerService: IOpenerService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@ISCMService private readonly scmService: ISCMService,
@INotificationService private readonly notificationService: INotificationService,
@IDialogService private readonly dialogService: IDialogService,
@IEditSessionsLogService private readonly logService: IEditSessionsLogService,
@IEnvironmentService private readonly environmentService: IEnvironmentService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IProductService private readonly productService: IProductService,
@IConfigurationService private configurationService: IConfigurationService,
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@IEditSessionIdentityService private readonly editSessionIdentityService: IEditSessionIdentityService,
@IQuickInputService private readonly quickInputService: IQuickInputService,
@ICommandService private commandService: ICommandService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IFileDialogService private readonly fileDialogService: IFileDialogService,
@ILifecycleService private readonly lifecycleService: ILifecycleService,
@IStorageService private readonly storageService: IStorageService,
@IActivityService private readonly activityService: IActivityService,
) {
super();
this.autoResumeEditSession();
this.registerActions();
this.registerViews();
this.registerContributedEditSessionOptions();
this.shouldShowViewsContext = EDIT_SESSIONS_SHOW_VIEW.bindTo(this.contextKeyService);
this._register(this.fileService.registerProvider(EditSessionsFileSystemProvider.SCHEMA, new EditSessionsFileSystemProvider(this.editSessionsStorageService)));
this.lifecycleService.onWillShutdown((e) => e.join(this.autoStoreEditSession(), { id: 'autoStoreEditSession', label: localize('autoStoreEditSession', 'Storing current edit session...') }));
this._register(this.editSessionsStorageService.onDidSignIn(() => this.updateAccountsMenuBadge()));
this._register(this.editSessionsStorageService.onDidSignOut(() => this.updateAccountsMenuBadge()));
}
private autoResumeEditSession() {
void this.progressService.withProgress(resumingProgressOptions, async () => {
performance.mark('code/willResumeEditSessionFromIdentifier');
type ResumeEvent = {};
type ResumeClassification = {
owner: 'joyceerhl'; comment: 'Reporting when an action is resumed from an edit session identifier.';
};
this.telemetryService.publicLog2<ResumeEvent, ResumeClassification>('editSessions.continue.resume');
const shouldAutoResumeOnReload = this.configurationService.getValue('workbench.editSessions.autoResume') === 'onReload';
if (this.environmentService.editSessionId !== undefined) {
this.logService.info(`Resuming edit session, reason: found editSessionId ${this.environmentService.editSessionId} in environment service...`);
await this.resumeEditSession(this.environmentService.editSessionId).finally(() => this.environmentService.editSessionId = undefined);
} else if (shouldAutoResumeOnReload && this.editSessionsStorageService.isSignedIn) {
this.logService.info('Resuming edit session, reason: edit sessions enabled...');
// Attempt to resume edit session based on edit workspace identifier
// Note: at this point if the user is not signed into edit sessions,
// we don't want them to be prompted to sign in and should just return early
await this.resumeEditSession(undefined, true);
} else if (shouldAutoResumeOnReload) {
// The application has previously launched via a protocol URL Continue On flow
const hasApplicationLaunchedFromContinueOnFlow = this.storageService.getBoolean(EditSessionsContribution.APPLICATION_LAUNCHED_VIA_CONTINUE_ON_STORAGE_KEY, StorageScope.APPLICATION, false);
const handlePendingEditSessions = () => {
// display a badge in the accounts menu but do not prompt the user to sign in again
this.updateAccountsMenuBadge();
// attempt a resume if we are in a pending state and the user just signed in
const disposable = this.editSessionsStorageService.onDidSignIn(async () => {
disposable.dispose();
this.resumeEditSession(undefined, true);
this.storageService.remove(EditSessionsContribution.APPLICATION_LAUNCHED_VIA_CONTINUE_ON_STORAGE_KEY, StorageScope.APPLICATION);
this.environmentService.continueOn = undefined;
});
};
if ((this.environmentService.continueOn !== undefined) &&
!this.editSessionsStorageService.isSignedIn &&
// and user has not yet been prompted to sign in on this machine
hasApplicationLaunchedFromContinueOnFlow === false
) {
this.storageService.store(EditSessionsContribution.APPLICATION_LAUNCHED_VIA_CONTINUE_ON_STORAGE_KEY, true, StorageScope.APPLICATION, StorageTarget.MACHINE);
await this.editSessionsStorageService.initialize(true);
if (this.editSessionsStorageService.isSignedIn) {
await this.resumeEditSession(undefined, true);
} else {
handlePendingEditSessions();
}
// store the fact that we prompted the user
} else if (!this.editSessionsStorageService.isSignedIn &&
// and user has been prompted to sign in on this machine
hasApplicationLaunchedFromContinueOnFlow === true
) {
handlePendingEditSessions();
}
}
performance.mark('code/didResumeEditSessionFromIdentifier');
});
}
private updateAccountsMenuBadge() {
if (this.editSessionsStorageService.isSignedIn) {
return this.accountsMenuBadgeDisposable.clear();
}
const badge = new NumberBadge(1, () => localize('check for pending edit sessions', 'Check for pending edit sessions'));
this.accountsMenuBadgeDisposable.value = this.activityService.showAccountsActivity({ badge, priority: 1 });
}
private async autoStoreEditSession() {
if (this.configurationService.getValue('workbench.experimental.editSessions.autoStore') === 'onShutdown') {
await this.progressService.withProgress({
location: ProgressLocation.Window,
type: 'syncing',
title: localize('store edit session', 'Storing edit session...')
}, async () => this.storeEditSession(false));
}
}
private registerViews() {
const container = Registry.as<IViewContainersRegistry>(ViewExtensions.ViewContainersRegistry).registerViewContainer(
{
id: EDIT_SESSIONS_CONTAINER_ID,
title: EDIT_SESSIONS_TITLE,
ctorDescriptor: new SyncDescriptor(
ViewPaneContainer,
[EDIT_SESSIONS_CONTAINER_ID, { mergeViewWithContainerWhenSingleView: true }]
),
icon: EDIT_SESSIONS_VIEW_ICON,
hideIfEmpty: true
}, ViewContainerLocation.Sidebar, { doNotRegisterOpenCommand: true }
);
this._register(this.instantiationService.createInstance(EditSessionsDataViews, container));
}
private registerActions() {
this.registerContinueEditSessionAction();
this.registerResumeLatestEditSessionAction();
this.registerStoreLatestEditSessionAction();
this.registerContinueInLocalFolderAction();
this.registerShowEditSessionViewAction();
this.registerShowEditSessionOutputChannelAction();
}
private registerShowEditSessionOutputChannelAction() {
this._register(registerAction2(class ShowEditSessionOutput extends Action2 {
constructor() {
super(showOutputChannelCommand);
}
run(accessor: ServicesAccessor, ...args: any[]) {
const outputChannel = accessor.get(IOutputService);
void outputChannel.showChannel(Constants.editSessionsLogChannelId);
}
}));
}
private registerShowEditSessionViewAction() {
const that = this;
this._register(registerAction2(class ShowEditSessionView extends Action2 {
constructor() {
super({
id: 'workbench.editSessions.actions.showEditSessions',
title: { value: localize('show edit session', "Show Edit Sessions"), original: 'Show Edit Sessions' },
category: EDIT_SESSION_SYNC_CATEGORY,
f1: true
});
}
async run(accessor: ServicesAccessor) {
that.shouldShowViewsContext.set(true);
const viewsService = accessor.get(IViewsService);
await viewsService.openView(EDIT_SESSIONS_DATA_VIEW_ID);
}
}));
}
private registerContinueEditSessionAction() {
const that = this;
this._register(registerAction2(class ContinueEditSessionAction extends Action2 {
constructor() {
super(continueWorkingOnCommand);
}
async run(accessor: ServicesAccessor, workspaceUri: URI | undefined): Promise<void> {
type ContinueEditSessionEvent = {};
type ContinueEditSessionClassification = {
owner: 'joyceerhl'; comment: 'Reporting when the continue edit session action is run.';
};
that.telemetryService.publicLog2<ContinueEditSessionEvent, ContinueEditSessionClassification>('editSessions.continue.store');
const shouldStoreEditSession = await that.shouldContinueOnWithEditSession();
// Run the store action to get back a ref
let ref: string | undefined;
if (shouldStoreEditSession) {
ref = await that.progressService.withProgress({
location: ProgressLocation.Notification,
type: 'syncing',
title: localize('store your edit session', 'Storing your edit session...')
}, async () => that.storeEditSession(false));
}
let uri = workspaceUri ?? await that.pickContinueEditSessionDestination();
if (uri === undefined) {
// If the user didn't end up picking a Continue On destination
// and we stored an edit session, clean up the stored edit session
if (ref !== undefined) {
void that.editSessionsStorageService.delete(ref);
}
return;
}
// Append the ref to the URI
if (ref !== undefined && uri !== 'noDestinationUri') {
const encodedRef = encodeURIComponent(ref);
uri = uri.with({
query: uri.query.length > 0 ? (uri.query + `&${queryParamName}=${encodedRef}&continueOn=1`) : `${queryParamName}=${encodedRef}&continueOn=1`
});
// Open the URI
that.logService.info(`Opening ${uri.toString()}`);
await that.openerService.open(uri, { openExternal: true });
} else if (!shouldStoreEditSession && uri !== 'noDestinationUri') {
// Open the URI without an edit session ref
that.logService.info(`Opening ${uri.toString()}`);
await that.openerService.open(uri, { openExternal: true });
} else if (ref === undefined && shouldStoreEditSession) {
that.logService.warn(`Failed to store edit session when invoking ${continueWorkingOnCommand.id}.`);
}
}
}));
}
private registerResumeLatestEditSessionAction(): void {
const that = this;
this._register(registerAction2(class ResumeLatestEditSessionAction extends Action2 {
constructor() {
super({
id: 'workbench.editSessions.actions.resumeLatest',
title: { value: localize('resume latest.v2', "Resume Latest Edit Session"), original: 'Resume Latest Edit Session' },
category: EDIT_SESSION_SYNC_CATEGORY,
f1: true,
});
}
async run(accessor: ServicesAccessor, editSessionId?: string): Promise<void> {
await that.progressService.withProgress(resumingProgressOptions, async () => {
type ResumeEvent = {};
type ResumeClassification = {
owner: 'joyceerhl'; comment: 'Reporting when the resume edit session action is invoked.';
};
that.telemetryService.publicLog2<ResumeEvent, ResumeClassification>('editSessions.resume');
await that.resumeEditSession(editSessionId);
});
}
}));
}
private registerStoreLatestEditSessionAction(): void {
const that = this;
this._register(registerAction2(class StoreLatestEditSessionAction extends Action2 {
constructor() {
super({
id: 'workbench.editSessions.actions.storeCurrent',
title: { value: localize('store current.v2', "Store Current Edit Session"), original: 'Store Current Edit Session' },
category: EDIT_SESSION_SYNC_CATEGORY,
f1: true,
});
}
async run(accessor: ServicesAccessor): Promise<void> {
await that.progressService.withProgress({
location: ProgressLocation.Notification,
title: localize('storing edit session', 'Storing edit session...')
}, async () => {
type StoreEvent = {};
type StoreClassification = {
owner: 'joyceerhl'; comment: 'Reporting when the store edit session action is invoked.';
};
that.telemetryService.publicLog2<StoreEvent, StoreClassification>('editSessions.store');
await that.storeEditSession(true);
});
}
}));
}
async resumeEditSession(ref?: string, silent?: boolean, force?: boolean): Promise<void> {
// Edit sessions are not currently supported in empty workspaces
// https://github.com/microsoft/vscode/issues/159220
if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY) {
return;
}
this.logService.info(ref !== undefined ? `Resuming edit session with ref ${ref}...` : 'Resuming edit session...');
if (silent && !(await this.editSessionsStorageService.initialize(false, true))) {
return;
}
const data = await this.editSessionsStorageService.read(ref);
if (!data) {
if (ref === undefined && !silent) {
this.notificationService.info(localize('no edit session', 'There are no edit sessions to resume.'));
} else if (ref !== undefined) {
this.notificationService.warn(localize('no edit session content for ref', 'Could not resume edit session contents for ID {0}.', ref));
}
this.logService.info(`Aborting resuming edit session as no edit session content is available to be applied from ref ${ref}.`);
return;
}
const editSession = data.editSession;
ref = data.ref;
if (editSession.version > EditSessionSchemaVersion) {
this.notificationService.error(localize('client too old', "Please upgrade to a newer version of {0} to resume this edit session.", this.productService.nameLong));
return;
}
try {
const { changes, conflictingChanges } = await this.generateChanges(editSession, ref, force);
if (changes.length === 0) {
return;
}
// TODO@joyceerhl Provide the option to diff files which would be overwritten by edit session contents
if (conflictingChanges.length > 0) {
const yes = localize('resume edit session yes', 'Yes');
const cancel = localize('resume edit session cancel', 'Cancel');
// Allow to show edit sessions
const result = await this.dialogService.show(
Severity.Warning,
conflictingChanges.length > 1 ?
localize('resume edit session warning many', 'Resuming your edit session will overwrite the following {0} files. Do you want to proceed?', conflictingChanges.length) :
localize('resume edit session warning 1', 'Resuming your edit session will overwrite {0}. Do you want to proceed?', basename(conflictingChanges[0].uri)),
[cancel, yes],
{
detail: conflictingChanges.length > 1 ? getFileNamesMessage(conflictingChanges.map((c) => c.uri)) : undefined,
cancelId: 0
});
if (result.choice === 0) {
return;
}
}
for (const { uri, type, contents } of changes) {
if (type === ChangeType.Addition) {
await this.fileService.writeFile(uri, decodeEditSessionFileContent(editSession.version, contents!));
} else if (type === ChangeType.Deletion && await this.fileService.exists(uri)) {
await this.fileService.del(uri);
}
}
this.logService.info(`Deleting edit session with ref ${ref} after successfully applying it to current workspace...`);
await this.editSessionsStorageService.delete(ref);
this.logService.info(`Deleted edit session with ref ${ref}.`);
} catch (ex) {
this.logService.error('Failed to resume edit session, reason: ', (ex as Error).toString());
this.notificationService.error(localize('resume failed', "Failed to resume your edit session."));
}
}
private async generateChanges(editSession: EditSession, ref: string, force = false) {
const changes: ({ uri: URI; type: ChangeType; contents: string | undefined })[] = [];
const conflictingChanges = [];
const workspaceFolders = this.contextService.getWorkspace().folders;
for (const folder of editSession.folders) {
const cancellationTokenSource = new CancellationTokenSource();
let folderRoot: IWorkspaceFolder | undefined;
if (folder.canonicalIdentity) {
// Look for an edit session identifier that we can use
for (const f of workspaceFolders) {
const identity = await this.editSessionIdentityService.getEditSessionIdentifier(f, cancellationTokenSource);
this.logService.info(`Matching identity ${identity} against edit session folder identity ${folder.canonicalIdentity}...`);
if (equals(identity, folder.canonicalIdentity)) {
folderRoot = f;
break;
}
if (identity !== undefined) {
const match = await this.editSessionIdentityService.provideEditSessionIdentityMatch(f, identity, folder.canonicalIdentity, cancellationTokenSource);
if (match === EditSessionIdentityMatch.Complete) {
folderRoot = f;
break;
} else if (match === EditSessionIdentityMatch.Partial &&
this.configurationService.getValue('workbench.experimental.editSessions.partialMatches.enabled') === true
) {
if (!force) {
// Surface partially matching edit session
this.notificationService.prompt(
Severity.Info,
localize('editSessionPartialMatch', 'You have a pending edit session for this workspace. Would you like to resume it?'),
[{ label: localize('resume', 'Resume'), run: () => this.resumeEditSession(ref, false, true) }]
);
} else {
folderRoot = f;
break;
}
}
}
}
} else {
folderRoot = workspaceFolders.find((f) => f.name === folder.name);
}
if (!folderRoot) {
this.logService.info(`Skipping applying ${folder.workingChanges.length} changes from edit session with ref ${ref} as no matching workspace folder was found.`);
return { changes: [], conflictingChanges: [] };
}
const localChanges = new Set<string>();
for (const repository of this.scmService.repositories) {
if (repository.provider.rootUri !== undefined &&
this.contextService.getWorkspaceFolder(repository.provider.rootUri)?.name === folder.name
) {
const repositoryChanges = this.getChangedResources(repository);
repositoryChanges.forEach((change) => localChanges.add(change.toString()));
}
}
for (const change of folder.workingChanges) {
const uri = joinPath(folderRoot.uri, change.relativeFilePath);
changes.push({ uri, type: change.type, contents: change.contents });
if (await this.willChangeLocalContents(localChanges, uri, change)) {
conflictingChanges.push({ uri, type: change.type, contents: change.contents });
}
}
}
return { changes, conflictingChanges };
}
private async willChangeLocalContents(localChanges: Set<string>, uriWithIncomingChanges: URI, incomingChange: Change) {
if (!localChanges.has(uriWithIncomingChanges.toString())) {
return false;
}
const { contents, type } = incomingChange;
switch (type) {
case (ChangeType.Addition): {
const [originalContents, incomingContents] = await Promise.all([sha1Hex(contents), sha1Hex(encodeBase64((await this.fileService.readFile(uriWithIncomingChanges)).value))]);
return originalContents !== incomingContents;
}
case (ChangeType.Deletion): {
return await this.fileService.exists(uriWithIncomingChanges);
}
default:
throw new Error('Unhandled change type.');
}
}
async storeEditSession(fromStoreCommand: boolean): Promise<string | undefined> {
const folders: Folder[] = [];
let hasEdits = false;
for (const repository of this.scmService.repositories) {
// Look through all resource groups and compute which files were added/modified/deleted
const trackedUris = this.getChangedResources(repository); // A URI might appear in more than one resource group
const workingChanges: Change[] = [];
const { rootUri } = repository.provider;
const workspaceFolder = rootUri ? this.contextService.getWorkspaceFolder(rootUri) : undefined;
let name = workspaceFolder?.name;
for (const uri of trackedUris) {
const workspaceFolder = this.contextService.getWorkspaceFolder(uri);
if (!workspaceFolder) {
this.logService.info(`Skipping working change ${uri.toString()} as no associated workspace folder was found.`);
continue;
}
name = name ?? workspaceFolder.name;
const relativeFilePath = relativePath(workspaceFolder.uri, uri) ?? uri.path;
// Only deal with file contents for now
try {
if (!(await this.fileService.stat(uri)).isFile) {
continue;
}
} catch { }
hasEdits = true;
if (await this.fileService.exists(uri)) {
const contents = encodeBase64((await this.fileService.readFile(uri)).value);
workingChanges.push({ type: ChangeType.Addition, fileType: FileType.File, contents: contents, relativeFilePath: relativeFilePath });
} else {
// Assume it's a deletion
workingChanges.push({ type: ChangeType.Deletion, fileType: FileType.File, contents: undefined, relativeFilePath: relativeFilePath });
}
}
const canonicalIdentity = workspaceFolder ? await this.editSessionIdentityService.getEditSessionIdentifier(workspaceFolder, new CancellationTokenSource()) : undefined;
folders.push({ workingChanges, name: name ?? '', canonicalIdentity: canonicalIdentity ?? undefined });
}
if (!hasEdits) {
this.logService.info('Skipping storing edit session as there are no edits to store.');
if (fromStoreCommand) {
this.notificationService.info(localize('no edits to store', 'Skipped storing edit session as there are no edits to store.'));
}
return undefined;
}
const data: EditSession = { folders, version: 2 };
try {
this.logService.info(`Storing edit session...`);
const ref = await this.editSessionsStorageService.write(data);
this.logService.info(`Stored edit session with ref ${ref}.`);
return ref;
} catch (ex) {
this.logService.error(`Failed to store edit session, reason: `, (ex as Error).toString());
type UploadFailedEvent = { reason: string };
type UploadFailedClassification = {
owner: 'joyceerhl'; comment: 'Reporting when Continue On server request fails.';
reason?: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; isMeasurement: true; comment: 'The reason that the server request failed.' };
};
if (ex instanceof UserDataSyncStoreError) {
switch (ex.code) {
case UserDataSyncErrorCode.TooLarge:
// Uploading a payload can fail due to server size limits
this.telemetryService.publicLog2<UploadFailedEvent, UploadFailedClassification>('editSessions.upload.failed', { reason: 'TooLarge' });
this.notificationService.error(localize('payload too large', 'Your edit session exceeds the size limit and cannot be stored.'));
break;
default:
this.telemetryService.publicLog2<UploadFailedEvent, UploadFailedClassification>('editSessions.upload.failed', { reason: 'unknown' });
this.notificationService.error(localize('payload failed', 'Your edit session cannot be stored.'));
break;
}
}
}
return undefined;
}
private getChangedResources(repository: ISCMRepository) {
return repository.provider.groups.elements.reduce((resources, resourceGroups) => {
resourceGroups.elements.forEach((resource) => resources.add(resource.sourceUri));
return resources;
}, new Set<URI>()); // A URI might appear in more than one resource group
}
private hasEditSession() {
for (const repository of this.scmService.repositories) {
if (this.getChangedResources(repository).size > 0) {
return true;
}
}
return false;
}
private async shouldContinueOnWithEditSession(): Promise<boolean> {
// If the user is already signed in, we should store edit session
if (this.editSessionsStorageService.isSignedIn) {
return this.hasEditSession();
}
// If the user has been asked before and said no, don't use edit sessions
if (this.configurationService.getValue(useEditSessionsWithContinueOn) === 'off') {
return false;
}
// Prompt the user to use edit sessions if they currently could benefit from using it
if (this.hasEditSession()) {
return this.editSessionsStorageService.initialize(true);
}
return false;
}
//#region Continue Edit Session extension contribution point
private registerContributedEditSessionOptions() {
continueEditSessionExtPoint.setHandler(extensions => {
const continueEditSessionOptions: ContinueEditSessionItem[] = [];
for (const extension of extensions) {
if (!isProposedApiEnabled(extension.description, 'contribEditSessions')) {
continue;
}
if (!Array.isArray(extension.value)) {
continue;
}
for (const contribution of extension.value) {
const command = MenuRegistry.getCommand(contribution.command);
if (!command) {
return;
}
const icon = command.icon;
const title = typeof command.title === 'string' ? command.title : command.title.value;
continueEditSessionOptions.push(new ContinueEditSessionItem(
ThemeIcon.isThemeIcon(icon) ? `$(${icon.id}) ${title}` : title,
command.id,
command.source,
ContextKeyExpr.deserialize(contribution.when)
));
}
}
this.continueEditSessionOptions = continueEditSessionOptions;
});
}
private registerContinueInLocalFolderAction(): void {
const that = this;
this._register(registerAction2(class ContinueInLocalFolderAction extends Action2 {
constructor() {
super(openLocalFolderCommand);
}
async run(accessor: ServicesAccessor): Promise<URI | undefined> {
const selection = await that.fileDialogService.showOpenDialog({
title: localize('continueEditSession.openLocalFolder.title', 'Select a local folder to continue your edit session in'),
canSelectFolders: true,
canSelectMany: false,
canSelectFiles: false,
availableFileSystems: [Schemas.file]
});
return selection?.length !== 1 ? undefined : URI.from({
scheme: that.productService.urlProtocol,
authority: Schemas.file,
path: selection[0].path
});
}
}));
}
private async pickContinueEditSessionDestination(): Promise<URI | 'noDestinationUri' | undefined> {
const quickPick = this.quickInputService.createQuickPick<ContinueEditSessionItem>();
const workspaceContext = this.contextService.getWorkbenchState() === WorkbenchState.FOLDER
? this.contextService.getWorkspace().folders[0].name
: this.contextService.getWorkspace().folders.map((folder) => folder.name).join(', ');
quickPick.title = localize('continueEditSessionPick.title', "Continue {0} on", `'${workspaceContext}'`);
quickPick.placeholder = localize('continueEditSessionPick.placeholder', 'Choose how you would like to continue working');
quickPick.items = this.createPickItems();
const command = await new Promise<string | undefined>((resolve, reject) => {
quickPick.onDidHide(() => resolve(undefined));
quickPick.onDidAccept((e) => {
const selection = quickPick.activeItems[0].command;
resolve(selection);
quickPick.hide();
});
quickPick.show();
});
quickPick.dispose();
if (command === undefined) {
return undefined;
}
try {
const uri = await this.commandService.executeCommand(command);
// Some continue on commands do not return a URI
// to support extensions which want to be in control
// of how the destination is opened
if (uri === undefined) { return 'noDestinationUri'; }
return URI.isUri(uri) ? uri : undefined;
} catch (ex) {
return undefined;
}
}
private createPickItems(): ContinueEditSessionItem[] {
const items = [...this.continueEditSessionOptions].filter((option) => option.when === undefined || this.contextKeyService.contextMatchesRules(option.when));
if (getVirtualWorkspaceLocation(this.contextService.getWorkspace()) !== undefined && isNative) {
items.push(new ContinueEditSessionItem(
'$(folder) ' + localize('continueEditSessionItem.openInLocalFolder.v2', 'Open in Local Folder'),
openLocalFolderCommand.id,
localize('continueEditSessionItem.builtin', 'Built-in')
));
}
return items.sort((item1, item2) => item1.label.localeCompare(item2.label));
}
}
class ContinueEditSessionItem implements IQuickPickItem {
constructor(
public readonly label: string,
public readonly command: string,
public readonly description?: string,
public readonly when?: ContextKeyExpression,
) { }
}
interface ICommand {
command: string;
group: string;
when: string;
}
const continueEditSessionExtPoint = ExtensionsRegistry.registerExtensionPoint<ICommand[]>({
extensionPoint: 'continueEditSession',
jsonSchema: {
description: localize('continueEditSessionExtPoint', 'Contributes options for continuing the current edit session in a different environment'),
type: 'array',
items: {
type: 'object',
properties: {
command: {
description: localize('continueEditSessionExtPoint.command', 'Identifier of the command to execute. The command must be declared in the \'commands\'-section and return a URI representing a different environment where the current edit session can be continued.'),
type: 'string'
},
group: {
description: localize('continueEditSessionExtPoint.group', 'Group into which this item belongs.'),
type: 'string'
},
when: {
description: localize('continueEditSessionExtPoint.when', 'Condition which must be true to show this item.'),
type: 'string'
}
},
required: ['command']
}
}
});
//#endregion
const workbenchRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchRegistry.registerWorkbenchContribution(EditSessionsContribution, LifecyclePhase.Restored);
Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration).registerConfiguration({
...workbenchConfigurationNodeBase,
'properties': {
'workbench.experimental.editSessions.autoStore': {
enum: ['onShutdown', 'off'],
enumDescriptions: [
localize('autoStore.onShutdown', "Automatically store current edit session on window close."),
localize('autoStore.off', "Never attempt to automatically store an edit session.")
],
'type': 'string',
'tags': ['experimental', 'usesOnlineServices'],
'default': 'off',
'markdownDescription': localize('autoStore', "Controls whether to automatically store an available edit session for the current workspace."),
},
'workbench.editSessions.autoResume': {
enum: ['onReload', 'off'],
enumDescriptions: [
localize('autoResume.onReload', "Automatically resume available edit session on window reload."),
localize('autoResume.off', "Never attempt to resume an edit session.")
],
'type': 'string',
'tags': ['usesOnlineServices'],
'default': 'onReload',
'markdownDescription': localize('autoResume', "Controls whether to automatically resume an available edit session for the current workspace."),
},
'workbench.editSessions.continueOn': {
enum: ['prompt', 'off'],
enumDescriptions: [
localize('continueOn.promptForAuth', 'Prompt the user to sign in to store edit sessions with Continue Working On.'),
localize('continueOn.off', 'Do not use edit sessions with Continue Working On unless the user has already turned on edit sessions.')
],
type: 'string',
tags: ['usesOnlineServices'],
default: 'prompt',
markdownDescription: localize('continueOn', 'Controls whether to prompt the user to store edit sessions when using Continue Working On.')
},
'workbench.experimental.editSessions.continueOn': {
enum: ['prompt', 'off'],
enumDescriptions: [
localize('continueOn.promptForAuth', 'Prompt the user to sign in to store edit sessions with Continue Working On.'),
localize('continueOn.off', 'Do not use edit sessions with Continue Working On unless the user has already turned on edit sessions.')
],
type: 'string',
tags: ['experimental', 'usesOnlineServices'],
default: 'prompt',
markdownDeprecationMessage: localize('continueOnDeprecated', 'This setting is deprecated in favor of {0}.', '`#workbench.experimental.continueOn#`'),
markdownDescription: localize('continueOn', 'Controls whether to prompt the user to store edit sessions when using Continue Working On.')
},
'workbench.experimental.editSessions.enabled': {
'type': 'boolean',
'tags': ['experimental', 'usesOnlineServices'],
'default': true,
'markdownDeprecationMessage': localize('editSessionsEnabledDeprecated', "This setting is deprecated as Edit Sessions are no longer experimental. Please see {0} and {1} for configuring behavior related to Edit Sessions.", '`#workbench.editSessions.autoResume#`', '`#workbench.editSessions.continueOn#`')
},
'workbench.experimental.editSessions.autoResume': {
enum: ['onReload', 'off'],
enumDescriptions: [
localize('autoResume.onReload', "Automatically resume available edit session on window reload."),
localize('autoResume.off', "Never attempt to resume an edit session.")
],
'type': 'string',
'tags': ['experimental', 'usesOnlineServices'],
'default': 'onReload',
'markdownDeprecationMessage': localize('autoResumeDeprecated', "This setting is deprecated in favor of {0}.", '`#workbench.editSessions.autoResume#`')
},
'workbench.experimental.editSessions.partialMatches.enabled': {
'type': 'boolean',
'tags': ['experimental', 'usesOnlineServices'],
'default': false,
'markdownDescription': localize('editSessionsPartialMatchesEnabled', "Controls whether to surface edit sessions which partially match the current session.")
}
}
});