mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 04:11:10 -05:00
feat: add code action prompt handlers for explain, fix and improve code
This commit is contained in:
63
src/core/prompts/__tests__/code-actions.test.ts
Normal file
63
src/core/prompts/__tests__/code-actions.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { explainCodePrompt, fixCodePrompt, improveCodePrompt } from '../code-actions';
|
||||
|
||||
describe('Code Action Prompts', () => {
|
||||
const testFilePath = 'test/file.ts';
|
||||
const testCode = 'function test() { return true; }';
|
||||
|
||||
describe('explainCodePrompt', () => {
|
||||
it('should format explain prompt correctly', () => {
|
||||
const prompt = explainCodePrompt(testFilePath, testCode);
|
||||
|
||||
expect(prompt).toContain(`@/${testFilePath}`);
|
||||
expect(prompt).toContain(testCode);
|
||||
expect(prompt).toContain('purpose and functionality');
|
||||
expect(prompt).toContain('Key components');
|
||||
expect(prompt).toContain('Important patterns');
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixCodePrompt', () => {
|
||||
it('should format fix prompt without diagnostics', () => {
|
||||
const prompt = fixCodePrompt(testFilePath, testCode);
|
||||
|
||||
expect(prompt).toContain(`@/${testFilePath}`);
|
||||
expect(prompt).toContain(testCode);
|
||||
expect(prompt).toContain('Address all detected problems');
|
||||
expect(prompt).not.toContain('Current problems detected');
|
||||
});
|
||||
|
||||
it('should format fix prompt with diagnostics', () => {
|
||||
const diagnostics = [
|
||||
{
|
||||
source: 'eslint',
|
||||
message: 'Missing semicolon',
|
||||
code: 'semi'
|
||||
},
|
||||
{
|
||||
message: 'Unused variable',
|
||||
severity: 1
|
||||
}
|
||||
];
|
||||
|
||||
const prompt = fixCodePrompt(testFilePath, testCode, diagnostics);
|
||||
|
||||
expect(prompt).toContain('Current problems detected:');
|
||||
expect(prompt).toContain('[eslint] Missing semicolon (semi)');
|
||||
expect(prompt).toContain('[Error] Unused variable');
|
||||
expect(prompt).toContain(testCode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('improveCodePrompt', () => {
|
||||
it('should format improve prompt correctly', () => {
|
||||
const prompt = improveCodePrompt(testFilePath, testCode);
|
||||
|
||||
expect(prompt).toContain(`@/${testFilePath}`);
|
||||
expect(prompt).toContain(testCode);
|
||||
expect(prompt).toContain('Code readability');
|
||||
expect(prompt).toContain('Performance optimization');
|
||||
expect(prompt).toContain('Best practices');
|
||||
expect(prompt).toContain('Error handling');
|
||||
});
|
||||
});
|
||||
});
|
||||
50
src/core/prompts/code-actions.ts
Normal file
50
src/core/prompts/code-actions.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
export const explainCodePrompt = (filePath: string, selectedText: string) => `
|
||||
Explain the following code from file path @/${filePath}:
|
||||
|
||||
\`\`\`
|
||||
${selectedText}
|
||||
\`\`\`
|
||||
|
||||
Please provide a clear and concise explanation of what this code does, including:
|
||||
1. The purpose and functionality
|
||||
2. Key components and their interactions
|
||||
3. Important patterns or techniques used
|
||||
`;
|
||||
|
||||
export const fixCodePrompt = (filePath: string, selectedText: string, diagnostics?: any[]) => {
|
||||
const diagnosticText = diagnostics && diagnostics.length > 0
|
||||
? `\nCurrent problems detected:
|
||||
${diagnostics.map(d => `- [${d.source || 'Error'}] ${d.message}${d.code ? ` (${d.code})` : ''}`).join('\n')}`
|
||||
: '';
|
||||
|
||||
return `
|
||||
Fix any issues in the following code from file path @/${filePath}
|
||||
${diagnosticText}
|
||||
|
||||
\`\`\`
|
||||
${selectedText}
|
||||
\`\`\`
|
||||
|
||||
Please:
|
||||
1. Address all detected problems listed above (if any)
|
||||
2. Identify any other potential bugs or issues
|
||||
3. Provide corrected code
|
||||
4. Explain what was fixed and why
|
||||
`;
|
||||
};
|
||||
|
||||
export const improveCodePrompt = (filePath: string, selectedText: string) => `
|
||||
Improve the following code from file path @/${filePath}:
|
||||
|
||||
\`\`\`
|
||||
${selectedText}
|
||||
\`\`\`
|
||||
|
||||
Please suggest improvements for:
|
||||
1. Code readability and maintainability
|
||||
2. Performance optimization
|
||||
3. Best practices and patterns
|
||||
4. Error handling and edge cases
|
||||
|
||||
Provide the improved code along with explanations for each enhancement.
|
||||
`;
|
||||
Reference in New Issue
Block a user