Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to dispose the Action #599

Merged
merged 14 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore:
- 'website'
- 'stories'
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
'**/__tests__/**/(*.)+(spec|test).[jt]s?(x)',
'**/test/**/(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: ['/node_modules/', 'esm', 'umd'],
testPathIgnorePatterns: ['/node_modules/', 'esm', 'umd', 'stories'],
// The directory where Jest should output its coverage files
coverageDirectory: 'coverage',
transformIgnorePatterns: ['node_modules/(?!(monaco-editor|.*dnd.*)/)'],
Expand Down
2 changes: 1 addition & 1 deletion src/controller/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { QuickCopyLineUp } from 'mo/monaco/quickCopyLineUp';
import { QuickUndo } from 'mo/monaco/quickUndo';
import { QuickRedo } from 'mo/monaco/quickRedo';
import { QuickCreateFile } from 'mo/monaco/quickCreateFile';
import type { Action2 } from 'mo/monaco/common';
import type { Action2 } from 'mo/monaco/action';

export interface IExtensionController extends Partial<Controller> {}

Expand Down
5 changes: 3 additions & 2 deletions src/i18n/selectLocaleAction.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'reflect-metadata';
import { container } from 'tsyringe';
import {
IQuickInputService,
QuickPickInput,
} from 'monaco-editor/esm/vs/platform/quickinput/common/quickInput';
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
import { container } from 'tsyringe';
import { Action2 } from 'mo/monaco/common';

import { localize } from './localize';
import { ILocaleService, LocaleService } from './localeService';
import { ILocale } from './localization';
import { Action2 } from 'mo/monaco/action';
import { KeyCode, KeyMod } from 'mo/monaco';
import { constants } from 'mo/services/builtinService/const';

Expand Down
96 changes: 96 additions & 0 deletions src/monaco/__tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands';
import { MenuRegistry } from 'monaco-editor/esm/vs/platform/actions/common/actions';
import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey';

import { Action2, registerAction2 } from 'mo/monaco/action';
import { KeybindingWeight } from '../common';

describe('Test monaco action', () => {
test('Register an Action via Action2 class', () => {
const mockRun = jest.fn();
class Test extends Action2 {
static readonly ID = 'TestAction';
static readonly LABEL = 'Test Action2';
constructor() {
super({
id: Test.ID,
label: Test.LABEL,
title: Test.LABEL,
alias: Test.LABEL,
precondition: true,
f1: true, // Whether show the QuickOpenFile in Command Palette
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
when: ContextKeyExpr.true,
},
menu: {
id: 'testMenu',
name: 'test',
},
});
}
run = mockRun;
}
const testAction = registerAction2(Test);
const command = CommandsRegistry.getCommand(Test.ID);
command.handler();
expect(testAction.dispose).not.toBeUndefined();
expect(command).not.toBeUndefined();
expect(mockRun).toBeCalled();
testAction.dispose();
});

test('Register Menus via Action2 class', () => {
class Test extends Action2 {
static readonly ID = 'TestMenu';
constructor() {
super({
id: Test.ID,
precondition: true,
f1: false, // Whether show the QuickOpenFile in Command Palette
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
when: ContextKeyExpr.true,
},
menu: [
{
id: 'testMenu1',
name: 'test1',
},
{
id: 'testMenu2',
name: 'test2',
},
],
});
}
run() {}
}
registerAction2(Test);
const menu = MenuRegistry.getMenuItems('testMenu1');
expect(menu.length).toBe(1);
});

test('Register Keybindings via Action2 class', () => {
class Test extends Action2 {
static readonly ID = 'testKeybinding';
constructor() {
super({
id: Test.ID,
precondition: false,
f1: true, // Whether show the QuickOpenFile in Command Palette
keybinding: [
{
weight: KeybindingWeight.WorkbenchContrib,
when: ContextKeyExpr.true,
},
],
});
}
run() {}
}
registerAction2(Test);
const command = CommandsRegistry.getCommand(Test.ID);
expect(command).not.toBeUndefined();
});
});
89 changes: 89 additions & 0 deletions src/monaco/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { DisposableStore } from 'monaco-editor/esm/vs/base/common/lifecycle';
import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey';
import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry';
import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands';
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
import {
MenuRegistry,
MenuId,
} from 'monaco-editor/esm/vs/platform/actions/common/actions';
import { IDisposable } from './common';

