Skip to content

Commit 7845791

Browse files
committed
feat(code-actions): add user input and customizable templates
Add ability to provide custom input when using code actions Make code action templates customizable and resettable Refactor code action handling for better maintainability Add state management for utility prompts
1 parent 1b26f91 commit 7845791

File tree

4 files changed

+80
-29
lines changed

4 files changed

+80
-29
lines changed

src/core/CodeActionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33

4-
const ACTION_NAMES = {
4+
export const ACTION_NAMES = {
55
EXPLAIN: 'Roo Cline: Explain Code',
66
FIX: 'Roo Cline: Fix Code',
77
IMPROVE: 'Roo Cline: Improve Code'

src/core/prompts/code-actions.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ type PromptParams = Record<string, string | any[]>;
22

33
const generateDiagnosticText = (diagnostics?: any[]) => {
44
if (!diagnostics?.length) return '';
5-
return `\nCurrent problems detected:\n${diagnostics.map(d =>
5+
return `\nCurrent problems detected:\n${diagnostics.map(d =>
66
`- [${d.source || 'Error'}] ${d.message}${d.code ? ` (${d.code})` : ''}`
77
).join('\n')}`;
88
};
99

10-
const createPrompt = (template: string, params: PromptParams): string => {
10+
export const createPrompt = (template: string, params: PromptParams): string => {
1111
let result = template;
1212
for (const [key, value] of Object.entries(params)) {
1313
if (key === 'diagnostics') {
@@ -16,11 +16,16 @@ const createPrompt = (template: string, params: PromptParams): string => {
1616
result = result.replaceAll(`\${${key}}`, value as string);
1717
}
1818
}
19+
20+
// Replace any remaining user_input placeholders with empty string
21+
result = result.replaceAll('${userInput}', '');
22+
1923
return result;
2024
};
2125

22-
const EXPLAIN_TEMPLATE = `
26+
export const EXPLAIN_TEMPLATE = `
2327
Explain the following code from file path @/\${filePath}:
28+
\${userInput}
2429
2530
\`\`\`
2631
\${selectedText}
@@ -32,9 +37,10 @@ Please provide a clear and concise explanation of what this code does, including
3237
3. Important patterns or techniques used
3338
`;
3439

35-
const FIX_TEMPLATE = `
40+
export const FIX_TEMPLATE = `
3641
Fix any issues in the following code from file path @/\${filePath}
3742
\${diagnosticText}
43+
\${userInput}
3844
3945
\`\`\`
4046
\${selectedText}
@@ -47,8 +53,9 @@ Please:
4753
4. Explain what was fixed and why
4854
`;
4955

50-
const IMPROVE_TEMPLATE = `
56+
export const IMPROVE_TEMPLATE = `
5157
Improve the following code from file path @/\${filePath}:
58+
\${userInput}
5259
5360
\`\`\`
5461
\${selectedText}
@@ -63,11 +70,18 @@ Please suggest improvements for:
6370
Provide the improved code along with explanations for each enhancement.
6471
`;
6572

66-
export const explainCodePrompt = (params: PromptParams) =>
73+
export const explainCodePrompt = (params: PromptParams) =>
6774
createPrompt(EXPLAIN_TEMPLATE, params);
6875

69-
export const fixCodePrompt = (params: PromptParams) =>
76+
export const fixCodePrompt = (params: PromptParams) =>
7077
createPrompt(FIX_TEMPLATE, params);
7178

72-
export const improveCodePrompt = (params: PromptParams) =>
73-
createPrompt(IMPROVE_TEMPLATE, params);
79+
export const improveCodePrompt = (params: PromptParams) =>
80+
createPrompt(IMPROVE_TEMPLATE, params);
81+
82+
// Get template based on prompt type
83+
export const defaultTemplates = {
84+
'EXPLAIN': EXPLAIN_TEMPLATE,
85+
'FIX': FIX_TEMPLATE,
86+
'IMPROVE': IMPROVE_TEMPLATE
87+
}

src/core/webview/ClineProvider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ import { enhancePrompt } from "../../utils/enhance-prompt"
4040
import { getCommitInfo, searchCommits, getWorkingState } from "../../utils/git"
4141
import { ConfigManager } from "../config/ConfigManager"
4242
import { CustomModesManager } from "../config/CustomModesManager"
43+
import {
44+
defaultTemplates,
45+
createPrompt
46+
} from "../prompts/code-actions"
47+
48+
import { ACTION_NAMES } from "../CodeActionProvider"
4349

4450
/*
4551
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
@@ -182,16 +188,18 @@ export class ClineProvider implements vscode.WebviewViewProvider {
182188
}
183189

184190
public static async handleCodeAction(
185-
promptGenerator: (params: Record<string, string | any[]>) => string,
191+
promptType: keyof typeof ACTION_NAMES,
186192
params: Record<string, string | any[]>
187193
): Promise<void> {
188194
const visibleProvider = ClineProvider.getVisibleInstance()
189195
if (!visibleProvider) {
190196
return
191197
}
192198

193-
const prompt = promptGenerator(params)
199+
const { utilPrompt } = await visibleProvider.getState()
194200

201+
const template = utilPrompt?.[promptType] ?? defaultTemplates[promptType]
202+
const prompt = createPrompt(template, params)
195203
await visibleProvider.initClineWithTask(prompt)
196204
}
197205

src/extension.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from "vscode"
55
import { ClineProvider } from "./core/webview/ClineProvider"
66
import { createClineAPI } from "./exports"
77
import "./utils/path" // necessary to have access to String.prototype.toPosix
8-
import { CodeActionProvider } from "./core/CodeActionProvider"
8+
import { ACTION_NAMES, CodeActionProvider } from "./core/CodeActionProvider"
99
import { explainCodePrompt, fixCodePrompt, improveCodePrompt } from "./core/prompts/code-actions"
1010
import { DIFF_VIEW_URI_SCHEME } from "./integrations/editor/DiffViewProvider"
1111

@@ -171,27 +171,56 @@ export function activate(context: vscode.ExtensionContext) {
171171
)
172172
);
173173

174+
// Helper function to handle code actions
175+
const registerCodeAction = (
176+
context: vscode.ExtensionContext,
177+
command: string,
178+
promptType: keyof typeof ACTION_NAMES,
179+
inputPrompt: string,
180+
inputPlaceholder: string
181+
) => {
182+
context.subscriptions.push(
183+
vscode.commands.registerCommand(command, async (filePath: string, selectedText: string, diagnostics?: any[]) => {
184+
const userInput = await vscode.window.showInputBox({
185+
prompt: inputPrompt,
186+
placeHolder: inputPlaceholder
187+
});
188+
189+
const params = {
190+
filePath,
191+
selectedText,
192+
...(diagnostics ? { diagnostics } : {}),
193+
...(userInput ? { userInput } : {})
194+
};
195+
196+
await ClineProvider.handleCodeAction(promptType, params);
197+
})
198+
);
199+
};
200+
174201
// Register code action commands
175-
context.subscriptions.push(
176-
vscode.commands.registerCommand("roo-cline.explainCode", async (filePath: string, selectedText: string) => {
177-
await ClineProvider.handleCodeAction(explainCodePrompt, { filePath, selectedText })
178-
})
202+
registerCodeAction(
203+
context,
204+
"roo-cline.explainCode",
205+
'EXPLAIN',
206+
"Any specific questions about this code?",
207+
"E.g. How does the error handling work?"
179208
);
180209

181-
context.subscriptions.push(
182-
vscode.commands.registerCommand("roo-cline.fixCode", async (filePath: string, selectedText: string, diagnostics?: any[]) => {
183-
await ClineProvider.handleCodeAction(fixCodePrompt, {
184-
filePath,
185-
selectedText,
186-
...(diagnostics ? { diagnostics } : {})
187-
})
188-
})
210+
registerCodeAction(
211+
context,
212+
"roo-cline.fixCode",
213+
'FIX',
214+
"Any specific concerns about fixing this code?",
215+
"E.g. Maintain backward compatibility"
189216
);
190217

191-
context.subscriptions.push(
192-
vscode.commands.registerCommand("roo-cline.improveCode", async (filePath: string, selectedText: string) => {
193-
await ClineProvider.handleCodeAction(improveCodePrompt, { filePath, selectedText })
194-
})
218+
registerCodeAction(
219+
context,
220+
"roo-cline.improveCode",
221+
'IMPROVE',
222+
"Any specific aspects you want to improve?",
223+
"E.g. Focus on performance optimization"
195224
);
196225

197226
return createClineAPI(outputChannel, sidebarProvider)

0 commit comments

Comments
 (0)