Skip to content

Commit 7f6ed01

Browse files
committed
Adopt @editorAction
1 parent 838ada3 commit 7f6ed01

51 files changed

Lines changed: 228 additions & 302 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/vs/editor/common/editorCommonExtensions.ts

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,96 @@ export const Command = ConfigBasicCommand;
2323
export const EditorCommand = ConfigEditorCommand;
2424
export type ICommandOptions = ICommandOptions;
2525

26-
// --- Keybinding extensions to make it more concise to express keybindings conditions
2726
export interface IEditorCommandMenuOptions {
2827
group?: string;
2928
order?: number;
3029
}
30+
export interface IActionOptions extends ICommandOptions {
31+
label: string;
32+
alias: string;
33+
menuOpts?: IEditorCommandMenuOptions;
34+
}
35+
export abstract class EditorAction extends ConfigEditorCommand {
36+
37+
public label: string;
38+
public alias: string;
39+
private menuOpts: IEditorCommandMenuOptions;
40+
41+
constructor(opts:IActionOptions) {
42+
super(opts);
43+
this.label = opts.label;
44+
this.alias = opts.alias;
45+
this.menuOpts = opts.menuOpts;
46+
}
47+
48+
public toMenuItem(): IMenuItem {
49+
if (!this.menuOpts) {
50+
return null;
51+
}
52+
53+
return {
54+
command: {
55+
id: this.id,
56+
title: this.label
57+
},
58+
when: this.precondition,
59+
group: this.menuOpts.group,
60+
order: this.menuOpts.order
61+
};
62+
}
63+
64+
public runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise<void> {
65+
accessor.get(ITelemetryService).publicLog('editorActionInvoked', { name: this.label, id: this.id });
66+
return this.run(accessor, editor);
67+
}
68+
69+
public abstract run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void | TPromise<void>;
70+
}
71+
72+
export interface IHandlerActionOptions extends IActionOptions {
73+
handlerId: string;
74+
}
75+
export abstract class HandlerEditorAction extends EditorAction {
76+
private _handlerId: string;
77+
78+
constructor(opts: IHandlerActionOptions) {
79+
super(opts);
80+
this._handlerId = opts.handlerId;
81+
}
82+
83+
public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void {
84+
editor.trigger(this.id, this._handlerId, null);
85+
}
86+
}
3187

3288
// --- Editor Actions
3389