export abstract class Action2 {
static readonly ID: string;
constructor(
readonly desc: Readonly<{
/**
* Specify visible in quick access view
*/
f1: boolean;
[key: string]: any;
}>
) {}
abstract run(accessor: ServicesAccessor, ...args: any[]): any;
}

export function registerAction2(Ctor: { new (): Action2 }): IDisposable {
const disposables = new DisposableStore();

const action = new Ctor();

const { f1, menu, keybinding, description, ...command } = action.desc;

// command
disposables.add(
CommandsRegistry.registerCommand({
id: command.id,
handler: (accessor, ...args) => action.run(accessor, ...args),
description: description,
})
);

// menu
if (Array.isArray(menu)) {
disposables.add(
MenuRegistry.appendMenuItems(
menu.map((item) => ({
id: item.id,
item: { command, ...item },
}))
)
);
} else if (menu) {
disposables.add(
MenuRegistry.appendMenuItem(menu.id, { command, ...menu })
);
}
if (f1) {
disposables.add(
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
command,
when: command.precondition,
})
);
disposables.add(MenuRegistry.addCommand(command));
}

// keybinding
if (Array.isArray(keybinding)) {
for (const item of keybinding) {
KeybindingsRegistry.registerKeybindingRule({
...item,
id: command.id,
when: command.precondition
? ContextKeyExpr.and(command.precondition, item.when)
: item.when,
});
}
} else if (keybinding) {
KeybindingsRegistry.registerKeybindingRule({
...keybinding,
id: command.id,
when: command.precondition
? ContextKeyExpr.and(command.precondition, keybinding.when)
: keybinding.when,
});
}

return disposables;
}
3 changes: 2 additions & 1 deletion src/monaco/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { KeyChord } from 'monaco-editor/esm/vs/base/common/keyCodes';
export type { IQuickInputService } from 'monaco-editor/esm/vs/platform/quickinput/common/quickInput';
export { KeybindingWeight, Action2 } from './common';
export { KeybindingWeight } from './common';
export { Action2 } from './action';
94 changes: 4 additions & 90 deletions src/monaco/common.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import {
IDisposable,
DisposableStore,
} from 'monaco-editor/esm/vs/base/common/lifecycle';
import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey';
import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry';
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands';
import { localize } from 'monaco-editor/esm/vs/nls';
import {
MenuRegistry,
MenuId,
} from 'monaco-editor/esm/vs/platform/actions/common/actions';

export interface IDisposable {
dispose(): void;
}

export enum KeybindingWeight {
EditorCore = 0,
Expand Down Expand Up @@ -40,81 +32,3 @@ export const CATEGORIES = {
original: 'Developer',
},
};

export abstract class Action2 {
constructor(
readonly desc: Readonly<{
/**
* Specify visible in quick access view
*/
f1: boolean;
[key: string]: any;
}>
) {}
abstract run(accessor: ServicesAccessor, ...args: any[]): any;
}

export function registerAction2(ctor: { new (): Action2 }): IDisposable {
const disposables = new DisposableStore();
// eslint-disable-next-line new-cap
const action = new ctor();

const { f1, menu, keybinding, description, ...command } = action.desc;

// command
disposables.add(
CommandsRegistry.registerCommand({
id: command.id,
handler: (accessor, ...args) => action.run(accessor, ...args),
description: description,
})
);

// menu
if (Array.isArray(menu)) {
disposables.add(
MenuRegistry.appendMenuItems(
menu.map((item) => ({
id: item.id,
item: { command, ...item },
}))
)
);
} else if (menu) {
disposables.add(
MenuRegistry.appendMenuItem(menu.id, { command, ...menu })
);
}
if (f1) {
disposables.add(
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
command,
when: command.precondition,
})
);
disposables.add(MenuRegistry.addCommand(command));
}

