Skip to content

Commit 2e56149

Browse files
committed
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
1 parent 35a7e43 commit 2e56149

File tree

7 files changed

+76
-10
lines changed

7 files changed

+76
-10
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@
118118
"command": "roo-cline.improveCode",
119119
"title": "Roo Code: Improve Code",
120120
"category": "Roo Code"
121+
},
122+
{
123+
"command": "roo-cline.addToContext",
124+
"title": "Roo Code: Add To Context",
125+
"category": "Roo Code"
121126
}
122127
],
123128
"menus": {
@@ -136,6 +141,11 @@
136141
"command": "roo-cline.improveCode",
137142
"when": "editorHasSelection",
138143
"group": "Roo Code@3"
144+
},
145+
{
146+
"command": "roo-cline.addToContext",
147+
"when": "editorHasSelection",
148+
"group": "Roo Code@4"
139149
}
140150
],
141151
"view/title": [

src/core/CodeActionProvider.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import * as vscode from "vscode"
2-
import { ClineProvider } from "./webview/ClineProvider"
32
import { EditorUtils } from "./EditorUtils"
43

54
export const ACTION_NAMES = {
65
EXPLAIN: "Roo Code: Explain Code",
76
FIX: "Roo Code: Fix Code",
7+
FIX_LOGIC: "Roo Code: Fix Logic",
88
IMPROVE: "Roo Code: Improve Code",
9+
ADD_TO_CONTEXT: "Roo Code: Add to Context",
910
} as const
1011

1112
const COMMAND_IDS = {
1213
EXPLAIN: "roo-cline.explainCode",
1314
FIX: "roo-cline.fixCode",
1415
IMPROVE: "roo-cline.improveCode",
16+
ADD_TO_CONTEXT: "roo-cline.addToContext",
1517
} as const
1618

1719
export class CodeActionProvider implements vscode.CodeActionProvider {
@@ -74,6 +76,13 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
7476
]),
7577
)
7678
}
79+
} else {
80+
actions.push(
81+
...this.createActionPair(ACTION_NAMES.FIX_LOGIC, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
82+
filePath,
83+
effectiveRange.text,
84+
]),
85+
)
7786
}
7887

7988
actions.push(
@@ -85,6 +94,15 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
8594
),
8695
)
8796

