From 9bf31d3ff7103bdf009e7337f5ac1ec5a1da31c0 Mon Sep 17 00:00:00 2001 From: Murilo Pires <50873657+MuriloFP@users.noreply.github.com> Date: Thu, 26 Jun 2025 21:34:14 -0300 Subject: [PATCH] fix terminal keyboard shortcut error when adding content to context (#5161) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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 --- src/activate/registerTerminalActions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/activate/registerTerminalActions.ts b/src/activate/registerTerminalActions.ts index eb494d66da..9773d01d38 100644 --- a/src/activate/registerTerminalActions.ts +++ b/src/activate/registerTerminalActions.ts @@ -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)