cloudaxe-opencode/sdks/vscode/src/extension.ts

138 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

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