aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-vscode/src/activate.ts
blob: cbcb8cc1a8ecfa86a895f2ed7e7042cc9233e391 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as vscode from "vscode";
import { CancellationToken, DebugConfiguration, ProviderResult, WorkspaceFolder } from "vscode";
import { DAPAdapter } from "./dap";
import lockfile from "./lockfile";

export function activateBunDebug(context: vscode.ExtensionContext, factory?: vscode.DebugAdapterDescriptorFactory) {
  lockfile(context);

  context.subscriptions.push(
    vscode.commands.registerCommand("extension.bun.runEditorContents", (resource: vscode.Uri) => {
      let targetResource = resource;
      if (!targetResource && vscode.window.activeTextEditor) {
        targetResource = vscode.window.activeTextEditor.document.uri;
      }
      if (targetResource) {
        vscode.debug.startDebugging(
          undefined,
          {
            type: "bun",
            name: "Run File",
            request: "launch",
            program: targetResource.fsPath,
          },
          { noDebug: true },
        );
      }
    }),
    vscode.commands.registerCommand("extension.bun.debugEditorContents", (resource: vscode.Uri) => {
      let targetResource = resource;
      if (!targetResource && vscode.window.activeTextEditor) {
        targetResource = vscode.window.activeTextEditor.document.uri;
      }
      if (targetResource) {
        vscode.debug.startDebugging(undefined, {
          type: "bun",
          name: "Debug File",
          request: "launch",
          program: targetResource.fsPath,
          stopOnEntry: true,
        });
      }
    }),
  );

  context.subscriptions.push(
    vscode.commands.registerCommand("extension.bun.getProgramName", config => {
      return vscode.window.showInputBox({
        placeHolder: "Please enter the name of a file in the workspace folder",
        value: "src/index.js",
      });
    }),
  );

  const provider = new BunConfigurationProvider();
  context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider("bun", provider));

  context.subscriptions.push(
    vscode.debug.registerDebugConfigurationProvider(
      "bun",
      {
        provideDebugConfigurations(folder: WorkspaceFolder | undefined): ProviderResult<DebugConfiguration[]> {
          return [
            {
              name: "Launch",
              request: "launch",
              type: "bun",
              program: "${file}",
            },
          ];
        },
      },
      vscode.DebugConfigurationProviderTriggerKind.Dynamic,
    ),
  );

  if (!factory) {
    factory = new InlineDebugAdapterFactory();
  }
  context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory("bun", factory));
  if ("dispose" in factory) {
    // @ts-expect-error ???
    context.subscriptions.push(factory);
  }
}

class BunConfigurationProvider implements vscode.DebugConfigurationProvider {
  resolveDebugConfiguration(
    folder: WorkspaceFolder | undefined,
    config: DebugConfiguration,
    token?: CancellationToken,
  ): ProviderResult<DebugConfiguration> {
    // if launch.json is missing or empty
    if (!config.type && !config.request && !config.name) {
      const editor = vscode.window.activeTextEditor;
      if (editor && editor.document.languageId === "javascript") {
        config.type = "bun";
        config.name = "Launch";
        config.request = "launch";
        config.program = "${file}";
        config.stopOnEntry = true;
      }
    }

    if (!config.program) {
      return vscode.window.showInformationMessage("Cannot find a program to debug").then(_ => {
        return undefined; // abort launch
      });
    }

    return config;
  }
}

class InlineDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
  createDebugAdapterDescriptor(_session: vscode.DebugSession): ProviderResult<vscode.DebugAdapterDescriptor> {
    return new vscode.DebugAdapterInlineImplementation(new DAPAdapter(_session));
  }
}