11import * as vscode from "vscode"
2- import * as path from "path"
32import { ClineProvider } from "./webview/ClineProvider"
3+ import { EditorUtils } from "./EditorUtils"
44
55export const ACTION_NAMES = {
66 EXPLAIN : "Roo Code: Explain Code" ,
@@ -14,100 +14,12 @@ const COMMAND_IDS = {
1414 IMPROVE : "roo-cline.improveCode" ,
1515} as const
1616
17- interface DiagnosticData {
18- message : string
19- severity : vscode . DiagnosticSeverity
20- code ?: string | number | { value : string | number ; target : vscode . Uri }
21- source ?: string
22- range : vscode . Range
23- }
24-
25- interface EffectiveRange {
26- range : vscode . Range
27- text : string
28- }
29-
3017export class CodeActionProvider implements vscode . CodeActionProvider {
3118 public static readonly providedCodeActionKinds = [
3219 vscode . CodeActionKind . QuickFix ,
3320 vscode . CodeActionKind . RefactorRewrite ,
3421 ]
3522
36- // Cache file paths for performance
37- private readonly filePathCache = new WeakMap < vscode . TextDocument , string > ( )
38-
39- private getEffectiveRange (
40- document : vscode . TextDocument ,
41- range : vscode . Range | vscode . Selection ,
42- ) : EffectiveRange | null {
43- try {
44- const selectedText = document . getText ( range )
45- if ( selectedText ) {
46- return { range, text : selectedText }
47- }
48-
49- const currentLine = document . lineAt ( range . start . line )
50- if ( ! currentLine . text . trim ( ) ) {
51- return null
52- }
53-
54- // Optimize range creation by checking bounds first
55- const startLine = Math . max ( 0 , currentLine . lineNumber - 1 )
56- const endLine = Math . min ( document . lineCount - 1 , currentLine . lineNumber + 1 )
57-
58- // Only create new positions if needed
59- const effectiveRange = new vscode . Range (
60- startLine === currentLine . lineNumber ? range . start : new vscode . Position ( startLine , 0 ) ,
61- endLine === currentLine . lineNumber
62- ? range . end
63- : new vscode . Position ( endLine , document . lineAt ( endLine ) . text . length ) ,
64- )
65-
66- return {
67- range : effectiveRange ,
68- text : document . getText ( effectiveRange ) ,
69- }
70- } catch ( error ) {
71- console . error ( "Error getting effective range:" , error )
72- return null
73- }
74- }
75-
76- private getFilePath ( document : vscode . TextDocument ) : string {
77- // Check cache first
78- let filePath = this . filePathCache . get ( document )
79- if ( filePath ) {
80- return filePath
81- }
82-
83- try {
84- const workspaceFolder = vscode . workspace . getWorkspaceFolder ( document . uri )
85- if ( ! workspaceFolder ) {
86- filePath = document . uri . fsPath
87- } else {
88- const relativePath = path . relative ( workspaceFolder . uri . fsPath , document . uri . fsPath )
89- filePath = ! relativePath || relativePath . startsWith ( ".." ) ? document . uri . fsPath : relativePath
90- }
91-
92- // Cache the result
93- this . filePathCache . set ( document , filePath )
94- return filePath
95- } catch ( error ) {
96- console . error ( "Error getting file path:" , error )
97- return document . uri . fsPath
98- }
99- }
100-
101- private createDiagnosticData ( diagnostic : vscode . Diagnostic ) : DiagnosticData {
102- return {
103- message : diagnostic . message ,
104- severity : diagnostic . severity ,
105- code : diagnostic . code ,
106- source : diagnostic . source ,
107- range : diagnostic . range , // Reuse the range object
108- }
109- }
110-
11123 private createAction ( title : string , kind : vscode . CodeActionKind , command : string , args : any [ ] ) : vscode . CodeAction {
11224 const action = new vscode . CodeAction ( title , kind )
11325 action . command = { command, title, arguments : args }
@@ -126,47 +38,34 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
12638 ]
12739 }
12840
129- private hasIntersectingRange ( range1 : vscode . Range , range2 : vscode . Range ) : boolean {
130- // Optimize range intersection check
131- return ! (
132- range2 . end . line < range1 . start . line ||
133- range2 . start . line > range1 . end . line ||
134- ( range2 . end . line === range1 . start . line && range2 . end . character < range1 . start . character ) ||
135- ( range2 . start . line === range1 . end . line && range2 . start . character > range1 . end . character )
136- )
137- }
138-
13941 public provideCodeActions (
14042 document : vscode . TextDocument ,
14143 range : vscode . Range | vscode . Selection ,
14244 context : vscode . CodeActionContext ,
14345 ) : vscode . ProviderResult < ( vscode . CodeAction | vscode . Command ) [ ] > {
14446 try {
145- const effectiveRange = this . getEffectiveRange ( document , range )
47+ const effectiveRange = EditorUtils . getEffectiveRange ( document , range )
14648 if ( ! effectiveRange ) {
14749 return [ ]
14850 }
14951
150- const filePath = this . getFilePath ( document )
52+ const filePath = EditorUtils . getFilePath ( document )
15153 const actions : vscode . CodeAction [ ] = [ ]
15254
153- // Create actions using helper method
154- // Add explain actions
15555 actions . push (
15656 ...this . createActionPair ( ACTION_NAMES . EXPLAIN , vscode . CodeActionKind . QuickFix , COMMAND_IDS . EXPLAIN , [
15757 filePath ,
15858 effectiveRange . text ,
15959 ] ) ,
16060 )
16161
162- // Only process diagnostics if they exist
16362 if ( context . diagnostics . length > 0 ) {
16463 const relevantDiagnostics = context . diagnostics . filter ( ( d ) =>
165- this . hasIntersectingRange ( effectiveRange . range , d . range ) ,
64+ EditorUtils . hasIntersectingRange ( effectiveRange . range , d . range ) ,
16665 )
16766
16867 if ( relevantDiagnostics . length > 0 ) {
169- const diagnosticMessages = relevantDiagnostics . map ( this . createDiagnosticData )
68+ const diagnosticMessages = relevantDiagnostics . map ( EditorUtils . createDiagnosticData )
17069 actions . push (
17170 ...this . createActionPair ( ACTION_NAMES . FIX , vscode . CodeActionKind . QuickFix , COMMAND_IDS . FIX , [
17271 filePath ,
@@ -177,7 +76,6 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
17776 }
17877 }
17978
180- // Add improve actions
18179 actions . push (
18280 ...this . createActionPair (
18381 ACTION_NAMES . IMPROVE ,
0 commit comments