Skip to content

Commit 1be92f7

Browse files
authored
Enhance command registration (#21)
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.
2 parents 50110d0 + ef1632c commit 1be92f7

4 files changed

Lines changed: 65 additions & 15 deletions

File tree

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@
8181
}
8282
}
8383
},
84+
"commands": [
85+
{
86+
"command": "auto-comment-blocks.singleLineBlock",
87+
"title": "Continue Single-Line Comment Block",
88+
"category": "Auto Comment Blocks"
89+
},
90+
{
91+
"command": "auto-comment-blocks.changeBladeMultiLineBlock",
92+
"title": "Toggle Blade Multi-Line Comment Style",
93+
"category": "Auto Comment Blocks"
94+
}
95+
],
8496
"keybindings": [
8597
{
8698
"command": "auto-comment-blocks.singleLineBlock",

src/configuration.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {Settings} from "./interfaces/settings";
1515
import {ExtraSingleLineCommentStyles, LineComment, SingleLineCommentStyle} from "./interfaces/commentStyles";
1616
import {ExtensionMetaData} from "./interfaces/extensionMetaData";
1717
import {JsonObject, JsonArray, LanguageId, MultiLineLanguageDefinitions, SingleLineLanguageDefinitions} from "./interfaces/utils";
18+
import {CommandRegistration} from "./interfaces/commands";
1819

1920
export class Configuration {
2021
/**************
@@ -161,22 +162,25 @@ export class Configuration {
161162

162163
/**
163164
* Register some VSCode commands.
164-
*
165-
* @returns {vscode.Disposable[]}
166165
*/
167-
public registerCommands(): vscode.Disposable[] {
168-
const singleLineBlockCommand = vscode.commands.registerTextEditorCommand("auto-comment-blocks.singleLineBlock", (textEditor, edit, args) => {
169-
this.handleSingleLineBlock(textEditor, edit);
170-
});
166+
public registerCommands(context: vscode.ExtensionContext) {
167+
const namespace = this.extensionData.get("namespace");
171168

172-
const changeBladeMultiLineBlockCommand = vscode.commands.registerTextEditorCommand(
173-
"auto-comment-blocks.changeBladeMultiLineBlock",
174-
(textEditor, edit, args) => {
175-
this.handleChangeBladeMultiLineBlock(textEditor);
176-
}
177-
);
169+
const commands: CommandRegistration[] = [
170+
{
171+
command: "singleLineBlock",
172+
handler: this.handleSingleLineBlock.bind(this),
173+
},
174+
{
175+
command: "changeBladeMultiLineBlock",
176+
handler: this.handleChangeBladeMultiLineBlock.bind(this),
177+
},
178+
];
178179

179-
return [singleLineBlockCommand, changeBladeMultiLineBlockCommand];
180+
commands.forEach(({command, handler}) => {
181+
const namespacedCommand = `${namespace}.${command}`;
182+
context.subscriptions.push(vscode.commands.registerTextEditorCommand(namespacedCommand, handler));
183+
});
180184
}
181185

182186
/**

src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ const disposables: vscode.Disposable[] = [];
1414

1515
export function activate(context: vscode.ExtensionContext) {
1616
const configureCommentBlocksDisposable = configuration.configureCommentBlocks();
17-
const registerCommandsDisposable = configuration.registerCommands();
1817

19-
disposables.push(...configureCommentBlocksDisposable, ...registerCommandsDisposable);
18+
configuration.registerCommands(context);
19+
20+
disposables.push(...configureCommentBlocksDisposable);
2021

2122
const extensionName = extensionData.get("namespace");
2223

src/interfaces/commands.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as vscode from "vscode";
2+
3+
/**
4+
* Defines the structure for registering commands.
5+
*/
6+
export interface CommandRegistration {
7+
/**
8+
* The command ID, corresponding to those defined in package.json
9+
*
10+
* @type {keyof Commands}
11+
*/
12+
command: keyof Commands;
13+
14+
/**
15+
* The command handler function.
16+
*
17+
* The parameters are passed to the handler automatically by VS Code when the
18+
* command is executed.
19+
*
20+
* @param textEditor The text editor
21+
* @param edit The text editor edits. Optional because some commands may not need it.
22+
* @returns void
23+
*/
24+
handler: (textEditor: vscode.TextEditor, edit?: vscode.TextEditorEdit) => void;
25+
}
26+
27+
/**
28+
* Defines command IDs without the namespace prefix.
29+
*/
30+
interface Commands {
31+
singleLineBlock: string;
32+
changeBladeMultiLineBlock: string;
33+
}

0 commit comments

Comments
 (0)