summaryrefslogtreecommitdiffhomepage
path: root/sdks/vscode/src/extension.ts
diff options
context:
space:
mode:
authorFrank <[email protected]>2025-07-27 15:54:45 -0400
committerFrank <[email protected]>2025-07-27 15:54:45 -0400
commita07d149e28ab8ece54296f5c707b1d2396b3c7ed (patch)
tree01439c35dca2f3b29b01cf94ac0951fd3eb2d3a6 /sdks/vscode/src/extension.ts
parent3eb982c8cd5a593f1649d49bd90eab4ae7fecd3f (diff)
downloadopencode-a07d149e28ab8ece54296f5c707b1d2396b3c7ed.tar.gz
opencode-a07d149e28ab8ece54296f5c707b1d2396b3c7ed.zip
vscode: add cmd+shift+esc keybinding
Diffstat (limited to 'sdks/vscode/src/extension.ts')
-rw-r--r--sdks/vscode/src/extension.ts120
1 files changed, 63 insertions, 57 deletions
diff --git a/sdks/vscode/src/extension.ts b/sdks/vscode/src/extension.ts
index fa6e5c55f..34ec8c64e 100644
--- a/sdks/vscode/src/extension.ts
+++ b/sdks/vscode/src/extension.ts
@@ -3,10 +3,13 @@ export function deactivate() {}
import * as vscode from "vscode"
+const TERMINAL_NAME = "opencode"
+
export function activate(context: vscode.ExtensionContext) {
- const TERMINAL_NAME = "opencode"
+ let openNewTerminalDisposable = vscode.commands.registerCommand("opencode.openNewTerminal", async () => {
+ await openTerminal()
+ })
- // Register command to open terminal in split screen and run opencode
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)
@@ -15,6 +18,27 @@ export function activate(context: vscode.ExtensionContext) {
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)
+ terminal.show()
+ }
+ })
+
+ context.subscriptions.push(openTerminalDisposable, addFilepathDisposable)
+
+ async function openTerminal() {
// Create a new terminal in split screen
const port = Math.floor(Math.random() * (65535 - 16384 + 1)) + 16384
const terminal = vscode.window.createTerminal({
@@ -57,64 +81,46 @@ export function activate(context: vscode.ExtensionContext) {
await appendPrompt(port, `In ${fileRef}`)
terminal.show()
}
- })
-
- // Register command to add filepath to terminal
- let addFilepathDisposable = vscode.commands.registerCommand("opencode.addFilepathToTerminal", async () => {
- const fileRef = getActiveFile()
- if (!fileRef) return
+ }
- const terminal = vscode.window.activeTerminal
- if (!terminal) return
+ 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 }),
+ })
+ }
- 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()
+ 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}`
+ }
}
- })
-
- context.subscriptions.push(openTerminalDisposable, addFilepathDisposable)
-}
-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 }),
- })
-}
-
-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}`
- }
+ return filepathWithAt
}
-
- return filepathWithAt
}