extension.ts 3.6 KB

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