提交 54a46c85 编写于 作者: J Johannes Rieken

make the registry be just a forwarder

上级 65d45fd9
...@@ -87,7 +87,7 @@ export interface IMenuRegistry { ...@@ -87,7 +87,7 @@ export interface IMenuRegistry {
getMenuItems(loc: MenuId): IMenuItem[]; getMenuItems(loc: MenuId): IMenuItem[];
} }
export const MenuRegistry: IMenuRegistry = new class { export const MenuRegistry: IMenuRegistry = new class implements IMenuRegistry {
private _commands: { [id: string]: ICommandAction } = Object.create(null); private _commands: { [id: string]: ICommandAction } = Object.create(null);
......
...@@ -15,6 +15,7 @@ import { IMessageService } from 'vs/platform/message/common/message'; ...@@ -15,6 +15,7 @@ import { IMessageService } from 'vs/platform/message/common/message';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import Severity from 'vs/base/common/severity'; import Severity from 'vs/base/common/severity';
import { IDisposable, combinedDisposable } from 'vs/base/common/lifecycle';
export const Extensions = { export const Extensions = {
WorkbenchActions: 'workbench.contributions.actions' WorkbenchActions: 'workbench.contributions.actions'
...@@ -30,92 +31,22 @@ export interface IWorkbenchActionRegistry { ...@@ -30,92 +31,22 @@ export interface IWorkbenchActionRegistry {
* Registers a workbench action to the platform. Workbench actions are not * Registers a workbench action to the platform. Workbench actions are not
* visible by default and can only be invoked through a keybinding if provided. * visible by default and can only be invoked through a keybinding if provided.
*/ */
registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): void; registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): IDisposable;
/**
* Unregisters a workbench action from the platform.
*/
unregisterWorkbenchAction(id: string): boolean;
/**
* Returns the workbench action descriptor for the given id or null if none.
*/
getWorkbenchAction(id: string): SyncActionDescriptor;
/**
* Returns an array of registered workbench actions known to the platform.
*/
getWorkbenchActions(): SyncActionDescriptor[];
/**
* Returns the alias associated with the given action or null if none.
*/
getAlias(actionId: string): string;
/**
* Returns the category for the given action or null if none.
*/
getCategory(actionId: string): string;
} }
interface IActionMeta { Registry.add(Extensions.WorkbenchActions, new class implements IWorkbenchActionRegistry {
alias: string;
category?: string;
}
class WorkbenchActionRegistry implements IWorkbenchActionRegistry { registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): IDisposable {
return this._registerWorkbenchCommandFromAction(descriptor, alias, category);
public registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): void {
registerWorkbenchCommandFromAction(descriptor, alias, category);
}
public unregisterWorkbenchAction(id: string): boolean {
return true;
}
public getWorkbenchAction(id: string): SyncActionDescriptor {
return null;
} }
public getCategory(id: string): string { private _registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, alias: string, category?: string): IDisposable {
const commandAction = MenuRegistry.getCommand(id); let registrations: IDisposable[] = [];
if (!commandAction || !commandAction.category) {
return null;
}
const { category } = commandAction;
if (typeof category === 'string') {
return category;
} else {
return category.value;
}
}
public getAlias(id: string): string {
const commandAction = MenuRegistry.getCommand(id);
if (!commandAction) {
return null;
}
const { title } = commandAction;
if (typeof title === 'string') {
return null;
} else {
return title.original;
}
}
public getWorkbenchActions(): SyncActionDescriptor[] {
return [];
}
}
Registry.add(Extensions.WorkbenchActions, new WorkbenchActionRegistry()); // command
registrations.push(CommandsRegistry.registerCommand(descriptor.id, this._createCommandHandler(descriptor)));
function registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, alias: string, category?: string): void { // keybinding
CommandsRegistry.registerCommand(descriptor.id, createCommandHandler(descriptor));
{
// register keybinding
const when = descriptor.keybindingContext; const when = descriptor.keybindingContext;
const weight = (typeof descriptor.keybindingWeight === 'undefined' ? KeybindingsRegistry.WEIGHT.workbenchContrib() : descriptor.keybindingWeight); const weight = (typeof descriptor.keybindingWeight === 'undefined' ? KeybindingsRegistry.WEIGHT.workbenchContrib() : descriptor.keybindingWeight);
const keybindings = descriptor.keybindings; const keybindings = descriptor.keybindings;
...@@ -129,71 +60,78 @@ function registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, al ...@@ -129,71 +60,78 @@ function registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, al
mac: keybindings && keybindings.mac, mac: keybindings && keybindings.mac,
linux: keybindings && keybindings.linux linux: keybindings && keybindings.linux
}); });
}
{ // menu item
// register menu item // TODO@Ben slightly weird if-check required because of
// https://github.com/Microsoft/vscode/blob/d28ace31aa147596e35adf101a27768a048c79ec/src/vs/workbench/parts/files/browser/fileActions.contribution.ts#L194
if (descriptor.label) { if (descriptor.label) {
// slightly weird if-check required because of
// https://github.com/Microsoft/vscode/blob/d28ace31aa147596e35adf101a27768a048c79ec/src/vs/workbench/parts/files/browser/fileActions.contribution.ts#L194 const command = {
MenuRegistry.appendMenuItem(MenuId.CommandPalette, { id: descriptor.id,
command: { title: { value: descriptor.label, original: alias },
id: descriptor.id, category
title: { value: descriptor.label, original: alias }, };
category
} MenuRegistry.addCommand(command);
});
registrations.push(MenuRegistry.appendMenuItem(MenuId.CommandPalette, { command }));
} }
// TODO@alex,joh
// support removal of keybinding rule
// support removal of command-ui
return combinedDisposable(registrations);
} }
}
function createCommandHandler(descriptor: SyncActionDescriptor): ICommandHandler { private _createCommandHandler(descriptor: SyncActionDescriptor): ICommandHandler {
return (accessor, args) => { return (accessor, args) => {
const messageService = accessor.get(IMessageService); const messageService = accessor.get(IMessageService);
const instantiationService = accessor.get(IInstantiationService); const instantiationService = accessor.get(IInstantiationService);
const telemetryService = accessor.get(ITelemetryService); const telemetryService = accessor.get(ITelemetryService);
const partService = accessor.get(IPartService); const partService = accessor.get(IPartService);
TPromise.as(triggerAndDisposeAction(instantiationService, telemetryService, partService, descriptor, args)).done(null, (err) => { TPromise.as(this._triggerAndDisposeAction(instantiationService, telemetryService, partService, descriptor, args)).done(null, (err) => {
messageService.show(Severity.Error, err); messageService.show(Severity.Error, err);
}); });
}; };
} }
private _triggerAndDisposeAction(instantitationService: IInstantiationService, telemetryService: ITelemetryService, partService: IPartService, descriptor: SyncActionDescriptor, args: any): TPromise<any> {
const actionInstance = instantitationService.createInstance(descriptor.syncDescriptor);
actionInstance.label = descriptor.label || actionInstance.label;
function triggerAndDisposeAction(instantitationService: IInstantiationService, telemetryService: ITelemetryService, partService: IPartService, descriptor: SyncActionDescriptor, args: any): TPromise<any> { // don't run the action when not enabled
const actionInstance = instantitationService.createInstance(descriptor.syncDescriptor); if (!actionInstance.enabled) {
actionInstance.label = descriptor.label || actionInstance.label; actionInstance.dispose();
// don't run the action when not enabled return void 0;
if (!actionInstance.enabled) { }
actionInstance.dispose();
return void 0; const from = args && args.from || 'keybinding';
} if (telemetryService) {
/* __GDPR__
"workbenchActionExecuted" : {
"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
telemetryService.publicLog('workbenchActionExecuted', { id: actionInstance.id, from });
}
const from = args && args.from || 'keybinding'; // run action when workbench is created
if (telemetryService) { return partService.joinCreation().then(() => {
/* __GDPR__ try {
"workbenchActionExecuted" : { return TPromise.as(actionInstance.run(undefined, { from })).then(() => {
"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }, actionInstance.dispose();
"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" } }, (err) => {
actionInstance.dispose();
return TPromise.wrapError(err);
});
} catch (err) {
actionInstance.dispose();
return TPromise.wrapError(err);
} }
*/ });
telemetryService.publicLog('workbenchActionExecuted', { id: actionInstance.id, from });
} }
});
// run action when workbench is created
return partService.joinCreation().then(() => {
try {
return TPromise.as(actionInstance.run(undefined, { from })).then(() => {
actionInstance.dispose();
}, (err) => {
actionInstance.dispose();
return TPromise.wrapError(err);
});
} catch (err) {
actionInstance.dispose();
return TPromise.wrapError(err);
}
});
}
...@@ -13,7 +13,7 @@ import { Registry } from 'vs/platform/registry/common/platform'; ...@@ -13,7 +13,7 @@ import { Registry } from 'vs/platform/registry/common/platform';
import { ResolvedKeybinding, ResolvedKeybindingPart } from 'vs/base/common/keyCodes'; import { ResolvedKeybinding, ResolvedKeybindingPart } from 'vs/base/common/keyCodes';
import { AriaLabelProvider, UserSettingsLabelProvider, UILabelProvider, ModifierLabels as ModLabels } from 'vs/base/common/keybindingLabels'; import { AriaLabelProvider, UserSettingsLabelProvider, UILabelProvider, ModifierLabels as ModLabels } from 'vs/base/common/keybindingLabels';
import { CommonEditorRegistry, EditorAction } from 'vs/editor/common/editorCommonExtensions'; import { CommonEditorRegistry, EditorAction } from 'vs/editor/common/editorCommonExtensions';
import { MenuRegistry, ILocalizedString, SyncActionDescriptor, ICommandAction } from 'vs/platform/actions/common/actions'; import { MenuRegistry, ILocalizedString, ICommandAction } from 'vs/platform/actions/common/actions';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions'; import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
import { EditorModel } from 'vs/workbench/common/editor'; import { EditorModel } from 'vs/workbench/common/editor';
import { IExtensionService } from 'vs/platform/extensions/common/extensions'; import { IExtensionService } from 'vs/platform/extensions/common/extensions';
...@@ -200,26 +200,21 @@ export class KeybindingsEditorModel extends EditorModel { ...@@ -200,26 +200,21 @@ export class KeybindingsEditorModel extends EditorModel {
} }
private static toKeybindingEntry(command: string, keybindingItem: ResolvedKeybindingItem, workbenchActionsRegistry: IWorkbenchActionRegistry, editorActions: {}): IKeybindingItem { private static toKeybindingEntry(command: string, keybindingItem: ResolvedKeybindingItem, workbenchActionsRegistry: IWorkbenchActionRegistry, editorActions: {}): IKeybindingItem {
const workbenchAction = workbenchActionsRegistry.getWorkbenchAction(command);
const menuCommand = MenuRegistry.getCommand(command); const menuCommand = MenuRegistry.getCommand(command);
const editorAction: EditorAction = editorActions[command]; const editorAction: EditorAction = editorActions[command];
return <IKeybindingItem>{ return <IKeybindingItem>{
keybinding: keybindingItem.resolvedKeybinding, keybinding: keybindingItem.resolvedKeybinding,
keybindingItem, keybindingItem,
command, command,
commandLabel: KeybindingsEditorModel.getCommandLabel(workbenchAction, menuCommand, editorAction), commandLabel: KeybindingsEditorModel.getCommandLabel(menuCommand, editorAction),
commandDefaultLabel: KeybindingsEditorModel.getCommandDefaultLabel(workbenchAction, menuCommand, workbenchActionsRegistry), commandDefaultLabel: KeybindingsEditorModel.getCommandDefaultLabel(menuCommand, workbenchActionsRegistry),
when: keybindingItem.when ? keybindingItem.when.serialize() : '', when: keybindingItem.when ? keybindingItem.when.serialize() : '',
source: keybindingItem.isDefault ? localize('default', "Default") : localize('user', "User") source: keybindingItem.isDefault ? localize('default', "Default") : localize('user', "User")
}; };
} }
private static getCommandDefaultLabel(workbenchAction: SyncActionDescriptor, menuCommand: ICommandAction, workbenchActionsRegistry: IWorkbenchActionRegistry): string { private static getCommandDefaultLabel(menuCommand: ICommandAction, workbenchActionsRegistry: IWorkbenchActionRegistry): string {
if (language !== LANGUAGE_DEFAULT) { if (language !== LANGUAGE_DEFAULT) {
if (workbenchAction) {
return workbenchActionsRegistry.getAlias(workbenchAction.id);
}
if (menuCommand && menuCommand.title && (<ILocalizedString>menuCommand.title).original) { if (menuCommand && menuCommand.title && (<ILocalizedString>menuCommand.title).original) {
return (<ILocalizedString>menuCommand.title).original; return (<ILocalizedString>menuCommand.title).original;
} }
...@@ -227,11 +222,7 @@ export class KeybindingsEditorModel extends EditorModel { ...@@ -227,11 +222,7 @@ export class KeybindingsEditorModel extends EditorModel {
return null; return null;
} }
private static getCommandLabel(workbenchAction: SyncActionDescriptor, menuCommand: ICommandAction, editorAction: EditorAction): string { private static getCommandLabel(menuCommand: ICommandAction, editorAction: EditorAction): string {
if (workbenchAction) {
return workbenchAction.label;
}
if (menuCommand) { if (menuCommand) {
return typeof menuCommand.title === 'string' ? menuCommand.title : menuCommand.title.value; return typeof menuCommand.title === 'string' ? menuCommand.title : menuCommand.title.value;
} }
...@@ -560,4 +551,4 @@ class KeybindingItemMatches { ...@@ -560,4 +551,4 @@ class KeybindingItemMatches {
} }
return false; return false;
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册