Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/common/contextkeys.ts
3291 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 { DisposableStore } from '../../base/common/lifecycle.js';
7
import { URI } from '../../base/common/uri.js';
8
import { localize } from '../../nls.js';
9
import { IContextKeyService, IContextKey, RawContextKey } from '../../platform/contextkey/common/contextkey.js';
10
import { basename, dirname, extname, isEqual } from '../../base/common/resources.js';
11
import { ILanguageService } from '../../editor/common/languages/language.js';
12
import { IFileService } from '../../platform/files/common/files.js';
13
import { IModelService } from '../../editor/common/services/model.js';
14
import { Schemas } from '../../base/common/network.js';
15
import { EditorInput } from './editor/editorInput.js';
16
import { IEditorResolverService } from '../services/editor/common/editorResolverService.js';
17
import { DEFAULT_EDITOR_ASSOCIATION } from './editor.js';
18
import { DiffEditorInput } from './editor/diffEditorInput.js';
19
20
//#region < --- Workbench --- >
21
22
export const WorkbenchStateContext = new RawContextKey<string>('workbenchState', undefined, { type: 'string', description: localize('workbenchState', "The kind of workspace opened in the window, either 'empty' (no workspace), 'folder' (single folder) or 'workspace' (multi-root workspace)") });
23
export const WorkspaceFolderCountContext = new RawContextKey<number>('workspaceFolderCount', 0, localize('workspaceFolderCount', "The number of root folders in the workspace"));
24
25
export const OpenFolderWorkspaceSupportContext = new RawContextKey<boolean>('openFolderWorkspaceSupport', true, true);
26
export const EnterMultiRootWorkspaceSupportContext = new RawContextKey<boolean>('enterMultiRootWorkspaceSupport', true, true);
27
export const EmptyWorkspaceSupportContext = new RawContextKey<boolean>('emptyWorkspaceSupport', true, true);
28
29
export const DirtyWorkingCopiesContext = new RawContextKey<boolean>('dirtyWorkingCopies', false, localize('dirtyWorkingCopies', "Whether there are any working copies with unsaved changes"));
30
31
export const RemoteNameContext = new RawContextKey<string>('remoteName', '', localize('remoteName', "The name of the remote the window is connected to or an empty string if not connected to any remote"));
32
33
export const VirtualWorkspaceContext = new RawContextKey<string>('virtualWorkspace', '', localize('virtualWorkspace', "The scheme of the current workspace is from a virtual file system or an empty string."));
34
export const TemporaryWorkspaceContext = new RawContextKey<boolean>('temporaryWorkspace', false, localize('temporaryWorkspace', "The scheme of the current workspace is from a temporary file system."));
35
36
export const HasWebFileSystemAccess = new RawContextKey<boolean>('hasWebFileSystemAccess', false, true); // Support for FileSystemAccess web APIs (https://wicg.github.io/file-system-access)
37
38
export const EmbedderIdentifierContext = new RawContextKey<string | undefined>('embedderIdentifier', undefined, localize('embedderIdentifier', 'The identifier of the embedder according to the product service, if one is defined'));
39
40
export const InAutomationContext = new RawContextKey<boolean>('inAutomation', false, localize('inAutomation', "Whether VS Code is running under automation/smoke test"));
41
42
//#endregion
43
44
//#region < --- Window --- >
45
46
export const IsMainWindowFullscreenContext = new RawContextKey<boolean>('isFullscreen', false, localize('isFullscreen', "Whether the main window is in fullscreen mode"));
47
export const IsAuxiliaryWindowFocusedContext = new RawContextKey<boolean>('isAuxiliaryWindowFocusedContext', false, localize('isAuxiliaryWindowFocusedContext', "Whether an auxiliary window is focused"));
48
49
export const IsWindowAlwaysOnTopContext = new RawContextKey<boolean>('isWindowAlwaysOnTop', false, localize('isWindowAlwaysOnTop', "Whether the window is always on top"));
50
51
export const IsAuxiliaryWindowContext = new RawContextKey<boolean>('isAuxiliaryWindow', false, localize('isAuxiliaryWindow', "Window is an auxiliary window"));
52
53
54
//#endregion
55
56
57
//#region < --- Editor --- >
58
59
// Editor State Context Keys
60
export const ActiveEditorDirtyContext = new RawContextKey<boolean>('activeEditorIsDirty', false, localize('activeEditorIsDirty', "Whether the active editor has unsaved changes"));
61
export const ActiveEditorPinnedContext = new RawContextKey<boolean>('activeEditorIsNotPreview', false, localize('activeEditorIsNotPreview', "Whether the active editor is not in preview mode"));
62
export const ActiveEditorFirstInGroupContext = new RawContextKey<boolean>('activeEditorIsFirstInGroup', false, localize('activeEditorIsFirstInGroup', "Whether the active editor is the first one in its group"));
63
export const ActiveEditorLastInGroupContext = new RawContextKey<boolean>('activeEditorIsLastInGroup', false, localize('activeEditorIsLastInGroup', "Whether the active editor is the last one in its group"));
64
export const ActiveEditorStickyContext = new RawContextKey<boolean>('activeEditorIsPinned', false, localize('activeEditorIsPinned', "Whether the active editor is pinned"));
65
export const ActiveEditorReadonlyContext = new RawContextKey<boolean>('activeEditorIsReadonly', false, localize('activeEditorIsReadonly', "Whether the active editor is read-only"));
66
export const ActiveCompareEditorCanSwapContext = new RawContextKey<boolean>('activeCompareEditorCanSwap', false, localize('activeCompareEditorCanSwap', "Whether the active compare editor can swap sides"));
67
export const ActiveEditorCanToggleReadonlyContext = new RawContextKey<boolean>('activeEditorCanToggleReadonly', true, localize('activeEditorCanToggleReadonly', "Whether the active editor can toggle between being read-only or writeable"));
68
export const ActiveEditorCanRevertContext = new RawContextKey<boolean>('activeEditorCanRevert', false, localize('activeEditorCanRevert', "Whether the active editor can revert"));
69
export const ActiveEditorCanSplitInGroupContext = new RawContextKey<boolean>('activeEditorCanSplitInGroup', true);
70
71
// Editor Kind Context Keys
72
export const ActiveEditorContext = new RawContextKey<string | null>('activeEditor', null, { type: 'string', description: localize('activeEditor', "The identifier of the active editor") });
73
export const ActiveEditorAvailableEditorIdsContext = new RawContextKey<string>('activeEditorAvailableEditorIds', '', localize('activeEditorAvailableEditorIds', "The available editor identifiers that are usable for the active editor"));
74
export const TextCompareEditorVisibleContext = new RawContextKey<boolean>('textCompareEditorVisible', false, localize('textCompareEditorVisible', "Whether a text compare editor is visible"));
75
export const TextCompareEditorActiveContext = new RawContextKey<boolean>('textCompareEditorActive', false, localize('textCompareEditorActive', "Whether a text compare editor is active"));
76
export const SideBySideEditorActiveContext = new RawContextKey<boolean>('sideBySideEditorActive', false, localize('sideBySideEditorActive', "Whether a side by side editor is active"));
77
78
// Editor Group Context Keys
79
export const EditorGroupEditorsCountContext = new RawContextKey<number>('groupEditorsCount', 0, localize('groupEditorsCount', "The number of opened editor groups"));
80
export const ActiveEditorGroupEmptyContext = new RawContextKey<boolean>('activeEditorGroupEmpty', false, localize('activeEditorGroupEmpty', "Whether the active editor group is empty"));
81
export const ActiveEditorGroupIndexContext = new RawContextKey<number>('activeEditorGroupIndex', 0, localize('activeEditorGroupIndex', "The index of the active editor group"));
82
export const ActiveEditorGroupLastContext = new RawContextKey<boolean>('activeEditorGroupLast', false, localize('activeEditorGroupLast', "Whether the active editor group is the last group"));
83
export const ActiveEditorGroupLockedContext = new RawContextKey<boolean>('activeEditorGroupLocked', false, localize('activeEditorGroupLocked', "Whether the active editor group is locked"));
84
export const MultipleEditorGroupsContext = new RawContextKey<boolean>('multipleEditorGroups', false, localize('multipleEditorGroups', "Whether there are multiple editor groups opened"));
85
export const SingleEditorGroupsContext = MultipleEditorGroupsContext.toNegated();
86
export const MultipleEditorsSelectedInGroupContext = new RawContextKey<boolean>('multipleEditorsSelectedInGroup', false, localize('multipleEditorsSelectedInGroup', "Whether multiple editors have been selected in an editor group"));
87
export const TwoEditorsSelectedInGroupContext = new RawContextKey<boolean>('twoEditorsSelectedInGroup', false, localize('twoEditorsSelectedInGroup', "Whether exactly two editors have been selected in an editor group"));
88
export const SelectedEditorsInGroupFileOrUntitledResourceContextKey = new RawContextKey<boolean>('SelectedEditorsInGroupFileOrUntitledResourceContextKey', true, localize('SelectedEditorsInGroupFileOrUntitledResourceContextKey', "Whether all selected editors in a group have a file or untitled resource associated"));
89
90
// Editor Part Context Keys
91
export const EditorPartMultipleEditorGroupsContext = new RawContextKey<boolean>('editorPartMultipleEditorGroups', false, localize('editorPartMultipleEditorGroups', "Whether there are multiple editor groups opened in an editor part"));
92
export const EditorPartSingleEditorGroupsContext = EditorPartMultipleEditorGroupsContext.toNegated();
93
export const EditorPartMaximizedEditorGroupContext = new RawContextKey<boolean>('editorPartMaximizedEditorGroup', false, localize('editorPartEditorGroupMaximized', "Editor Part has a maximized group"));
94
95
// Editor Layout Context Keys
96
export const EditorsVisibleContext = new RawContextKey<boolean>('editorIsOpen', false, localize('editorIsOpen', "Whether an editor is open"));
97
export const InEditorZenModeContext = new RawContextKey<boolean>('inZenMode', false, localize('inZenMode', "Whether Zen mode is enabled"));
98
export const IsMainEditorCenteredLayoutContext = new RawContextKey<boolean>('isCenteredLayout', false, localize('isMainEditorCenteredLayout', "Whether centered layout is enabled for the main editor"));
99
export const SplitEditorsVertically = new RawContextKey<boolean>('splitEditorsVertically', false, localize('splitEditorsVertically', "Whether editors split vertically"));
100
export const MainEditorAreaVisibleContext = new RawContextKey<boolean>('mainEditorAreaVisible', true, localize('mainEditorAreaVisible', "Whether the editor area in the main window is visible"));
101
export const EditorTabsVisibleContext = new RawContextKey<boolean>('editorTabsVisible', true, localize('editorTabsVisible', "Whether editor tabs are visible"));
102
103
//#endregion
104
105
106
//#region < --- Side Bar --- >
107
108
export const SideBarVisibleContext = new RawContextKey<boolean>('sideBarVisible', false, localize('sideBarVisible', "Whether the sidebar is visible"));
109
export const SidebarFocusContext = new RawContextKey<boolean>('sideBarFocus', false, localize('sideBarFocus', "Whether the sidebar has keyboard focus"));
110
export const ActiveViewletContext = new RawContextKey<string>('activeViewlet', '', localize('activeViewlet', "The identifier of the active viewlet"));
111
112
//#endregion
113
114
115
//#region < --- Status Bar --- >
116
117
export const StatusBarFocused = new RawContextKey<boolean>('statusBarFocused', false, localize('statusBarFocused', "Whether the status bar has keyboard focus"));
118
119
//#endregion
120
121
//#region < --- Title Bar --- >
122
123
export const TitleBarStyleContext = new RawContextKey<string>('titleBarStyle', 'custom', localize('titleBarStyle', "Style of the window title bar"));
124
export const TitleBarVisibleContext = new RawContextKey<boolean>('titleBarVisible', false, localize('titleBarVisible', "Whether the title bar is visible"));
125
export const IsCompactTitleBarContext = new RawContextKey<boolean>('isCompactTitleBar', false, localize('isCompactTitleBar', "Title bar is in compact mode"));
126
127
//#endregion
128
129
130
//#region < --- Banner --- >
131
132
export const BannerFocused = new RawContextKey<boolean>('bannerFocused', false, localize('bannerFocused', "Whether the banner has keyboard focus"));
133
134
//#endregion
135
136
137
//#region < --- Notifications --- >
138
139
export const NotificationFocusedContext = new RawContextKey<boolean>('notificationFocus', true, localize('notificationFocus', "Whether a notification has keyboard focus"));
140
export const NotificationsCenterVisibleContext = new RawContextKey<boolean>('notificationCenterVisible', false, localize('notificationCenterVisible', "Whether the notifications center is visible"));
141
export const NotificationsToastsVisibleContext = new RawContextKey<boolean>('notificationToastsVisible', false, localize('notificationToastsVisible', "Whether a notification toast is visible"));
142
143
//#endregion
144
145
146
//#region < --- Auxiliary Bar --- >
147
148
export const ActiveAuxiliaryContext = new RawContextKey<string>('activeAuxiliary', '', localize('activeAuxiliary', "The identifier of the active auxiliary panel"));
149
export const AuxiliaryBarFocusContext = new RawContextKey<boolean>('auxiliaryBarFocus', false, localize('auxiliaryBarFocus', "Whether the auxiliary bar has keyboard focus"));
150
export const AuxiliaryBarVisibleContext = new RawContextKey<boolean>('auxiliaryBarVisible', false, localize('auxiliaryBarVisible', "Whether the auxiliary bar is visible"));
151
export const AuxiliaryBarMaximizedContext = new RawContextKey<boolean>('auxiliaryBarMaximized', false, localize('auxiliaryBarMaximized', "Whether the auxiliary bar is maximized"));
152
153
//#endregion
154
155
156
//#region < --- Panel --- >
157
158
export const ActivePanelContext = new RawContextKey<string>('activePanel', '', localize('activePanel', "The identifier of the active panel"));
159
export const PanelFocusContext = new RawContextKey<boolean>('panelFocus', false, localize('panelFocus', "Whether the panel has keyboard focus"));
160
export const PanelPositionContext = new RawContextKey<string>('panelPosition', 'bottom', localize('panelPosition', "The position of the panel, always 'bottom'"));
161
export const PanelAlignmentContext = new RawContextKey<string>('panelAlignment', 'center', localize('panelAlignment', "The alignment of the panel, either 'center', 'left', 'right' or 'justify'"));
162
export const PanelVisibleContext = new RawContextKey<boolean>('panelVisible', false, localize('panelVisible', "Whether the panel is visible"));
163
export const PanelMaximizedContext = new RawContextKey<boolean>('panelMaximized', false, localize('panelMaximized', "Whether the panel is maximized"));
164
165
//#endregion
166
167
168
//#region < --- Views --- >
169
170
export const FocusedViewContext = new RawContextKey<string>('focusedView', '', localize('focusedView', "The identifier of the view that has keyboard focus"));
171
export function getVisbileViewContextKey(viewId: string): string { return `view.${viewId}.visible`; }
172
173
//#endregion
174
175
176
//#region < --- Resources --- >
177
178
export class ResourceContextKey {
179
180
// NOTE: DO NOT CHANGE THE DEFAULT VALUE TO ANYTHING BUT
181
// UNDEFINED! IT IS IMPORTANT THAT DEFAULTS ARE INHERITED
182
// FROM THE PARENT CONTEXT AND ONLY UNDEFINED DOES THIS
183
184
static readonly Scheme = new RawContextKey<string>('resourceScheme', undefined, { type: 'string', description: localize('resourceScheme', "The scheme of the resource") });
185
static readonly Filename = new RawContextKey<string>('resourceFilename', undefined, { type: 'string', description: localize('resourceFilename', "The file name of the resource") });
186
static readonly Dirname = new RawContextKey<string>('resourceDirname', undefined, { type: 'string', description: localize('resourceDirname', "The folder name the resource is contained in") });
187
static readonly Path = new RawContextKey<string>('resourcePath', undefined, { type: 'string', description: localize('resourcePath', "The full path of the resource") });
188
static readonly LangId = new RawContextKey<string>('resourceLangId', undefined, { type: 'string', description: localize('resourceLangId', "The language identifier of the resource") });
189
static readonly Resource = new RawContextKey<string>('resource', undefined, { type: 'URI', description: localize('resource', "The full value of the resource including scheme and path") });
190
static readonly Extension = new RawContextKey<string>('resourceExtname', undefined, { type: 'string', description: localize('resourceExtname', "The extension name of the resource") });
191
static readonly HasResource = new RawContextKey<boolean>('resourceSet', undefined, { type: 'boolean', description: localize('resourceSet', "Whether a resource is present or not") });
192
static readonly IsFileSystemResource = new RawContextKey<boolean>('isFileSystemResource', undefined, { type: 'boolean', description: localize('isFileSystemResource', "Whether the resource is backed by a file system provider") });
193
194
private readonly _disposables = new DisposableStore();
195
196
private _value: URI | undefined;
197
private readonly _resourceKey: IContextKey<string | null>;
198
private readonly _schemeKey: IContextKey<string | null>;
199
private readonly _filenameKey: IContextKey<string | null>;
200
private readonly _dirnameKey: IContextKey<string | null>;
201
private readonly _pathKey: IContextKey<string | null>;
202
private readonly _langIdKey: IContextKey<string | null>;
203
private readonly _extensionKey: IContextKey<string | null>;
204
private readonly _hasResource: IContextKey<boolean>;
205
private readonly _isFileSystemResource: IContextKey<boolean>;
206
207
constructor(
208
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
209
@IFileService private readonly _fileService: IFileService,
210
@ILanguageService private readonly _languageService: ILanguageService,
211
@IModelService private readonly _modelService: IModelService
212
) {
213
this._schemeKey = ResourceContextKey.Scheme.bindTo(this._contextKeyService);
214
this._filenameKey = ResourceContextKey.Filename.bindTo(this._contextKeyService);
215
this._dirnameKey = ResourceContextKey.Dirname.bindTo(this._contextKeyService);
216
this._pathKey = ResourceContextKey.Path.bindTo(this._contextKeyService);
217
this._langIdKey = ResourceContextKey.LangId.bindTo(this._contextKeyService);
218
this._resourceKey = ResourceContextKey.Resource.bindTo(this._contextKeyService);
219
this._extensionKey = ResourceContextKey.Extension.bindTo(this._contextKeyService);
220
this._hasResource = ResourceContextKey.HasResource.bindTo(this._contextKeyService);
221
this._isFileSystemResource = ResourceContextKey.IsFileSystemResource.bindTo(this._contextKeyService);
222
223
this._disposables.add(_fileService.onDidChangeFileSystemProviderRegistrations(() => {
224
const resource = this.get();
225
this._isFileSystemResource.set(Boolean(resource && _fileService.hasProvider(resource)));
226
}));
227
228
this._disposables.add(_modelService.onModelAdded(model => {
229
if (isEqual(model.uri, this.get())) {
230
this._setLangId();
231
}
232
}));
233
this._disposables.add(_modelService.onModelLanguageChanged(e => {
234
if (isEqual(e.model.uri, this.get())) {
235
this._setLangId();
236
}
237
}));
238
}
239
240
dispose(): void {
241
this._disposables.dispose();
242
}
243
244
private _setLangId(): void {
245
const value = this.get();
246
if (!value) {
247
this._langIdKey.set(null);
248
return;
249
}
250
const langId = this._modelService.getModel(value)?.getLanguageId() ?? this._languageService.guessLanguageIdByFilepathOrFirstLine(value);
251
this._langIdKey.set(langId);
252
}
253
254
set(value: URI | null | undefined) {
255
value = value ?? undefined;
256
if (isEqual(this._value, value)) {
257
return;
258
}
259
this._value = value;
260
this._contextKeyService.bufferChangeEvents(() => {
261
this._resourceKey.set(value ? value.toString() : null);
262
this._schemeKey.set(value ? value.scheme : null);
263
this._filenameKey.set(value ? basename(value) : null);
264
this._dirnameKey.set(value ? this.uriToPath(dirname(value)) : null);
265
this._pathKey.set(value ? this.uriToPath(value) : null);
266
this._setLangId();
267
this._extensionKey.set(value ? extname(value) : null);
268
this._hasResource.set(Boolean(value));
269
this._isFileSystemResource.set(value ? this._fileService.hasProvider(value) : false);
270
});
271
}
272
273
private uriToPath(uri: URI): string {
274
if (uri.scheme === Schemas.file) {
275
return uri.fsPath;
276
}
277
278
return uri.path;
279
}
280
281
reset(): void {
282
this._value = undefined;
283
this._contextKeyService.bufferChangeEvents(() => {
284
this._resourceKey.reset();
285
this._schemeKey.reset();
286
this._filenameKey.reset();
287
this._dirnameKey.reset();
288
this._pathKey.reset();
289
this._langIdKey.reset();
290
this._extensionKey.reset();
291
this._hasResource.reset();
292
this._isFileSystemResource.reset();
293
});
294
}
295
296
get(): URI | undefined {
297
return this._value;
298
}
299
}
300
301
//#endregion
302
303
export function applyAvailableEditorIds(contextKey: IContextKey<string>, editor: EditorInput | undefined | null, editorResolverService: IEditorResolverService): void {
304
if (!editor) {
305
contextKey.set('');
306
return;
307
}
308
309
const editors = getAvailableEditorIds(editor, editorResolverService);
310
contextKey.set(editors.join(','));
311
}
312
313
function getAvailableEditorIds(editor: EditorInput, editorResolverService: IEditorResolverService): string[] {
314
// Non text editor untitled files cannot be easily serialized between
315
// extensions so instead we disable this context key to prevent common
316
// commands that act on the active editor.
317
if (editor.resource?.scheme === Schemas.untitled && editor.editorId !== DEFAULT_EDITOR_ASSOCIATION.id) {
318
return [];
319
}
320
321
// Diff editors. The original and modified resources of a diff editor
322
// *should* be the same, but calculate the set intersection just to be safe.
323
if (editor instanceof DiffEditorInput) {
324
const original = getAvailableEditorIds(editor.original, editorResolverService);
325
const modified = new Set(getAvailableEditorIds(editor.modified, editorResolverService));
326
return original.filter(editor => modified.has(editor));
327
}
328
329
// Normal editors.
330
if (editor.resource) {
331
return editorResolverService.getEditors(editor.resource).map(editor => editor.id);
332
}
333
334
return [];
335
}
336
337