Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/actions/common/menusExtensionPoint.ts
5221 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 { localize } from '../../../../nls.js';
7
import { isFalsyOrWhitespace } from '../../../../base/common/strings.js';
8
import * as resources from '../../../../base/common/resources.js';
9
import { IJSONSchema } from '../../../../base/common/jsonSchema.js';
10
import { IExtensionPointUser, ExtensionMessageCollector, ExtensionsRegistry } from '../../extensions/common/extensionsRegistry.js';
11
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
12
import { MenuId, MenuRegistry, IMenuItem, ISubmenuItem } from '../../../../platform/actions/common/actions.js';
13
import { URI } from '../../../../base/common/uri.js';
14
import { Disposable, DisposableStore } from '../../../../base/common/lifecycle.js';
15
import { ThemeIcon } from '../../../../base/common/themables.js';
16
import { index } from '../../../../base/common/arrays.js';
17
import { isProposedApiEnabled } from '../../extensions/common/extensions.js';
18
import { ILocalizedString } from '../../../../platform/action/common/action.js';
19
import { IExtensionFeatureTableRenderer, IExtensionFeaturesRegistry, IRenderedData, IRowData, ITableData, Extensions as ExtensionFeaturesExtensions } from '../../extensionManagement/common/extensionFeatures.js';
20
import { IExtensionManifest, IKeyBinding } from '../../../../platform/extensions/common/extensions.js';
21
import { Registry } from '../../../../platform/registry/common/platform.js';
22
import { SyncDescriptor } from '../../../../platform/instantiation/common/descriptors.js';
23
import { platform } from '../../../../base/common/process.js';
24
import { MarkdownString } from '../../../../base/common/htmlContent.js';
25
import { ResolvedKeybinding } from '../../../../base/common/keybindings.js';
26
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
27
import { ApiProposalName } from '../../../../platform/extensions/common/extensionsApiProposals.js';
28
29
interface IAPIMenu {
30
readonly key: string;
31
readonly id: MenuId;
32
readonly description: string;
33
readonly proposed?: ApiProposalName;
34
readonly supportsSubmenus?: boolean; // defaults to true
35
}
36
37
const apiMenus: IAPIMenu[] = [
38
{
39
key: 'commandPalette',
40
id: MenuId.CommandPalette,
41
description: localize('menus.commandPalette', "The Command Palette"),
42
supportsSubmenus: false
43
},
44
{
45
key: 'touchBar',
46
id: MenuId.TouchBarContext,
47
description: localize('menus.touchBar', "The touch bar (macOS only)"),
48
supportsSubmenus: false
49
},
50
{
51
key: 'editor/title',
52
id: MenuId.EditorTitle,
53
description: localize('menus.editorTitle', "The editor title menu")
54
},
55
{
56
key: 'editor/title/run',
57
id: MenuId.EditorTitleRun,
58
description: localize('menus.editorTitleRun', "Run submenu inside the editor title menu")
59
},
60
{
61
key: 'editor/context',
62
id: MenuId.EditorContext,
63
description: localize('menus.editorContext', "The editor context menu")
64
},
65
{
66
key: 'editor/context/copy',
67
id: MenuId.EditorContextCopy,
68
description: localize('menus.editorContextCopyAs', "'Copy as' submenu in the editor context menu")
69
},
70
{
71
key: 'editor/context/share',
72
id: MenuId.EditorContextShare,
73
description: localize('menus.editorContextShare', "'Share' submenu in the editor context menu"),
74
proposed: 'contribShareMenu'
75
},
76
{
77
key: 'explorer/context',
78
id: MenuId.ExplorerContext,
79
description: localize('menus.explorerContext', "The file explorer context menu")
80
},
81
{
82
key: 'explorer/context/share',
83
id: MenuId.ExplorerContextShare,
84
description: localize('menus.explorerContextShare', "'Share' submenu in the file explorer context menu"),
85
proposed: 'contribShareMenu'
86
},
87
{
88
key: 'editor/title/context',
89
id: MenuId.EditorTitleContext,
90
description: localize('menus.editorTabContext', "The editor tabs context menu")
91
},
92
{
93
key: 'editor/title/context/share',
94
id: MenuId.EditorTitleContextShare,
95
description: localize('menus.editorTitleContextShare', "'Share' submenu inside the editor title context menu"),
96
proposed: 'contribShareMenu'
97
},
98
{
99
key: 'debug/callstack/context',
100
id: MenuId.DebugCallStackContext,
101
description: localize('menus.debugCallstackContext', "The debug callstack view context menu")
102
},
103
{
104
key: 'debug/variables/context',
105
id: MenuId.DebugVariablesContext,
106
description: localize('menus.debugVariablesContext', "The debug variables view context menu")
107
},
108
{
109
key: 'debug/watch/context',
110
id: MenuId.DebugWatchContext,
111
description: localize('menus.debugWatchContext', "The debug watch view context menu")
112
},
113
{
114
key: 'debug/toolBar',
115
id: MenuId.DebugToolBar,
116
description: localize('menus.debugToolBar', "The debug toolbar menu")
117
},
118
{
119
key: 'debug/createConfiguration',
120
id: MenuId.DebugCreateConfiguration,
121
proposed: 'contribDebugCreateConfiguration',
122
description: localize('menus.debugCreateConfiguation', "The debug create configuration menu")
123
},
124
{
125
key: 'notebook/variables/context',
126
id: MenuId.NotebookVariablesContext,
127
description: localize('menus.notebookVariablesContext', "The notebook variables view context menu")
128
},
129
{
130
key: 'menuBar/home',
131
id: MenuId.MenubarHomeMenu,
132
description: localize('menus.home', "The home indicator context menu (web only)"),
133
proposed: 'contribMenuBarHome',
134
supportsSubmenus: false
135
},
136
{
137
key: 'menuBar/edit/copy',
138
id: MenuId.MenubarCopy,
139
description: localize('menus.opy', "'Copy as' submenu in the top level Edit menu")
140
},
141
{
142
key: 'scm/title',
143
id: MenuId.SCMTitle,
144
description: localize('menus.scmTitle', "The Source Control title menu")
145
},
146
{
147
key: 'scm/sourceControl',
148
id: MenuId.SCMSourceControl,
149
description: localize('menus.scmSourceControl', "The Source Control menu")
150
},
151
{
152
key: 'scm/repositories/title',
153
id: MenuId.SCMSourceControlTitle,
154
description: localize('menus.scmSourceControlTitle', "The Source Control Repositories title menu"),
155
proposed: 'contribSourceControlTitleMenu'
156
},
157
{
158
key: 'scm/repository',
159
id: MenuId.SCMSourceControlInline,
160
description: localize('menus.scmSourceControlInline', "The Source Control repository menu"),
161
},
162
{
163
key: 'scm/resourceState/context',
164
id: MenuId.SCMResourceContext,
165
description: localize('menus.resourceStateContext', "The Source Control resource state context menu")
166
},
167
{
168
key: 'scm/resourceFolder/context',
169
id: MenuId.SCMResourceFolderContext,
170
description: localize('menus.resourceFolderContext', "The Source Control resource folder context menu")
171
},
172
{
173
key: 'scm/resourceGroup/context',
174
id: MenuId.SCMResourceGroupContext,
175
description: localize('menus.resourceGroupContext', "The Source Control resource group context menu")
176
},
177
{
178
key: 'scm/change/title',
179
id: MenuId.SCMChangeContext,
180
description: localize('menus.changeTitle', "The Source Control inline change menu")
181
},
182
{
183
key: 'scm/inputBox',
184
id: MenuId.SCMInputBox,
185
description: localize('menus.input', "The Source Control input box menu"),
186
proposed: 'contribSourceControlInputBoxMenu'
187
},
188
{
189
key: 'scm/history/title',
190
id: MenuId.SCMHistoryTitle,
191
description: localize('menus.scmHistoryTitle', "The Source Control History title menu"),
192
proposed: 'contribSourceControlHistoryTitleMenu'
193
},
194
{
195
key: 'scm/historyItem/context',
196
id: MenuId.SCMHistoryItemContext,
197
description: localize('menus.historyItemContext', "The Source Control history item context menu"),
198
proposed: 'contribSourceControlHistoryItemMenu'
199
},
200
{
201
key: 'scm/historyItemRef/context',
202
id: MenuId.SCMHistoryItemRefContext,
203
description: localize('menus.historyItemRefContext', "The Source Control history item reference context menu"),
204
proposed: 'contribSourceControlHistoryItemMenu'
205
},
206
{
207
key: 'scm/artifactGroup/context',
208
id: MenuId.SCMArtifactGroupContext,
209
description: localize('menus.artifactGroupContext', "The Source Control artifact group context menu"),
210
proposed: 'contribSourceControlArtifactGroupMenu'
211
},
212
{
213
key: 'scm/artifact/context',
214
id: MenuId.SCMArtifactContext,
215
description: localize('menus.artifactContext', "The Source Control artifact context menu"),
216
proposed: 'contribSourceControlArtifactMenu'
217
},
218
{
219
key: 'statusBar/remoteIndicator',
220
id: MenuId.StatusBarRemoteIndicatorMenu,
221
description: localize('menus.statusBarRemoteIndicator', "The remote indicator menu in the status bar"),
222
supportsSubmenus: false
223
},
224
{
225
key: 'terminal/context',
226
id: MenuId.TerminalInstanceContext,
227
description: localize('menus.terminalContext', "The terminal context menu")
228
},
229
{
230
key: 'terminal/title/context',
231
id: MenuId.TerminalTabContext,
232
description: localize('menus.terminalTabContext', "The terminal tabs context menu")
233
},
234
{
235
key: 'view/title',
236
id: MenuId.ViewTitle,
237
description: localize('view.viewTitle', "The contributed view title menu")
238
},
239
{
240
key: 'viewContainer/title',
241
id: MenuId.ViewContainerTitle,
242
description: localize('view.containerTitle', "The contributed view container title menu"),
243
proposed: 'contribViewContainerTitle'
244
},
245
{
246
key: 'view/item/context',
247
id: MenuId.ViewItemContext,
248
description: localize('view.itemContext', "The contributed view item context menu")
249
},
250
{
251
key: 'comments/comment/editorActions',
252
id: MenuId.CommentEditorActions,
253
description: localize('commentThread.editorActions', "The contributed comment editor actions"),
254
proposed: 'contribCommentEditorActionsMenu'
255
},
256
{
257
key: 'comments/commentThread/title',
258
id: MenuId.CommentThreadTitle,
259
description: localize('commentThread.title', "The contributed comment thread title menu")
260
},
261
{
262
key: 'comments/commentThread/context',
263
id: MenuId.CommentThreadActions,
264
description: localize('commentThread.actions', "The contributed comment thread context menu, rendered as buttons below the comment editor"),
265
supportsSubmenus: false
266
},
267
{
268
key: 'comments/commentThread/additionalActions',
269
id: MenuId.CommentThreadAdditionalActions,
270
description: localize('commentThread.actions', "The contributed comment thread context menu, rendered as buttons below the comment editor"),
271
supportsSubmenus: true,
272
proposed: 'contribCommentThreadAdditionalMenu'
273
},
274
{
275
key: 'comments/commentThread/title/context',
276
id: MenuId.CommentThreadTitleContext,
277
description: localize('commentThread.titleContext', "The contributed comment thread title's peek context menu, rendered as a right click menu on the comment thread's peek title."),
278
proposed: 'contribCommentPeekContext'
279
},
280
{
281
key: 'comments/comment/title',
282
id: MenuId.CommentTitle,
283
description: localize('comment.title', "The contributed comment title menu")
284
},
285
{
286
key: 'comments/comment/context',
287
id: MenuId.CommentActions,
288
description: localize('comment.actions', "The contributed comment context menu, rendered as buttons below the comment editor"),
289
supportsSubmenus: false
290
},
291
{
292
key: 'comments/commentThread/comment/context',
293
id: MenuId.CommentThreadCommentContext,
294
description: localize('comment.commentContext', "The contributed comment context menu, rendered as a right click menu on the an individual comment in the comment thread's peek view."),
295
proposed: 'contribCommentPeekContext'
296
},
297
{
298
key: 'commentsView/commentThread/context',
299
id: MenuId.CommentsViewThreadActions,
300
description: localize('commentsView.threadActions', "The contributed comment thread context menu in the comments view"),
301
proposed: 'contribCommentsViewThreadMenus'
302
},
303
{
304
key: 'notebook/toolbar',
305
id: MenuId.NotebookToolbar,
306
description: localize('notebook.toolbar', "The contributed notebook toolbar menu")
307
},
308
{
309
key: 'notebook/kernelSource',
310
id: MenuId.NotebookKernelSource,
311
description: localize('notebook.kernelSource', "The contributed notebook kernel sources menu"),
312
proposed: 'notebookKernelSource'
313
},
314
{
315
key: 'notebook/cell/title',
316
id: MenuId.NotebookCellTitle,
317
description: localize('notebook.cell.title', "The contributed notebook cell title menu")
318
},
319
{
320
key: 'notebook/cell/execute',
321
id: MenuId.NotebookCellExecute,
322
description: localize('notebook.cell.execute', "The contributed notebook cell execution menu")
323
},
324
{
325
key: 'interactive/toolbar',
326
id: MenuId.InteractiveToolbar,
327
description: localize('interactive.toolbar', "The contributed interactive toolbar menu"),
328
},
329
{
330
key: 'interactive/cell/title',
331
id: MenuId.InteractiveCellTitle,
332
description: localize('interactive.cell.title', "The contributed interactive cell title menu"),
333
},
334
{
335
key: 'issue/reporter',
336
id: MenuId.IssueReporter,
337
description: localize('issue.reporter', "The contributed issue reporter menu")
338
},
339
{
340
key: 'testing/item/context',
341
id: MenuId.TestItem,
342
description: localize('testing.item.context', "The contributed test item menu"),
343
},
344
{
345
key: 'testing/item/gutter',
346
id: MenuId.TestItemGutter,
347
description: localize('testing.item.gutter.title', "The menu for a gutter decoration for a test item"),
348
},
349
{
350
key: 'testing/profiles/context',
351
id: MenuId.TestProfilesContext,
352
description: localize('testing.profiles.context.title', "The menu for configuring testing profiles."),
353
},
354
{
355
key: 'testing/item/result',
356
id: MenuId.TestPeekElement,
357
description: localize('testing.item.result.title', "The menu for an item in the Test Results view or peek."),
358
},
359
{
360
key: 'testing/message/context',
361
id: MenuId.TestMessageContext,
362
description: localize('testing.message.context.title', "A prominent button overlaying editor content where the message is displayed"),
363
},
364
{
365
key: 'testing/message/content',
366
id: MenuId.TestMessageContent,
367
description: localize('testing.message.content.title', "Context menu for the message in the results tree"),
368
},
369
{
370
key: 'extension/context',
371
id: MenuId.ExtensionContext,
372
description: localize('menus.extensionContext', "The extension context menu")
373
},
374
{
375
key: 'timeline/title',
376
id: MenuId.TimelineTitle,
377
description: localize('view.timelineTitle', "The Timeline view title menu")
378
},
379
{
380
key: 'timeline/item/context',
381
id: MenuId.TimelineItemContext,
382
description: localize('view.timelineContext', "The Timeline view item context menu")
383
},
384
{
385
key: 'ports/item/context',
386
id: MenuId.TunnelContext,
387
description: localize('view.tunnelContext', "The Ports view item context menu")
388
},
389
{
390
key: 'ports/item/origin/inline',
391
id: MenuId.TunnelOriginInline,
392
description: localize('view.tunnelOriginInline', "The Ports view item origin inline menu")
393
},
394
{
395
key: 'ports/item/port/inline',
396
id: MenuId.TunnelPortInline,
397
description: localize('view.tunnelPortInline', "The Ports view item port inline menu")
398
},
399
{
400
key: 'file/newFile',
401
id: MenuId.NewFile,
402
description: localize('file.newFile', "The 'New File...' quick pick, shown on welcome page and File menu."),
403
supportsSubmenus: false,
404
},
405
{
406
key: 'webview/context',
407
id: MenuId.WebviewContext,
408
description: localize('webview.context', "The webview context menu")
409
},
410
{
411
key: 'file/share',
412
id: MenuId.MenubarShare,
413
description: localize('menus.share', "Share submenu shown in the top level File menu."),
414
proposed: 'contribShareMenu'
415
},
416
{
417
key: 'editor/inlineCompletions/actions',
418
id: MenuId.InlineCompletionsActions,
419
description: localize('inlineCompletions.actions', "The actions shown when hovering on an inline completion"),
420
supportsSubmenus: false,
421
proposed: 'inlineCompletionsAdditions'
422
},
423
{
424
key: 'editor/content',
425
id: MenuId.EditorContent,
426
description: localize('merge.toolbar', "The prominent button in an editor, overlays its content"),
427
proposed: 'contribEditorContentMenu'
428
},
429
{
430
key: 'editor/lineNumber/context',
431
id: MenuId.EditorLineNumberContext,
432
description: localize('editorLineNumberContext', "The contributed editor line number context menu")
433
},
434
{
435
key: 'mergeEditor/result/title',
436
id: MenuId.MergeInputResultToolbar,
437
description: localize('menus.mergeEditorResult', "The result toolbar of the merge editor"),
438
proposed: 'contribMergeEditorMenus'
439
},
440
{
441
key: 'multiDiffEditor/content',
442
id: MenuId.MultiDiffEditorContent,
443
description: localize('menus.multiDiffEditorContent', "A prominent button overlaying the multi diff editor"),
444
proposed: 'contribEditorContentMenu'
445
},
446
{
447
key: 'multiDiffEditor/resource/title',
448
id: MenuId.MultiDiffEditorFileToolbar,
449
description: localize('menus.multiDiffEditorResource', "The resource toolbar in the multi diff editor"),
450
proposed: 'contribMultiDiffEditorMenus'
451
},
452
{
453
key: 'diffEditor/gutter/hunk',
454
id: MenuId.DiffEditorHunkToolbar,
455
description: localize('menus.diffEditorGutterToolBarMenus', "The gutter toolbar in the diff editor"),
456
proposed: 'contribDiffEditorGutterToolBarMenus'
457
},
458
{
459
key: 'diffEditor/gutter/selection',
460
id: MenuId.DiffEditorSelectionToolbar,
461
description: localize('menus.diffEditorGutterToolBarMenus', "The gutter toolbar in the diff editor"),
462
proposed: 'contribDiffEditorGutterToolBarMenus'
463
},
464
{
465
key: 'searchPanel/aiResults/commands',
466
id: MenuId.SearchActionMenu,
467
description: localize('searchPanel.aiResultsCommands', "The commands that will contribute to the menu rendered as buttons next to the AI search title"),
468
},
469
{
470
key: 'editor/context/chat',
471
id: MenuId.ChatTextEditorMenu,
472
description: localize('menus.chatTextEditor', "The Chat submenu in the text editor context menu."),
473
supportsSubmenus: false,
474
proposed: 'chatParticipantPrivate'
475
},
476
{
477
key: 'chat/input/editing/sessionToolbar',
478
id: MenuId.ChatEditingSessionChangesToolbar,
479
description: localize('menus.chatEditingSessionChangesToolbar', "The Chat Editing widget toolbar menu for session changes."),
480
proposed: 'chatSessionsProvider'
481
},
482
{
483
// TODO: rename this to something like: `chatSessions/item/inline`
484
key: 'chat/chatSessions',
485
id: MenuId.AgentSessionsContext,
486
description: localize('menus.chatSessions', "The Chat Sessions menu."),
487
supportsSubmenus: false,
488
proposed: 'chatSessionsProvider'
489
},
490
{
491
key: 'chatSessions/newSession',
492
id: MenuId.AgentSessionsCreateSubMenu,
493
description: localize('menus.chatSessionsNewSession', "Menu for new chat sessions."),
494
supportsSubmenus: false,
495
proposed: 'chatSessionsProvider'
496
},
497
{
498
key: 'chat/multiDiff/context',
499
id: MenuId.ChatMultiDiffContext,
500
description: localize('menus.chatMultiDiffContext', "The Chat Multi-Diff context menu."),
501
supportsSubmenus: false,
502
proposed: 'chatSessionsProvider',
503
},
504
{
505
key: 'chat/editor/inlineGutter',
506
id: MenuId.ChatEditorInlineGutter,
507
description: localize('menus.chatEditorInlineGutter', "The inline gutter menu in the chat editor."),
508
supportsSubmenus: false,
509
proposed: 'contribChatEditorInlineGutterMenu',
510
},
511
{
512
key: 'chat/contextUsage/actions',
513
id: MenuId.ChatContextUsageActions,
514
description: localize('menus.chatContextUsageActions', "Actions in the chat context usage details popup."),
515
proposed: 'chatParticipantAdditions'
516
},
517
];
518
519
namespace schema {
520
521
// --- menus, submenus contribution point
522
523
export interface IUserFriendlyMenuItem {
524
command: string;
525
alt?: string;
526
when?: string;
527
group?: string;
528
}
529
530
export interface IUserFriendlySubmenuItem {
531
submenu: string;
532
when?: string;
533
group?: string;
534
}
535
536
export interface IUserFriendlySubmenu {
537
id: string;
538
label: string;
539
icon?: IUserFriendlyIcon;
540
}
541
542
export function isMenuItem(item: IUserFriendlyMenuItem | IUserFriendlySubmenuItem): item is IUserFriendlyMenuItem {
543
return typeof (item as IUserFriendlyMenuItem).command === 'string';
544
}
545
546
export function isValidMenuItem(item: IUserFriendlyMenuItem, collector: ExtensionMessageCollector): boolean {
547
if (typeof item.command !== 'string') {
548
collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'command'));
549
return false;
550
}
551
if (item.alt && typeof item.alt !== 'string') {
552
collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'alt'));
553
return false;
554
}
555
if (item.when && typeof item.when !== 'string') {
556
collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'when'));
557
return false;
558
}
559
if (item.group && typeof item.group !== 'string') {
560
collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'group'));
561
return false;
562
}
563
564
return true;
565
}
566
567
export function isValidSubmenuItem(item: IUserFriendlySubmenuItem, collector: ExtensionMessageCollector): boolean {
568
if (typeof item.submenu !== 'string') {
569
collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'submenu'));
570
return false;
571
}
572
if (item.when && typeof item.when !== 'string') {
573
collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'when'));
574
return false;
575
}
576
if (item.group && typeof item.group !== 'string') {
577
collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'group'));
578
return false;
579
}
580
581
return true;
582
}
583
584
export function isValidItems(items: (IUserFriendlyMenuItem | IUserFriendlySubmenuItem)[], collector: ExtensionMessageCollector): boolean {
585
if (!Array.isArray(items)) {
586
collector.error(localize('requirearray', "submenu items must be an array"));
587
return false;
588
}
589
590
for (const item of items) {
591
if (isMenuItem(item)) {
592
if (!isValidMenuItem(item, collector)) {
593
return false;
594
}
595
} else {
596
if (!isValidSubmenuItem(item, collector)) {
597
return false;
598
}
599
}
600
}
601
602
return true;
603
}
604
605
export function isValidSubmenu(submenu: IUserFriendlySubmenu, collector: ExtensionMessageCollector): boolean {
606
if (typeof submenu !== 'object') {
607
collector.error(localize('require', "submenu items must be an object"));
608
return false;
609
}
610
611
if (typeof submenu.id !== 'string') {
612
collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'id'));
613
return false;
614
}
615
if (typeof submenu.label !== 'string') {
616
collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'label'));
617
return false;
618
}
619
620
return true;
621
}
622
623
const menuItem: IJSONSchema = {
624
type: 'object',
625
required: ['command'],
626
properties: {
627
command: {
628
description: localize('vscode.extension.contributes.menuItem.command', 'Identifier of the command to execute. The command must be declared in the \'commands\'-section'),
629
type: 'string'
630
},
631
alt: {
632
description: localize('vscode.extension.contributes.menuItem.alt', 'Identifier of an alternative command to execute. The command must be declared in the \'commands\'-section'),
633
type: 'string'
634
},
635
when: {
636
description: localize('vscode.extension.contributes.menuItem.when', 'Condition which must be true to show this item'),
637
type: 'string'
638
},
639
group: {
640
description: localize('vscode.extension.contributes.menuItem.group', 'Group into which this item belongs'),
641
type: 'string'
642
}
643
}
644
};
645
646
const submenuItem: IJSONSchema = {
647
type: 'object',
648
required: ['submenu'],
649
properties: {
650
submenu: {
651
description: localize('vscode.extension.contributes.menuItem.submenu', 'Identifier of the submenu to display in this item.'),
652
type: 'string'
653
},
654
when: {
655
description: localize('vscode.extension.contributes.menuItem.when', 'Condition which must be true to show this item'),
656
type: 'string'
657
},
658
group: {
659
description: localize('vscode.extension.contributes.menuItem.group', 'Group into which this item belongs'),
660
type: 'string'
661
}
662
}
663
};
664
665
const submenu: IJSONSchema = {
666
type: 'object',
667
required: ['id', 'label'],
668
properties: {
669
id: {
670
description: localize('vscode.extension.contributes.submenu.id', 'Identifier of the menu to display as a submenu.'),
671
type: 'string'
672
},
673
label: {
674
description: localize('vscode.extension.contributes.submenu.label', 'The label of the menu item which leads to this submenu.'),
675
type: 'string'
676
},
677
icon: {
678
description: localize({ key: 'vscode.extension.contributes.submenu.icon', comment: ['do not translate or change "\\$(zap)", \\ in front of $ is important.'] }, '(Optional) Icon which is used to represent the submenu in the UI. Either a file path, an object with file paths for dark and light themes, or a theme icon references, like "\\$(zap)"'),
679
anyOf: [{
680
type: 'string'
681
},
682
{
683
type: 'object',
684
properties: {
685
light: {
686
description: localize('vscode.extension.contributes.submenu.icon.light', 'Icon path when a light theme is used'),
687
type: 'string'
688
},
689
dark: {
690
description: localize('vscode.extension.contributes.submenu.icon.dark', 'Icon path when a dark theme is used'),
691
type: 'string'
692
}
693
}
694
}]
695
}
696
}
697
};
698
699
export const menusContribution: IJSONSchema = {
700
description: localize('vscode.extension.contributes.menus', "Contributes menu items to the editor"),
701
type: 'object',
702
properties: index(apiMenus, menu => menu.key, menu => ({
703
markdownDescription: menu.proposed ? localize('proposed', "Proposed API, requires `enabledApiProposal: [\"{0}\"]` - {1}", menu.proposed, menu.description) : menu.description,
704
type: 'array',
705
items: menu.supportsSubmenus === false ? menuItem : { oneOf: [menuItem, submenuItem] }
706
})),
707
additionalProperties: {
708
description: 'Submenu',
709
type: 'array',
710
items: { oneOf: [menuItem, submenuItem] }
711
}
712
};
713
714
export const submenusContribution: IJSONSchema = {
715
description: localize('vscode.extension.contributes.submenus', "Contributes submenu items to the editor"),
716
type: 'array',
717
items: submenu
718
};
719
720
// --- commands contribution point
721
722
export interface IUserFriendlyCommand {
723
command: string;
724
title: string | ILocalizedString;
725
shortTitle?: string | ILocalizedString;
726
enablement?: string;
727
category?: string | ILocalizedString;
728
icon?: IUserFriendlyIcon;
729
}
730
731
export type IUserFriendlyIcon = string | { light: string; dark: string };
732
733
export function isValidCommand(command: IUserFriendlyCommand, collector: ExtensionMessageCollector): boolean {
734
if (!command) {
735
collector.error(localize('nonempty', "expected non-empty value."));
736
return false;
737
}
738
if (isFalsyOrWhitespace(command.command)) {
739
collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'command'));
740
return false;
741
}
742
if (!isValidLocalizedString(command.title, collector, 'title')) {
743
return false;
744
}
745
if (command.shortTitle && !isValidLocalizedString(command.shortTitle, collector, 'shortTitle')) {
746
return false;
747
}
748
if (command.enablement && typeof command.enablement !== 'string') {
749
collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'precondition'));
750
return false;
751
}
752
if (command.category && !isValidLocalizedString(command.category, collector, 'category')) {
753
return false;
754
}
755
if (!isValidIcon(command.icon, collector)) {
756
return false;
757
}
758
return true;
759
}
760
761
function isValidIcon(icon: IUserFriendlyIcon | undefined, collector: ExtensionMessageCollector): boolean {
762
if (typeof icon === 'undefined') {
763
return true;
764
}
765
if (typeof icon === 'string') {
766
return true;
767
} else if (typeof icon.dark === 'string' && typeof icon.light === 'string') {
768
return true;
769
}
770
collector.error(localize('opticon', "property `icon` can be omitted or must be either a string or a literal like `{dark, light}`"));
771
return false;
772
}
773
774
function isValidLocalizedString(localized: string | ILocalizedString, collector: ExtensionMessageCollector, propertyName: string): boolean {
775
if (typeof localized === 'undefined') {
776
collector.error(localize('requireStringOrObject', "property `{0}` is mandatory and must be of type `string` or `object`", propertyName));
777
return false;
778
} else if (typeof localized === 'string' && isFalsyOrWhitespace(localized)) {
779
collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", propertyName));
780
return false;
781
} else if (typeof localized !== 'string' && (isFalsyOrWhitespace(localized.original) || isFalsyOrWhitespace(localized.value))) {
782
collector.error(localize('requirestrings', "properties `{0}` and `{1}` are mandatory and must be of type `string`", `${propertyName}.value`, `${propertyName}.original`));
783
return false;
784
}
785
786
return true;
787
}
788
789
const commandType: IJSONSchema = {
790
type: 'object',
791
required: ['command', 'title'],
792
properties: {
793
command: {
794
description: localize('vscode.extension.contributes.commandType.command', 'Identifier of the command to execute'),
795
type: 'string'
796
},
797
title: {
798
description: localize('vscode.extension.contributes.commandType.title', 'Title by which the command is represented in the UI'),
799
type: 'string'
800
},
801
shortTitle: {
802
markdownDescription: localize('vscode.extension.contributes.commandType.shortTitle', '(Optional) Short title by which the command is represented in the UI. Menus pick either `title` or `shortTitle` depending on the context in which they show commands.'),
803
type: 'string'
804
},
805
category: {
806
description: localize('vscode.extension.contributes.commandType.category', '(Optional) Category string by which the command is grouped in the UI'),
807
type: 'string'
808
},
809
enablement: {
810
description: localize('vscode.extension.contributes.commandType.precondition', '(Optional) Condition which must be true to enable the command in the UI (menu and keybindings). Does not prevent executing the command by other means, like the `executeCommand`-api.'),
811
type: 'string'
812
},
813
icon: {
814
description: localize({ key: 'vscode.extension.contributes.commandType.icon', comment: ['do not translate or change "\\$(zap)", \\ in front of $ is important.'] }, '(Optional) Icon which is used to represent the command in the UI. Either a file path, an object with file paths for dark and light themes, or a theme icon references, like "\\$(zap)"'),
815
anyOf: [{
816
type: 'string'
817
},
818
{
819
type: 'object',
820
properties: {
821
light: {
822
description: localize('vscode.extension.contributes.commandType.icon.light', 'Icon path when a light theme is used'),
823
type: 'string'
824
},
825
dark: {
826
description: localize('vscode.extension.contributes.commandType.icon.dark', 'Icon path when a dark theme is used'),
827
type: 'string'
828
}
829
}
830
}]
831
}
832
}
833
};
834
835
export const commandsContribution: IJSONSchema = {
836
description: localize('vscode.extension.contributes.commands', "Contributes commands to the command palette."),
837
oneOf: [
838
commandType,
839
{
840
type: 'array',
841
items: commandType
842
}
843
]
844
};
845
}
846
847
const _commandRegistrations = new DisposableStore();
848
849
export const commandsExtensionPoint = ExtensionsRegistry.registerExtensionPoint<schema.IUserFriendlyCommand | schema.IUserFriendlyCommand[]>({
850
extensionPoint: 'commands',
851
jsonSchema: schema.commandsContribution,
852
activationEventsGenerator: function* (contribs: readonly schema.IUserFriendlyCommand[]) {
853
for (const contrib of contribs) {
854
if (contrib.command) {
855
yield `onCommand:${contrib.command}`;
856
}
857
}
858
}
859
});
860
861
commandsExtensionPoint.setHandler(extensions => {
862
863
function handleCommand(userFriendlyCommand: schema.IUserFriendlyCommand, extension: IExtensionPointUser<unknown>) {
864
865
if (!schema.isValidCommand(userFriendlyCommand, extension.collector)) {
866
return;
867
}
868
869
const { icon, enablement, category, title, shortTitle, command } = userFriendlyCommand;
870
871
let absoluteIcon: { dark: URI; light?: URI } | ThemeIcon | undefined;
872
if (icon) {
873
if (typeof icon === 'string') {
874
absoluteIcon = ThemeIcon.fromString(icon) ?? { dark: resources.joinPath(extension.description.extensionLocation, icon), light: resources.joinPath(extension.description.extensionLocation, icon) };
875
876
} else {
877
absoluteIcon = {
878
dark: resources.joinPath(extension.description.extensionLocation, icon.dark),
879
light: resources.joinPath(extension.description.extensionLocation, icon.light)
880
};
881
}
882
}
883
884
const existingCmd = MenuRegistry.getCommand(command);
885
if (existingCmd) {
886
if (existingCmd.source) {
887
extension.collector.info(localize('dup1', "Command `{0}` already registered by {1} ({2})", userFriendlyCommand.command, existingCmd.source.title, existingCmd.source.id));
888
} else {
889
extension.collector.info(localize('dup0', "Command `{0}` already registered", userFriendlyCommand.command));
890
}
891
}
892
_commandRegistrations.add(MenuRegistry.addCommand({
893
id: command,
894
title,
895
source: { id: extension.description.identifier.value, title: extension.description.displayName ?? extension.description.name },
896
shortTitle,
897
tooltip: title,
898
category,
899
precondition: ContextKeyExpr.deserialize(enablement),
900
icon: absoluteIcon
901
}));
902
}
903
904
// remove all previous command registrations
905
_commandRegistrations.clear();
906
907
for (const extension of extensions) {
908
const { value } = extension;
909
if (Array.isArray(value)) {
910
for (const command of value) {
911
handleCommand(command, extension);
912
}
913
} else {
914
handleCommand(value, extension);
915
}
916
}
917
});
918
919
interface IRegisteredSubmenu {
920
readonly id: MenuId;
921
readonly label: string;
922
readonly icon?: { dark: URI; light?: URI } | ThemeIcon;
923
}
924
925
const _submenus = new Map<string, IRegisteredSubmenu>();
926
927
const submenusExtensionPoint = ExtensionsRegistry.registerExtensionPoint<schema.IUserFriendlySubmenu[]>({
928
extensionPoint: 'submenus',
929
jsonSchema: schema.submenusContribution
930
});
931
932
submenusExtensionPoint.setHandler(extensions => {
933
934
_submenus.clear();
935
936
for (const extension of extensions) {
937
const { value, collector } = extension;
938
939
for (const [, submenuInfo] of Object.entries(value)) {
940
941
if (!schema.isValidSubmenu(submenuInfo, collector)) {
942
continue;
943
}
944
945
if (!submenuInfo.id) {
946
collector.warn(localize('submenuId.invalid.id', "`{0}` is not a valid submenu identifier", submenuInfo.id));
947
continue;
948
}
949
if (_submenus.has(submenuInfo.id)) {
950
collector.info(localize('submenuId.duplicate.id', "The `{0}` submenu was already previously registered.", submenuInfo.id));
951
continue;
952
}
953
if (!submenuInfo.label) {
954
collector.warn(localize('submenuId.invalid.label', "`{0}` is not a valid submenu label", submenuInfo.label));
955
continue;
956
}
957
958
let absoluteIcon: { dark: URI; light?: URI } | ThemeIcon | undefined;
959
if (submenuInfo.icon) {
960
if (typeof submenuInfo.icon === 'string') {
961
absoluteIcon = ThemeIcon.fromString(submenuInfo.icon) || { dark: resources.joinPath(extension.description.extensionLocation, submenuInfo.icon) };
962
} else {
963
absoluteIcon = {
964
dark: resources.joinPath(extension.description.extensionLocation, submenuInfo.icon.dark),
965
light: resources.joinPath(extension.description.extensionLocation, submenuInfo.icon.light)
966
};
967
}
968
}
969
970
const item: IRegisteredSubmenu = {
971
id: MenuId.for(`api:${submenuInfo.id}`),
972
label: submenuInfo.label,
973
icon: absoluteIcon
974
};
975
976
_submenus.set(submenuInfo.id, item);
977
}
978
}
979
});
980
981
const _apiMenusByKey = new Map(apiMenus.map(menu => ([menu.key, menu])));
982
const _menuRegistrations = new DisposableStore();
983
const _submenuMenuItems = new Map<string /* menu id */, Set<string /* submenu id */>>();
984
985
const menusExtensionPoint = ExtensionsRegistry.registerExtensionPoint<{ [loc: string]: (schema.IUserFriendlyMenuItem | schema.IUserFriendlySubmenuItem)[] }>({
986
extensionPoint: 'menus',
987
jsonSchema: schema.menusContribution,
988
deps: [submenusExtensionPoint]
989
});
990
991
menusExtensionPoint.setHandler(extensions => {
992
993
// remove all previous menu registrations
994
_menuRegistrations.clear();
995
_submenuMenuItems.clear();
996
997
for (const extension of extensions) {
998
const { value, collector } = extension;
999
1000
for (const entry of Object.entries(value)) {
1001
if (!schema.isValidItems(entry[1], collector)) {
1002
continue;
1003
}
1004
1005
let menu = _apiMenusByKey.get(entry[0]);
1006
1007
if (!menu) {
1008
const submenu = _submenus.get(entry[0]);
1009
1010
if (submenu) {
1011
menu = {
1012
key: entry[0],
1013
id: submenu.id,
1014
description: ''
1015
};
1016
}
1017
}
1018
1019
if (!menu) {
1020
continue;
1021
}
1022
1023
if (menu.proposed && !isProposedApiEnabled(extension.description, menu.proposed)) {
1024
collector.error(localize('proposedAPI.invalid', "{0} is a proposed menu identifier. It requires 'package.json#enabledApiProposals: [\"{1}\"]' and is only available when running out of dev or with the following command line switch: --enable-proposed-api {2}", entry[0], menu.proposed, extension.description.identifier.value));
1025
continue;
1026
}
1027
1028
for (const menuItem of entry[1]) {
1029
let item: IMenuItem | ISubmenuItem;
1030
1031
if (schema.isMenuItem(menuItem)) {
1032
const command = MenuRegistry.getCommand(menuItem.command);
1033
const alt = menuItem.alt && MenuRegistry.getCommand(menuItem.alt) || undefined;
1034
1035
if (!command) {
1036
collector.error(localize('missing.command', "Menu item references a command `{0}` which is not defined in the 'commands' section.", menuItem.command));
1037
continue;
1038
}
1039
if (menuItem.alt && !alt) {
1040
collector.warn(localize('missing.altCommand', "Menu item references an alt-command `{0}` which is not defined in the 'commands' section.", menuItem.alt));
1041
}
1042
if (menuItem.command === menuItem.alt) {
1043
collector.info(localize('dupe.command', "Menu item references the same command as default and alt-command"));
1044
}
1045
1046
item = { command, alt, group: undefined, order: undefined, when: undefined };
1047
} else {
1048
if (menu.supportsSubmenus === false) {
1049
collector.error(localize('unsupported.submenureference', "Menu item references a submenu for a menu which doesn't have submenu support."));
1050
continue;
1051
}
1052
1053
const submenu = _submenus.get(menuItem.submenu);
1054
1055
if (!submenu) {
1056
collector.error(localize('missing.submenu', "Menu item references a submenu `{0}` which is not defined in the 'submenus' section.", menuItem.submenu));
1057
continue;
1058
}
1059
1060
let submenuRegistrations = _submenuMenuItems.get(menu.id.id);
1061
1062
if (!submenuRegistrations) {
1063
submenuRegistrations = new Set();
1064
_submenuMenuItems.set(menu.id.id, submenuRegistrations);
1065
}
1066
1067
if (submenuRegistrations.has(submenu.id.id)) {
1068
collector.warn(localize('submenuItem.duplicate', "The `{0}` submenu was already contributed to the `{1}` menu.", menuItem.submenu, entry[0]));
1069
continue;
1070
}
1071
1072
submenuRegistrations.add(submenu.id.id);
1073
1074
item = { submenu: submenu.id, icon: submenu.icon, title: submenu.label, group: undefined, order: undefined, when: undefined };
1075
}
1076
1077
if (menuItem.group) {
1078
const idx = menuItem.group.lastIndexOf('@');
1079
if (idx > 0) {
1080
item.group = menuItem.group.substr(0, idx);
1081
item.order = Number(menuItem.group.substr(idx + 1)) || undefined;
1082
} else {
1083
item.group = menuItem.group;
1084
}
1085
}
1086
1087
if (menu.id === MenuId.ViewContainerTitle && !menuItem.when?.includes('viewContainer == workbench.view.debug')) {
1088
// Not a perfect check but enough to communicate that this proposed extension point is currently only for the debug view container
1089
collector.error(localize('viewContainerTitle.when', "The {0} menu contribution must check {1} in its {2} clause.", '`viewContainer/title`', '`viewContainer == workbench.view.debug`', '"when"'));
1090
continue;
1091
}
1092
1093
item.when = ContextKeyExpr.deserialize(menuItem.when);
1094
_menuRegistrations.add(MenuRegistry.appendMenuItem(menu.id, item));
1095
}
1096
}
1097
}
1098
});
1099
1100
class CommandsTableRenderer extends Disposable implements IExtensionFeatureTableRenderer {
1101
1102
readonly type = 'table';
1103
1104
constructor(
1105
@IKeybindingService private readonly _keybindingService: IKeybindingService
1106
) { super(); }
1107
1108
shouldRender(manifest: IExtensionManifest): boolean {
1109
return !!manifest.contributes?.commands;
1110
}
1111
1112
render(manifest: IExtensionManifest): IRenderedData<ITableData> {
1113
const rawCommands = manifest.contributes?.commands || [];
1114
const commands = rawCommands.map(c => ({
1115
id: c.command,
1116
title: c.title,
1117
keybindings: [] as ResolvedKeybinding[],
1118
menus: [] as string[]
1119
}));
1120
1121
const byId = index(commands, c => c.id);
1122
1123
const menus = manifest.contributes?.menus || {};
1124
1125
// Add to commandPalette array any commands not explicitly contributed to it
1126
const implicitlyOnCommandPalette = index(commands, c => c.id);
1127
if (menus['commandPalette']) {
1128
for (const command of menus['commandPalette']) {
1129
delete implicitlyOnCommandPalette[command.command];
1130
}
1131
}
1132
1133
if (Object.keys(implicitlyOnCommandPalette).length) {
1134
if (!menus['commandPalette']) {
1135
menus['commandPalette'] = [];
1136
}
1137
for (const command in implicitlyOnCommandPalette) {
1138
menus['commandPalette'].push({ command });
1139
}
1140
}
1141
1142
for (const context in menus) {
1143
for (const menu of menus[context]) {
1144
1145
// This typically happens for the commandPalette context
1146
if (menu.when === 'false') {
1147
continue;
1148
}
1149
if (menu.command) {
1150
let command = byId[menu.command];
1151
if (command) {
1152
if (!command.menus.includes(context)) {
1153
command.menus.push(context);
1154
}
1155
} else {
1156
command = { id: menu.command, title: '', keybindings: [], menus: [context] };
1157
byId[command.id] = command;
1158
commands.push(command);
1159
}
1160
}
1161
}
1162
}
1163
1164
const rawKeybindings = manifest.contributes?.keybindings ? (Array.isArray(manifest.contributes.keybindings) ? manifest.contributes.keybindings : [manifest.contributes.keybindings]) : [];
1165
1166
rawKeybindings.forEach(rawKeybinding => {
1167
const keybinding = this.resolveKeybinding(rawKeybinding);
1168
1169
if (!keybinding) {
1170
return;
1171
}
1172
1173
let command = byId[rawKeybinding.command];
1174
1175
if (command) {
1176
command.keybindings.push(keybinding);
1177
} else {
1178
command = { id: rawKeybinding.command, title: '', keybindings: [keybinding], menus: [] };
1179
byId[command.id] = command;
1180
commands.push(command);
1181
}
1182
});
1183
1184
if (!commands.length) {
1185
return { data: { headers: [], rows: [] }, dispose: () => { } };
1186
}
1187
1188
const headers = [
1189
localize('command name', "ID"),
1190
localize('command title', "Title"),
1191
localize('keyboard shortcuts', "Keyboard Shortcuts"),
1192
localize('menuContexts', "Menu Contexts")
1193
];
1194
1195
const rows: IRowData[][] = commands.sort((a, b) => a.id.localeCompare(b.id))
1196
.map(command => {
1197
return [
1198
new MarkdownString().appendMarkdown(`\`${command.id}\``),
1199
typeof command.title === 'string' ? command.title : command.title.value,
1200
command.keybindings,
1201
new MarkdownString().appendMarkdown(`${command.menus.sort((a, b) => a.localeCompare(b)).map(menu => `\`${menu}\``).join('&nbsp;')}`),
1202
];
1203
});
1204
1205
return {
1206
data: {
1207
headers,
1208
rows
1209
},
1210
dispose: () => { }
1211
};
1212
}
1213
1214
private resolveKeybinding(rawKeyBinding: IKeyBinding): ResolvedKeybinding | undefined {
1215
let key: string | undefined;
1216
1217
switch (platform) {
1218
case 'win32': key = rawKeyBinding.win; break;
1219
case 'linux': key = rawKeyBinding.linux; break;
1220
case 'darwin': key = rawKeyBinding.mac; break;
1221
}
1222
1223
return this._keybindingService.resolveUserBinding(key ?? rawKeyBinding.key)[0];
1224
}
1225
1226
}
1227
1228
Registry.as<IExtensionFeaturesRegistry>(ExtensionFeaturesExtensions.ExtensionFeaturesRegistry).registerExtensionFeature({
1229
id: 'commands',
1230
label: localize('commands', "Commands"),
1231
access: {
1232
canToggle: false,
1233
},
1234
renderer: new SyncDescriptor(CommandsTableRenderer),
1235
});
1236
1237