Skip to content

Commit ef1632c

Browse files
committed
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.
1 parent 2b60148 commit ef1632c

3 files changed

Lines changed: 53 additions & 15 deletions

File tree

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)