Skip to content

Commit 26a4061

Browse files
author
Frank
committed
wip: vscode extension
1 parent db2fbed commit 26a4061

1 file changed

Lines changed: 85 additions & 45 deletions

File tree

‎sdks/vscode/src/extension.ts‎

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,110 @@
11
// This method is called when your extension is deactivated
22
export function deactivate() {}
33

4-
import * as vscode from "vscode";
4+
import * as vscode from "vscode"
55

66
export function activate(context: vscode.ExtensionContext) {
7-
const TERMINAL_NAME = "opencode Terminal";
8-
97
// Register command to open terminal in split screen and run opencode
108
let openTerminalDisposable = vscode.commands.registerCommand("opencode.openTerminal", async () => {
119
// Create a new terminal in split screen
10+
const port = Math.floor(Math.random() * (65535 - 16384 + 1)) + 16384
1211
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+
},
1417
location: {
1518
viewColumn: vscode.ViewColumn.Beside,
1619
preserveFocus: false,
1720
},
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
1931

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+
})
2351

2452
// Register command to add filepath to terminal
2553
let addFilepathDisposable = vscode.commands.registerCommand("opencode.addFilepathToTerminal", async () => {
26-
const activeEditor = vscode.window.activeTextEditor;
54+
const fileRef = getActiveFile()
55+
if (!fileRef) return
2756

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
3259

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
3563

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+
})
4067

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
6099

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}`
66106
}
67-
});
107+
}
68108

69-
context.subscriptions.push(openTerminalDisposable, addFilepathDisposable);
109+
return filepathWithAt
70110
}

0 commit comments

Comments
 (0)