registerCodeActions.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import * as vscode from "vscode"
  2. import { CodeActionId, CodeActionName } from "@roo-code/types"
  3. import { getCodeActionCommand } from "../utils/commands"
  4. import { EditorUtils } from "../integrations/editor/EditorUtils"
  5. import { ClineProvider } from "../core/webview/ClineProvider"
  6. export const registerCodeActions = (context: vscode.ExtensionContext) => {
  7. registerCodeAction(context, "explainCode", "EXPLAIN")
  8. registerCodeAction(context, "fixCode", "FIX")
  9. registerCodeAction(context, "improveCode", "IMPROVE")
  10. registerCodeAction(context, "addToContext", "ADD_TO_CONTEXT")
  11. }
  12. const registerCodeAction = (context: vscode.ExtensionContext, command: CodeActionId, promptType: CodeActionName) => {
  13. let userInput: string | undefined
  14. context.subscriptions.push(
  15. vscode.commands.registerCommand(getCodeActionCommand(command), async (...args: any[]) => {
  16. // Handle both code action and direct command cases.
  17. let filePath: string
  18. let selectedText: string
  19. let startLine: number | undefined
  20. let endLine: number | undefined
  21. let diagnostics: any[] | undefined
  22. if (args.length > 1) {
  23. // Called from code action.
  24. ;[filePath, selectedText, startLine, endLine, diagnostics] = args
  25. } else {
  26. // Called directly from command palette.
  27. const context = EditorUtils.getEditorContext()
  28. if (!context) {
  29. return
  30. }
  31. ;({ filePath, selectedText, startLine, endLine, diagnostics } = context)
  32. }
  33. const params = {
  34. ...{ filePath, selectedText },
  35. ...(startLine !== undefined ? { startLine: startLine.toString() } : {}),
  36. ...(endLine !== undefined ? { endLine: endLine.toString() } : {}),
  37. ...(diagnostics ? { diagnostics } : {}),
  38. ...(userInput ? { userInput } : {}),
  39. }
  40. await ClineProvider.handleCodeAction(command, promptType, params)
  41. }),
  42. )
  43. }