Skip to content

Commit 7c875f1

Browse files
committed
feat: add code action prompt handlers for explain, fix and improve code
1 parent 343d3fa commit 7c875f1

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { explainCodePrompt, fixCodePrompt, improveCodePrompt } from '../code-actions';
2+
3+
describe('Code Action Prompts', () => {
4+
const testFilePath = 'test/file.ts';
5+
const testCode = 'function test() { return true; }';
6+
7+
describe('explainCodePrompt', () => {
8+
it('should format explain prompt correctly', () => {
9+
const prompt = explainCodePrompt(testFilePath, testCode);
10+
11+
expect(prompt).toContain(`@/${testFilePath}`);
12+
expect(prompt).toContain(testCode);
13+
expect(prompt).toContain('purpose and functionality');
14+
expect(prompt).toContain('Key components');
15+
expect(prompt).toContain('Important patterns');
16+
});
17+
});
18+
19+
describe('fixCodePrompt', () => {
20+
it('should format fix prompt without diagnostics', () => {
21+
const prompt = fixCodePrompt(testFilePath, testCode);
22+
23+
expect(prompt).toContain(`@/${testFilePath}`);
24+
expect(prompt).toContain(testCode);
25+
expect(prompt).toContain('Address all detected problems');
26+
expect(prompt).not.toContain('Current problems detected');
27+
});
28+
29+
it('should format fix prompt with diagnostics', () => {
30+
const diagnostics = [
31+
{
32+
source: 'eslint',
33+
message: 'Missing semicolon',
34+
code: 'semi'
35+
},
36+
{
37+
message: 'Unused variable',
38+
severity: 1
39+
}
40+
];
41+
42+
const prompt = fixCodePrompt(testFilePath, testCode, diagnostics);
43+
44+
expect(prompt).toContain('Current problems detected:');
45+
expect(prompt).toContain('[eslint] Missing semicolon (semi)');
46+
expect(prompt).toContain('[Error] Unused variable');
47+
expect(prompt).toContain(testCode);
48+
});
49+
});
50+
51+
describe('improveCodePrompt', () => {
52+
it('should format improve prompt correctly', () => {
53+
const prompt = improveCodePrompt(testFilePath, testCode);
54+
55+
expect(prompt).toContain(`@/${testFilePath}`);
56+
expect(prompt).toContain(testCode);
57+
expect(prompt).toContain('Code readability');
58+
expect(prompt).toContain('Performance optimization');
59+
expect(prompt).toContain('Best practices');
60+
expect(prompt).toContain('Error handling');
61+
});
62+
});
63+
});

src/core/prompts/code-actions.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export const explainCodePrompt = (filePath: string, selectedText: string) => `
2+
Explain the following code from file path @/${filePath}:
3+
4+
\`\`\`
5+
${selectedText}
6+
\`\`\`
7+
8+
Please provide a clear and concise explanation of what this code does, including:
9+
1. The purpose and functionality
10+
2. Key components and their interactions
11+
3. Important patterns or techniques used
12+
`;
13+
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}
23+
24+
\`\`\`
25+
${selectedText}
26+
\`\`\`
27+
28+
Please:
29+
1. Address all detected problems listed above (if any)
30+
2. Identify any other potential bugs or issues
31+
3. Provide corrected code
32+
4. Explain what was fixed and why
33+
`;
34+
};
35+
36+
export const improveCodePrompt = (filePath: string, selectedText: string) => `
37+
Improve the following code from file path @/${filePath}:
38+
39+
\`\`\`
40+
${selectedText}
41+
\`\`\`
42+
43+
Please suggest improvements for:
44+
1. Code readability and maintainability
45+
2. Performance optimization
46+
3. Best practices and patterns
47+
4. Error handling and edge cases
48+
49+
Provide the improved code along with explanations for each enhancement.
50+
`;

0 commit comments

Comments
 (0)