Prompt file imported from faouzi96/md-code-assist (
.github/prompts/new-command.prompt.md). Copyright stays with the author.
Create New VS Code Command
Your goal is to scaffold a new VS Code command for md-code-assist.
Required Information
Ask the user for:
- Command name (e.g., "formatCurrentBlock")
- Command title (shown in Command Palette, e.g., "Format Current Code Block")
- Description of what the command does
- Keybinding (optional, e.g.,
Ctrl+Shift+F) - When clause (optional, e.g.,
editorLangId == markdown)
Tasks
1. Create Command Handler
Create src/commands/<commandName>.ts:
import * as vscode from 'vscode';
export async function <commandName>(context: vscode.ExtensionContext): Promise<void> {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('No active editor');
return;
}
// Implementation here
}
2. Register in package.json
Add to contributes.commands:
{
"command": "mdCodeAssist.<commandName>",
"title": "<Command Title>",
"category": "Markdown Code Assistant"
}
3. Add Keybinding (if provided)
Add to contributes.keybindings:
{
"command": "mdCodeAssist.<commandName>",
"key": "<keybinding>",
"when": "<whenClause>"
}
4. Register in Extension Entry Point
Update src/extension.ts:
import { <commandName> } from './commands/<commandName>';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(
'mdCodeAssist.<commandName>',
() => <commandName>(context)
)
);
}
5. Add Menu Contribution (optional)
For editor context menu, add to contributes.menus:
"editor/context": [
{
"command": "mdCodeAssist.<commandName>",
"when": "editorLangId == markdown",
"group": "mdCodeAssist"
}
]
6. Create Test
Create test/commands/<commandName>.test.ts.
Checklist
- Command handler created
- Registered in package.json commands
- Keybinding added (if applicable)
- Registered in extension.ts
- Menu contribution added (if applicable)
- Test file created
- Manually tested in Extension Development Host