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