Refactor command registration (#6)

Refactor command registration

As we continue to register more commands for our extension,
'extensions.ts' has become gross and hard to read. Commands are now
defined in a `commands: Command[]` list in 'commands.ts':

    type Command = {
      name: string, // Name of the command e.g. 'mind-reader.selectTheme'
      callback: () => void // Callback to register for the command
    }

This list is imported into 'extension.ts' and registered
programatically, reducing the required lines of "extra" code required
to register commands in 'extension.ts' from 4 per command to 7 total.
This should help keep 'extension.ts' squeaky clean :)
This commit is contained in:
Jake Grossman 2021-11-01 15:32:06 -05:00 committed by GitHub
parent d105544596
commit 6904b5dacf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 48 deletions

79
src/commands.ts Normal file
View File

@ -0,0 +1,79 @@
import * as vscode from 'vscode';
/**
* @type {Object} Command // Command to register with the VS Code Extension API
* @prop {string} command // Name of the command; e.g., 'mind-reader.selectTheme'
* @prop {callback} callback // Callback to register when `command` is invoked
*/
type Command = {
name: string,
callback: () => void
};
// The list of commands to register in the extension
const commands: Command[] = [
{
name: 'mind-reader.selectTheme',
// callbacks can be inlined...
callback: () => vscode.commands.executeCommand('workbench.action.selectTheme'),
},
{
name: 'mind-reader.increaseFontScale',
callback: increaseFontScale, // ...or factored out into separate functions below
},
{
name: 'mind-reader.decreaseFontScale',
callback: decreaseFontScale,
},
{
name: 'mind-reader.resetFontScale',
callback: resetFontScale,
},
{
name: 'mind-reader.increaseEditorScale',
callback: increaseEditorScale,
},
{
name: 'mind-reader.decreaseEditorScale',
callback: decreaseEditorScale,
},
{
name: 'mind-reader.resetEditorScale',
callback: resetEditorScale,
},
];
// COMMAND CALLBACK IMPLEMENTATIONS
function increaseFontScale(): void {
vscode.commands.executeCommand('editor.action.fontZoomIn');
}
function decreaseFontScale(): void {
vscode.commands.executeCommand('editor.action.fontZoomOut');
}
function resetFontScale(): void {
vscode.commands.executeCommand('editor.action.fontZoomReset');
}
function increaseEditorScale(): void {
vscode.commands.executeCommand('workbench.action.zoomIn');
}
function decreaseEditorScale(): void {
vscode.commands.executeCommand('workbench.action.zoomOut');
}
function resetEditorScale(): void {
vscode.commands.executeCommand('workbench.action.zoomReset');
}
export default commands;

View File

@ -1,5 +1,7 @@
import * as vscode from 'vscode';
import * as pl from './pylex';
import commands from './commands';
let parser: pl.Parser = new pl.Parser();
@ -9,54 +11,14 @@ export function activate(context: vscode.ExtensionContext) {
parser.parse('Beep Boop');
// Increase Font Scale
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.increaseFontScale', () => {
vscode.commands.executeCommand('editor.action.fontZoomIn');
})
);
// Decrease Font Scale
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.decreaseFontScale', () => {
vscode.commands.executeCommand('editor.action.fontZoomOut');
})
);
// Reset Font Scale
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.resetFontScale', () => {
vscode.commands.executeCommand('editor.action.fontZoomReset');
})
);
// Increase Editor Scale
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.increaseEditorScale', () => {
vscode.commands.executeCommand('workbench.action.zoomIn');
})
);
// Decrease Editor Scale
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.decreaseEditorScale', () => {
vscode.commands.executeCommand('workbench.action.zoomOut');
})
);
// Reset Editor Scale
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.resetEditorScale', () => {
vscode.commands.executeCommand('workbench.action.zoomReset');
})
);
// Select Theme
context.subscriptions.push(
vscode.commands.registerCommand('mind-reader.selectTheme', () => {
vscode.commands.executeCommand('workbench.action.selectTheme');
})
);
// Register Commands
commands.forEach(command => {
let disposable = vscode.commands.registerCommand(
command.name,
command.callback
);
context.subscriptions.push(disposable);
});
}
export function deactivate() {}