Skip to content

Commit e7e5511

Browse files
authored
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.
1 parent b46f6ad commit e7e5511

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,19 @@
185185
],
186186
"roo-code.contextMenu": [
187187
{
188-
"command": "roo-cline.explainCode",
188+
"command": "roo-cline.addToContext",
189189
"group": "1_actions@1"
190190
},
191191
{
192-
"command": "roo-cline.fixCode",
192+
"command": "roo-cline.explainCode",
193193
"group": "1_actions@2"
194194
},
195195
{
196-
"command": "roo-cline.improveCode",
196+
"command": "roo-cline.fixCode",
197197
"group": "1_actions@3"
198198
},
199199
{
200-
"command": "roo-cline.addToContext",
200+
"command": "roo-cline.improveCode",
201201
"group": "1_actions@4"
202202
}
203203
],

src/activate/registerCodeActions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,24 @@ const registerCodeAction = (
5353
// Handle both code action and direct command cases.
5454
let filePath: string
5555
let selectedText: string
56+
let startLine: number | undefined
57+
let endLine: number | undefined
5658
let diagnostics: any[] | undefined
5759

5860
if (args.length > 1) {
5961
// Called from code action.
60-
;[filePath, selectedText, diagnostics] = args
62+
;[filePath, selectedText, startLine, endLine, diagnostics] = args
6163
} else {
6264
// Called directly from command palette.
6365
const context = EditorUtils.getEditorContext()
6466
if (!context) return
65-
;({ filePath, selectedText, diagnostics } = context)
67+
;({ filePath, selectedText, startLine, endLine, diagnostics } = context)
6668
}
6769

6870
const params = {
6971
...{ filePath, selectedText },
72+
...(startLine !== undefined ? { startLine: startLine.toString() } : {}),
73+
...(endLine !== undefined ? { endLine: endLine.toString() } : {}),
7074
...(diagnostics ? { diagnostics } : {}),
7175
...(userInput ? { userInput } : {}),
7276
}

src/core/CodeActionProvider.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,26 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
5656
const filePath = EditorUtils.getFilePath(document)
5757
const actions: vscode.CodeAction[] = []
5858

59+
actions.push(
60+
this.createAction(
61+
ACTION_NAMES.ADD_TO_CONTEXT,
62+
vscode.CodeActionKind.QuickFix,
63+
COMMAND_IDS.ADD_TO_CONTEXT,
64+
[
65+
filePath,
66+
effectiveRange.text,
67+
effectiveRange.range.start.line + 1,
68+
effectiveRange.range.end.line + 1,
69+
],
70+
),
71+
)
72+
5973
actions.push(
6074
...this.createActionPair(ACTION_NAMES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [
6175
filePath,
6276
effectiveRange.text,
77+
effectiveRange.range.start.line + 1,
78+
effectiveRange.range.end.line + 1,
6379
]),
6480
)
6581

@@ -74,6 +90,8 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
7490
...this.createActionPair(ACTION_NAMES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
7591
filePath,
7692
effectiveRange.text,
93+
effectiveRange.range.start.line + 1,
94+
effectiveRange.range.end.line + 1,
7795
diagnosticMessages,
7896
]),
7997
)
@@ -83,6 +101,8 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
83101
...this.createActionPair(ACTION_NAMES.FIX_LOGIC, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
84102
filePath,
85103
effectiveRange.text,
104+
effectiveRange.range.start.line + 1,
105+
effectiveRange.range.end.line + 1,
86106
]),
87107
)
88108
}
@@ -92,16 +112,12 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
92112
ACTION_NAMES.IMPROVE,
93113
vscode.CodeActionKind.RefactorRewrite,
94114
COMMAND_IDS.IMPROVE,
95-
[filePath, effectiveRange.text],
96-
),
97-
)
98-
99-
actions.push(
100-
this.createAction(
101-
ACTION_NAMES.ADD_TO_CONTEXT,
102-
vscode.CodeActionKind.QuickFix,
103-
COMMAND_IDS.ADD_TO_CONTEXT,
104-
[filePath, effectiveRange.text],
115+
[
116+
filePath,
117+
effectiveRange.text,
118+
effectiveRange.range.start.line + 1,
119+
effectiveRange.range.end.line + 1,
120+
],
105121
),
106122
)
107123