90+
export function editorAction(constructor:{ new(): EditorAction; }): void {
91+
CommonEditorRegistry.registerEditorAction(new constructor());
92+
}
93+
3494
export module CommonEditorRegistry {
3595

96+
// --- Editor Actions
97+
3698
export function registerEditorAction(desc:EditorAction) {
37-
(<EditorContributionRegistry>Registry.as(Extensions.EditorCommonContributions)).registerEditorAction(desc);
99+
EditorContributionRegistry.INSTANCE.registerEditorAction(desc);
38100
}
39-
40101
export function getEditorActions(): EditorAction[] {
41-
return (<EditorContributionRegistry>Registry.as(Extensions.EditorCommonContributions)).getEditorActions();
102+
return EditorContributionRegistry.INSTANCE.getEditorActions();
42103
}
43104

44105
// --- Editor Contributions
106+
45107
export function registerEditorContribution(ctor:editorCommon.ICommonEditorContributionCtor): void {
46-
(<EditorContributionRegistry>Registry.as(Extensions.EditorCommonContributions)).registerEditorContribution(ctor);
108+
EditorContributionRegistry.INSTANCE.registerEditorContribution(ctor);
47109
}
48110
export function getEditorContributions(): editorCommon.ICommonEditorContributionDescriptor[] {
49-
return (<EditorContributionRegistry>Registry.as(Extensions.EditorCommonContributions)).getEditorContributions();
111+
return EditorContributionRegistry.INSTANCE.getEditorContributions();
50112
}
51113

52114
// --- Editor Commands
115+
53116
export function commandWeight(importance: number = 0): number {
54117
return KeybindingsRegistry.WEIGHT.editorContrib(importance);
55118
}
@@ -101,6 +164,8 @@ var Extensions = {
101164

102165
class EditorContributionRegistry {
103166

167+
public static INSTANCE = new EditorContributionRegistry();
168+
104169
private editorContributions: editorCommon.ICommonEditorContributionDescriptor[];
105170
private editorActions: EditorAction[];
106171

@@ -133,64 +198,4 @@ class EditorContributionRegistry {
133198
return this.editorActions.slice(0);
134199
}
135200
}
136-
Registry.add(Extensions.EditorCommonContributions, new EditorContributionRegistry());
137-
138-
export interface IActionOptions extends ICommandOptions {
139-
label: string;
140-
alias: string;
141-
menuOpts?: IEditorCommandMenuOptions;
142-
}
143-
144-
export abstract class EditorAction extends ConfigEditorCommand {
145-
146-
public label: string;
147-
public alias: string;
148-
private menuOpts: IEditorCommandMenuOptions;
149-
150-
constructor(opts:IActionOptions) {
151-
super(opts);
152-
this.label = opts.label;
153-
this.alias = opts.alias;
154-
this.menuOpts = opts.menuOpts;
155-
}
156-
157-
public toMenuItem(): IMenuItem {
158-
if (!this.menuOpts) {
159-
return null;
160-
}
161-
162-
return {
163-
command: {
164-
id: this.id,
165-
title: this.label
166-
},
167-
when: this.precondition,
168-
group: this.menuOpts.group,
169-
order: this.menuOpts.order
170-
};
171-
}
172-
173-
public runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise<void> {
174-
accessor.get(ITelemetryService).publicLog('editorActionInvoked', { name: this.label, id: this.id });
175-
return this.run(accessor, editor);
176-
}
177-
178-
public abstract run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void | TPromise<void>;
179-
}
180-
181-
export interface IHandlerActionOptions extends IActionOptions {
182-
handlerId: string;
183-
}
184-
185-
export abstract class HandlerEditorAction extends EditorAction {
186-
private _handlerId: string;
187-
188-
constructor(opts: IHandlerActionOptions) {
189-
super(opts);
190-
this._handlerId = opts.handlerId;
191-
}
192-
193-
public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void {
194-
editor.trigger(this.id, this._handlerId, null);
195-
}
196-
}
201+
Registry.add(Extensions.EditorCommonContributions, EditorContributionRegistry.INSTANCE);

src/vs/editor/contrib/accessibility/browser/accessibility.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {KbCtxKey, IKeybindingContextKey, IKeybindingService} from 'vs/platform/k
1919
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
2020
import {GlobalScreenReaderNVDA} from 'vs/editor/common/config/commonEditorConfig';
2121
import {ICommonCodeEditor, IEditorContribution, EditorContextKeys} from 'vs/editor/common/editorCommon';
22-
import {CommonEditorRegistry, EditorAction, EditorCommand, Command} from 'vs/editor/common/editorCommonExtensions';
22+
import {editorAction, CommonEditorRegistry, EditorAction, EditorCommand, Command} from 'vs/editor/common/editorCommonExtensions';
2323
import {ICodeEditor, IOverlayWidget, IOverlayWidgetPosition} from 'vs/editor/browser/editorBrowser';
2424
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
2525
import {ToggleTabFocusModeAction} from 'vs/editor/contrib/toggleTabFocusMode/common/toggleTabFocusMode';
@@ -189,6 +189,7 @@ class AccessibilityHelpWidget extends Widget implements IOverlayWidget {
189189
}
190190
}
191191

192+
@editorAction
192193
class ShowAccessibilityHelpAction extends EditorAction {
193194

194195
constructor() {
@@ -211,7 +212,6 @@ class ShowAccessibilityHelpAction extends EditorAction {
211212
}
212213

213214
EditorBrowserRegistry.registerEditorContribution(AccessibilityHelpController);
214-
CommonEditorRegistry.registerEditorAction(new ShowAccessibilityHelpAction());
215215

216216
const AccessibilityHelpCommand = EditorCommand.bindToContribution<AccessibilityHelpController>(AccessibilityHelpController.get);
217217

src/vs/editor/contrib/carretOperations/common/carretOperations.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as nls from 'vs/nls';
88
import {ICommand, ICommonCodeEditor, EditorContextKeys} from 'vs/editor/common/editorCommon';
9-
import {IActionOptions, EditorAction, CommonEditorRegistry, ServicesAccessor} from 'vs/editor/common/editorCommonExtensions';
9+
import {IActionOptions, editorAction, EditorAction, ServicesAccessor} from 'vs/editor/common/editorCommonExtensions';
1010
import {MoveCarretCommand} from './moveCarretCommand';
1111

1212
class MoveCarretAction extends EditorAction {
@@ -32,8 +32,8 @@ class MoveCarretAction extends EditorAction {
3232
}
3333
}
3434

35+
@editorAction
3536
class MoveCarretLeftAction extends MoveCarretAction {
36-
3737
constructor() {
3838
super(true, {
3939
id: 'editor.action.moveCarretLeftAction',
@@ -44,8 +44,8 @@ class MoveCarretLeftAction extends MoveCarretAction {
4444
}
4545
}
4646

47+
@editorAction
4748
class MoveCarretRightAction extends MoveCarretAction {
48-
4949
constructor() {
5050
super(false, {
5151
id: 'editor.action.moveCarretRightAction',
@@ -55,6 +55,3 @@ class MoveCarretRightAction extends MoveCarretAction {
5555
});
5656
}
5757
}
58-
59-
CommonEditorRegistry.registerEditorAction(new MoveCarretLeftAction());
60-
CommonEditorRegistry.registerEditorAction(new MoveCarretRightAction());

src/vs/editor/contrib/clipboard/browser/clipboard.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ import * as browser from 'vs/base/browser/browser';
1212
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
1313
import {findFocusedEditor} from 'vs/editor/common/config/config';
1414
import * as editorCommon from 'vs/editor/common/editorCommon';
15-
import {IActionOptions, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
15+
import {editorAction, IActionOptions, EditorAction} from 'vs/editor/common/editorCommonExtensions';
1616

1717
import EditorContextKeys = editorCommon.EditorContextKeys;
1818

1919
const CLIPBOARD_CONTEXT_MENU_GROUP = '9_cutcopypaste';
2020

21+
function conditionalEditorAction(testCommand:string) {
22+
if (!browser.supportsExecCommand(testCommand)) {
23+
return () => {};
24+
}
25+
return editorAction;
26+
}
27+
2128
abstract class ExecCommandAction extends EditorAction {
2229

2330
private browserCommand:string;
@@ -44,6 +51,7 @@ abstract class ExecCommandAction extends EditorAction {
4451
}
4552
}
4653

54+
@conditionalEditorAction('cut')
4755
class ExecCommandCutAction extends ExecCommandAction {
4856

4957
constructor() {
@@ -73,6 +81,7 @@ class ExecCommandCutAction extends ExecCommandAction {
7381
}
7482
}
7583

84+
@conditionalEditorAction('copy')
7685
class ExecCommandCopyAction extends ExecCommandAction {
7786

7887
constructor() {
@@ -102,6 +111,7 @@ class ExecCommandCopyAction extends ExecCommandAction {
102111
}
103112
}
104113

114+
@conditionalEditorAction('paste')
105115
class ExecCommandPasteAction extends ExecCommandAction {
106116

107117
constructor() {
@@ -122,13 +132,3 @@ class ExecCommandPasteAction extends ExecCommandAction {
122132
});
123133
}
124134
}
125-
126-
if (browser.supportsExecCommand('cut')) {
127-
CommonEditorRegistry.registerEditorAction(new ExecCommandCutAction());
128-
}
129-
if (browser.supportsExecCommand('copy')) {
130-
CommonEditorRegistry.registerEditorAction(new ExecCommandCopyAction());
131-
}
132-
if (browser.supportsExecCommand('paste')) {
133-
CommonEditorRegistry.registerEditorAction(new ExecCommandPasteAction());
134-
}

src/vs/editor/contrib/comment/common/comment.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import * as nls from 'vs/nls';
88
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
99
import {ICommand, ICommonCodeEditor, EditorContextKeys} from 'vs/editor/common/editorCommon';
10-
import {IActionOptions, EditorAction, CommonEditorRegistry, ServicesAccessor} from 'vs/editor/common/editorCommonExtensions';
10+
import {editorAction, IActionOptions, EditorAction, ServicesAccessor} from 'vs/editor/common/editorCommonExtensions';
1111
import {BlockCommentCommand} from './blockCommentCommand';
1212
import {LineCommentCommand, Type} from './lineCommentCommand';
1313

@@ -39,6 +39,7 @@ abstract class CommentLineAction extends EditorAction {
3939

4040
}
4141

42+
@editorAction
4243
class ToggleCommentLineAction extends CommentLineAction {
4344
constructor() {
4445
super(Type.Toggle, {
@@ -54,6 +55,7 @@ class ToggleCommentLineAction extends CommentLineAction {
5455
}
5556
}
5657

58+
@editorAction
5759
class AddLineCommentAction extends CommentLineAction {
5860
constructor() {
5961
super(Type.ForceAdd, {
@@ -69,6 +71,7 @@ class AddLineCommentAction extends CommentLineAction {
6971
}
7072
}
7173

74+
@editorAction
7275
class RemoveLineCommentAction extends CommentLineAction {
7376
constructor() {
7477
super(Type.ForceRemove, {
@@ -84,6 +87,7 @@ class RemoveLineCommentAction extends CommentLineAction {
8487
}
8588
}
8689

90+
@editorAction
8791
class BlockCommentAction extends EditorAction {
8892

8993
constructor() {
@@ -111,9 +115,3 @@ class BlockCommentAction extends EditorAction {
111115
editor.executeCommands(this.id, commands);
112116
}
113117
}
114-
115-
// register actions
116-
CommonEditorRegistry.registerEditorAction(new ToggleCommentLineAction());
117-
CommonEditorRegistry.registerEditorAction(new AddLineCommentAction());
118-
CommonEditorRegistry.registerEditorAction(new RemoveLineCommentAction());
119-
CommonEditorRegistry.registerEditorAction(new BlockCommentAction());

src/vs/editor/contrib/contextmenu/browser/contextmenu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {IContextMenuService, IContextViewService} from 'vs/platform/contextview/
1616
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
1717
import {IMenuService, IMenu, MenuId} from 'vs/platform/actions/common/actions';
1818
import {ICommonCodeEditor, IEditorContribution, MouseTargetType, EditorContextKeys} from 'vs/editor/common/editorCommon';
19-
import {ServicesAccessor, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
19+
import {editorAction, ServicesAccessor, EditorAction} from 'vs/editor/common/editorCommonExtensions';
2020
import {ICodeEditor, IEditorMouseEvent} from 'vs/editor/browser/editorBrowser';
2121
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
2222

@@ -222,6 +222,7 @@ class ContextMenuController implements IEditorContribution {
222222
}
223223
}
224224

225+
@editorAction
225226
class ShowContextMenu extends EditorAction {
226227

227228
constructor() {
@@ -244,4 +245,3 @@ class ShowContextMenu extends EditorAction {
244245
}
245246

246247
EditorBrowserRegistry.registerEditorContribution(ContextMenuController);
247-
CommonEditorRegistry.registerEditorAction(new ShowContextMenu());

src/vs/editor/contrib/defineKeybinding/browser/defineKeybinding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {IOSupport} from 'vs/platform/keybinding/common/keybindingResolver';
1919
import {KbExpr, IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
2020
import {Range} from 'vs/editor/common/core/range';
2121
import * as editorCommon from 'vs/editor/common/editorCommon';
22-
import {ServicesAccessor, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
22+
import {editorAction, ServicesAccessor, EditorAction} from 'vs/editor/common/editorCommonExtensions';
2323
import {ICodeEditor, IOverlayWidget, IOverlayWidgetPosition, OverlayWidgetPositionPreference} from 'vs/editor/browser/editorBrowser';
2424
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
2525
import {CodeSnippet, getSnippetController} from 'vs/editor/contrib/snippet/common/snippet';
@@ -445,6 +445,7 @@ class DefineKeybindingWidget implements IOverlayWidget {
445445
}
446446
}
447447

448+
@editorAction
448449
export class DefineKeybindingAction extends EditorAction {
449450

450451
static ID = 'editor.action.defineKeybinding';
@@ -485,4 +486,3 @@ function isInterestingEditorModel(editor:editorCommon.ICommonCodeEditor): boolea
485486
}
486487

487488
EditorBrowserRegistry.registerEditorContribution(DefineKeybindingController);
488-
CommonEditorRegistry.registerEditorAction(new DefineKeybindingAction());

0 commit comments

Comments
 (0)