Skip to content

Commit 1b26f91

Browse files
committed
refactor(code-actions): implement parameter object pattern for prompt generation
- Extract prompt templates into constants - Add createPrompt utility for template string handling - Consolidate code action handling in ClineProvider - Update tests to use new parameter object pattern
1 parent 02a8eb9 commit 1b26f91

File tree

4 files changed

+79
-40
lines changed

4 files changed

+79
-40
lines changed

src/core/prompts/__tests__/code-actions.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ describe('Code Action Prompts', () => {
66

77
describe('explainCodePrompt', () => {
88
it('should format explain prompt correctly', () => {
9-
const prompt = explainCodePrompt(testFilePath, testCode);
9+
const prompt = explainCodePrompt({
10+
filePath: testFilePath,
11+
selectedText: testCode
12+
});
1013

1114
expect(prompt).toContain(`@/${testFilePath}`);
1215
expect(prompt).toContain(testCode);
@@ -18,7 +21,10 @@ describe('Code Action Prompts', () => {
1821

1922
describe('fixCodePrompt', () => {
2023
it('should format fix prompt without diagnostics', () => {
21-
const prompt = fixCodePrompt(testFilePath, testCode);
24+
const prompt = fixCodePrompt({
25+
filePath: testFilePath,
26+
selectedText: testCode
27+
});
2228

2329
expect(prompt).toContain(`@/${testFilePath}`);
2430
expect(prompt).toContain(testCode);
@@ -39,7 +45,11 @@ describe('Code Action Prompts', () => {
3945
}
4046
];
4147

42-
const prompt = fixCodePrompt(testFilePath, testCode, diagnostics);
48+
const prompt = fixCodePrompt({
49+
filePath: testFilePath,
50+
selectedText: testCode,
51+
diagnostics
52+
});
4353

4454
expect(prompt).toContain('Current problems detected:');
4555
expect(prompt).toContain('[eslint] Missing semicolon (semi)');
@@ -50,7 +60,10 @@ describe('Code Action Prompts', () => {
5060

5161
describe('improveCodePrompt', () => {
5262
it('should format improve prompt correctly', () => {
53-
const prompt = improveCodePrompt(testFilePath, testCode);
63+
const prompt = improveCodePrompt({
64+
filePath: testFilePath,
65+
selectedText: testCode
66+
});
5467

5568
expect(prompt).toContain(`@/${testFilePath}`);
5669
expect(prompt).toContain(testCode);

src/core/prompts/code-actions.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1-
export const explainCodePrompt = (filePath: string, selectedText: string) => `
2-
Explain the following code from file path @/${filePath}:
1+
type PromptParams = Record<string, string | any[]>;
2+
3+
const generateDiagnosticText = (diagnostics?: any[]) => {
4+
if (!diagnostics?.length) return '';
5+
return `\nCurrent problems detected:\n${diagnostics.map(d =>
6+
`- [${d.source || 'Error'}] ${d.message}${d.code ? ` (${d.code})` : ''}`
7+
).join('\n')}`;
8+
};
9+
10+
const createPrompt = (template: string, params: PromptParams): string => {
11+
let result = template;
12+
for (const [key, value] of Object.entries(params)) {
13+
if (key === 'diagnostics') {
14+
result = result.replaceAll('${diagnosticText}', generateDiagnosticText(value as any[]));
15+
} else {
16+
result = result.replaceAll(`\${${key}}`, value as string);
17+
}
18+
}
19+
return result;
20+
};
21+
22+
const EXPLAIN_TEMPLATE = `
23+
Explain the following code from file path @/\${filePath}:
324
425
\`\`\`
5-
${selectedText}
26+
\${selectedText}
627
\`\`\`
728
829
Please provide a clear and concise explanation of what this code does, including:
@@ -11,18 +32,12 @@ Please provide a clear and concise explanation of what this code does, including
1132
3. Important patterns or techniques used
1233
`;
1334

14-
export const fixCodePrompt = (filePath: string, selectedText: string, diagnostics?: any[]) => {
15-
const diagnosticText = diagnostics && diagnostics.length > 0
16-
? `\nCurrent problems detected:
17-
${diagnostics.map(d => `- [${d.source || 'Error'}] ${d.message}${d.code ? ` (${d.code})` : ''}`).join('\n')}`
18-
: '';
19-
20-
return `
21-
Fix any issues in the following code from file path @/${filePath}
22-
${diagnosticText}
35+
const FIX_TEMPLATE = `
36+
Fix any issues in the following code from file path @/\${filePath}
37+
\${diagnosticText}
2338
2439
\`\`\`
25-
${selectedText}
40+
\${selectedText}
2641
\`\`\`
2742
2843
Please:
@@ -31,13 +46,12 @@ Please:
3146
3. Provide corrected code
3247
4. Explain what was fixed and why
3348
`;
34-
};
3549

36-
export const improveCodePrompt = (filePath: string, selectedText: string) => `
37-
Improve the following code from file path @/${filePath}:
50+
const IMPROVE_TEMPLATE = `
51+
Improve the following code from file path @/\${filePath}:
3852
3953
\`\`\`
40-
${selectedText}
54+
\${selectedText}
4155
\`\`\`
4256
4357
Please suggest improvements for:
@@ -47,4 +61,13 @@ Please suggest improvements for:
4761
4. Error handling and edge cases
4862
4963
Provide the improved code along with explanations for each enhancement.
50-
`;
64+
`;
65+
66+
export const explainCodePrompt = (params: PromptParams) =>
67+
createPrompt(EXPLAIN_TEMPLATE, params);
68+
69+
export const fixCodePrompt = (params: PromptParams) =>
70+
createPrompt(FIX_TEMPLATE, params);
71+
72+
export const improveCodePrompt = (params: PromptParams) =>
73+
createPrompt(IMPROVE_TEMPLATE, params);

src/core/webview/ClineProvider.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ export class ClineProvider implements vscode.WebviewViewProvider {
181181
return findLast(Array.from(this.activeInstances), (instance) => instance.view?.visible === true)
182182
}
183183

184+
public static async handleCodeAction(
185+
promptGenerator: (params: Record<string, string | any[]>) => string,
186+
params: Record<string, string | any[]>
187+
): Promise<void> {
188+
const visibleProvider = ClineProvider.getVisibleInstance()
189+
if (!visibleProvider) {
190+
return
191+
}
192+
193+
const prompt = promptGenerator(params)
194+
195+
await visibleProvider.initClineWithTask(prompt)
196+
}
197+
184198
resolveWebviewView(
185199
webviewView: vscode.WebviewView | vscode.WebviewPanel,
186200
//context: vscode.WebviewViewResolveContext<unknown>, used to recreate a deallocated webview, but we don't need this since we use retainContextWhenHidden

src/extension.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,34 +174,23 @@ export function activate(context: vscode.ExtensionContext) {
174174
// Register code action commands
175175
context.subscriptions.push(
176176
vscode.commands.registerCommand("roo-cline.explainCode", async (filePath: string, selectedText: string) => {
177-
const visibleProvider = ClineProvider.getVisibleInstance()
178-
if (!visibleProvider) {
179-
return
180-
}
181-
const prompt = explainCodePrompt(filePath, selectedText)
182-
await visibleProvider.initClineWithTask(prompt)
177+
await ClineProvider.handleCodeAction(explainCodePrompt, { filePath, selectedText })
183178
})
184179
);
185180

186181
context.subscriptions.push(
187182
vscode.commands.registerCommand("roo-cline.fixCode", async (filePath: string, selectedText: string, diagnostics?: any[]) => {
188-
const visibleProvider = ClineProvider.getVisibleInstance()
189-
if (!visibleProvider) {
190-
return
191-
}
192-
const prompt = fixCodePrompt(filePath, selectedText, diagnostics)
193-
await visibleProvider.initClineWithTask(prompt)
183+
await ClineProvider.handleCodeAction(fixCodePrompt, {
184+
filePath,
185+
selectedText,
186+
...(diagnostics ? { diagnostics } : {})
187+
})
194188
})
195189
);
196190

197191
context.subscriptions.push(
198192
vscode.commands.registerCommand("roo-cline.improveCode", async (filePath: string, selectedText: string) => {
199-
const visibleProvider = ClineProvider.getVisibleInstance()
200-
if (!visibleProvider) {
201-
return
202-
}
203-
const prompt = improveCodePrompt(filePath, selectedText)
204-
await visibleProvider.initClineWithTask(prompt)
193+
await ClineProvider.handleCodeAction(improveCodePrompt, { filePath, selectedText })
205194
})
206195
);
207196

0 commit comments

Comments
 (0)