Skip to content

Commit

Permalink
feat(command): 命令注册支持分组功能 (#10)
Browse files Browse the repository at this point in the history
* feat(command): 命令注册支持分组功能
  • Loading branch information
czfadmin authored Sep 29, 2024
1 parent dd3fe0b commit 2cdc5b2
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 176 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-crabs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vsc-extension-helper': patch
---

命令注册更改为延迟注册
5 changes: 5 additions & 0 deletions .changeset/tasty-walls-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vsc-extension-helper': patch
---

增加命令分组功能
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ log/
**/*/typings/auto-generated

modern.config.local.*
**/*/out/
out/

bookmark-manager.json
36 changes: 0 additions & 36 deletions examples/hello-world/out/commands/index.js

This file was deleted.

1 change: 0 additions & 1 deletion examples/hello-world/out/commands/index.js.map

This file was deleted.

65 changes: 0 additions & 65 deletions examples/hello-world/out/extension.js

This file was deleted.

1 change: 0 additions & 1 deletion examples/hello-world/out/extension.js.map

This file was deleted.

38 changes: 0 additions & 38 deletions examples/hello-world/out/test/extension.test.js

This file was deleted.

1 change: 0 additions & 1 deletion examples/hello-world/out/test/extension.test.js.map

This file was deleted.

8 changes: 8 additions & 0 deletions examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
{
"command": "hello-world.bar",
"title": "Hello World"
},
{
"command": "hello-world.group1.helloworld",
"title": "Group1.HelloWorld"
},
{
"command": "hello-world.group2.helloworld2",
"title": "Group2.HelloWorld2"
}
]
},
Expand Down
34 changes: 26 additions & 8 deletions examples/hello-world/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ export const activate = withActivate({
extensionId: 'hello-world',
})(async function (ctx: vscode.ExtensionContext) {
// 1.use hoc
// withCommand({ name: 'foo' })(function foo() {
// vscode.window.showInformationMessage('foo command');
// });

// // 2. use hoc without options
// withCommand(function bar(...args: any[]) {
// vscode.window.showInformationMessage('bar command');
// });
withCommand({ name: 'foo' })(function foo() {
vscode.window.showInformationMessage('foo command');
});

// 2. use hoc without options
withCommand(function bar(...args: any[]) {
vscode.window.showInformationMessage('bar command');
});

// 3. use group
withCommand({
name: 'helloworld',
group: ['group1'],
})(function helloworld(...args: any[]) {
vscode.window.showInformationMessage('group1: hello world');
});

function helloworld2(...args: any[]) {
vscode.window.showInformationMessage('group1: hello world2');
}
helloworld2.group = ['group2'];

// 4.
withCommand({
name: 'helloworld2',
})(helloworld2);
});

// 2.
Expand Down
10 changes: 7 additions & 3 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function command(options?: Omit<CommandOptions, 'textEditor'>) {
const cmdName =
typeof propertyKey === 'symbol' ? propertyKey.toString() : propertyKey;

const name = options?.name || cmdName;
const name: string = options?.name || cmdName;

const _options = {
...(options || {}),
name,
Expand All @@ -38,10 +39,13 @@ export function textEditorCommand(options?: TextEditorCommandOptions) {
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
) {

const cmdName =
typeof propertyKey === 'symbol' ? propertyKey.toString() : propertyKey;
const name = options?.name || cmdName;
const _options = { ...(options || {}), name, textEditor: true };

const name: string = options?.name || cmdName;

const _options = { ...(options || { group: [] }), name, textEditor: true };

internalRegisterCommand(_options, target[propertyKey]);
};
Expand Down
52 changes: 35 additions & 17 deletions src/hoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@ import {
registerContext,
useExtensionContext,
} from './context';
import { WithActiveOptions, CommandOptions, VoidHandlerType } from './types';
import { WithActiveOptions, CommandOptions, IVoidHandler } from './types';
import { internalRegisterCommand } from './utils';

function withCommandHelper(
optionsOrHandler: Omit<CommandOptions, 'textEditor'> | VoidHandlerType,
optionsOrHandler: Omit<CommandOptions, 'textEditor'> | IVoidHandler,
isTextEditorCmd = false,
) {
const funcPrefix = isTextEditorCmd ? 'TextEditor' : '';
const noNameError = new Error(
`The registered command name cannot be empty, please use \`function funcName(...args:any[]){}\` or \`with${funcPrefix}Command(options)(function funcName(...args:any[]){}\` to register Order`,
);

let cmdName = '';
let internalHandler = optionsOrHandler;
let internalOptions: CommandOptions = {};

const paramIsHandler = typeof optionsOrHandler === 'function';

if (paramIsHandler) {
if (!optionsOrHandler.name) {
throw noNameError;
}

internalHandler = optionsOrHandler;
internalOptions = {
name: optionsOrHandler.name,
group:
'group' in optionsOrHandler ? (optionsOrHandler.group as string[]) : [],
textEditor: isTextEditorCmd,
};
// 此时直接注册,后面的返回只是为了欺骗ts的不进行报错
Expand All @@ -40,37 +46,49 @@ function withCommandHelper(
);
}

return function (handler: VoidHandlerType) {
return function (handler: IVoidHandler) {
if (
!paramIsHandler &&
(!handler.name || (optionsOrHandler && !optionsOrHandler.name))
) {
throw noNameError;
}
cmdName = paramIsHandler ? cmdName : handler.name;

if (paramIsHandler) {
return;
}

internalOptions.name =
internalOptions.name || paramIsHandler ? cmdName : handler.name;

internalHandler = paramIsHandler ? internalHandler : handler;
if (!paramIsHandler) {
internalOptions = {
...internalOptions,
...optionsOrHandler,
textEditor: isTextEditorCmd,
};

if (!internalOptions.group) {
internalOptions.group =
(typeof handler.group === 'string' ? [handler.group] : handler.group) ||
[];
}

internalOptions = {
...internalOptions,
...optionsOrHandler,
textEditor: isTextEditorCmd,
};

internalRegisterCommand(
internalOptions,
internalHandler as VoidHandlerType,
internalHandler as IVoidHandler,
);
};
}
/**
* @zh 通过高阶函数创建一个命令
* @zh 通过高阶函数创建一个命令, 当第一个参数传递函数时, 将无法使用分组功能
* @en Create a command through a higher-order function
* @param optionsOrHandler
* @returns
*/
export function withCommand(
optionsOrHandler: CommandOptions | VoidHandlerType,
optionsOrHandler: CommandOptions | IVoidHandler,
) {
return withCommandHelper(optionsOrHandler, false);
}
Expand All @@ -82,7 +100,7 @@ export function withCommand(
* @returns
*/
export function withTextEditorCommand(
optionsOrHandler: CommandOptions | VoidHandlerType,
optionsOrHandler: CommandOptions | IVoidHandler,
) {
return withCommandHelper(optionsOrHandler, true);
}
Expand Down Expand Up @@ -124,7 +142,7 @@ export function withActivate(
context: ctx,
});
await handler(ctx);
registerCommands()
registerCommands();
};
}

Expand All @@ -134,7 +152,7 @@ export function withActivate(
context: ctx,
});
handler(ctx);
registerCommands()
registerCommands();
};
};
}
Expand All @@ -145,7 +163,7 @@ export function withActivate(
* @param handler 自定义的取消激活函数
* @returns
*/
export function withDeactivate(handler?: VoidHandlerType) {
export function withDeactivate(handler?: IVoidHandler) {
return function (ctx: ExtensionContext) {
clearGlobalContext();
handler?.();
Expand Down
Loading

0 comments on commit 2cdc5b2

Please sign in to comment.