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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
import * as vscode from "vscode";
import type { CancellationToken, DebugConfiguration, ProviderResult, WorkspaceFolder } from "vscode";
import type { DAP } from "../../../bun-debug-adapter-protocol";
import { DebugAdapter } from "../../../bun-debug-adapter-protocol";
import { DebugSession } from "@vscode/debugadapter";
const debugConfiguration: vscode.DebugConfiguration = {
type: "bun",
request: "launch",
name: "Debug Bun",
program: "${file}",
watch: true,
};
const runConfiguration: vscode.DebugConfiguration = {
type: "bun",
request: "launch",
name: "Run Bun",
program: "${file}",
watch: true,
};
const attachConfiguration: vscode.DebugConfiguration = {
type: "bun",
request: "attach",
name: "Attach to Bun",
url: "ws://localhost:6499/",
};
const debugConfigurations: vscode.DebugConfiguration[] = [debugConfiguration, attachConfiguration];
export default function (context: vscode.ExtensionContext, factory?: vscode.DebugAdapterDescriptorFactory) {
context.subscriptions.push(
vscode.commands.registerCommand("extension.bun.runFile", (resource: vscode.Uri) => {
let targetResource = resource;
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri;
}
if (targetResource) {
vscode.debug.startDebugging(undefined, runConfiguration, {
noDebug: true,
});
}
}),
vscode.commands.registerCommand("extension.bun.debugFile", (resource: vscode.Uri) => {
let targetResource = resource;
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri;
}
if (targetResource) {
vscode.debug.startDebugging(undefined, {
...debugConfiguration,
program: targetResource.fsPath,
});
}
}),
);
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 debugConfigurations;
},
},
vscode.DebugConfigurationProviderTriggerKind.Dynamic,
),
);
if (!factory) {
factory = new InlineDebugAdapterFactory();
}
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory("bun", factory));
if ("dispose" in factory && typeof factory.dispose === "function") {
// @ts-ignore
context.subscriptions.push(factory);
}
}
class BunConfigurationProvider implements vscode.DebugConfigurationProvider {
resolveDebugConfiguration(
folder: WorkspaceFolder | undefined,
config: DebugConfiguration,
token?: CancellationToken,
): ProviderResult<DebugConfiguration> {
if (!config.type && !config.request && !config.name) {
const editor = vscode.window.activeTextEditor;
if (editor && isJavaScript(editor.document.languageId)) {
Object.assign(config, debugConfiguration);
}
}
return config;
}
}
class InlineDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(_session: vscode.DebugSession): ProviderResult<vscode.DebugAdapterDescriptor> {
const adapter = new VSCodeAdapter(_session);
return new vscode.DebugAdapterInlineImplementation(adapter);
}
}
function isJavaScript(languageId: string): boolean {
return (
languageId === "javascript" ||
languageId === "javascriptreact" ||
languageId === "typescript" ||
languageId === "typescriptreact"
);
}
export class VSCodeAdapter extends DebugSession {
#adapter: DebugAdapter;
#dap: vscode.OutputChannel;
constructor(session: vscode.DebugSession) {
super();
this.#dap = vscode.window.createOutputChannel("Debug Adapter Protocol");
this.#adapter = new DebugAdapter({
sendToAdapter: this.sendMessage.bind(this),
});
}
sendMessage(message: DAP.Request | DAP.Response | DAP.Event): void {
console.log("[dap] -->", message);
this.#dap.appendLine("--> " + JSON.stringify(message));
const { type } = message;
if (type === "response") {
this.sendResponse(message);
} else if (type === "event") {
this.sendEvent(message);
} else {
throw new Error(`Not supported: ${type}`);
}
}
handleMessage(message: DAP.Event | DAP.Request | DAP.Response): void {
console.log("[dap] <--", message);
this.#dap.appendLine("<-- " + JSON.stringify(message));
this.#adapter.accept(message);
}
dispose() {
this.#adapter.close();
this.#dap.dispose();
}
}
|