src/core/EditorUtils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export interface EditorContext {
3838
filePath: string
3939
/** The effective text selected or derived from the document. */
4040
selectedText: string
41+
/** The starting line number of the selected text (1-based). */
42+
startLine: number
43+
/** The ending line number of the selected text (1-based). */
44+
endLine: number
4145
/** Optional list of diagnostics associated with the effective range. */
4246
diagnostics?: DiagnosticData[]
4347
}
@@ -194,6 +198,8 @@ export class EditorUtils {
194198
return {
195199
filePath,
196200
selectedText: effectiveRange.text,
201+
startLine: effectiveRange.range.start.line + 1, // Convert to 1-based line numbers
202+
endLine: effectiveRange.range.end.line + 1, // Convert to 1-based line numbers
197203
...(diagnostics.length > 0 ? { diagnostics } : {}),
198204
}
199205
} catch (error) {

src/core/__tests__/CodeActionProvider.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ describe("CodeActionProvider", () => {
7575
const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext)
7676

7777
expect(actions).toHaveLength(7) // 2 explain + 2 fix logic + 2 improve + 1 add to context
78-
expect((actions as any)[0].title).toBe(`${ACTION_NAMES.EXPLAIN} in New Task`)
79-
expect((actions as any)[1].title).toBe(`${ACTION_NAMES.EXPLAIN} in Current Task`)
80-
expect((actions as any)[2].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in New Task`)
81-
expect((actions as any)[3].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in Current Task`)
82-
expect((actions as any)[4].title).toBe(`${ACTION_NAMES.IMPROVE} in New Task`)
83-
expect((actions as any)[5].title).toBe(`${ACTION_NAMES.IMPROVE} in Current Task`)
84-
expect((actions as any)[6].title).toBe(ACTION_NAMES.ADD_TO_CONTEXT)
78+
expect((actions as any)[0].title).toBe(ACTION_NAMES.ADD_TO_CONTEXT)
79+
expect((actions as any)[1].title).toBe(`${ACTION_NAMES.EXPLAIN} in New Task`)
80+
expect((actions as any)[2].title).toBe(`${ACTION_NAMES.EXPLAIN} in Current Task`)
81+
expect((actions as any)[3].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in New Task`)
82+
expect((actions as any)[4].title).toBe(`${ACTION_NAMES.FIX_LOGIC} in Current Task`)
83+
expect((actions as any)[5].title).toBe(`${ACTION_NAMES.IMPROVE} in New Task`)
84+
expect((actions as any)[6].title).toBe(`${ACTION_NAMES.IMPROVE} in Current Task`)
8585
})
8686

8787
it("should provide fix action instead of fix logic when diagnostics exist", () => {

src/shared/support-prompt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const supportPromptConfigs: Record<string, SupportPromptConfig> = {
3535
\${userInput}`,
3636
},
3737
EXPLAIN: {
38-
template: `Explain the following code from file path @/\${filePath}:
38+
template: `Explain the following code from file path @/\${filePath} \${startLine}:\${endLine}
3939
\${userInput}
4040
4141
\`\`\`
@@ -48,7 +48,7 @@ Please provide a clear and concise explanation of what this code does, including
4848
3. Important patterns or techniques used`,
4949
},
5050
FIX: {
51-
template: `Fix any issues in the following code from file path @/\${filePath}
51+
template: `Fix any issues in the following code from file path @/\${filePath} \${startLine}:\${endLine}
5252
\${diagnosticText}
5353
\${userInput}
5454
@@ -63,7 +63,7 @@ Please:
6363
4. Explain what was fixed and why`,
6464
},
6565
IMPROVE: {
66-
template: `Improve the following code from file path @/\${filePath}:
66+
template: `Improve the following code from file path @/\${filePath} \${startLine}:\${endLine}
6767
\${userInput}
6868
6969
\`\`\`
@@ -79,7 +79,7 @@ Please suggest improvements for:
7979
Provide the improved code along with explanations for each enhancement.`,
8080
},
8181
ADD_TO_CONTEXT: {
82-
template: `\${filePath}:
82+
template: `\${filePath}:\${startLine}:\${endLine}
8383
\`\`\`
8484
\${selectedText}
8585
\`\`\``,

0 commit comments

Comments
 (0)