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.
/*---------------------------------------------------------------------------------------------
enablement.ts ×17
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable } from '../../../../base/common/lifecycle.js';
import { IObservable, IReader, ITransaction, transaction } from '../../../../base/common/observable.js';
import { ObservableMemento, observableMemento } from '../../../../platform/observable/common/observableMemento.js';
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
export const enum ContributionEnablementState {
DisabledProfile,
DisabledWorkspace,
EnabledProfile,
EnabledWorkspace,
}
export function isContributionEnabled(state: ContributionEnablementState): boolean {
return state === ContributionEnablementState.EnabledProfile || state === ContributionEnablementState.EnabledWorkspace;
enablement.ts ×1
}
export function isContributionDisabled(state: ContributionEnablementState): boolean {
return !isContributionEnabled(state);
}
export interface IEnablementModel {
readEnabled(key: string, reader?: IReader): ContributionEnablementState;
setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void;
remove(key: string): void;
}
type EnablementMap = ReadonlyMap<string, boolean>;
return JSON.stringify([...value]);
}
function mapFromStorage(value: string): EnablementMap {
const parsed = JSON.parse(value);
return new Map(Array.isArray(parsed) ? parsed : []);
}
/**
* A reusable enablement model for string-keyed contributions. Uses
* `observableMemento` to persist enable/disable state in both profile-scoped
* and workspace-scoped storage.
*
* Resolution order: if a workspace-scoped entry exists for a key, it wins.
* Otherwise, the profile-scoped entry is used. The default (absence of any
* entry) is {@link ContributionEnablementState.EnabledProfile}.
*/
export class EnablementModel extends Disposable implements IEnablementModel {
private readonly _profileState: ObservableMemento<EnablementMap>;
private readonly _workspaceState: ObservableMemento<EnablementMap>;
constructor(
@IStorageService storageService: IStorageService,
) {
super();
const mapMemento = observableMemento<EnablementMap>({
key: storageKey,
defaultValue: new Map(),
toStorage: mapToStorage,
fromStorage: mapFromStorage,
});
this._profileState = this._register(
mapMemento(StorageScope.PROFILE, StorageTarget.MACHINE, storageService)
);
this._workspaceState = this._register(
mapMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE, storageService)
);
}
readEnabled(key: string, reader?: IReader): ContributionEnablementState {
}
readEnabledWithWorkspaceKey(profileKey: string, workspaceKey: string | undefined, reader?: IReader): ContributionEnablementState {
if (workspaceKey !== undefined && wsMap.has(workspaceKey)) {
const profileMap = this._profileState.read(reader);
if (profileMap.has(profileKey)) {
? ContributionEnablementState.EnabledProfile
}
return ContributionEnablementState.EnabledProfile;
setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void {
}
setEnabledWithWorkspaceKey(profileKey: string, workspaceKey: string | undefined, state: ContributionEnablementState, tx?: ITransaction): void {
case ContributionEnablementState.EnabledProfile: {
// Enabled-profile is the default: remove key from profile state,
// and also remove any workspace override.
this._deleteFromMap(this._profileState, profileKey, tx);
if (workspaceKey !== undefined) {
this._deleteFromMap(this._workspaceState, workspaceKey, tx);
}
break;
}
this._setInMap(this._profileState, profileKey, false, tx);
if (workspaceKey !== undefined) {
this._deleteFromMap(this._workspaceState, workspaceKey, tx);
}
break;
}
if (workspaceKey === undefined) {
throw new Error('Cannot enable a contribution for a workspace without a workspace key.');
}
break;
}
if (workspaceKey === undefined) {
throw new Error('Cannot disable a contribution for a workspace without a workspace key.');
}
break;
}
}
remove(key: string): void {
this._deleteFromMap(this._workspaceState, key);
}
private _setInMap(memento: ObservableMemento<EnablementMap>, key: string, value: boolean, tx?: ITransaction): void {
if (current.get(key) === value) {
return;
}
next.set(key, value);
memento.set(next, tx);
}
private _deleteFromMap(memento: ObservableMemento<EnablementMap>, key: string, tx?: ITransaction): void {
if (!current.has(key)) {
return;
}
next.delete(key);
memento.set(next, tx);
export class CollisionEnablementModel implements IEnablementModel {
constructor(
private readonly _collisionGroups: IObservable<ReadonlyMap<string, readonly string[]>>,
) { }
readEnabled(key: string, reader?: IReader): ContributionEnablementState {
if (!isContributionEnabled(baseState)) {
}
const group = this._collisionGroups.read(reader).get(key);
if (!group) {
}
for (const otherId of group) {
if (otherId === key) {
return baseState;
}
if (isContributionEnabled(this._base.readEnabled(otherId, reader))) {
}
return baseState;
setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void {
const isEnabling = state === ContributionEnablementState.EnabledProfile || state === ContributionEnablementState.EnabledWorkspace;
enablement.ts ×2
const group = isEnabling ? this._collisionGroups.get().get(key) : undefined;
if (!group) {
return;
}
const updateGroup = (innerTx: ITransaction) => {
this._base.setEnabled(key, state, innerTx);
for (const otherId of group) {
if (otherId !== key) {
this._base.setEnabled(otherId, ContributionEnablementState.DisabledWorkspace, innerTx);
}
}
};
if (tx) {
updateGroup(tx);
transaction(innerTx => updateGroup(innerTx));
}
remove(key: string): void {
}