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

make the registry be just a forwarder

上级 65d45fd9
......@@ -87,7 +87,7 @@ export interface IMenuRegistry {
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);
......
......@@ -15,6 +15,7 @@ import { IMessageService } from 'vs/platform/message/common/message';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import Severity from 'vs/base/common/severity';
import { IDisposable, combinedDisposable } from 'vs/base/common/lifecycle';
export const Extensions = {
WorkbenchActions: 'workbench.contributions.actions'
......@@ -30,92 +31,22 @@ 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.
*/
registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): void;
/**
* 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;
registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): IDisposable;
}
interface IActionMeta {
alias: string;
category?: string;
}
Registry.add(Extensions.WorkbenchActions, new class implements IWorkbenchActionRegistry {
class WorkbenchActionRegistry implements IWorkbenchActionRegistry {
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;
registerWorkbenchAction(descriptor: SyncActionDescriptor, alias: string, category?: string): IDisposable {
return this._registerWorkbenchCommandFromAction(descriptor, alias, category);
}
public getCategory(id: string): string {
const commandAction = MenuRegistry.getCommand(id);
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 [];
}
}
private _registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, alias: string, category?: string): IDisposable {
let registrations: IDisposable[] = [];
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 {
CommandsRegistry.registerCommand(descriptor.id, createCommandHandler(descriptor));
{
// register keybinding
// keybinding
const when = descriptor.keybindingContext;
const weight = (typeof descriptor.keybindingWeight === 'undefined' ? KeybindingsRegistry.WEIGHT.workbenchContrib() : descriptor.keybindingWeight);
const keybindings = descriptor.keybindings;
......@@ -129,71 +60,78 @@ function registerWorkbenchCommandFromAction(descriptor: SyncActionDescriptor, al
mac: keybindings && keybindings.mac,
linux: keybindings && keybindings.linux
});
}
{
// register menu item
// 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) {
// slightly weird if-check required because of
// https://github.com/Microsoft/vscode/blob/d28ace31aa147596e35adf101a27768a048c79ec/src/vs/workbench/parts/files/browser/fileActions.contribution.ts#L194
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
command: {
id: descriptor.id,
title: { value: descriptor.label, original: alias },
category
}
});
const command = {
id: descriptor.id,
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 {
return (accessor, args) => {
const messageService = accessor.get(IMessageService);
const instantiationService = accessor.get(IInstantiationService);
const telemetryService = accessor.get(ITelemetryService);
const partService = accessor.get(IPartService);
private _createCommandHandler(descriptor: SyncActionDescriptor): ICommandHandler {
return (accessor, args) => {
const messageService = accessor.get(IMessageService);
const instantiationService = accessor.get(IInstantiationService);
const telemetryService = accessor.get(ITelemetryService);
const partService = accessor.get(IPartService);
TPromise.as(triggerAndDisposeAction(instantiationService, telemetryService, partService, descriptor, args)).done(null, (err) => {
messageService.show(Severity.Error, err);
});
};
}
TPromise.as(this._triggerAndDisposeAction(instantiationService, telemetryService, partService, descriptor, args)).done(null, (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> {
const actionInstance = instantitationService.createInstance(descriptor.syncDescriptor);
actionInstance.label = descriptor.label || actionInstance.label;
// don't run the action when not enabled
if (!actionInstance.enabled) {
actionInstance.dispose();
// don't run the action when not enabled
if (!actionInstance.enabled) {
actionInstance.dispose();
return void 0;
}
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';
if (telemetryService) {
/* __GDPR__
"workbenchActionExecuted" : {
"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// 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);
}
*/
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';
import { ResolvedKeybinding, ResolvedKeybindingPart } from 'vs/base/common/keyCodes';
import { AriaLabelProvider, UserSettingsLabelProvider, UILabelProvider, ModifierLabels as ModLabels } from 'vs/base/common/keybindingLabels';
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 { EditorModel } from 'vs/workbench/common/editor';
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
......@@ -200,26 +200,21 @@ export class KeybindingsEditorModel extends EditorModel {
}
private static toKeybindingEntry(command: string, keybindingItem: ResolvedKeybindingItem, workbenchActionsRegistry: IWorkbenchActionRegistry, editorActions: {}): IKeybindingItem {
const workbenchAction = workbenchActionsRegistry.getWorkbenchAction(command);
const menuCommand = MenuRegistry.getCommand(command);
const editorAction: EditorAction = editorActions[command];
return <IKeybindingItem>{
keybinding: keybindingItem.resolvedKeybinding,
keybindingItem,
command,
commandLabel: KeybindingsEditorModel.getCommandLabel(workbenchAction, menuCommand, editorAction),
commandDefaultLabel: KeybindingsEditorModel.getCommandDefaultLabel(workbenchAction, menuCommand, workbenchActionsRegistry),
commandLabel: KeybindingsEditorModel.getCommandLabel(menuCommand, editorAction),
commandDefaultLabel: KeybindingsEditorModel.getCommandDefaultLabel(menuCommand, workbenchActionsRegistry),
when: keybindingItem.when ? keybindingItem.when.serialize() : '',
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 (workbenchAction) {
return workbenchActionsRegistry.getAlias(workbenchAction.id);
}
if (menuCommand && menuCommand.title && (<ILocalizedString>menuCommand.title).original) {
return (<ILocalizedString>menuCommand.title).original;
}
......@@ -227,11 +222,7 @@ export class KeybindingsEditorModel extends EditorModel {
return null;
}
private static getCommandLabel(workbenchAction: SyncActionDescriptor, menuCommand: ICommandAction, editorAction: EditorAction): string {
if (workbenchAction) {
return workbenchAction.label;
}
private static getCommandLabel(menuCommand: ICommandAction, editorAction: EditorAction): string {
if (menuCommand) {
return typeof menuCommand.title === 'string' ? menuCommand.title : menuCommand.title.value;
}
......@@ -560,4 +551,4 @@ class KeybindingItemMatches {
}
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.
先完成此消息的编辑!
想要评论请 注册