2
0

CodeActionProvider.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as vscode from "vscode"
  2. import { EditorUtils } from "./EditorUtils"
  3. export type CodeActionName = "EXPLAIN" | "FIX" | "IMPROVE" | "ADD_TO_CONTEXT" | "NEW_TASK"
  4. export type CodeActionId =
  5. | "roo-cline.explainCode"
  6. | "roo-cline.fixCode"
  7. | "roo-cline.improveCode"
  8. | "roo-cline.addToContext"
  9. | "roo-cline.newTask"
  10. export const ACTION_TITLES: Record<CodeActionName, string> = {
  11. EXPLAIN: "Explain with Roo Code",
  12. FIX: "Fix with Roo Code",
  13. IMPROVE: "Improve with Roo Code",
  14. ADD_TO_CONTEXT: "Add to Roo Code",
  15. NEW_TASK: "New Roo Code Task",
  16. } as const
  17. export const COMMAND_IDS: Record<CodeActionName, CodeActionId> = {
  18. EXPLAIN: "roo-cline.explainCode",
  19. FIX: "roo-cline.fixCode",
  20. IMPROVE: "roo-cline.improveCode",
  21. ADD_TO_CONTEXT: "roo-cline.addToContext",
  22. NEW_TASK: "roo-cline.newTask",
  23. } as const
  24. export class CodeActionProvider implements vscode.CodeActionProvider {
  25. public static readonly providedCodeActionKinds = [
  26. vscode.CodeActionKind.QuickFix,
  27. vscode.CodeActionKind.RefactorRewrite,
  28. ]
  29. private createAction(
  30. title: string,
  31. kind: vscode.CodeActionKind,
  32. command: CodeActionId,
  33. args: any[],
  34. ): vscode.CodeAction {
  35. const action = new vscode.CodeAction(title, kind)
  36. action.command = { command, title, arguments: args }
  37. return action
  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_TITLES.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. if (context.diagnostics.length > 0) {
  65. const relevantDiagnostics = context.diagnostics.filter((d) =>
  66. EditorUtils.hasIntersectingRange(effectiveRange.range, d.range),
  67. )
  68. if (relevantDiagnostics.length > 0) {
  69. actions.push(
  70. this.createAction(ACTION_TITLES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
  71. filePath,
  72. effectiveRange.text,
  73. effectiveRange.range.start.line + 1,
  74. effectiveRange.range.end.line + 1,
  75. relevantDiagnostics.map(EditorUtils.createDiagnosticData),
  76. ]),
  77. )
  78. }
  79. } else {
  80. actions.push(
  81. this.createAction(ACTION_TITLES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [
  82. filePath,
  83. effectiveRange.text,
  84. effectiveRange.range.start.line + 1,
  85. effectiveRange.range.end.line + 1,
  86. ]),
  87. )
  88. actions.push(
  89. this.createAction(ACTION_TITLES.IMPROVE, vscode.CodeActionKind.QuickFix, COMMAND_IDS.IMPROVE, [
  90. filePath,
  91. effectiveRange.text,
  92. effectiveRange.range.start.line + 1,
  93. effectiveRange.range.end.line + 1,
  94. ]),
  95. )
  96. }
  97. return actions
  98. } catch (error) {
  99. console.error("Error providing code actions:", error)
  100. return []
  101. }
  102. }
  103. }