Hey everyone! 👋 I've been playing with VS Code's context menu feature and finally built something that's saving me *so* much time during code reviews and daily refactoring. I get tired of typing the same prompts over and over for my AI assistant, so I made a custom menu that pops up with common refactor requests.
It's basically a `tasks.json` setup combined with some keybindings. Now I can just highlight a block of code, right-click, and choose something like "**Extract function/method**" or "**Add error handling**" and my AI gets the perfect context and instruction every time.
Hereβs a snippet from my `tasks.json` to show the structure:
```json
{
"version": "2.0.0",
"tasks": [
{
"label": "AI: Add Error Handling",
"type": "shell",
"command": "echo '## Context: Selected coden## Instruction: Wrap this code in a try-except block, log the exception, and re-raise or return a default value appropriately.' | pbcopy",
"presentation": {
"reveal": "never"
}
},
{
"label": "AI: Simplify Conditional",
"type": "shell",
"command": "echo '## Context: Selected coden## Instruction: Simplify this complex conditional logic. Make it more readable and maintainable.' | pbcopy",
"presentation": {
"reveal": "never"
}
}
]
}
```
Then I mapped these tasks to the command palette and context menu through `settings.json`:
```json
{
"menus": {
"editor/context": [
{
"command": "workbench.action.tasks.runTask",
"args": "AI: Add Error Handling",
"group": "ai_refactor"
}
]
}
}
```
**My most-used items on the menu:**
- **Extract function/method**
- **Add detailed docstring**
- **Check for edge cases**
- **Simplify complex logic**
- **Convert to list/dict comprehension**
- **Add logging**
The beauty is that the prompt template is pre-filled, so I'm not wasting tokens re-explaining the task every time. I just select the code, the prompt gets copied to my clipboard (or sent directly via an extension), and I paste it into my AI assistant chat. It makes the interaction super smooth and repeatable.
This is a game-changer for maintaining consistency, especially when pairing or doing team reviews. You could even share the `tasks.json` config with your team to standardize refactoring requests.
What are your go-to refactoring prompts? I'd love to expand my menu with more ideas!
Happy coding!
Clean code, happy life
I'm the platform lead at a mid-size fintech where we run about 200 microservices on a multi-tenant EKS cluster, and my team lives in VS Code for both feature work and infrastructure-as-code with Terraform and Helm.
Your approach with `tasks.json` is clever for personal use, but for team adoption or scaling this pattern, I'd compare it to other extensibility routes:
1. **Team Configuration & Onboarding Effort**: Your method requires each developer to manually copy the `tasks.json` and keybindings. For my team of 40 engineers, that's a non-starter. A VS Code extension published to our private marketplace installs in one click. The custom tasks approach adds about 30-60 minutes of setup per developer to integrate with their specific AI CLI tool.
2. **Context & Prompt Complexity**: Shell commands echoing text to clipboard are limited to static prompts. In my setup, I use a VS Code extension that injects dynamic context - like the current file path, project type (Go service, React component), and even JIRA ticket ID from the branch name - into the AI call. Your static method can't conditionally change the instruction based on whether the selected code is a Python API route or a Terraform module.
3. **Integration with Existing AI Toolchain**: The `shell` command assumes `pbcopy` (macOS). For Windows/Linux teams, you need OS detection. More importantly, it doesn't handle the actual AI call. In our workflow, the extension calls a internal CLI that logs to Datadog and enforces cost controls. Your snippet stops at copying the prompt, leaving the actual execution manual.
4. **Maintenance and Iteration**: Updating a prompt in a shared `tasks.json` file means a PR and a sync across the team. With a private extension, we version it and developers auto-update. We've iterated our "Extract Method" prompt 12 times in the last quarter based on code review feedback; pushing those changes via an extension is trivial.
For a solo developer or a small, tight-knit team, your custom tasks are a fantastic, lightweight starting point. For any team larger than 5-6 engineers or with a need to audit/improve prompt effectiveness, I'd immediately recommend building a simple private VS Code extension. The jump in complexity isn't huge - you're basically wrapping your JSON structure in an `activationEvents` and `contributes.menus` declaration - but the payoff in consistency is massive.
To make the call clean for you, tell us how many people would use this and whether you need to enforce a specific AI model (like GPT-4 for security reviews vs. Claude for refactoring).
YAML is not a programming language, but I treat it like one.