CodeActionProvider.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import * as vscode from "vscode"
  2. import { EditorUtils } from "./EditorUtils"
  3. export const ACTION_NAMES = {
  4. EXPLAIN: "Roo Code: Explain Code",
  5. FIX: "Roo Code: Fix Code",
  6. FIX_LOGIC: "Roo Code: Fix Logic",
  7. IMPROVE: "Roo Code: Improve Code",
  8. ADD_TO_CONTEXT: "Roo Code: Add to Context",
  9. NEW_TASK: "Roo Code: New Task",
  10. } as const
  11. export const COMMAND_IDS = {
  12. EXPLAIN: "roo-cline.explainCode",
  13. FIX: "roo-cline.fixCode",
  14. IMPROVE: "roo-cline.improveCode",
  15. ADD_TO_CONTEXT: "roo-cline.addToContext",
  16. NEW_TASK: "roo-cline.newTask",
  17. } as const
  18. export class CodeActionProvider implements vscode.CodeActionProvider {
  19. public static readonly providedCodeActionKinds = [
  20. vscode.CodeActionKind.QuickFix,
  21. vscode.CodeActionKind.RefactorRewrite,
  22. ]
  23. private createAction(title: string, kind: vscode.CodeActionKind, command: string, args: any[]): vscode.CodeAction {
  24. const action = new vscode.CodeAction(title, kind)
  25. action.command = { command, title, arguments: args }
  26. return action
  27. }
  28. private createActionPair(
  29. baseTitle: string,
  30. kind: vscode.CodeActionKind,
  31. baseCommand: string,
  32. args: any[],
  33. ): vscode.CodeAction[] {
  34. return [
  35. this.createAction(`${baseTitle} in New Task`, kind, baseCommand, args),
  36. this.createAction(`${baseTitle} in Current Task`, kind, `${baseCommand}InCurrentTask`, args),
  37. ]
  38. }
  39. public provideCodeActions(
  40. document: vscode.TextDocument,
  41. range: vscode.Range | vscode.Selection,
  42. context: vscode.CodeActionContext,
  43. ): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> {
  44. try {
  45. const effectiveRange = EditorUtils.getEffectiveRange(document, range)
  46. if (!effectiveRange) {
  47. return []
  48. }
  49. const filePath = EditorUtils.getFilePath(document)
  50. const actions: vscode.CodeAction[] = []
  51. actions.push(
  52. this.createAction(
  53. ACTION_NAMES.ADD_TO_CONTEXT,
  54. vscode.CodeActionKind.QuickFix,
  55. COMMAND_IDS.ADD_TO_CONTEXT,
  56. [
  57. filePath,
  58. effectiveRange.text,
  59. effectiveRange.range.start.line + 1,
  60. effectiveRange.range.end.line + 1,
  61. ],
  62. ),
  63. )
  64. actions.push(
  65. ...this.createActionPair(ACTION_NAMES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [
  66. filePath,
  67. effectiveRange.text,
  68. effectiveRange.range.start.line + 1,
  69. effectiveRange.range.end.line + 1,
  70. ]),
  71. )
  72. if (context.diagnostics.length > 0) {
  73. const relevantDiagnostics = context.diagnostics.filter((d) =>
  74. EditorUtils.hasIntersectingRange(effectiveRange.range, d.range),
  75. )
  76. if (relevantDiagnostics.length > 0) {
  77. const diagnosticMessages = relevantDiagnostics.map(EditorUtils.createDiagnosticData)
  78. actions.push(
  79. ...this.createActionPair(ACTION_NAMES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
  80. filePath,
  81. effectiveRange.text,
  82. effectiveRange.range.start.line + 1,
  83. effectiveRange.range.end.line + 1,
  84. diagnosticMessages,
  85. ]),
  86. )
  87. }
  88. } else {
  89. actions.push(
  90. ...this.createActionPair(ACTION_NAMES.FIX_LOGIC, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
  91. filePath,
  92. effectiveRange.text,
  93. effectiveRange.range.start.line + 1,
  94. effectiveRange.range.end.line + 1,
  95. ]),
  96. )
  97. }
  98. actions.push(
  99. ...this.createActionPair(
  100. ACTION_NAMES.IMPROVE,
  101. vscode.CodeActionKind.RefactorRewrite,
  102. COMMAND_IDS.IMPROVE,
  103. [
  104. filePath,
  105. effectiveRange.text,
  106. effectiveRange.range.start.line + 1,
  107. effectiveRange.range.end.line + 1,
  108. ],
  109. ),
  110. )
  111. return actions
  112. } catch (error) {
  113. console.error("Error providing code actions:", error)
  114. return []
  115. }
  116. }
  117. }