From e7e5511b6525e2946d4420c657b6d04c177ba19f Mon Sep 17 00:00:00 2001 From: Sam Hoang Van Date: Fri, 28 Mar 2025 23:01:16 +0700 Subject: [PATCH] feat: prioritize "Add to Context" and add line number tracking (#2063) - Move "Add to Context" to the top of submenu and code actions for improved accessibility - Add line number tracking (startLine/endLine) to EditorContext and code actions - Update templates in support-prompt.ts to include line numbers in file references - Ensure backward compatibility with existing code This change improves the UX by making the frequently used "Add to Context" action more accessible and enhances context awareness by tracking and displaying line numbers for selected code. --- package.json | 8 ++--- src/activate/registerCodeActions.ts | 8 +++-- src/core/CodeActionProvider.ts | 36 +++++++++++++------ src/core/EditorUtils.ts | 6 ++++ src/core/__tests__/CodeActionProvider.test.ts | 14 ++++---- src/shared/support-prompt.ts | 8 ++--- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 830b77d2ca..cf2d7a5e66 100644 --- a/package.json +++ b/package.json @@ -185,19 +185,19 @@ ], "roo-code.contextMenu": [ { - "command": "roo-cline.explainCode", + "command": "roo-cline.addToContext", "group": "1_actions@1" }, { - "command": "roo-cline.fixCode", + "command": "roo-cline.explainCode", "group": "1_actions@2" }, { - "command": "roo-cline.improveCode", + "command": "roo-cline.fixCode", "group": "1_actions@3" }, { - "command": "roo-cline.addToContext", + "command": "roo-cline.improveCode", "group": "1_actions@4" } ], diff --git a/src/activate/registerCodeActions.ts b/src/activate/registerCodeActions.ts index 35e0766628..31f474442d 100644 --- a/src/activate/registerCodeActions.ts +++ b/src/activate/registerCodeActions.ts @@ -53,20 +53,24 @@ const registerCodeAction = ( // Handle both code action and direct command cases. let filePath: string let selectedText: string + let startLine: number | undefined + let endLine: number | undefined let diagnostics: any[] | undefined if (args.length > 1) { // Called from code action. - ;[filePath, selectedText, diagnostics] = args + ;[filePath, selectedText, startLine, endLine, diagnostics] = args } else { // Called directly from command palette. const context = EditorUtils.getEditorContext() if (!context) return - ;({ filePath, selectedText, diagnostics } = context) + ;({ filePath, selectedText, startLine, endLine, diagnostics } = context) } const params = { ...{ filePath, selectedText }, + ...(startLine !== undefined ? { startLine: startLine.toString() } : {}), + ...(endLine !== undefined ? { endLine: endLine.toString() } : {}), ...(diagnostics ? { diagnostics } : {}), ...(userInput ? { userInput } : {}), } diff --git a/src/core/CodeActionProvider.ts b/src/core/CodeActionProvider.ts index 040021a51f..f9a90e854e 100644 --- a/src/core/CodeActionProvider.ts +++ b/src/core/CodeActionProvider.ts @@ -56,10 +56,26 @@ export class CodeActionProvider implements vscode.CodeActionProvider { const filePath = EditorUtils.getFilePath(document) const actions: vscode.CodeAction[] = [] + actions.push( + this.createAction( + ACTION_NAMES.ADD_TO_CONTEXT, + vscode.CodeActionKind.QuickFix, + COMMAND_IDS.ADD_TO_CONTEXT, + [ + filePath, + effectiveRange.text, + effectiveRange.range.start.line + 1, + effectiveRange.range.end.line + 1, + ], + ), + ) + actions.push( ...this.createActionPair(ACTION_NAMES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [ filePath, effectiveRange.text, + effectiveRange.range.start.line + 1, + effectiveRange.range.end.line + 1, ]), ) @@ -74,6 +90,8 @@ export class CodeActionProvider implements vscode.CodeActionProvider { ...this.createActionPair(ACTION_NAMES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [ filePath, effectiveRange.text, + effectiveRange.range.start.line + 1, + effectiveRange.range.end.line + 1, diagnosticMessages, ]), ) @@ -83,6 +101,8 @@ export class CodeActionProvider implements vscode.CodeActionProvider { ...this.createActionPair(ACTION_NAMES.FIX_LOGIC, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [ filePath, effectiveRange.text, + effectiveRange.range.start.line + 1, + effectiveRange.range.end.line + 1, ]), ) } @@ -92,16 +112,12 @@ export class CodeActionProvider implements vscode.CodeActionProvider { ACTION_NAMES.IMPROVE, vscode.CodeActionKind.RefactorRewrite, COMMAND_IDS.IMPROVE, - [filePath, effectiveRange.text], - ), - ) - - actions.push( - this.createAction( - ACTION_NAMES.ADD_TO_CONTEXT, - vscode.CodeActionKind.QuickFix, - COMMAND_IDS.ADD_TO_CONTEXT, - [filePath, effectiveRange.text], + [ + filePath, + effectiveRange.text, + effectiveRange.range.start.line + 1, + effectiveRange.range.end.line + 1, + ], ), ) diff --git a/src/core/EditorUtils.ts b/src/core/EditorUtils.ts index ee81353b7b..eb7aa6c800 100644 --- a/src/core/EditorUtils.ts +++ b/src/core/EditorUtils.ts @@ -38,6 +38,10 @@ export interface EditorContext { filePath: string /** The effective text selected or derived from the document. */ selectedText: string + /** The starting line number of the selected text (1-based). */ + startLine: number + /** The ending line number of the selected text (1-based). */ + endLine: number /** Optional list of diagnostics associated with the effective range. */ diagnostics?: DiagnosticData[] } @@ -194,6 +198,8 @@ export class EditorUtils { return { filePath, selectedText: effectiveRange.text, + startLine: effectiveRange.range.start.line + 1, // Convert to 1-based line numbers + endLine: effectiveRange.range.end.line + 1, // Convert to 1-based line numbers ...(diagnostics.length > 0 ? { diagnostics } : {}), } } catch (error) { diff --git a/src/core/__tests__/CodeActionProvider.test.ts b/src/core/__tests__/CodeActionProvider.test.ts index 6042f41b2b..6ea2adf894 100644 --- a/src/core/__tests__/CodeActionProvider.test.ts +++ b/src/core/__tests__/CodeActionProvider.test.ts @@ -75,13 +75,13 @@ describe("CodeActionProvider", () => { const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext) expect(actions).toHaveLength(7) // 2 explain + 2 fix logic + 2 improve + 1 add to context - expect((actions as any)[0].title).toBe(`${ACTION_NAMES.EXPLAIN} in New Task`) - expect((actions as any)[1].title).toBe(`${ACTION_NAMES.EXPLAIN} in Current Task`) - expect((actions as any)[2].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in New Task`) - expect((actions as any)[3].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in Current Task`) - expect((actions as any)[4].title).toBe(`${ACTION_NAMES.IMPROVE} in New Task`) - expect((actions as any)[5].title).toBe(`${ACTION_NAMES.IMPROVE} in Current Task`) - expect((actions as any)[6].title).toBe(ACTION_NAMES.ADD_TO_CONTEXT) + expect((actions as any)[0].title).toBe(ACTION_NAMES.ADD_TO_CONTEXT) + expect((actions as any)[1].title).toBe(`${ACTION_NAMES.EXPLAIN} in New Task`) + expect((actions as any)[2].title).toBe(`${ACTION_NAMES.EXPLAIN} in Current Task`) + expect((actions as any)[3].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in New Task`) + expect((actions as any)[4].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in Current Task`) + expect((actions as any)[5].title).toBe(`${ACTION_NAMES.IMPROVE} in New Task`) + expect((actions as any)[6].title).toBe(`${ACTION_NAMES.IMPROVE} in Current Task`) }) it("should provide fix action instead of fix logic when diagnostics exist", () => { diff --git a/src/shared/support-prompt.ts b/src/shared/support-prompt.ts index cc5e3e1d0d..d6391ff380 100644 --- a/src/shared/support-prompt.ts +++ b/src/shared/support-prompt.ts @@ -35,7 +35,7 @@ const supportPromptConfigs: Record = { \${userInput}`, }, EXPLAIN: { - template: `Explain the following code from file path @/\${filePath}: + template: `Explain the following code from file path @/\${filePath} \${startLine}:\${endLine} \${userInput} \`\`\` @@ -48,7 +48,7 @@ Please provide a clear and concise explanation of what this code does, including 3. Important patterns or techniques used`, }, FIX: { - template: `Fix any issues in the following code from file path @/\${filePath} + template: `Fix any issues in the following code from file path @/\${filePath} \${startLine}:\${endLine} \${diagnosticText} \${userInput} @@ -63,7 +63,7 @@ Please: 4. Explain what was fixed and why`, }, IMPROVE: { - template: `Improve the following code from file path @/\${filePath}: + template: `Improve the following code from file path @/\${filePath} \${startLine}:\${endLine} \${userInput} \`\`\` @@ -79,7 +79,7 @@ Please suggest improvements for: Provide the improved code along with explanations for each enhancement.`, }, ADD_TO_CONTEXT: { - template: `\${filePath}: + template: `\${filePath}:\${startLine}:\${endLine} \`\`\` \${selectedText} \`\`\``,