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

More cleanup

上级 ce86f17e
......@@ -109,7 +109,7 @@ export class CodeEditorWidget extends CommonCodeEditor implements editorBrowser.
}
CommonEditorRegistry.getEditorActions().forEach((action) => {
let internalAction = new InternalEditorAction(action, this, this._instantiationService);
let internalAction = new InternalEditorAction(action, this, this._instantiationService, this._keybindingService);
this._actions[internalAction.id] = internalAction;
});
}
......
......@@ -582,7 +582,7 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom
public getSupportedActions(): editorCommon.IEditorAction[] {
let result = this.getActions();
result = result.filter(action => action.isSupported(true));
result = result.filter(action => action.isSupported());
return result;
}
......@@ -594,11 +594,8 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom
public trigger(source:string, handlerId:string, payload:any): void {
payload = payload || {};
var candidate = this.getAction(handlerId);
if(candidate !== null) {
if (candidate.enabled) {
this._telemetryService.publicLog('editorActionInvoked', {name: candidate.label, id: candidate.id} );
TPromise.as(candidate.run()).done(null, onUnexpectedError);
}
if (candidate !== null) {
TPromise.as(candidate.run()).done(null, onUnexpectedError);
} else {
if (!this.cursor) {
return;
......
......@@ -8,7 +8,7 @@ import {TPromise} from 'vs/base/common/winjs.base';
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
import {IEditorService} from 'vs/platform/editor/common/editor';
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingService, IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {ICommandAndKeybindingRule, KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import * as editorCommon from 'vs/editor/common/editorCommon';
import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
......@@ -111,6 +111,12 @@ export abstract class EditorCommand extends Command {
return;
}
return editor.invokeWithinContext((editorAccessor) => {
const kbService = accessor.get(IKeybindingService);
if (!kbService.contextMatchesRules(this.precondition)) {
// precondition does not hold
return;
}
return this.runEditorCommand(editorAccessor, editor, args);
});
}
......
......@@ -8,6 +8,7 @@ import {TPromise} from 'vs/base/common/winjs.base';
import {IActionDescriptor, ICommonCodeEditor, IEditorAction} from 'vs/editor/common/editorCommon';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {EditorAction} from 'vs/editor/common/editorCommonExtensions';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
export abstract class AbstractInternalEditorAction {
......@@ -28,28 +29,31 @@ export class InternalEditorAction extends AbstractInternalEditorAction implement
private _actual: EditorAction;
private _instantiationService:IInstantiationService;
constructor(actual:EditorAction, editor:ICommonCodeEditor, instantiationService:IInstantiationService) {
private _keybindingService:IKeybindingService;
constructor(
actual:EditorAction,
editor:ICommonCodeEditor,
@IInstantiationService instantiationService:IInstantiationService,
@IKeybindingService keybindingService:IKeybindingService
) {
super(actual.id, actual.label, actual.alias, editor);
this._actual = actual;
this._instantiationService = instantiationService;
this._keybindingService = keybindingService;
}
public get enabled():boolean {
return this._instantiationService.invokeFunction((accessor) => {
return this._actual.enabled(accessor, this._editor);
});
}
public isSupported(forceEditorTextFocus:boolean):boolean {
return this._instantiationService.invokeFunction((accessor) => {
return this._actual.supported(accessor, this._editor, forceEditorTextFocus);
});
public isSupported():boolean {
return this._keybindingService.contextMatchesRules(this._actual.precondition);
}
public run(): TPromise<void> {
if (!this.isSupported()) {
return TPromise.as(void 0);
}
return this._instantiationService.invokeFunction((accessor) => {
return TPromise.as(this._actual.run(accessor, this._editor));
return TPromise.as(this._actual.runEditorCommand(accessor, this._editor, null));
});
}
}
......@@ -64,11 +68,7 @@ export class DynamicEditorAction extends AbstractInternalEditorAction implements
this._run = descriptor.run;
}
public get enabled():boolean {
return true;
}
public isSupported(forceEditorTextFocus:boolean):boolean {
public isSupported():boolean {
return true;
}
......
......@@ -3481,8 +3481,7 @@ export interface IEditorAction {
id: string;
label: string;
alias: string;
enabled: boolean;
isSupported(forceEditorTextFocus:boolean):boolean;
isSupported():boolean;
run(): TPromise<void>;
}
......
......@@ -8,7 +8,6 @@ import {illegalArgument} from 'vs/base/common/errors';
import URI from 'vs/base/common/uri';
import {TPromise} from 'vs/base/common/winjs.base';
import {ServicesAccessor, IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {Registry} from 'vs/platform/platform';
......@@ -171,35 +170,11 @@ export abstract class EditorAction extends ConfigEditorCommand {
};
}
protected runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise<void> {
if (!this.enabled(accessor, editor)) {
return;
}
public runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise<void> {
accessor.get(ITelemetryService).publicLog('editorActionInvoked', { name: this.label, id: this.id });
return this.run(accessor, editor);
}
public enabled(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): boolean {
if (!this.supported(accessor, editor, false)) {
return false;
}
return true;
}
public supported(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor, forceEditorTextFocus:boolean): boolean {
const kbService = accessor.get(IKeybindingService);
let override = null;
if (forceEditorTextFocus) {
override = {
editorTextFocus: true
};
}
return kbService.contextMatchesRules(this.precondition, override);
}
public abstract run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void | TPromise<void>;
}
......
......@@ -75,7 +75,7 @@ class RemoveLineCommentAction extends CommentLineAction {
id: 'editor.action.removeCommentLine',
label: nls.localize('comment.line.remove', "Remove Line Comment"),
alias: 'Remove Line Comment',
precondition: EditorContextKeys.Writable
precondition: EditorContextKeys.Writable,
kbOpts: {
kbExpr: EditorContextKeys.TextFocus,
primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_U)
......
......@@ -52,13 +52,11 @@ export class EditorActionCommandEntry extends QuickOpenEntryGroup {
// Some actions are enabled only when editor has focus
this.editor.focus();
if (this.action.enabled) {
try {
let promise = this.action.run() || TPromise.as(null);
promise.done(null, onUnexpectedError);
} catch (error) {
onUnexpectedError(error);
}
try {
let promise = this.action.run() || TPromise.as(null);
promise.done(null, onUnexpectedError);
} catch (error) {
onUnexpectedError(error);
}
}, onUnexpectedError);
......
......@@ -2758,8 +2758,7 @@ declare module monaco.editor {
id: string;
label: string;
alias: string;
enabled: boolean;
isSupported(forceEditorTextFocus: boolean): boolean;
isSupported(): boolean;
run(): Promise<void>;
}
......
......@@ -186,14 +186,9 @@ export abstract class AbstractKeybindingService {
return new ScopedKeybindingService(this, this._onDidChangeContextKey, domNode);
}
public contextMatchesRules(rules: KbExpr, overrideKeys?:any): boolean {
public contextMatchesRules(rules: KbExpr): boolean {
const ctx = Object.create(null);
this.getContext(this._myContextId).fillInContext(ctx);
if (overrideKeys) {
for (let key in overrideKeys) {
ctx[key] = overrideKeys[key];
}
}
const result = KeybindingResolver.contextMatchesRules(ctx, rules);
// console.group(rules.serialize() + ' -> ' + result);
// rules.keys().forEach(key => { console.log(key, ctx[key]); });
......
......@@ -478,7 +478,7 @@ export interface IKeybindingService {
onDidChangeContext: Event<string[]>;
createKey<T>(key: string, defaultValue: T): IKeybindingContextKey<T>;
contextMatchesRules(rules: KbExpr, overrideKeys?:any): boolean;
contextMatchesRules(rules: KbExpr): boolean;
getContextValue<T>(key: string): T;
createScoped(domNode: IKeybindingScopeLocation): IKeybindingService;
......
......@@ -187,7 +187,7 @@ class EditorActionCommandEntry extends BaseCommandEntry {
if (mode === Mode.OPEN) {
// Use a timeout to give the quick open widget a chance to close itself first
TPromise.timeout(50).done(() => {
if (this.action && this.action.enabled) {
if (this.action) {
try {
this.telemetryService.publicLog('workbenchActionExecuted', { id: this.action.id, from: 'quick open' });
(this.action.run() || TPromise.as(null)).done(null, (err) => this.onError(err));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册