opencode_offline/sdks/vscode/src/extension.ts

127 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-07-20 23:33:41 +08:00
// This method is called when your extension is deactivated
export function deactivate() {}
2025-07-23 03:28:09 +08:00
import * as vscode from "vscode"
2025-07-20 23:33:41 +08:00
2025-07-28 03:54:45 +08:00
const TERMINAL_NAME = "opencode"
2025-07-20 23:33:41 +08:00
export function activate(context: vscode.ExtensionContext) {
2025-07-28 03:54:45 +08:00
let openNewTerminalDisposable = vscode.commands.registerCommand("opencode.openNewTerminal", async () => {
await openTerminal()
})
2025-07-23 03:36:55 +08:00
2025-07-22 03:48:42 +08:00
let openTerminalDisposable = vscode.commands.registerCommand("opencode.openTerminal", async () => {
// An opencode terminal already exists => focus it
const existingTerminal = vscode.window.terminals.find((t) => t.name === TERMINAL_NAME)
if (existingTerminal) {
existingTerminal.show()
return
}
2025-07-28 03:54:45 +08:00
await openTerminal()
})
let addFilepathDisposable = vscode.commands.registerCommand("opencode.addFilepathToTerminal", async () => {
const fileRef = getActiveFile()
if (!fileRef) return
const terminal = vscode.window.activeTerminal
if (!terminal) return
if (terminal.name === TERMINAL_NAME) {
// @ts-ignore
const port = terminal.creationOptions.env?.["_EXTENSION_OPENCODE_PORT"]
port ? await appendPrompt(parseInt(port), fileRef) : terminal.sendText(fileRef)
terminal.show()
}
})
context.subscriptions.push(openTerminalDisposable, addFilepathDisposable)
async function openTerminal() {
2025-07-22 03:48:42 +08:00
// Create a new terminal in split screen
2025-07-23 03:28:09 +08:00
const port = Math.floor(Math.random() * (65535 - 16384 + 1)) + 16384
2025-07-22 03:48:42 +08:00
const terminal = vscode.window.createTerminal({
2025-07-23 03:36:55 +08:00
name: TERMINAL_NAME,
2025-07-23 03:28:09 +08:00
iconPath: {
light: vscode.Uri.file(context.asAbsolutePath("images/button-dark.svg")),
dark: vscode.Uri.file(context.asAbsolutePath("images/button-light.svg")),
},
2025-07-22 03:48:42 +08:00
location: {
viewColumn: vscode.ViewColumn.Beside,
preserveFocus: false,
},
2025-07-23 03:28:09 +08:00
env: {
_EXTENSION_OPENCODE_PORT: port.toString(),
},
})
2025-07-23 03:36:55 +08:00
terminal.show()
terminal.sendText(`OPENCODE_CALLER=vscode opencode --port ${port}`)
2025-07-23 03:28:09 +08:00
const fileRef = getActiveFile()
if (!fileRef) return
2025-07-20 23:33:41 +08:00
2025-07-23 03:28:09 +08:00
// Wait for the terminal to be ready
let tries = 10
let connected = false
do {
await new Promise((resolve) => setTimeout(resolve, 200))
try {
await fetch(`http://localhost:${port}/app`)
connected = true
break
} catch (e) {}
tries--
} while (tries > 0)
// If connected, append the prompt to the terminal
if (connected) {
await appendPrompt(port, `In ${fileRef}`)
2025-07-23 03:36:55 +08:00
terminal.show()
2025-07-23 03:28:09 +08:00
}
2025-07-28 03:54:45 +08:00
}
2025-07-20 23:33:41 +08:00
2025-07-28 03:54:45 +08:00
async function appendPrompt(port: number, text: string) {
await fetch(`http://localhost:${port}/tui/append-prompt`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ text }),
})
}
2025-07-20 23:33:41 +08:00
2025-07-28 03:54:45 +08:00
function getActiveFile() {
const activeEditor = vscode.window.activeTextEditor
if (!activeEditor) return
const document = activeEditor.document
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri)
if (!workspaceFolder) return
// Get the relative path from workspace root
const relativePath = vscode.workspace.asRelativePath(document.uri)
let filepathWithAt = `@${relativePath}`
// Check if there's a selection and add line numbers
const selection = activeEditor.selection
if (!selection.isEmpty) {
// Convert to 1-based line numbers
const startLine = selection.start.line + 1
const endLine = selection.end.line + 1
if (startLine === endLine) {
// Single line selection
filepathWithAt += `#L${startLine}`
} else {
// Multi-line selection
filepathWithAt += `#L${startLine}-${endLine}`
}
2025-07-23 03:36:55 +08:00
}
2025-07-23 03:28:09 +08:00
2025-07-28 03:54:45 +08:00
return filepathWithAt
2025-07-23 03:28:09 +08:00
}
2025-07-20 23:33:41 +08:00
}