summaryrefslogtreecommitdiffhomepage
path: root/sdks/vscode/src/extension.ts
blob: ce5cf5bf3ebc2b830722092717c34bf8cc5d469a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// This method is called when your extension is deactivated
export function deactivate() {}

import * as vscode from "vscode";

export function activate(context: vscode.ExtensionContext) {
  const TERMINAL_NAME = "opencode Terminal";

  // Register command to open terminal in split screen and run opencode
  let openTerminalDisposable = vscode.commands.registerCommand(
    "opencode.openTerminal",
    async () => {
      // Create a new terminal in split screen
      const terminal = vscode.window.createTerminal({
        name: TERMINAL_NAME,
        location: {
          viewColumn: vscode.ViewColumn.Beside,
          preserveFocus: false,
        },
      });

      // Show the terminal
      terminal.show();

      // Send the opencode command to the terminal
      terminal.sendText("opencode");
    }
  );

  // Register command to add filepath to terminal
  let addFilepathDisposable = vscode.commands.registerCommand(
    "opencode.addFilepathToTerminal",
    async () => {
      const activeEditor = vscode.window.activeTextEditor;

      if (!activeEditor) {
        vscode.window.showInformationMessage("No active file to get path from");
        return;
      }

      const document = activeEditor.document;
      const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);

      if (!workspaceFolder) {
        vscode.window.showInformationMessage("File is not in a workspace");
        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}`;
        }
      }

      // Get or create terminal
      let terminal = vscode.window.activeTerminal;
      if (terminal?.name === TERMINAL_NAME) {
        terminal.sendText(filepathWithAt);
        terminal.show();
      }
    }
  );

  context.subscriptions.push(openTerminalDisposable, addFilepathDisposable);
}