-
Notifications
You must be signed in to change notification settings - Fork 27.8k
/
workspaces.contribution.ts
134 lines (117 loc) · 6.44 KB
/
workspaces.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { Registry } from 'vs/platform/registry/common/platform';
import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry, IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { hasWorkspaceFileExtension, IWorkspaceContextService, WorkbenchState, WORKSPACE_SUFFIX } from 'vs/platform/workspace/common/workspace';
import { Disposable } from 'vs/base/common/lifecycle';
import { IFileService } from 'vs/platform/files/common/files';
import { INeverShowAgainOptions, INotificationService, NeverShowAgainScope, Severity } from 'vs/platform/notification/common/notification';
import { URI } from 'vs/base/common/uri';
import { isEqual, joinPath } from 'vs/base/common/resources';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { isVirtualWorkspace } from 'vs/platform/workspace/common/virtualWorkspace';
import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { ActiveEditorContext, ResourceContextKey, TemporaryWorkspaceContext } from 'vs/workbench/common/contextkeys';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { TEXT_FILE_EDITOR_ID } from 'vs/workbench/contrib/files/common/files';
/**
* A workbench contribution that will look for `.code-workspace` files in the root of the
* workspace folder and open a notification to suggest to open one of the workspaces.
*/
export class WorkspacesFinderContribution extends Disposable implements IWorkbenchContribution {
constructor(
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@INotificationService private readonly notificationService: INotificationService,
@IFileService private readonly fileService: IFileService,
@IQuickInputService private readonly quickInputService: IQuickInputService,
@IHostService private readonly hostService: IHostService,
@IStorageService private readonly storageService: IStorageService
) {
super();
this.findWorkspaces();
}
private async findWorkspaces(): Promise<void> {
const folder = this.contextService.getWorkspace().folders[0];
if (!folder || this.contextService.getWorkbenchState() !== WorkbenchState.FOLDER || isVirtualWorkspace(this.contextService.getWorkspace())) {
return; // require a single (non virtual) root folder
}
const rootFileNames = (await this.fileService.resolve(folder.uri)).children?.map(child => child.name);
if (Array.isArray(rootFileNames)) {
const workspaceFiles = rootFileNames.filter(hasWorkspaceFileExtension);
if (workspaceFiles.length > 0) {
this.doHandleWorkspaceFiles(folder.uri, workspaceFiles);
}
}
}
private doHandleWorkspaceFiles(folder: URI, workspaces: string[]): void {
const neverShowAgain: INeverShowAgainOptions = { id: 'workspaces.dontPromptToOpen', scope: NeverShowAgainScope.WORKSPACE, isSecondary: true };
// Prompt to open one workspace
if (workspaces.length === 1) {
const workspaceFile = workspaces[0];
this.notificationService.prompt(Severity.Info, localize('workspaceFound', "This folder contains a workspace file '{0}'. Do you want to open it? [Learn more]({1}) about workspace files.", workspaceFile, 'https://go.microsoft.com/fwlink/?linkid=2025315'), [{
label: localize('openWorkspace', "Open Workspace"),
run: () => this.hostService.openWindow([{ workspaceUri: joinPath(folder, workspaceFile) }])
}], {
neverShowAgain,
silent: !this.storageService.isNew(StorageScope.WORKSPACE) // https://github.com/microsoft/vscode/issues/125315
});
}
// Prompt to select a workspace from many
else if (workspaces.length > 1) {
this.notificationService.prompt(Severity.Info, localize('workspacesFound', "This folder contains multiple workspace files. Do you want to open one? [Learn more]({0}) about workspace files.", 'https://go.microsoft.com/fwlink/?linkid=2025315'), [{
label: localize('selectWorkspace', "Select Workspace"),
run: () => {
this.quickInputService.pick(
workspaces.map(workspace => ({ label: workspace } as IQuickPickItem)),
{ placeHolder: localize('selectToOpen', "Select a workspace to open") }).then(pick => {
if (pick) {
this.hostService.openWindow([{ workspaceUri: joinPath(folder, pick.label) }]);
}
});
}
}], {
neverShowAgain,
silent: !this.storageService.isNew(StorageScope.WORKSPACE) // https://github.com/microsoft/vscode/issues/125315
});
}
}
}
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(WorkspacesFinderContribution, 'WorkspacesFinderContribution', LifecyclePhase.Eventually);
// Render "Open Workspace" button in *.code-workspace files
registerAction2(class extends Action2 {
constructor() {
super({
id: 'workbench.action.openWorkspaceFromEditor',
title: { original: 'Open Workspace', value: localize('openWorkspace', "Open Workspace") },
f1: false,
menu: {
id: MenuId.EditorContent,
when: ContextKeyExpr.and(
ResourceContextKey.Extension.isEqualTo(WORKSPACE_SUFFIX),
ActiveEditorContext.isEqualTo(TEXT_FILE_EDITOR_ID),
TemporaryWorkspaceContext.toNegated()
)
}
});
}
async run(accessor: ServicesAccessor, uri: URI): Promise<void> {
const hostService = accessor.get(IHostService);
const contextService = accessor.get(IWorkspaceContextService);
const notificationService = accessor.get(INotificationService);
if (contextService.getWorkbenchState() === WorkbenchState.WORKSPACE) {
const workspaceConfiguration = contextService.getWorkspace().configuration;
if (workspaceConfiguration && isEqual(workspaceConfiguration, uri)) {
notificationService.info(localize('alreadyOpen', "This workspace is already open."));
return; // workspace already opened
}
}
return hostService.openWindow([{ workspaceUri: uri }]);
}
});