// keybinding
if (Array.isArray(keybinding)) {
for (const item of keybinding) {
KeybindingsRegistry.registerKeybindingRule({
...item,
id: command.id,
when: command.precondition
? ContextKeyExpr.and(command.precondition, item.when)
: item.when,
});
}
} else if (keybinding) {
KeybindingsRegistry.registerKeybindingRule({
...keybinding,
id: command.id,
when: command.precondition
? ContextKeyExpr.and(command.precondition, keybinding.when)
: keybinding.when,
});
}

return disposables;
}
2 changes: 0 additions & 2 deletions src/monaco/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ import 'monaco-editor/esm/vs/editor/standalone/browser/referenceSearch/standalon
import 'monaco-editor/esm/vs/editor/standalone/browser/toggleHighContrast/toggleHighContrast';

export * from 'monaco-editor/esm/vs/editor/editor.api.js';

export { KeybindingWeight, Action2 } from './common';
10 changes: 6 additions & 4 deletions src/monaco/quickAccessSettingsAction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'reflect-metadata';
import { container } from 'tsyringe';
import { localize } from 'monaco-editor/esm/vs/nls';
import { KeyMod, KeyCode } from 'mo/monaco';
import { KeyChord } from 'monaco-editor/esm/vs/base/common/keyCodes';
import { ISettingsService, SettingsService } from 'mo/services';
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
import { container } from 'tsyringe';
import { Action2, KeybindingWeight } from './common';

import { KeyMod, KeyCode } from 'mo/monaco';
import { KeybindingWeight } from 'mo/monaco/common';
import { Action2 } from 'mo/monaco/action';
import { constants } from 'mo/services/builtinService/const';
import { ISettingsService, SettingsService } from 'mo/services';

export class QuickAccessSettings extends Action2 {
static readonly ID = constants.ACTION_QUICK_ACCESS_SETTINGS;
Expand Down
10 changes: 6 additions & 4 deletions src/monaco/quickAccessViewAction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'reflect-metadata';
import { container } from 'tsyringe';

import { localize } from 'monaco-editor/esm/vs/nls';
import { DisposableStore } from 'monaco-editor/esm/vs/base/common/lifecycle';
import { QuickCommandNLS } from 'monaco-editor/esm/vs/editor/common/standaloneStrings';
Expand All @@ -24,12 +26,12 @@ import {
import { stripIcons } from 'monaco-editor/esm/vs/base/common/iconLabels';

import { KeyMod, KeyCode, editor as MonacoEditor } from 'mo/monaco';
import { container } from 'tsyringe';
import { EditorService, IEditorService } from 'mo/services';
import { Action2, KeybindingWeight } from './common';
import { MonacoService } from './monacoService';
import { registerQuickAccessProvider } from './quickAccessProvider';
import { constants } from 'mo/services/builtinService/const';
import { Action2 } from 'mo/monaco/action';
import { KeybindingWeight } from 'mo/monaco/common';
import { MonacoService } from 'mo/monaco/monacoService';
import { registerQuickAccessProvider } from 'mo/monaco/quickAccessProvider';

export class CommandQuickAccessProvider extends AbstractEditorCommandsQuickAccessProvider {
static PREFIX = '>';
Expand Down
6 changes: 4 additions & 2 deletions src/monaco/quickCopyLineUp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'reflect-metadata';
import { container } from 'tsyringe';
import { localize } from 'mo/i18n/localize';
import { KeyMod, KeyCode } from 'mo/monaco';
import { EditorService, IEditorService } from 'mo/services';
import { container } from 'tsyringe';
import { Action2, KeybindingWeight } from './common';

import { constants } from 'mo/services/builtinService/const';
import { KeybindingWeight } from 'mo/monaco/common';
import { Action2 } from 'mo/monaco/action';

export class QuickCopyLineUp extends Action2 {
static readonly ID = constants.ACTION_QUICK_COPY_LINE_UP;
Expand Down
Loading