提交 d2bac293 编写于 作者: A Alex Dima

Move editor command registration to coreCommands.ts

上级 bf562ce5
......@@ -11,8 +11,7 @@ import { IContextKeyService, ContextKeyExpr } from 'vs/platform/contextkey/commo
import { ICommandAndKeybindingRule, IKeybindings } from 'vs/platform/keybinding/common/keybindingsRegistry';
import * as editorCommon from 'vs/editor/common/editorCommon';
import { ICodeEditorService, getCodeEditor } from 'vs/editor/common/services/codeEditorService';
import { CommandsRegistry, ICommandHandler, ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import H = editorCommon.Handler;
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
export interface ICommandKeybindingsOptions extends IKeybindings {
kbExpr?: ContextKeyExpr;
......@@ -27,22 +26,20 @@ export interface ICommandOptions {
}
export abstract class Command {
public id: string;
public precondition: ContextKeyExpr;
private kbOpts: ICommandKeybindingsOptions;
private description: ICommandHandlerDescription;
public readonly id: string;
public readonly precondition: ContextKeyExpr;
private readonly _kbOpts: ICommandKeybindingsOptions;
private readonly _description: ICommandHandlerDescription;
constructor(opts: ICommandOptions) {
this.id = opts.id;
this.precondition = opts.precondition;
this.kbOpts = opts.kbOpts;
this.description = opts.description;
this._kbOpts = opts.kbOpts;
this._description = opts.description;
}
public abstract runCommand(accessor: ServicesAccessor, args: any): void | TPromise<void>;
public toCommandAndKeybindingRule(defaultWeight: number): ICommandAndKeybindingRule {
const kbOpts = this.kbOpts || { primary: 0 };
const kbOpts = this._kbOpts || { primary: 0 };
let kbWhen = kbOpts.kbExpr;
if (this.precondition) {
......@@ -65,23 +62,37 @@ export abstract class Command {
win: kbOpts.win,
linux: kbOpts.linux,
mac: kbOpts.mac,
description: this.description
description: this._description
};
}
public abstract runCommand(accessor: ServicesAccessor, args: any): void | TPromise<void>;
}
export interface IContributionCommandOptions<T> extends ICommandOptions {
handler: (controller: T) => void;
}
export interface EditorControllerCommand<T extends editorCommon.IEditorContribution> {
new (opts: IContributionCommandOptions<T>): EditorCommand;
}
export interface IContributionCommandOptions<T> extends ICommandOptions {
handler: (controller: T) => void;
function findFocusedEditor(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor {
return accessor.get(ICodeEditorService).getFocusedCodeEditor();
}
function getWorkbenchActiveEditor(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor {
const editorService = accessor.get(IEditorService);
let activeEditor = (<any>editorService).getActiveEditor && (<any>editorService).getActiveEditor();
return getCodeEditor(activeEditor);
}
export abstract class EditorCommand extends Command {
/**
* Create a command class that is bound to a certain editor contribution.
*/
public static bindToContribution<T extends editorCommon.IEditorContribution>(controllerGetter: (editor: editorCommon.ICommonCodeEditor) => T): EditorControllerCommand<T> {
return class EditorControllerCommandImpl extends EditorCommand {
private _callback: (controller: T) => void;
......@@ -100,19 +111,20 @@ export abstract class EditorCommand extends Command {
};
}
constructor(opts: ICommandOptions) {
super(opts);
}
public runCommand(accessor: ServicesAccessor, args: any): void | TPromise<void> {
// Find the editor with text focus
let editor = findFocusedEditor(accessor);
if (!editor) {
editor = getActiveEditorWidget(accessor);
// Fallback to use what the workbench considers the active editor
editor = getWorkbenchActiveEditor(accessor);
}
if (!editor) {
// well, at least we tried...
return;
}
return editor.invokeWithinContext((editorAccessor) => {
const kbService = editorAccessor.get(IContextKeyService);
if (!kbService.contextMatchesRules(this.precondition)) {
......@@ -126,47 +138,3 @@ export abstract class EditorCommand extends Command {
public abstract runEditorCommand(accessor: ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise<void>;
}
function findFocusedEditor(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor {
return accessor.get(ICodeEditorService).getFocusedCodeEditor();
}
function withCodeEditorFromCommandHandler(accessor: ServicesAccessor, callback: (editor: editorCommon.ICommonCodeEditor) => void): void {
let editor = findFocusedEditor(accessor);
if (editor) {
callback(editor);
}
}
function getActiveEditorWidget(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor {
const editorService = accessor.get(IEditorService);
let activeEditor = (<any>editorService).getActiveEditor && (<any>editorService).getActiveEditor();
return getCodeEditor(activeEditor);
}
function triggerEditorHandler(handlerId: string, accessor: ServicesAccessor, args: any): void {
withCodeEditorFromCommandHandler(accessor, (editor) => {
editor.trigger('keyboard', handlerId, args);
});
}
class CoreCommand extends Command {
public runCommand(accessor: ServicesAccessor, args: any): void {
triggerEditorHandler(this.id, accessor, args);
}
}
function registerOverwritableCommand(handlerId: string, handler: ICommandHandler): void {
CommandsRegistry.registerCommand(handlerId, handler);
CommandsRegistry.registerCommand('default:' + handlerId, handler);
}
function registerCoreDispatchCommand(handlerId: string): void {
registerOverwritableCommand(handlerId, triggerEditorHandler.bind(null, handlerId));
}
registerCoreDispatchCommand(H.Type);
registerCoreDispatchCommand(H.ReplacePreviousChar);
registerCoreDispatchCommand(H.CompositionStart);
registerCoreDispatchCommand(H.CompositionEnd);
registerCoreDispatchCommand(H.Paste);
registerCoreDispatchCommand(H.Cut);
......@@ -1522,7 +1522,7 @@ namespace Config {
return accessor.get(ICodeEditorService).getFocusedCodeEditor();
}
function getActiveEditorWidget(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor {
function getWorkbenchActiveEditor(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor {
const editorService = accessor.get(IEditorService);
let activeEditor = (<any>editorService).getActiveEditor && (<any>editorService).getActiveEditor();
return getCodeEditor(activeEditor);
......@@ -1532,7 +1532,13 @@ namespace Config {
KeybindingsRegistry.registerCommandAndKeybindingRule(command.toCommandAndKeybindingRule(CORE_WEIGHT));
}
class BaseTextInputAwareCommand extends Command {
/**
* A command that will:
* 1. invoke a command on the focused editor.
* 2. otherwise, invoke a browser built-in command on the `activeElement`.
* 3. otherwise, invoke a command on the workbench active editor.
*/
class EditorOrNativeTextInputCommand extends Command {
private readonly _editorHandler: string | EditorCommand;
private readonly _inputHandler: string;
......@@ -1559,7 +1565,7 @@ namespace Config {
}
// Redirecting to last active editor
let activeEditor = getActiveEditorWidget(accessor);
let activeEditor = getWorkbenchActiveEditor(accessor);
if (activeEditor) {
activeEditor.focus();
return this._runEditorHandler(activeEditor, args);
......@@ -1578,7 +1584,7 @@ namespace Config {
}
}
registerCommand(new BaseTextInputAwareCommand({
registerCommand(new EditorOrNativeTextInputCommand({
editorHandler: CoreNavigationCommands.SelectAll,
inputHandler: 'selectAll',
id: 'editor.action.selectAll',
......@@ -1590,7 +1596,7 @@ namespace Config {
}
}));
registerCommand(new BaseTextInputAwareCommand({
registerCommand(new EditorOrNativeTextInputCommand({
editorHandler: H.Undo,
inputHandler: 'undo',
id: H.Undo,
......@@ -1602,7 +1608,7 @@ namespace Config {
}
}));
registerCommand(new BaseTextInputAwareCommand({
registerCommand(new EditorOrNativeTextInputCommand({
editorHandler: H.Redo,
inputHandler: 'redo',
id: H.Redo,
......@@ -1616,4 +1622,41 @@ namespace Config {
}
}));
/**
* A command that will invoke a command on the focused editor.
*/
class EditorHandlerCommand extends Command {
private readonly _handlerId: string;
constructor(id: string, handlerId: string) {
super({
id: id,
precondition: null
});
this._handlerId = handlerId;
}
public runCommand(accessor: ServicesAccessor, args: any): void {
const editor = findFocusedEditor(accessor);
if (!editor) {
return;
}
editor.trigger('keyboard', this._handlerId, args);
}
}
function registerOverwritableCommand(handlerId: string): void {
registerCommand(new EditorHandlerCommand('default:' + handlerId, handlerId));
registerCommand(new EditorHandlerCommand(handlerId, handlerId));
}
registerOverwritableCommand(H.Type);
registerOverwritableCommand(H.ReplacePreviousChar);
registerOverwritableCommand(H.CompositionStart);
registerOverwritableCommand(H.CompositionEnd);
registerOverwritableCommand(H.Paste);
registerOverwritableCommand(H.Cut);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册