mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-22 10:14:22 +01:00
vscode: eslint fix
This commit is contained in:
@@ -1,46 +1,46 @@
|
|||||||
// This method is called when your extension is deactivated
|
// This method is called when your extension is deactivated
|
||||||
export function deactivate() {}
|
export function deactivate() {}
|
||||||
|
|
||||||
import * as vscode from "vscode"
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
const TERMINAL_NAME = "opencode"
|
const TERMINAL_NAME = "opencode";
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
let openNewTerminalDisposable = vscode.commands.registerCommand("opencode.openNewTerminal", async () => {
|
let openNewTerminalDisposable = vscode.commands.registerCommand("opencode.openNewTerminal", async () => {
|
||||||
await openTerminal()
|
await openTerminal();
|
||||||
})
|
});
|
||||||
|
|
||||||
let openTerminalDisposable = vscode.commands.registerCommand("opencode.openTerminal", async () => {
|
let openTerminalDisposable = vscode.commands.registerCommand("opencode.openTerminal", async () => {
|
||||||
// An opencode terminal already exists => focus it
|
// An opencode terminal already exists => focus it
|
||||||
const existingTerminal = vscode.window.terminals.find((t) => t.name === TERMINAL_NAME)
|
const existingTerminal = vscode.window.terminals.find((t) => t.name === TERMINAL_NAME);
|
||||||
if (existingTerminal) {
|
if (existingTerminal) {
|
||||||
existingTerminal.show()
|
existingTerminal.show();
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await openTerminal()
|
await openTerminal();
|
||||||
})
|
});
|
||||||
|
|
||||||
let addFilepathDisposable = vscode.commands.registerCommand("opencode.addFilepathToTerminal", async () => {
|
let addFilepathDisposable = vscode.commands.registerCommand("opencode.addFilepathToTerminal", async () => {
|
||||||
const fileRef = getActiveFile()
|
const fileRef = getActiveFile();
|
||||||
if (!fileRef) return
|
if (!fileRef) {return;}
|
||||||
|
|
||||||
const terminal = vscode.window.activeTerminal
|
const terminal = vscode.window.activeTerminal;
|
||||||
if (!terminal) return
|
if (!terminal) {return;}
|
||||||
|
|
||||||
if (terminal.name === TERMINAL_NAME) {
|
if (terminal.name === TERMINAL_NAME) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const port = terminal.creationOptions.env?.["_EXTENSION_OPENCODE_PORT"]
|
const port = terminal.creationOptions.env?.["_EXTENSION_OPENCODE_PORT"];
|
||||||
port ? await appendPrompt(parseInt(port), fileRef) : terminal.sendText(fileRef)
|
port ? await appendPrompt(parseInt(port), fileRef) : terminal.sendText(fileRef);
|
||||||
terminal.show()
|
terminal.show();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
context.subscriptions.push(openTerminalDisposable, addFilepathDisposable)
|
context.subscriptions.push(openTerminalDisposable, addFilepathDisposable);
|
||||||
|
|
||||||
async function openTerminal() {
|
async function openTerminal() {
|
||||||
// Create a new terminal in split screen
|
// Create a new terminal in split screen
|
||||||
const port = Math.floor(Math.random() * (65535 - 16384 + 1)) + 16384
|
const port = Math.floor(Math.random() * (65535 - 16384 + 1)) + 16384;
|
||||||
const terminal = vscode.window.createTerminal({
|
const terminal = vscode.window.createTerminal({
|
||||||
name: TERMINAL_NAME,
|
name: TERMINAL_NAME,
|
||||||
iconPath: {
|
iconPath: {
|
||||||
@@ -55,32 +55,32 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
_EXTENSION_OPENCODE_PORT: port.toString(),
|
_EXTENSION_OPENCODE_PORT: port.toString(),
|
||||||
OPENCODE_CALLER: "vscode",
|
OPENCODE_CALLER: "vscode",
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
terminal.show()
|
terminal.show();
|
||||||
terminal.sendText(`opencode --port ${port}`)
|
terminal.sendText(`opencode --port ${port}`);
|
||||||
|
|
||||||
const fileRef = getActiveFile()
|
const fileRef = getActiveFile();
|
||||||
if (!fileRef) return
|
if (!fileRef) {return;}
|
||||||
|
|
||||||
// Wait for the terminal to be ready
|
// Wait for the terminal to be ready
|
||||||
let tries = 10
|
let tries = 10;
|
||||||
let connected = false
|
let connected = false;
|
||||||
do {
|
do {
|
||||||
await new Promise((resolve) => setTimeout(resolve, 200))
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||||
try {
|
try {
|
||||||
await fetch(`http://localhost:${port}/app`)
|
await fetch(`http://localhost:${port}/app`);
|
||||||
connected = true
|
connected = true;
|
||||||
break
|
break;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
tries--
|
tries--;
|
||||||
} while (tries > 0)
|
} while (tries > 0);
|
||||||
|
|
||||||
// If connected, append the prompt to the terminal
|
// If connected, append the prompt to the terminal
|
||||||
if (connected) {
|
if (connected) {
|
||||||
await appendPrompt(port, `In ${fileRef}`)
|
await appendPrompt(port, `In ${fileRef}`);
|
||||||
terminal.show()
|
terminal.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,37 +91,37 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ text }),
|
body: JSON.stringify({ text }),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getActiveFile() {
|
function getActiveFile() {
|
||||||
const activeEditor = vscode.window.activeTextEditor
|
const activeEditor = vscode.window.activeTextEditor;
|
||||||
if (!activeEditor) return
|
if (!activeEditor) {return;}
|
||||||
|
|
||||||
const document = activeEditor.document
|
const document = activeEditor.document;
|
||||||
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri)
|
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
|
||||||
if (!workspaceFolder) return
|
if (!workspaceFolder) {return;}
|
||||||
|
|
||||||
// Get the relative path from workspace root
|
// Get the relative path from workspace root
|
||||||
const relativePath = vscode.workspace.asRelativePath(document.uri)
|
const relativePath = vscode.workspace.asRelativePath(document.uri);
|
||||||
let filepathWithAt = `@${relativePath}`
|
let filepathWithAt = `@${relativePath}`;
|
||||||
|
|
||||||
// Check if there's a selection and add line numbers
|
// Check if there's a selection and add line numbers
|
||||||
const selection = activeEditor.selection
|
const selection = activeEditor.selection;
|
||||||
if (!selection.isEmpty) {
|
if (!selection.isEmpty) {
|
||||||
// Convert to 1-based line numbers
|
// Convert to 1-based line numbers
|
||||||
const startLine = selection.start.line + 1
|
const startLine = selection.start.line + 1;
|
||||||
const endLine = selection.end.line + 1
|
const endLine = selection.end.line + 1;
|
||||||
|
|
||||||
if (startLine === endLine) {
|
if (startLine === endLine) {
|
||||||
// Single line selection
|
// Single line selection
|
||||||
filepathWithAt += `#L${startLine}`
|
filepathWithAt += `#L${startLine}`;
|
||||||
} else {
|
} else {
|
||||||
// Multi-line selection
|
// Multi-line selection
|
||||||
filepathWithAt += `#L${startLine}-${endLine}`
|
filepathWithAt += `#L${startLine}-${endLine}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepathWithAt
|
return filepathWithAt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user