forked from irinazheltisheva/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.ts
More file actions
132 lines (108 loc) · 5.33 KB
/
Copy pathactions.ts
File metadata and controls
132 lines (108 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { Registry } from 'vs/platform/registry/common/platform';
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ICommandHandler, CommandsRegistry } from 'vs/platform/commands/common/commands';
import { SyncActionDescriptor, MenuRegistry, MenuId, ICommandAction } from 'vs/platform/actions/common/actions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { ContextKeyExpr, ContextKeyExpression } from 'vs/platform/contextkey/common/contextkey';
export const Extensions = {
WorkbenchActions: 'workbench.contributions.actions'
};
export interface IWorkbenchActionRegistry {
/**
* Registers a workbench action to the platform. Workbench actions are not
* visible by default and can only be invoked through a keybinding if provided.
* @deprecated Register directly with KeybindingsRegistry and MenuRegistry or use registerAction2 instead.
*/
registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string, when?: ContextKeyExpr): IDisposable;
}
Registry.add(Extensions.WorkbenchActions, new class implements IWorkbenchActionRegistry {
registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string, when?: ContextKeyExpression): IDisposable {
return this.registerWorkbenchCommandFromAction(descriptor, alias, category, when);
}
private registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, alias: string, category?: string, when?: ContextKeyExpression): IDisposable {
const registrations = new DisposableStore();
// command
registrations.add(CommandsRegistry.registerCommand(descriptor.id, this.createCommandHandler(descriptor)));
// keybinding
const weight = (typeof descriptor.keybindingWeight === 'undefined' ? KeybindingWeight.WorkbenchContrib : descriptor.keybindingWeight);
const keybindings = descriptor.keybindings;
KeybindingsRegistry.registerKeybindingRule({
id: descriptor.id,
weight: weight,
when:
descriptor.keybindingContext && when
? ContextKeyExpr.and(descriptor.keybindingContext, when)
: descriptor.keybindingContext || when || null,
primary: keybindings ? keybindings.primary : 0,
secondary: keybindings?.secondary,
win: keybindings?.win,
mac: keybindings?.mac,
linux: keybindings?.linux
});
// menu item
// TODO@Rob slightly weird if-check required because of
// https://github.com/microsoft/vscode/blob/master/src/vs/workbench/contrib/search/electron-browser/search.contribution.ts#L266
if (descriptor.label) {
let idx = alias.indexOf(': ');
let categoryOriginal = '';
if (idx > 0) {
categoryOriginal = alias.substr(0, idx);
alias = alias.substr(idx + 2);
}
const command: ICommandAction = {
id: descriptor.id,
title: { value: descriptor.label, original: alias },
category: category ? { value: category, original: categoryOriginal } : undefined
};
MenuRegistry.addCommand(command);
registrations.add(MenuRegistry.appendMenuItem(MenuId.CommandPalette, { command, when }));
}
// TODO@alex,joh
// support removal of keybinding rule
// support removal of command-ui
return registrations;
}
private createCommandHandler(descriptor: SyncActionDescriptor): ICommandHandler {
return async (accessor, args) => {
const notificationService = accessor.get(INotificationService);
const instantiationService = accessor.get(IInstantiationService);
const lifecycleService = accessor.get(ILifecycleService);
try {
await this.triggerAndDisposeAction(instantiationService, lifecycleService, descriptor, args);
} catch (error) {
notificationService.error(error);
}
};
}
private async triggerAndDisposeAction(instantiationService: IInstantiationService, lifecycleService: ILifecycleService, descriptor: SyncActionDescriptor, args: unknown): Promise<void> {
// run action when workbench is created
await lifecycleService.when(LifecyclePhase.Ready);
const actionInstance = instantiationService.createInstance(descriptor.syncDescriptor);
actionInstance.label = descriptor.label || actionInstance.label;
// don't run the action when not enabled
if (!actionInstance.enabled) {
actionInstance.dispose();
return;
}
// otherwise run and dispose
try {
const from = (args as any)?.from || 'keybinding';
await actionInstance.run(undefined, { from });
} finally {
actionInstance.dispose();
}
}
});
export const CATEGORIES = {
View: { value: localize('view', "View"), original: 'View' },
Help: { value: localize('help', "Help"), original: 'Help' },
Developer: { value: localize({ key: 'developer', comment: ['A developer on Code itself or someone diagnosing issues in Code'] }, "Developer"), original: 'Developer' }
};