提交 5718cf2e 编写于 作者: A Alex Dima

Adopt EditorAction2 in comment actions

上级 377b177a
...@@ -6,111 +6,122 @@ ...@@ -6,111 +6,122 @@
import * as nls from 'vs/nls'; import * as nls from 'vs/nls';
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
import {TPromise} from 'vs/base/common/winjs.base'; import {ICommand, ICommonCodeEditor} from 'vs/editor/common/editorCommon';
import {EditorAction} from 'vs/editor/common/editorAction'; import {EditorKbExpr, EditorAction2, CommonEditorRegistry, ServicesAccessor} from 'vs/editor/common/editorCommonExtensions';
import {ICommand, ICommonCodeEditor, IEditorActionDescriptorData} from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry, ContextKey, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions';
import {BlockCommentCommand} from './blockCommentCommand'; import {BlockCommentCommand} from './blockCommentCommand';
import {LineCommentCommand, Type} from './lineCommentCommand'; import {LineCommentCommand, Type} from './lineCommentCommand';
import {KbExpr} from 'vs/platform/keybinding/common/keybinding';
class CommentLineAction extends EditorAction { abstract class CommentLineAction extends EditorAction2 {
static ID = 'editor.action.commentLine';
private _type: Type; private _type: Type;
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor, type: Type) { constructor(id:string, label:string, alias:string, type:Type) {
super(descriptor, editor); super(id, label, alias, true);
this._type = type; this._type = type;
} }
public run(): TPromise<void> { public run(accessor:ServicesAccessor, editor:ICommonCodeEditor): void {
let model = this.editor.getModel(); let model = editor.getModel();
if (!model) { if (!model) {
return; return;
} }
var commands: ICommand[] = []; var commands: ICommand[] = [];
var selections = this.editor.getSelections(); var selections = editor.getSelections();
var opts = model.getOptions(); var opts = model.getOptions();
for (var i = 0; i < selections.length; i++) { for (var i = 0; i < selections.length; i++) {
commands.push(new LineCommentCommand(selections[i], opts.tabSize, this._type)); commands.push(new LineCommentCommand(selections[i], opts.tabSize, this._type));
} }
this.editor.executeCommands(this.id, commands); editor.executeCommands(this.id, commands);
return TPromise.as(null);
} }
} }
class ToggleCommentLineAction extends CommentLineAction { class ToggleCommentLineAction extends CommentLineAction {
static ID = 'editor.action.commentLine'; constructor() {
super(
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor) { 'editor.action.commentLine',
super(descriptor, editor, Type.Toggle); nls.localize('comment.line', "Toggle Line Comment"),
'Toggle Line Comment',
Type.Toggle
);
this.kbOpts = {
kbExpr: KbExpr.and(EditorKbExpr.TextFocus, EditorKbExpr.Writable),
primary: KeyMod.CtrlCmd | KeyCode.US_SLASH
};
} }
} }
class AddLineCommentAction extends CommentLineAction { class AddLineCommentAction extends CommentLineAction {
static ID = 'editor.action.addCommentLine'; constructor() {
super(
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor) { 'editor.action.addCommentLine',
super(descriptor, editor, Type.ForceAdd); nls.localize('comment.line.add', "Add Line Comment"),
'Add Line Comment',
Type.ForceAdd
);
this.kbOpts = {
kbExpr: KbExpr.and(EditorKbExpr.TextFocus, EditorKbExpr.Writable),
primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_C)
};
} }
} }
class RemoveLineCommentAction extends CommentLineAction { class RemoveLineCommentAction extends CommentLineAction {
static ID = 'editor.action.removeCommentLine'; constructor() {
super(
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor) { 'editor.action.removeCommentLine',
super(descriptor, editor, Type.ForceRemove); nls.localize('comment.line.remove', "Remove Line Comment"),
'Remove Line Comment',
Type.ForceRemove
);
this.kbOpts = {
kbExpr: KbExpr.and(EditorKbExpr.TextFocus, EditorKbExpr.Writable),
primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_U)
};
} }
} }
class BlockCommentAction extends EditorAction { class BlockCommentAction extends EditorAction2 {
static ID = 'editor.action.blockComment'; constructor() {
super(
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor) { 'editor.action.blockComment',
super(descriptor, editor); nls.localize('comment.block', "Toggle Block Comment"),
'Toggle Block Comment',
true
);
this.kbOpts = {
kbExpr: KbExpr.and(EditorKbExpr.TextFocus, EditorKbExpr.Writable),
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_A,
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_A }
};
} }
public run(): TPromise<boolean> { public run(accessor:ServicesAccessor, editor:ICommonCodeEditor): void {
var commands: ICommand[] = []; var commands: ICommand[] = [];
var selections = this.editor.getSelections(); var selections = editor.getSelections();
for (var i = 0; i < selections.length; i++) { for (var i = 0; i < selections.length; i++) {
commands.push(new BlockCommentCommand(selections[i])); commands.push(new BlockCommentCommand(selections[i]));
} }
this.editor.executeCommands(this.id, commands); editor.executeCommands(this.id, commands);
return TPromise.as(null);
} }
} }
// register actions // register actions
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(ToggleCommentLineAction, ToggleCommentLineAction.ID, nls.localize('comment.line', "Toggle Line Comment"), { CommonEditorRegistry.registerEditorAction2(new ToggleCommentLineAction());
context: ContextKey.EditorTextFocus, CommonEditorRegistry.registerEditorAction2(new AddLineCommentAction());
primary: KeyMod.CtrlCmd | KeyCode.US_SLASH CommonEditorRegistry.registerEditorAction2(new RemoveLineCommentAction());
}, 'Toggle Line Comment')); CommonEditorRegistry.registerEditorAction2(new BlockCommentAction());
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(AddLineCommentAction, AddLineCommentAction.ID, nls.localize('comment.line.add', "Add Line Comment"), {
context: ContextKey.EditorTextFocus,
primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_C)
}, 'Add Line Comment'));
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(RemoveLineCommentAction, RemoveLineCommentAction.ID, nls.localize('comment.line.remove', "Remove Line Comment"), {
context: ContextKey.EditorTextFocus,
primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_U)
}, 'Remove Line Comment'));
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(BlockCommentAction, BlockCommentAction.ID, nls.localize('comment.block', "Toggle Block Comment"), {
context: ContextKey.EditorTextFocus,
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_A,
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_A }
}, 'Toggle Block Comment'));
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册