extension.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // This method is called when your extension is deactivated
  2. export function deactivate() {}
  3. import * as vscode from "vscode"
  4. export function activate(context: vscode.ExtensionContext) {
  5. const TERMINAL_NAME = "opencode"
  6. // Register command to open terminal in split screen and run opencode
  7. let openTerminalDisposable = vscode.commands.registerCommand("opencode.openTerminal", async () => {
  8. // Create a new terminal in split screen
  9. const port = Math.floor(Math.random() * (65535 - 16384 + 1)) + 16384
  10. const terminal = vscode.window.createTerminal({
  11. name: TERMINAL_NAME,
  12. iconPath: {
  13. light: vscode.Uri.file(context.asAbsolutePath("images/button-dark.svg")),
  14. dark: vscode.Uri.file(context.asAbsolutePath("images/button-light.svg")),
  15. },
  16. location: {
  17. viewColumn: vscode.ViewColumn.Beside,
  18. preserveFocus: false,
  19. },
  20. env: {
  21. _EXTENSION_OPENCODE_PORT: port.toString(),
  22. },
  23. })
  24. terminal.show()
  25. terminal.sendText(`OPENCODE_THEME=system OPENCODE_CALLER=vscode opencode --port ${port}`)
  26. const fileRef = getActiveFile()
  27. if (!fileRef) return
  28. // Wait for the terminal to be ready
  29. let tries = 10
  30. let connected = false
  31. do {
  32. await new Promise((resolve) => setTimeout(resolve, 200))
  33. try {
  34. await fetch(`http://localhost:${port}/app`)
  35. connected = true
  36. break
  37. } catch (e) {}
  38. tries--
  39. } while (tries > 0)
  40. // If connected, append the prompt to the terminal
  41. if (connected) {
  42. await appendPrompt(port, `In ${fileRef}`)
  43. terminal.show()
  44. }
  45. })
  46. // Register command to add filepath to terminal
  47. let addFilepathDisposable = vscode.commands.registerCommand("opencode.addFilepathToTerminal", async () => {
  48. const fileRef = getActiveFile()
  49. if (!fileRef) return
  50. const terminal = vscode.window.activeTerminal
  51. if (!terminal) return
  52. if (terminal.name === TERMINAL_NAME) {
  53. // @ts-ignore
  54. const port = terminal.creationOptions.env?.["_EXTENSION_OPENCODE_PORT"]
  55. port ? await appendPrompt(parseInt(port), fileRef) : terminal.sendText(fileRef)
  56. terminal.show()
  57. }
  58. })
  59. context.subscriptions.push(openTerminalDisposable, addFilepathDisposable)
  60. }
  61. async function appendPrompt(port: number, text: string) {
  62. await fetch(`http://localhost:${port}/tui/append-prompt`, {
  63. method: "POST",
  64. headers: {
  65. "Content-Type": "application/json",
  66. },
  67. body: JSON.stringify({ text }),
  68. })
  69. }
  70. function getActiveFile() {
  71. const activeEditor = vscode.window.activeTextEditor
  72. if (!activeEditor) return
  73. const document = activeEditor.document
  74. const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri)
  75. if (!workspaceFolder) return
  76. // Get the relative path from workspace root
  77. const relativePath = vscode.workspace.asRelativePath(document.uri)
  78. let filepathWithAt = `@${relativePath}`
  79. // Check if there's a selection and add line numbers
  80. const selection = activeEditor.selection
  81. if (!selection.isEmpty) {
  82. // Convert to 1-based line numbers
  83. const startLine = selection.start.line + 1
  84. const endLine = selection.end.line + 1
  85. if (startLine === endLine) {
  86. // Single line selection
  87. filepathWithAt += `#L${startLine}`
  88. } else {
  89. // Multi-line selection
  90. filepathWithAt += `#L${startLine}-${endLine}`
  91. }
  92. }
  93. return filepathWithAt
  94. }