src/vs/workbench/contrib/chat/common/enablement.ts

227 LOC · 204 covered · 23 uncovered · 63 ranges · 714 concepts · 21 introducers · 418 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- enablement.ts ×17
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 { Disposable } from '../../../../base/common/lifecycle.js';
7 > import { IObservable, IReader, ITransaction, transaction } from '../../../../base/common/observable.js';
8 > import { ObservableMemento, observableMemento } from '../../../../platform/observable/common/observableMemento.js';
9 > import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
10 >
11 > export const enum ContributionEnablementState {
12 > DisabledProfile,
13 > DisabledWorkspace,
14 > EnabledProfile,
15 > EnabledWorkspace,
16 > }
17 >
18 > export function isContributionEnabled(state: ContributionEnablementState): boolean {
19 > return state === ContributionEnablementState.EnabledProfile || state === ContributionEnablementState.EnabledWorkspace; enablement.ts ×1
20 > }
22 > export function isContributionDisabled(state: ContributionEnablementState): boolean {
23 return !isContributionEnabled(state);
24 }
26 > export interface IEnablementModel {
27 > readEnabled(key: string, reader?: IReader): ContributionEnablementState;
28 > setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void;
29 > remove(key: string): void;
30 > }
31 >
32 > type EnablementMap = ReadonlyMap<string, boolean>;
33 >
34 > function mapToStorage(value: EnablementMap): string { enablement.ts ×9
35 > return JSON.stringify([...value]);
36 > }
38 function mapFromStorage(value: string): EnablementMap {
39 const parsed = JSON.parse(value);
40 return new Map(Array.isArray(parsed) ? parsed : []);
41 }
43 > /**
44 > * A reusable enablement model for string-keyed contributions. Uses
45 > * `observableMemento` to persist enable/disable state in both profile-scoped
46 > * and workspace-scoped storage.
47 > *
48 > * Resolution order: if a workspace-scoped entry exists for a key, it wins.
49 > * Otherwise, the profile-scoped entry is used. The default (absence of any
50 > * entry) is {@link ContributionEnablementState.EnabledProfile}.
51 > */
52 > export class EnablementModel extends Disposable implements IEnablementModel {
53 > private readonly _profileState: ObservableMemento<EnablementMap>;
54 > private readonly _workspaceState: ObservableMemento<EnablementMap>;
55 >
56 > constructor(
57 > storageKey: string, mcpService.ts ×2
58 > @IStorageService storageService: IStorageService,
59 > ) {
60 > super();
61 >
62 > const mapMemento = observableMemento<EnablementMap>({
63 > key: storageKey,
64 > defaultValue: new Map(),
65 > toStorage: mapToStorage,
66 > fromStorage: mapFromStorage,
67 > });
68 >
69 > this._profileState = this._register(
70 > mapMemento(StorageScope.PROFILE, StorageTarget.MACHINE, storageService)
71 > );
72 >
73 > this._workspaceState = this._register(
74 > mapMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE, storageService)
75 > );
76 > }
78 > readEnabled(key: string, reader?: IReader): ContributionEnablementState {
79 > return this.readEnabledWithWorkspaceKey(key, key, reader); enablement.ts ×3
80 > }
82 > readEnabledWithWorkspaceKey(profileKey: string, workspaceKey: string | undefined, reader?: IReader): ContributionEnablementState {
83 > const wsMap = this._workspaceState.read(reader); enablement.ts ×3
84 > if (workspaceKey !== undefined && wsMap.has(workspaceKey)) {
85 > return wsMap.get(workspaceKey)! enablement.ts ×4
86 > ? ContributionEnablementState.EnabledWorkspace enablement.ts ×1
87 > : ContributionEnablementState.DisabledWorkspace; enablement.ts ×3
90 > const profileMap = this._profileState.read(reader);
91 > if (profileMap.has(profileKey)) {
92 > return profileMap.get(profileKey)! enablement.ts ×3
93 ? ContributionEnablementState.EnabledProfile
94 > : ContributionEnablementState.DisabledProfile; enablement.ts ×3
95 > }
97 > return ContributionEnablementState.EnabledProfile;
100 > setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void {
101 > this.setEnabledWithWorkspaceKey(key, key, state, tx); enablement.ts ×9
102 > }
104 > setEnabledWithWorkspaceKey(profileKey: string, workspaceKey: string | undefined, state: ContributionEnablementState, tx?: ITransaction): void {
105 > switch (state) { enablement.ts ×9
106 > case ContributionEnablementState.EnabledProfile: {
107 // Enabled-profile is the default: remove key from profile state,
108 // and also remove any workspace override.
109 this._deleteFromMap(this._profileState, profileKey, tx);
110 if (workspaceKey !== undefined) {
111 this._deleteFromMap(this._workspaceState, workspaceKey, tx);
112 }
113 break;
114 }
115 > case ContributionEnablementState.DisabledProfile: { enablement.ts ×9
116 > // Store disabled in profile, remove workspace override. enablement.ts ×3
117 > this._setInMap(this._profileState, profileKey, false, tx);
118 > if (workspaceKey !== undefined) {
119 > this._deleteFromMap(this._workspaceState, workspaceKey, tx);
120 > }
121 > break;
122 > }
123 > case ContributionEnablementState.EnabledWorkspace: { enablement.ts ×9
124 > // Workspace override: always store explicitly. enablement.ts ×4
125 > if (workspaceKey === undefined) {
126 throw new Error('Cannot enable a contribution for a workspace without a workspace key.');
127 }
128 > this._setInMap(this._workspaceState, workspaceKey, true, tx); enablement.ts ×4
129 > break;
130 > }
131 > case ContributionEnablementState.DisabledWorkspace: { enablement.ts ×9
132 > // Workspace override: always store explicitly. enablement.ts ×3
133 > if (workspaceKey === undefined) {
134 throw new Error('Cannot disable a contribution for a workspace without a workspace key.');
135 }
136 > this._setInMap(this._workspaceState, workspaceKey, false, tx); enablement.ts ×3
137 > break;
138 > }
140 >
141 > }
143 > remove(key: string): void {
144 > this._deleteFromMap(this._profileState, key); enablement.ts ×3
145 > this._deleteFromMap(this._workspaceState, key);
146 > }
148 > private _setInMap(memento: ObservableMemento<EnablementMap>, key: string, value: boolean, tx?: ITransaction): void {
149 > const current = memento.get(); enablement.ts ×9
150 > if (current.get(key) === value) {
151 return;
152 }
153 > const next = new Map(current); enablement.ts ×9
154 > next.set(key, value);
155 > memento.set(next, tx);
156 > }
158 > private _deleteFromMap(memento: ObservableMemento<EnablementMap>, key: string, tx?: ITransaction): void {
159 > const current = memento.get(); enablement.ts ×2
160 > if (!current.has(key)) {
161 > return;
162 > }
163 > const next = new Map(current); enablement.ts ×3
164 > next.delete(key);
165 > memento.set(next, tx);
168 >
169 > export class CollisionEnablementModel implements IEnablementModel {
170 >
171 > constructor(
172 > private readonly _base: IEnablementModel, enablement.ts ×1
173 > private readonly _collisionGroups: IObservable<ReadonlyMap<string, readonly string[]>>,
174 > ) { }
176 > readEnabled(key: string, reader?: IReader): ContributionEnablementState {
177 > const baseState = this._base.readEnabled(key, reader); enablement.ts ×3
178 >
179 > if (!isContributionEnabled(baseState)) {
180 > return baseState; enablement.ts ×1
181 > }
183 > const group = this._collisionGroups.read(reader).get(key);
184 > if (!group) {
185 > return baseState; enablement.ts ×1
186 > }
188 > for (const otherId of group) {
189 > if (otherId === key) {
190 > return baseState;
191 > }
192 > if (isContributionEnabled(this._base.readEnabled(otherId, reader))) {
193 > return ContributionEnablementState.DisabledProfile; enablement.ts ×1
194 > }
196 return baseState;
199 > setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void {
200 > const isEnabling = state === ContributionEnablementState.EnabledProfile || state === ContributionEnablementState.EnabledWorkspace; enablement.ts ×2
201 > const group = isEnabling ? this._collisionGroups.get().get(key) : undefined;
202 >
203 > if (!group) {
204 > this._base.setEnabled(key, state, tx); enablement.ts ×1
205 > return;
206 > }
208 > const updateGroup = (innerTx: ITransaction) => {
209 > this._base.setEnabled(key, state, innerTx);
210 > for (const otherId of group) {
211 > if (otherId !== key) {
212 > this._base.setEnabled(otherId, ContributionEnablementState.DisabledWorkspace, innerTx);
213 > }
214 > }
215 > };
216 >
217 > if (tx) {
218 updateGroup(tx);
219 > } else { enablement.ts ×2
220 > transaction(innerTx => updateGroup(innerTx));
221 > }
224 > remove(key: string): void {
225 > this._base.remove(key); enablement.ts ×3
226 > }