extension.ts 3.7 KB

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