fix terminal keyboard shortcut error when adding content to context (#5161)

## Fix terminal keyboard shortcut error when adding content to context

**Fixes:** [#2276](https://github.com/RooCodeInc/Roo-Code/issues/2276)

**Problem:**
- Using keyboard shortcuts to add terminal content to context threw error: "Cannot read properties of undefined (reading 'selection')"
- Context menu worked correctly, but keyboard shortcuts failed

**Root Cause:**
- Command handler accessed `args.selection` without null checking
- When triggered via keyboard shortcut, VS Code passes `undefined` for `args` parameter
- When triggered via context menu, VS Code passes an object with `selection` property

**Solution:**
- Changed `args.selection` to `args?.selection` using optional chaining
- Maintains existing fallback behavior when no selection is available
- Preserves backward compatibility with context menu functionality

**Files Modified:**
- `src/activate/registerTerminalActions.ts` - Added null safety for args parameter

**Testing:**
-  Keyboard shortcuts now work without errors
-  Context menu functionality preserved
-  Fallback to `Terminal.getTerminalContents()` works in both scenarios
This commit is contained in:
Murilo Pires 2025-06-26 21:34:14 -03:00 committed by GitHub
parent 43357967f0
commit 9bf31d3ff7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -20,7 +20,7 @@ const registerTerminalAction = (
) => {
context.subscriptions.push(
vscode.commands.registerCommand(getTerminalCommand(command), async (args: any) => {
let content = args.selection
let content = args?.selection
if (!content || content === "") {
content = await Terminal.getTerminalContents(promptType === "TERMINAL_ADD_TO_CONTEXT" ? -1 : 1)