Skip to content

Enhance command registration#21

Merged
yCodeTech merged 2 commits intomasterfrom
refactor/commands
Feb 4, 2026
Merged

Enhance command registration#21
yCodeTech merged 2 commits intomasterfrom
refactor/commands

Conversation

@yCodeTech
Copy link
Owner

@yCodeTech yCodeTech commented Feb 4, 2026

This pull request refactors the way VS Code commands are registered in the extension by introducing a new CommandRegistration interface, updating the command registration logic to use this interface, and making related updates to the extension activation process and configuration. These changes improve code maintainability and scalability for future commands.

Command registration improvements:

  • Introduced a new CommandRegistration interface in src/interfaces/commands.ts to standardize the structure for registering commands, including command IDs and handler functions.
  • Updated Configuration.registerCommands in src/configuration.ts to use the new CommandRegistration interface and dynamically register commands with namespace prefixes, improving scalability and reducing duplication.

Extension activation updates:

  • Modified src/extension.ts to align with the new command registration approach, ensuring commands are registered with the correct context and subscriptions.

VS Code integration:

  • Added new command definitions for auto-comment-blocks.singleLineBlock and auto-comment-blocks.changeBladeMultiLineBlock to the commands section in package.json, ensuring they are discoverable and properly categorized in VS Code.

- Added `singleLineBlock` and `changeBladeMultiLineBlock` commands to the package.json to show that this extension contributes commands. This allows the vscode extension info page show that it contributes these commands, and also shows the commands in the command center.
- Added new `CommandRegistration` and `Commands` interfaces.

    - `CommandRegistration` to define the structure for registering commands, ie. the command name and the handler function.

    - `Commands` to define the command ID's to enable intellisense on the `command` property of `CommandRegistration`.

- Refactored `Configuration::registerCommands` method to make registering commands easier and more maintainable.

    - Removed the return as it will no longer return anything.

    - Added new `context` param to allow pushing the disposable to the context subscriptions automatically, instead of returning it to the `activate` function of the extension.

    - Removed the `registerCommandsDisposable` variable from the `activate` function of the extension as it no longer returns.

    - Added the `context` to the `registerCommands` function call as the param.

    - Added an array of `CommandRegistration` objects to define the commands and their handlers. Defining the handlers like this, doesn't require manually passing the args from vscode's `registerTextEditorCommand` method as it will do it automatically. We use `.bind(this)` to ensure the `this` is the `Configuration` instance when using inside the handler functions.

    - Added a loop to iterate through each of the command array objects, and for each one, it registers the command and the handler with vscode and pushes it's disposable directly to the context subscriptions.
@yCodeTech yCodeTech changed the title Refactor/commands Enhance command registration Feb 4, 2026
@yCodeTech yCodeTech requested a review from Copilot February 4, 2026 09:04
@yCodeTech yCodeTech added the enhancement New feature or quality of life enhancement label Feb 4, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors VS Code command registration by introducing a structured CommandRegistration interface to improve code organization and maintainability. The changes standardize how commands are registered and make the codebase more scalable for adding future commands.

Changes:

  • Introduced a CommandRegistration interface to define command structure with command IDs and handler functions
  • Refactored Configuration.registerCommands() to accept ExtensionContext, use the new interface, and register commands with dynamic namespace prefixes
  • Updated extension activation to pass context directly to registerCommands() instead of collecting disposables
  • Added command definitions to package.json for VS Code command palette discoverability

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/interfaces/commands.ts New interface defining command registration structure with command IDs and handler signatures
src/configuration.ts Refactored registerCommands method to use CommandRegistration interface and register commands directly to context
src/extension.ts Updated activation to pass context to registerCommands instead of managing disposables locally
package.json Added explicit command definitions for singleLineBlock and changeBladeMultiLineBlock commands

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +7 to +12
/**
* The command ID, corresponding to those defined in package.json
*
* @type {keyof Commands}
*/
command: keyof Commands;
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states "The command ID, corresponding to those defined in package.json" but this is misleading. The field expects only the command suffix (e.g., "singleLineBlock"), not the full namespaced command ID as defined in package.json (e.g., "auto-comment-blocks.singleLineBlock"). The namespace is prepended in the registerCommands method. Consider updating the comment to clarify this, such as: "The command suffix without the namespace prefix. The full command ID in package.json will be namespace.command".

Copilot uses AI. Check for mistakes.
},
{
command: "changeBladeMultiLineBlock",
handler: this.handleChangeBladeMultiLineBlock.bind(this),
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handleChangeBladeMultiLineBlock method (line 958 in src/configuration.ts) only accepts one parameter (textEditor: vscode.TextEditor), but registerTextEditorCommand always provides both textEditor and edit parameters. While JavaScript will ignore the extra parameter, this creates a signature mismatch with the CommandRegistration.handler interface. The handler should accept the edit parameter even if it doesn't use it, for consistency and to match the VS Code API contract: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void.

Suggested change
handler: this.handleChangeBladeMultiLineBlock.bind(this),
handler: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => {
this.handleChangeBladeMultiLineBlock(textEditor);
},

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +24
* @param edit The text editor edits. Optional because some commands may not need it.
* @returns void
*/
handler: (textEditor: vscode.TextEditor, edit?: vscode.TextEditorEdit) => void;
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The edit parameter should not be optional. The VS Code API for registerTextEditorCommand always provides both textEditor and edit parameters to the handler function. The comment on line 21 stating "Optional because some commands may not need it" is misleading - whether a command uses the parameter is different from whether the parameter is provided by the API. The handler signature should be: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void.

Suggested change
* @param edit The text editor edits. Optional because some commands may not need it.
* @returns void
*/
handler: (textEditor: vscode.TextEditor, edit?: vscode.TextEditorEdit) => void;
* @param edit The text editor edits. Always provided by VS Code, though some commands may not use it.
* @returns void
*/
handler: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void;

Copilot uses AI. Check for mistakes.
@yCodeTech yCodeTech merged commit 1be92f7 into master Feb 4, 2026
7 checks passed
@yCodeTech yCodeTech deleted the refactor/commands branch February 4, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or quality of life enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant