From 2e561496203b78591412f7b4b9604a3f5e4250cc Mon Sep 17 00:00:00 2001 From: sam hoang Date: Thu, 30 Jan 2025 17:23:12 +0700 Subject: [PATCH] feat: add 'Add To Context' code action - Add new command registration and menu item - Add new code action type and command ID - Add support prompt config for adding code to context - Add message handling in webview for setting chat box content - Add logic to append selected code to existing chat input --- package.json | 10 ++++++++++ src/core/CodeActionProvider.ts | 20 +++++++++++++++++++- src/core/webview/ClineProvider.ts | 10 ++++++++++ src/extension.ts | 7 ++++--- src/shared/ExtensionMessage.ts | 2 +- src/shared/support-prompt.ts | 19 ++++++++++++++----- webview-ui/src/components/chat/ChatView.tsx | 18 ++++++++++++++++++ 7 files changed, 76 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index fa3b84f..5df7d03 100644 --- a/package.json +++ b/package.json @@ -118,6 +118,11 @@ "command": "roo-cline.improveCode", "title": "Roo Code: Improve Code", "category": "Roo Code" + }, + { + "command": "roo-cline.addToContext", + "title": "Roo Code: Add To Context", + "category": "Roo Code" } ], "menus": { @@ -136,6 +141,11 @@ "command": "roo-cline.improveCode", "when": "editorHasSelection", "group": "Roo Code@3" + }, + { + "command": "roo-cline.addToContext", + "when": "editorHasSelection", + "group": "Roo Code@4" } ], "view/title": [ diff --git a/src/core/CodeActionProvider.ts b/src/core/CodeActionProvider.ts index 24dc72c..0754c1a 100644 --- a/src/core/CodeActionProvider.ts +++ b/src/core/CodeActionProvider.ts @@ -1,17 +1,19 @@ import * as vscode from "vscode" -import { ClineProvider } from "./webview/ClineProvider" import { EditorUtils } from "./EditorUtils" export const ACTION_NAMES = { EXPLAIN: "Roo Code: Explain Code", FIX: "Roo Code: Fix Code", + FIX_LOGIC: "Roo Code: Fix Logic", IMPROVE: "Roo Code: Improve Code", + ADD_TO_CONTEXT: "Roo Code: Add to Context", } as const const COMMAND_IDS = { EXPLAIN: "roo-cline.explainCode", FIX: "roo-cline.fixCode", IMPROVE: "roo-cline.improveCode", + ADD_TO_CONTEXT: "roo-cline.addToContext", } as const export class CodeActionProvider implements vscode.CodeActionProvider { @@ -74,6 +76,13 @@ export class CodeActionProvider implements vscode.CodeActionProvider { ]), ) } + } else { + actions.push( + ...this.createActionPair(ACTION_NAMES.FIX_LOGIC, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [ + filePath, + effectiveRange.text, + ]), + ) } actions.push( @@ -85,6 +94,15 @@ export class CodeActionProvider implements vscode.CodeActionProvider { ), ) + actions.push( + this.createAction( + ACTION_NAMES.ADD_TO_CONTEXT, + vscode.CodeActionKind.QuickFix, + COMMAND_IDS.ADD_TO_CONTEXT, + [filePath, effectiveRange.text], + ), + ) + return actions } catch (error) { console.error("Error providing code actions:", error) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 58ec513..08a95ae 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -238,6 +238,16 @@ export class ClineProvider implements vscode.WebviewViewProvider { const prompt = supportPrompt.create(promptType, params, customSupportPrompts) + if (command.endsWith("addToContext")) { + await visibleProvider.postMessageToWebview({ + type: "invoke", + invoke: "setChatBoxMessage", + text: prompt, + }) + + return + } + if (visibleProvider.cline && command.endsWith("InCurrentTask")) { await visibleProvider.postMessageToWebview({ type: "invoke", diff --git a/src/extension.ts b/src/extension.ts index 37cc8c0..719f38d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -172,7 +172,6 @@ export function activate(context: vscode.ExtensionContext) { context: vscode.ExtensionContext, command: string, promptType: keyof typeof ACTION_NAMES, - inNewTask: boolean, inputPrompt?: string, inputPlaceholder?: string, ) => { @@ -222,10 +221,10 @@ export function activate(context: vscode.ExtensionContext) { inputPlaceholder?: string, ) => { // Register new task version - registerCodeAction(context, baseCommand, promptType, true, inputPrompt, inputPlaceholder) + registerCodeAction(context, baseCommand, promptType, inputPrompt, inputPlaceholder) // Register current task version - registerCodeAction(context, `${baseCommand}InCurrentTask`, promptType, false, inputPrompt, inputPlaceholder) + registerCodeAction(context, `${baseCommand}InCurrentTask`, promptType, inputPrompt, inputPlaceholder) } // Register code action commands @@ -253,6 +252,8 @@ export function activate(context: vscode.ExtensionContext) { "E.g. Focus on performance optimization", ) + registerCodeAction(context, "roo-cline.addToContext", "ADD_TO_CONTEXT") + return createClineAPI(outputChannel, sidebarProvider) } diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index 69cc518..a226beb 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -50,7 +50,7 @@ export interface ExtensionMessage { | "historyButtonClicked" | "promptsButtonClicked" | "didBecomeVisible" - invoke?: "sendMessage" | "primaryButtonClick" | "secondaryButtonClick" + invoke?: "sendMessage" | "primaryButtonClick" | "secondaryButtonClick" | "setChatBoxMessage" state?: ExtensionState images?: string[] ollamaModels?: string[] diff --git a/src/shared/support-prompt.ts b/src/shared/support-prompt.ts index 881c060..d80e9e4 100644 --- a/src/shared/support-prompt.ts +++ b/src/shared/support-prompt.ts @@ -18,8 +18,8 @@ export const createPrompt = (template: string, params: PromptParams): string => } } - // Replace any remaining user_input placeholders with empty string - result = result.replaceAll("${userInput}", "") + // Replace any remaining placeholders with empty strings + result = result.replaceAll(/\${[^}]*}/g, "") return result } @@ -42,7 +42,7 @@ const supportPromptConfigs: Record = { EXPLAIN: { label: "Explain Code", description: - "Get detailed explanations of code snippets, functions, or entire files. Useful for understanding complex code or learning new patterns. Available in the editor context menu (right-click on selected code).", + "Get detailed explanations of code snippets, functions, or entire files. Useful for understanding complex code or learning new patterns. Available in code actions (lightbulb icon in the editor). and the editor context menu (right-click on selected code).", template: `Explain the following code from file path @/\${filePath}: \${userInput} @@ -58,7 +58,7 @@ Please provide a clear and concise explanation of what this code does, including FIX: { label: "Fix Issues", description: - "Get help identifying and resolving bugs, errors, or code quality issues. Provides step-by-step guidance for fixing problems. Available in the editor context menu (right-click on selected code).", + "Get help identifying and resolving bugs, errors, or code quality issues. Provides step-by-step guidance for fixing problems. Available in code actions (lightbulb icon in the editor). and the editor context menu (right-click on selected code).", template: `Fix any issues in the following code from file path @/\${filePath} \${diagnosticText} \${userInput} @@ -76,7 +76,7 @@ Please: IMPROVE: { label: "Improve Code", description: - "Receive suggestions for code optimization, better practices, and architectural improvements while maintaining functionality. Available in the editor context menu (right-click on selected code).", + "Receive suggestions for code optimization, better practices, and architectural improvements while maintaining functionality. Available in code actions (lightbulb icon in the editor). and the editor context menu (right-click on selected code).", template: `Improve the following code from file path @/\${filePath}: \${userInput} @@ -92,6 +92,15 @@ Please suggest improvements for: Provide the improved code along with explanations for each enhancement.`, }, + ADD_TO_CONTEXT: { + label: "Add to Context", + description: + "Add context to your current task or conversation. Useful for providing additional information or clarifications. Available in code actions (lightbulb icon in the editor). and the editor context menu (right-click on selected code).", + template: `@/\${filePath}: +\`\`\` +\${selectedText} +\`\`\``, + }, } as const type SupportPromptType = keyof typeof supportPromptConfigs diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index 0a32eef..76543f3 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -330,6 +330,20 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie [messages.length, clineAsk], ) + const handleSetChatBoxMessage = useCallback( + (text: string, images: string[]) => { + // Avoid nested template literals by breaking down the logic + let newValue = text + if (inputValue !== "") { + newValue = inputValue + " " + text + } + + setInputValue(newValue) + setSelectedImages([...selectedImages, ...images]) + }, + [inputValue, selectedImages], + ) + const startNewTask = useCallback(() => { vscode.postMessage({ type: "clearTask" }) }, []) @@ -429,6 +443,9 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie case "sendMessage": handleSendMessage(message.text ?? "", message.images ?? []) break + case "setChatBoxMessage": + handleSetChatBoxMessage(message.text ?? "", message.images ?? []) + break case "primaryButtonClick": handlePrimaryButtonClick() break @@ -444,6 +461,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie textAreaDisabled, enableButtons, handleSendMessage, + handleSetChatBoxMessage, handlePrimaryButtonClick, handleSecondaryButtonClick, ],