Conversation
- 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.
There was a problem hiding this comment.
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
CommandRegistrationinterface to define command structure with command IDs and handler functions - Refactored
Configuration.registerCommands()to acceptExtensionContext, 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.
| /** | ||
| * The command ID, corresponding to those defined in package.json | ||
| * | ||
| * @type {keyof Commands} | ||
| */ | ||
| command: keyof Commands; |
There was a problem hiding this comment.
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".
| }, | ||
| { | ||
| command: "changeBladeMultiLineBlock", | ||
| handler: this.handleChangeBladeMultiLineBlock.bind(this), |
There was a problem hiding this comment.
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.
| handler: this.handleChangeBladeMultiLineBlock.bind(this), | |
| handler: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => { | |
| this.handleChangeBladeMultiLineBlock(textEditor); | |
| }, |
| * @param edit The text editor edits. Optional because some commands may not need it. | ||
| * @returns void | ||
| */ | ||
| handler: (textEditor: vscode.TextEditor, edit?: vscode.TextEditorEdit) => void; |
There was a problem hiding this comment.
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.
| * @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; |
This pull request refactors the way VS Code commands are registered in the extension by introducing a new
CommandRegistrationinterface, 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:
CommandRegistrationinterface insrc/interfaces/commands.tsto standardize the structure for registering commands, including command IDs and handler functions.Configuration.registerCommandsinsrc/configuration.tsto use the newCommandRegistrationinterface and dynamically register commands with namespace prefixes, improving scalability and reducing duplication.Extension activation updates:
src/extension.tsto align with the new command registration approach, ensuring commands are registered with the correct context and subscriptions.VS Code integration:
auto-comment-blocks.singleLineBlockandauto-comment-blocks.changeBladeMultiLineBlockto thecommandssection inpackage.json, ensuring they are discoverable and properly categorized in VS Code.