Skip to content

Commit 81ecff0

Browse files
committed
make Action2 an abstract class so that ctor is clear and so that we can do things before/after run
1 parent e7b0df3 commit 81ecff0

6 files changed

Lines changed: 264 additions & 223 deletions

File tree

src/vs/platform/actions/common/actions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,20 +348,20 @@ export class SyncActionDescriptor {
348348

349349
type OneOrN<T> = T | T[];
350350

351-
export interface IAction2Description extends ICommandAction {
351+
export interface IAction2Options extends ICommandAction {
352352
f1?: boolean;
353353
menu?: OneOrN<{ id: MenuId } & Omit<IMenuItem, 'command'>>;
354354
keybinding?: Omit<IKeybindingRule, 'id'>;
355355
}
356356

357-
export interface IAction2 {
358-
readonly desc: IAction2Description;
359-
run(accessor: ServicesAccessor, ...args: any): any;
357+
export abstract class Action2 {
358+
constructor(readonly desc: Readonly<IAction2Options>) { }
359+
abstract run(accessor: ServicesAccessor, ...args: any[]): any;
360360
}
361361

362-
export function registerAction2(action: IAction2): IDisposable {
362+
export function registerAction2(ctor: { new(): Action2 }): IDisposable {
363363
const disposables = new DisposableStore();
364-
364+
const action = new ctor();
365365
disposables.add(CommandsRegistry.registerCommand({
366366
id: action.desc.id,
367367
handler: (accessor, ...args) => action.run(accessor, ...args),

src/vs/workbench/contrib/bulkEdit/browser/bulkEdit.contribution.ts

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
2323
import { WorkbenchListFocusContextKey } from 'vs/platform/list/browser/listService';
2424
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
2525
import { URI } from 'vs/base/common/uri';
26-
import { MenuId, registerAction2, IAction2 } from 'vs/platform/actions/common/actions';
26+
import { MenuId, registerAction2, Action2 } from 'vs/platform/actions/common/actions';
2727
import { IEditorInput } from 'vs/workbench/common/editor';
2828
import type { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
2929

@@ -112,28 +112,30 @@ class BulkEditPreviewContribution {
112112

113113

114114
// CMD: accept
115-
registerAction2(new class ApplyAction implements IAction2 {
116-
117-
readonly desc = {
118-
id: 'refactorPreview.apply',
119-
title: { value: localize('apply', "Apply Refactoring"), original: 'Apply Refactoring' },
120-
category: localize('cat', "Refactor Preview"),
121-
icon: { id: 'codicon/check' },
122-
precondition: BulkEditPreviewContribution.ctxEnabled,
123-
menu: [{
124-
id: MenuId.ViewTitle,
125-
when: ContextKeyExpr.equals('view', BulkEditPane.ID),
126-
group: 'navigation'
127-
}, {
128-
id: MenuId.BulkEditPaneContext,
129-
order: 1
130-
}],
131-
keybinding: {
132-
weight: KeybindingWeight.WorkbenchContrib,
133-
when: BulkEditPreviewContribution.ctxEnabled,
134-
primary: KeyMod.Shift + KeyCode.Enter,
135-
}
136-
};
115+
registerAction2(class ApplyAction extends Action2 {
116+
117+
constructor() {
118+
super({
119+
id: 'refactorPreview.apply',
120+
title: { value: localize('apply', "Apply Refactoring"), original: 'Apply Refactoring' },
121+
category: localize('cat', "Refactor Preview"),
122+
icon: { id: 'codicon/check' },
123+
precondition: BulkEditPreviewContribution.ctxEnabled,
124+
menu: [{
125+
id: MenuId.ViewTitle,
126+
when: ContextKeyExpr.equals('view', BulkEditPane.ID),
127+
group: 'navigation'
128+
}, {
129+
id: MenuId.BulkEditPaneContext,
130+
order: 1
131+
}],
132+
keybinding: {
133+
weight: KeybindingWeight.WorkbenchContrib,
134+
when: BulkEditPreviewContribution.ctxEnabled,
135+
primary: KeyMod.Shift + KeyCode.Enter,
136+
}
137+
});
138+
}
137139

138140
run(accessor: ServicesAccessor): any {
139141
const panelService = accessor.get(IPanelService);
@@ -145,23 +147,25 @@ registerAction2(new class ApplyAction implements IAction2 {
145147
});
146148

147149
// CMD: discard
148-
registerAction2(new class DiscardAction implements IAction2 {
149-
150-
readonly desc = {
151-
id: 'refactorPreview.discard',
152-
title: { value: localize('Discard', "Discard Refactoring"), original: 'Discard Refactoring' },
153-
category: localize('cat', "Refactor Preview"),
154-
icon: { id: 'codicon/clear-all' },
155-
precondition: BulkEditPreviewContribution.ctxEnabled,
156-
menu: [{
157-
id: MenuId.ViewTitle,
158-
when: ContextKeyExpr.equals('view', BulkEditPane.ID),
159-
group: 'navigation'
160-
}, {
161-
id: MenuId.BulkEditPaneContext,
162-
order: 2
163-
}]
164-
};
150+
registerAction2(class DiscardAction extends Action2 {
151+
152+
constructor() {
153+
super({
154+
id: 'refactorPreview.discard',
155+
title: { value: localize('Discard', "Discard Refactoring"), original: 'Discard Refactoring' },
156+
category: localize('cat', "Refactor Preview"),
157+
icon: { id: 'codicon/clear-all' },
158+
precondition: BulkEditPreviewContribution.ctxEnabled,
159+
menu: [{
160+
id: MenuId.ViewTitle,
161+
when: ContextKeyExpr.equals('view', BulkEditPane.ID),
162+
group: 'navigation'
163+
}, {
164+
id: MenuId.BulkEditPaneContext,
165+
order: 2
166+
}]
167+
});
168+
}
165169

166170
run(accessor: ServicesAccessor): void | Promise<void> {
167171
const panelService = accessor.get(IPanelService);
@@ -174,24 +178,26 @@ registerAction2(new class DiscardAction implements IAction2 {
174178

175179

176180
// CMD: toggle
177-
registerAction2(new class ToggleAction implements IAction2 {
178-
179-
readonly desc = {
180-
id: 'refactorPreview.toggleCheckedState',
181-
title: { value: localize('toogleSelection', "Accept Change"), original: 'Accept Change' },
182-
category: localize('cat', "Refactor Preview"),
183-
precondition: BulkEditPreviewContribution.ctxEnabled,
184-
toggled: BulkEditPane.ctxChangeChecked,
185-
keybinding: {
186-
weight: KeybindingWeight.WorkbenchContrib,
187-
when: WorkbenchListFocusContextKey,
188-
primary: KeyCode.Space,
189-
},
190-
menu: {
191-
id: MenuId.BulkEditPaneContext,
192-
group: 'navigation'
193-
}
194-
};
181+
registerAction2(class ToggleAction extends Action2 {
182+
183+
constructor() {
184+
super({
185+
id: 'refactorPreview.toggleCheckedState',
186+
title: { value: localize('toogleSelection', "Accept Change"), original: 'Accept Change' },
187+
category: localize('cat', "Refactor Preview"),
188+
precondition: BulkEditPreviewContribution.ctxEnabled,
189+
toggled: BulkEditPane.ctxChangeChecked,
190+
keybinding: {
191+
weight: KeybindingWeight.WorkbenchContrib,
192+
when: WorkbenchListFocusContextKey,
193+
primary: KeyCode.Space,
194+
},
195+
menu: {
196+
id: MenuId.BulkEditPaneContext,
197+
group: 'navigation'
198+
}
199+
});
200+
}
195201

196202
run(accessor: ServicesAccessor): void | Promise<void> {
197203
const panelService = accessor.get(IPanelService);

src/vs/workbench/contrib/extensions/browser/extensions.contribution.ts

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { localize } from 'vs/nls';
77
import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes';
88
import { Registry } from 'vs/platform/registry/common/platform';
9-
import { SyncActionDescriptor, MenuRegistry, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
9+
import { SyncActionDescriptor, MenuRegistry, MenuId, registerAction2, Action2 } from 'vs/platform/actions/common/actions';
1010
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
1111
import { ExtensionsLabel, ExtensionsChannelId, PreferencesLabel, IExtensionManagementService, IExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionManagement';
1212
import { IExtensionManagementServerService, IExtensionTipsService } from 'vs/workbench/services/extensionManagement/common/extensionManagement';
@@ -341,16 +341,20 @@ MenuRegistry.appendMenuItem(MenuId.GlobalActivity, {
341341

342342
// Extension Context Menu
343343

344-
registerAction2({
345-
desc: {
346-
id: 'workbench.extensions.action.copyExtension',
347-
title: { value: localize('workbench.extensions.action.copyExtension', "Copy"), original: 'Copy' },
348-
menu: {
349-
id: MenuId.ExtensionContext,
350-
group: '1_copy'
351-
}
352-
},
353-
async run(accessor, extensionId: string) {
344+
registerAction2(class extends Action2 {
345+
346+
constructor() {
347+
super({
348+
id: 'workbench.extensions.action.copyExtension',
349+
title: { value: localize('workbench.extensions.action.copyExtension', "Copy"), original: 'Copy' },
350+
menu: {
351+
id: MenuId.ExtensionContext,
352+
group: '1_copy'
353+
}
354+
});
355+
}
356+
357+
async run(accessor: ServicesAccessor, extensionId: string) {
354358
const extensionWorkbenchService = accessor.get(IExtensionsWorkbenchService);
355359
let extension = extensionWorkbenchService.local.filter(e => areSameExtensions(e.identifier, { id: extensionId }))[0]
356360
|| (await extensionWorkbenchService.queryGallery({ names: [extensionId], pageSize: 1 }, CancellationToken.None)).firstPage[0];
@@ -367,33 +371,41 @@ registerAction2({
367371
}
368372
});
369373

370-
registerAction2({
371-
desc: {
372-
id: 'workbench.extensions.action.copyExtensionId',
373-
title: { value: localize('workbench.extensions.action.copyExtensionId', "Copy Extension Id"), original: 'Copy Extension Id' },
374-
menu: {
375-
id: MenuId.ExtensionContext,
376-
group: '1_copy'
377-
}
378-
},
379-
async run(accessor, id: string) {
374+
registerAction2(class extends Action2 {
375+
376+
constructor() {
377+
super({
378+
id: 'workbench.extensions.action.copyExtensionId',
379+
title: { value: localize('workbench.extensions.action.copyExtensionId', "Copy Extension Id"), original: 'Copy Extension Id' },
380+
menu: {
381+
id: MenuId.ExtensionContext,
382+
group: '1_copy'
383+
}
384+
});
385+
}
386+
387+
async run(accessor: ServicesAccessor, id: string) {
380388
await accessor.get(IClipboardService).writeText(id);
381-
},
389+
}
382390
});
383391

384-
registerAction2({
385-
desc: {
386-
id: 'workbench.extensions.action.configure',
387-
title: { value: localize('workbench.extensions.action.configure', "Configure..."), original: 'Configure...' },
388-
menu: {
389-
id: MenuId.ExtensionContext,
390-
group: '2_configure',
391-
when: ContextKeyExpr.and(ContextKeyExpr.equals('extensionStatus', 'installed'), ContextKeyExpr.has('extensionHasConfiguration'))
392-
}
393-
},
394-
async run(accessor, id: string) {
392+
registerAction2(class extends Action2 {
393+
394+
constructor() {
395+
super({
396+
id: 'workbench.extensions.action.configure',
397+
title: { value: localize('workbench.extensions.action.configure', "Configure..."), original: 'Configure...' },
398+
menu: {
399+
id: MenuId.ExtensionContext,
400+
group: '2_configure',
401+
when: ContextKeyExpr.and(ContextKeyExpr.equals('extensionStatus', 'installed'), ContextKeyExpr.has('extensionHasConfiguration'))
402+
}
403+
});
404+
}
405+
406+
async run(accessor: ServicesAccessor, id: string) {
395407
await accessor.get(IPreferencesService).openSettings(false, `@ext:${id}`);
396-
},
408+
}
397409
});
398410

399411
const workbenchRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);

0 commit comments

Comments
 (0)