Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/enablementActions.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Action, IAction } from '../../../../base/common/actions.js';
7
import { localize } from '../../../../nls.js';
8
import { IWorkspaceContextService, WorkbenchState } from '../../../../platform/workspace/common/workspace.js';
9
import { ContributionEnablementState, IEnablementModel, isContributionDisabled } from '../common/enablement.js';
10
11
/**
12
* Creates the four standard enablement actions (Enable, Enable Workspace,
13
* Disable, Disable Workspace) for a contribution identified by a string key.
14
*/
15
export function createEnablementActions(
16
key: string,
17
enablementModel: IEnablementModel,
18
idPrefix: string,
19
): [enable: Action, enableWorkspace: Action, disable: Action, disableWorkspace: Action] {
20
return [
21
new Action(`${idPrefix}.enable`, localize('enable', "Enable"), undefined, true,
22
() => { enablementModel.setEnabled(key, ContributionEnablementState.EnabledProfile); return Promise.resolve(); }),
23
new Action(`${idPrefix}.enableForWorkspace`, localize('enableForWorkspace', "Enable (Workspace)"), undefined, true,
24
() => { enablementModel.setEnabled(key, ContributionEnablementState.EnabledWorkspace); return Promise.resolve(); }),
25
new Action(`${idPrefix}.disable`, localize('disable', "Disable"), undefined, true,
26
() => { enablementModel.setEnabled(key, ContributionEnablementState.DisabledProfile); return Promise.resolve(); }),
27
new Action(`${idPrefix}.disableForWorkspace`, localize('disableForWorkspace', "Disable (Workspace)"), undefined, true,
28
() => { enablementModel.setEnabled(key, ContributionEnablementState.DisabledWorkspace); return Promise.resolve(); }),
29
];
30
}
31
32
/**
33
* Builds the standard enablement context-menu action group for a
34
* contribution. Returns either the enable or disable actions depending
35
* on the current state, with workspace variants included only when a
36
* workspace is open.
37
*/
38
export function buildEnablementContextMenuGroup(
39
enablementState: ContributionEnablementState,
40
key: string,
41
enablementModel: IEnablementModel,
42
workspaceContextService: IWorkspaceContextService,
43
idPrefix: string,
44
): IAction[] {
45
const hasWorkspace = workspaceContextService.getWorkbenchState() !== WorkbenchState.EMPTY;
46
const [enable, enableWorkspace, disable, disableWorkspace] = createEnablementActions(key, enablementModel, idPrefix);
47
const actions: IAction[] = [];
48
if (isContributionDisabled(enablementState)) {
49
actions.push(enable);
50
if (hasWorkspace) {
51
actions.push(enableWorkspace);
52
}
53
} else {
54
actions.push(disable);
55
if (hasWorkspace) {
56
actions.push(disableWorkspace);
57
}
58
}
59
return actions;
60
}
61
62