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
This commit is contained in:
sam hoang
2025-01-30 17:23:12 +07:00
parent 35a7e433f2
commit 2e56149620
7 changed files with 76 additions and 10 deletions

View File

@@ -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": [

View File

@@ -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)

View File

@@ -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",

View File

@@ -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)
}

View File

@@ -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[]

View File

@@ -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<string, SupportPromptConfig> = {
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

View File

@@ -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,
],