提交 bd4e85d8 编写于 作者: J Johannes Rieken

move CommandRegistry out of KeybindingsRegistry

上级 739acb2b
......@@ -15,9 +15,10 @@ import {ConfigurationService, IContent, IStat} from 'vs/platform/configuration/c
import {IEditor, IEditorInput, IEditorOptions, IEditorService, IResourceInput, ITextEditorModel, Position} from 'vs/platform/editor/common/editor';
import {AbstractExtensionService, ActivatedExtension} from 'vs/platform/extensions/common/abstractExtensionService';
import {IExtensionDescription} from 'vs/platform/extensions/common/extensions';
import {ICommandHandler} from 'vs/platform/commands/common/commands';
import {KeybindingService} from 'vs/platform/keybinding/browser/keybindingServiceImpl';
import {IOSupport} from 'vs/platform/keybinding/common/keybindingResolver';
import {ICommandHandler, ICommandsMap, IKeybindingItem} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingItem} from 'vs/platform/keybinding/common/keybinding';
import {IConfirmation, IMessageService} from 'vs/platform/message/common/message';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import * as editorCommon from 'vs/editor/common/editorCommon';
......@@ -202,7 +203,7 @@ export class StandaloneKeybindingService extends KeybindingService {
private static LAST_GENERATED_ID = 0;
private _dynamicKeybindings: IKeybindingItem[];
private _dynamicCommands: ICommandsMap;
private _dynamicCommands: { [id: string]: ICommandHandler };
constructor(configurationService: IConfigurationService, messageService: IMessageService, domNode: HTMLElement) {
super(configurationService, messageService);
......
......@@ -9,7 +9,8 @@ import {IDisposable, dispose} from 'vs/base/common/lifecycle';
import {IContextViewService} from 'vs/platform/contextview/browser/contextView';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {AbstractKeybindingService} from 'vs/platform/keybinding/browser/keybindingServiceImpl';
import {ICommandHandler, IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandler} from 'vs/platform/commands/common/commands';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IActionDescriptor, ICodeEditorWidgetCreationOptions, IDiffEditorOptions, IModel, IModelChangedEvent, EventType} from 'vs/editor/common/editorCommon';
import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
......
......@@ -9,7 +9,8 @@ import URI from 'vs/base/common/uri';
import {SyncDescriptor1, createSyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {ICommandHandler, IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandler} from 'vs/platform/commands/common/commands';
import {ICommandDescriptor, KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {Registry} from 'vs/platform/platform';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
......
......@@ -10,7 +10,8 @@ import URI from 'vs/base/common/uri';
import {TPromise} from 'vs/base/common/winjs.base';
import {IEditorService} from 'vs/platform/editor/common/editor';
import {optional} from 'vs/platform/instantiation/common/instantiation';
import {ICommandHandler, IKeybindingService, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandler} from 'vs/platform/commands/common/commands';
import {IKeybindingService, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {Position} from 'vs/editor/common/core/position';
import {Range} from 'vs/editor/common/core/range';
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {TPromise} from 'vs/base/common/winjs.base';
import {TypeConstraint, validateConstraints} from 'vs/base/common/types';
import {IInstantiationService, ServicesAccessor, createDecorator} from 'vs/platform/instantiation/common/instantiation';
import {registerSingleton} from 'vs/platform/instantiation/common/extensions';
import {IExtensionService} from 'vs/platform/extensions/common/extensions';
export const ICommandService = createDecorator('commandService');
export interface ICommandService {
executeCommand<T>(commandId: string, ...args: any[]): TPromise<T>;
executeCommand(commandId: string, ...args: any[]): TPromise<any>;
}
export interface ICommandsMap {
[id: string]: ICommand;
}
export interface ICommandHandler {
(accessor: ServicesAccessor, ...args: any[]): void;
}
export interface ICommand {
handler: ICommandHandler;
description?: ICommandHandlerDescription;
}
export interface ICommandHandlerDescription {
description: string;
args: { name: string; description?: string; constraint?: TypeConstraint; }[];
returns?: string;
}
export interface ICommandRegistry {
registerCommand(id: string, command: ICommandHandler): void;
registerCommand(id: string, command: ICommand): void;
getCommand(id: string): ICommand;
getCommands(): ICommandsMap;
}
function isCommand(thing: any): thing is ICommand {
return typeof thing === 'object'
&& typeof (<ICommand>thing).handler === 'function'
&& (!(<ICommand>thing).description || typeof (<ICommand>thing).description === 'object');
}
export const CommandsRegistry: ICommandRegistry = new class implements ICommandRegistry {
private _commands: { [id: string]: ICommand } = Object.create(null);
registerCommand(id: string, commandOrDesc: ICommandHandler | ICommand): void {
// if (this._commands[id] !== void 0) {
// throw new Error(`command already exists: '${id}'`);
// }
if (!commandOrDesc) {
throw new Error(`invalid command`);
}
if (!isCommand(commandOrDesc)) {
// simple handler
this._commands[id] = { handler: commandOrDesc };
} else {
const {handler, description} = commandOrDesc;
if (description) {
// add argument validation if rich command metadata is provided
const constraints: TypeConstraint[] = [];
for (let arg of description.args) {
constraints.push(arg.constraint);
}
this._commands[id] = {
description,
handler(accessor, ...args: any[]) {
validateConstraints(args, constraints);
return handler(accessor, ...args);
}
};
} else {
// add as simple handler
this._commands[id] = { handler };
}
}
}
getCommand(id: string): ICommand {
return this._commands[id];
}
getCommands(): ICommandsMap {
return this._commands;
}
};
class DefaultCommandService implements ICommandService {
serviceId: any;
constructor(
@IInstantiationService private _instantiationService: IInstantiationService,
@IExtensionService private _extensionService: IExtensionService
) {
//
}
executeCommand<T>(id: string, ...args: any[]): TPromise<T> {
const command = CommandsRegistry.getCommand(id);
if (!command) {
return TPromise.wrapError(new Error(`command '${id}' not found`));
}
this._extensionService.activateByEvent(`onCommand:${id}`).then(_ => {
try {
const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command].concat(args));
return TPromise.as(result);
} catch (err) {
return TPromise.wrapError(err);
}
});
}
}
registerSingleton(ICommandService, DefaultCommandService);
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
suite('Command Tests', function () {
test('register command - no handler', function () {
assert.throws(() => CommandsRegistry.registerCommand('foo', null));
});
// test('register command - dupe', function () {
// CommandsRegistry.registerCommand('foo', () => { });
// assert.throws(() => CommandsRegistry.registerCommand('foo', () => { }));
// });
});
\ No newline at end of file
......@@ -15,8 +15,9 @@ import {TPromise} from 'vs/base/common/winjs.base';
import * as dom from 'vs/base/browser/dom';
import {IKeyboardEvent, StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {CommandsRegistry, ICommandHandler, ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import {KeybindingResolver} from 'vs/platform/keybinding/common/keybindingResolver';
import {ICommandHandler, ICommandHandlerDescription, IKeybindingContextKey, IKeybindingItem, IKeybindingScopeLocation, IKeybindingService, SET_CONTEXT_COMMAND_ID, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingContextKey, IKeybindingItem, IKeybindingScopeLocation, IKeybindingService, SET_CONTEXT_COMMAND_ID, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {IStatusbarService} from 'vs/platform/statusbar/common/statusbar';
import {IMessageService} from 'vs/platform/message/common/message';
......@@ -207,7 +208,7 @@ export abstract class AbstractKeybindingService {
}
public hasCommand(commandId: string): boolean {
return !!KeybindingsRegistry.getCommands()[commandId];
return !!CommandsRegistry.getCommands()[commandId];
}
public abstract executeCommand(commandId: string, args: any): TPromise<any>;
......@@ -312,7 +313,7 @@ export abstract class KeybindingService extends AbstractKeybindingService implem
}
private _getAllCommandsAsComment(): string {
const commands = KeybindingsRegistry.getCommands();
const commands = CommandsRegistry.getCommands();
const unboundCommands: string[] = [];
const boundCommands = this._getResolver().getDefaultBoundCommands();
......@@ -336,7 +337,7 @@ export abstract class KeybindingService extends AbstractKeybindingService implem
}
protected _getCommandHandler(commandId: string): ICommandHandler {
return KeybindingsRegistry.getCommands()[commandId];
return CommandsRegistry.getCommand(commandId).handler;
}
private _dispatch(e: IKeyboardEvent): void {
......
......@@ -6,9 +6,8 @@
import {IHTMLContentElement} from 'vs/base/common/htmlContent';
import {Keybinding} from 'vs/base/common/keyCodes';
import {TypeConstraint} from 'vs/base/common/types';
import {TPromise} from 'vs/base/common/winjs.base';
import {ServiceIdentifier, ServicesAccessor, createDecorator} from 'vs/platform/instantiation/common/instantiation';
import {ServiceIdentifier, createDecorator} from 'vs/platform/instantiation/common/instantiation';
import Event from 'vs/base/common/event';
export interface IUserFriendlyKeybinding {
......@@ -436,21 +435,6 @@ export interface IKeybindingItem {
weight2: number;
}
export interface ICommandHandler {
(accessor: ServicesAccessor, ...args: any[]): void;
description?: string | ICommandHandlerDescription;
}
export interface ICommandHandlerDescription {
description: string;
args: { name: string; description?: string; constraint?: TypeConstraint; }[];
returns?: string;
}
export interface ICommandsMap {
[id: string]: ICommandHandler;
}
export interface IKeybindingContextKey<T> {
set(value: T): void;
reset(): void;
......
......@@ -6,8 +6,8 @@
import {BinaryKeybindings, KeyCode} from 'vs/base/common/keyCodes';
import * as platform from 'vs/base/common/platform';
import {TypeConstraint, validateConstraints} from 'vs/base/common/types';
import {ICommandHandler, ICommandHandlerDescription, ICommandsMap, IKeybindingItem, IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingItem, IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {CommandsRegistry, ICommandHandler, ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import {Registry} from 'vs/platform/platform';
export interface ICommandRule extends IKeybindings {
......@@ -18,13 +18,12 @@ export interface ICommandRule extends IKeybindings {
export interface ICommandDescriptor extends ICommandRule {
handler: ICommandHandler;
description?: string | ICommandHandlerDescription;
description?: ICommandHandlerDescription;
}
export interface IKeybindingsRegistry {
registerCommandRule(rule: ICommandRule);
registerCommandDesc(desc: ICommandDescriptor): void;
getCommands(): ICommandsMap;
getDefaultKeybindings(): IKeybindingItem[];
WEIGHT: {
......@@ -39,7 +38,6 @@ export interface IKeybindingsRegistry {
class KeybindingsRegistryImpl implements IKeybindingsRegistry {
private _keybindings: IKeybindingItem[];
private _commands: ICommandsMap;
public WEIGHT = {
editorCore: (importance: number = 0): number => {
......@@ -61,7 +59,6 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
constructor() {
this._keybindings = [];
this._commands = Object.create(null);
}
/**
......@@ -101,36 +98,7 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
public registerCommandDesc(desc: ICommandDescriptor): void {
this.registerCommandRule(desc);
// if (_commands[desc.id]) {
// console.warn('Duplicate handler for command: ' + desc.id);
// }
// this._commands[desc.id] = desc.handler;
let handler = desc.handler;
let description = desc.description || handler.description;
// add argument validation if rich command metadata is provided
if (typeof description === 'object') {
let constraints: TypeConstraint[] = [];
for (let arg of description.args) {
constraints.push(arg.constraint);
}
handler = function (accesor, ...args: any[]) {
validateConstraints(args, constraints);
return desc.handler(accesor, ...args);
};
}
// make sure description is there
handler.description = description;
// register handler
this._commands[desc.id] = handler;
}
public getCommands(): ICommandsMap {
return this._commands;
CommandsRegistry.registerCommand(desc.id, desc);
}
private registerDefaultKeybinding(keybinding: number, commandId: string, weight1: number, weight2: number, when: KbExpr): void {
......
......@@ -6,6 +6,7 @@
import * as assert from 'assert';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
suite('Keybinding Registry', () => {
......@@ -23,7 +24,6 @@ suite('Keybinding Registry', () => {
KeybindingsRegistry.registerCommandDesc({
id: 'test2',
description: 'test',
when: undefined,
primary: undefined,
weight: 0,
......@@ -46,10 +46,10 @@ suite('Keybinding Registry', () => {
}
});
KeybindingsRegistry.getCommands()['test'].apply(undefined, [undefined, 'string']);
KeybindingsRegistry.getCommands()['test2'].apply(undefined, [undefined, 'string']);
assert.throws(() => KeybindingsRegistry.getCommands()['test3'].apply(undefined, [undefined, 'string']));
assert.equal(KeybindingsRegistry.getCommands()['test3'].apply(undefined, [undefined, 1]), true);
CommandsRegistry.getCommands()['test'].handler.apply(undefined, [undefined, 'string']);
CommandsRegistry.getCommands()['test2'].handler.apply(undefined, [undefined, 'string']);
assert.throws(() => CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 'string']));
assert.equal(CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 1]), true);
});
});
......@@ -12,7 +12,7 @@ import * as typeConverters from 'vs/workbench/api/node/extHostTypeConverters';
import * as types from 'vs/workbench/api/node/extHostTypes';
import {ISingleEditOperation} from 'vs/editor/common/editorCommon';
import * as modes from 'vs/editor/common/modes';
import {ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import {ExtHostCommands} from 'vs/workbench/api/node/extHostCommands';
import {IQuickFix2} from 'vs/editor/contrib/quickFix/common/quickFix';
import {IOutline} from 'vs/editor/contrib/quickOpen/common/quickOpen';
......
......@@ -6,7 +6,7 @@
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
import {validateConstraint} from 'vs/base/common/types';
import {ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import {TPromise} from 'vs/base/common/winjs.base';
import {ExtHostEditors} from 'vs/workbench/api/node/extHostEditors';
import * as extHostTypes from 'vs/workbench/api/node/extHostTypes';
......
......@@ -21,7 +21,7 @@ import {Position as EditorPosition} from 'vs/platform/editor/common/editor';
import {IMessage, IExtensionDescription} from 'vs/platform/extensions/common/extensions';
import {StatusbarAlignment as MainThreadStatusBarAlignment} from 'vs/platform/statusbar/common/statusbar';
import {ITelemetryInfo} from 'vs/platform/telemetry/common/telemetry';
import {ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import * as editorCommon from 'vs/editor/common/editorCommon';
import * as modes from 'vs/editor/common/modes';
......
......@@ -6,7 +6,9 @@
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {IKeybindingService, ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {CommandsRegistry, ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import {TPromise} from 'vs/base/common/winjs.base';
import {ExtHostContext, ExtHostCommandsShape} from './extHostProtocol';
......@@ -49,7 +51,7 @@ export class MainThreadCommands {
}
$getCommands(): Thenable<string[]> {
return TPromise.as(Object.keys(KeybindingsRegistry.getCommands()));
return TPromise.as(Object.keys(CommandsRegistry.getCommands()));
}
}
......@@ -61,7 +63,7 @@ KeybindingsRegistry.registerCommandDesc({
return accessor.get(IThreadService).get(ExtHostContext.ExtHostCommands).$getContributedCommandHandlerDescriptions().then(result => {
// add local commands
const commands = KeybindingsRegistry.getCommands();
const commands = CommandsRegistry.getCommands();
for (let id in commands) {
let {description} = commands[id];
if (description) {
......
......@@ -10,7 +10,7 @@ import {Registry} from 'vs/platform/platform';
import {IAction} from 'vs/base/common/actions';
import {KeybindingsRegistry, ICommandDescriptor} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {IPartService} from 'vs/workbench/services/part/common/partService';
import {ICommandHandler} from 'vs/platform/keybinding/common/keybinding';
import {ICommandHandler} from 'vs/platform/commands/common/commands';
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IMessageService} from 'vs/platform/message/common/message';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
......
......@@ -19,7 +19,7 @@ import {MarkerService} from 'vs/platform/markers/common/markerService';
import {IMarkerService} from 'vs/platform/markers/common/markers';
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
import {IModelService} from 'vs/editor/common/services/modelService';
import {ExtHostLanguageFeatures} from 'vs/workbench/api/node/extHostLanguageFeatures';
import {MainThreadLanguageFeatures} from 'vs/workbench/api/node/mainThreadLanguageFeatures';
......@@ -62,7 +62,7 @@ suite('ExtHostLanguageFeatureCommands', function() {
services.set(IKeybindingService, <IKeybindingService>{
executeCommand(id, args): any {
let handler = KeybindingsRegistry.getCommands()[id];
let {handler} = CommandsRegistry.getCommands()[id];
return TPromise.as(instantiationService.invokeFunction(handler, args));
}
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册