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", 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; +}