From 2b60148c135e3495ccd383c45944792ce922fbfd Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Wed, 4 Feb 2026 08:40:17 +0000 Subject: [PATCH 1/2] build: define the commands the extension contributes in package.json. - 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. --- package.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/package.json b/package.json index 399d5bb..34277d1 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,18 @@ } } }, + "commands": [ + { + "command": "auto-comment-blocks.singleLineBlock", + "title": "Continue Single-Line Comment Block", + "category": "Auto Comment Blocks" + }, + { + "command": "auto-comment-blocks.changeBladeMultiLineBlock", + "title": "Toggle Blade Multi-Line Comment Style", + "category": "Auto Comment Blocks" + } + ], "keybindings": [ { "command": "auto-comment-blocks.singleLineBlock", From ef1632c4fdad2fc66636430be09b1c9c46446254 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Wed, 4 Feb 2026 08:56:47 +0000 Subject: [PATCH 2/2] refactor: `registerCommands` method and add new command interfaces. - 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. --- src/configuration.ts | 30 +++++++++++++++++------------- src/extension.ts | 5 +++-- src/interfaces/commands.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/interfaces/commands.ts diff --git a/src/configuration.ts b/src/configuration.ts index 80e3207..e32e9a7 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -15,6 +15,7 @@ import {Settings} from "./interfaces/settings"; import {ExtraSingleLineCommentStyles, LineComment, SingleLineCommentStyle} from "./interfaces/commentStyles"; import {ExtensionMetaData} from "./interfaces/extensionMetaData"; import {JsonObject, JsonArray, LanguageId, MultiLineLanguageDefinitions, SingleLineLanguageDefinitions} from "./interfaces/utils"; +import {CommandRegistration} from "./interfaces/commands"; export class Configuration { /************** @@ -161,22 +162,25 @@ export class Configuration { /** * Register some VSCode commands. - * - * @returns {vscode.Disposable[]} */ - public registerCommands(): vscode.Disposable[] { - const singleLineBlockCommand = vscode.commands.registerTextEditorCommand("auto-comment-blocks.singleLineBlock", (textEditor, edit, args) => { - this.handleSingleLineBlock(textEditor, edit); - }); + public registerCommands(context: vscode.ExtensionContext) { + const namespace = this.extensionData.get("namespace"); - const changeBladeMultiLineBlockCommand = vscode.commands.registerTextEditorCommand( - "auto-comment-blocks.changeBladeMultiLineBlock", - (textEditor, edit, args) => { - this.handleChangeBladeMultiLineBlock(textEditor); - } - ); + const commands: CommandRegistration[] = [ + { + command: "singleLineBlock", + handler: this.handleSingleLineBlock.bind(this), + }, + { + command: "changeBladeMultiLineBlock", + handler: this.handleChangeBladeMultiLineBlock.bind(this), + }, + ]; - return [singleLineBlockCommand, changeBladeMultiLineBlockCommand]; + commands.forEach(({command, handler}) => { + const namespacedCommand = `${namespace}.${command}`; + context.subscriptions.push(vscode.commands.registerTextEditorCommand(namespacedCommand, handler)); + }); } /** diff --git a/src/extension.ts b/src/extension.ts index 700dc39..a9a945e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,9 +14,10 @@ const disposables: vscode.Disposable[] = []; export function activate(context: vscode.ExtensionContext) { const configureCommentBlocksDisposable = configuration.configureCommentBlocks(); - const registerCommandsDisposable = configuration.registerCommands(); - disposables.push(...configureCommentBlocksDisposable, ...registerCommandsDisposable); + configuration.registerCommands(context); + + disposables.push(...configureCommentBlocksDisposable); const extensionName = extensionData.get("namespace"); diff --git a/src/interfaces/commands.ts b/src/interfaces/commands.ts new file mode 100644 index 0000000..f39d732 --- /dev/null +++ b/src/interfaces/commands.ts @@ -0,0 +1,33 @@ +import * as vscode from "vscode"; + +/** + * Defines the structure for registering commands. + */ +export interface CommandRegistration { + /** + * The command ID, corresponding to those defined in package.json + * + * @type {keyof Commands} + */ + command: keyof Commands; + + /** + * The command handler function. + * + * The parameters are passed to the handler automatically by VS Code when the + * command is executed. + * + * @param textEditor The text editor + * @param edit The text editor edits. Optional because some commands may not need it. + * @returns void + */ + handler: (textEditor: vscode.TextEditor, edit?: vscode.TextEditorEdit) => void; +} + +/** + * Defines command IDs without the namespace prefix. + */ +interface Commands { + singleLineBlock: string; + changeBladeMultiLineBlock: string; +}