97+
actions.push(
98+
this.createAction(
99+
ACTION_NAMES.ADD_TO_CONTEXT,
100+
vscode.CodeActionKind.QuickFix,
101+
COMMAND_IDS.ADD_TO_CONTEXT,
102+
[filePath, effectiveRange.text],
103+
),
104+
)
105+
88106
return actions
89107
} catch (error) {
90108
console.error("Error providing code actions:", error)

src/core/webview/ClineProvider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ export class ClineProvider implements vscode.WebviewViewProvider {
238238

239239
const prompt = supportPrompt.create(promptType, params, customSupportPrompts)
240240

241+
if (command.endsWith("addToContext")) {
242+
await visibleProvider.postMessageToWebview({
243+
type: "invoke",
244+
invoke: "setChatBoxMessage",
245+
text: prompt,
246+
})
247+
248+
return
249+
}
250+
241251
if (visibleProvider.cline && command.endsWith("InCurrentTask")) {
242252
await visibleProvider.postMessageToWebview({
243253
type: "invoke",

src/extension.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ export function activate(context: vscode.ExtensionContext) {
172172
context: vscode.ExtensionContext,
173173
command: string,
174174
promptType: keyof typeof ACTION_NAMES,
175-
inNewTask: boolean,
176175
inputPrompt?: string,
177176
inputPlaceholder?: string,
178177
) => {
@@ -222,10 +221,10 @@ export function activate(context: vscode.ExtensionContext) {
222221
inputPlaceholder?: string,
223222
) => {
224223
// Register new task version
225-
registerCodeAction(context, baseCommand, promptType, true, inputPrompt, inputPlaceholder)
224+
registerCodeAction(context, baseCommand, promptType, inputPrompt, inputPlaceholder)
226225

227226
// Register current task version
228-
registerCodeAction(context, `${baseCommand}InCurrentTask`, promptType, false, inputPrompt, inputPlaceholder)
227+
registerCodeAction(context, `${baseCommand}InCurrentTask`, promptType, inputPrompt, inputPlaceholder)
229228
}
230229

231230
// Register code action commands
@@ -253,6 +252,8 @@ export function activate(context: vscode.ExtensionContext) {
253252
"E.g. Focus on performance optimization",
254253
)
255254

255+
registerCodeAction(context, "roo-cline.addToContext", "ADD_TO_CONTEXT")
256+
256257
return createClineAPI(outputChannel, sidebarProvider)
257258
}
258259

src/shared/ExtensionMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface ExtensionMessage {
5050
| "historyButtonClicked"
5151
| "promptsButtonClicked"
5252
| "didBecomeVisible"
53-
invoke?: "sendMessage" | "primaryButtonClick" | "secondaryButtonClick"
53+
invoke?: "sendMessage" | "primaryButtonClick" | "secondaryButtonClick" | "setChatBoxMessage"
5454
state?: ExtensionState
5555
images?: string[]
5656
ollamaModels?: string[]

src/shared/support-prompt.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export const createPrompt = (template: string, params: PromptParams): string =>
1818
}
1919
}
2020

21-
// Replace any remaining user_input placeholders with empty string
22-
result = result.replaceAll("${userInput}", "")
21+
// Replace any remaining placeholders with empty strings
22+
result = result.replaceAll(/\${[^}]*}/g, "")
2323

2424
return result
2525
}
@@ -42,7 +42,7 @@ const supportPromptConfigs: Record<string, SupportPromptConfig> = {
4242
EXPLAIN: {
4343
label: "Explain Code",
4444
description:
45-
"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).",
45+
"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).",
4646
template: `Explain the following code from file path @/\${filePath}:
4747
\${userInput}
4848
@@ -58,7 +58,7 @@ Please provide a clear and concise explanation of what this code does, including
5858
FIX: {
5959
label: "Fix Issues",
6060
description:
61-
"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).",
61+
"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).",
6262
template: `Fix any issues in the following code from file path @/\${filePath}
6363
\${diagnosticText}
6464
\${userInput}
@@ -76,7 +76,7 @@ Please:
7676
IMPROVE: {
7777
label: "Improve Code",
7878
description:
79-
"Receive suggestions for code optimization, better practices, and architectural improvements while maintaining functionality. Available in the editor context menu (right-click on selected code).",
79+
"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).",
8080
template: `Improve the following code from file path @/\${filePath}:
8181
\${userInput}
8282
@@ -92,6 +92,15 @@ Please suggest improvements for:
9292
9393
Provide the improved code along with explanations for each enhancement.`,
9494
},
95+
ADD_TO_CONTEXT: {
96+
label: "Add to Context",
97+
description:
98+
"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).",
99+
template: `@/\${filePath}:
100+
\`\`\`
101+
\${selectedText}
102+
\`\`\``,
103+
},
95104
} as const
96105

97106
type SupportPromptType = keyof typeof supportPromptConfigs

webview-ui/src/components/chat/ChatView.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,20 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
330330
[messages.length, clineAsk],
331331
)
332332

333+
const handleSetChatBoxMessage = useCallback(
334+
(text: string, images: string[]) => {
335+
// Avoid nested template literals by breaking down the logic
336+
let newValue = text
337+
if (inputValue !== "") {
338+
newValue = inputValue + " " + text
339+
}
340+
341+
setInputValue(newValue)
342+
setSelectedImages([...selectedImages, ...images])
343+
},
344+
[inputValue, selectedImages],
345+
)
346+
333347
const startNewTask = useCallback(() => {
334348
vscode.postMessage({ type: "clearTask" })
335349
}, [])
@@ -429,6 +443,9 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
429443
case "sendMessage":
430444
handleSendMessage(message.text ?? "", message.images ?? [])
431445
break
446+
case "setChatBoxMessage":
447+
handleSetChatBoxMessage(message.text ?? "", message.images ?? [])
448+
break
432449
case "primaryButtonClick":
433450
handlePrimaryButtonClick()
434451
break
@@ -444,6 +461,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
444461
textAreaDisabled,
445462
enableButtons,
446463
handleSendMessage,
464+
handleSetChatBoxMessage,
447465
handlePrimaryButtonClick,
448466
handleSecondaryButtonClick,
449467
],

0 commit comments

Comments
 (0)