diff options
author | 2023-09-01 18:56:25 -0700 | |
---|---|---|
committer | 2023-09-01 18:56:25 -0700 | |
commit | 94a4cda713195bbaf70b4d888d0d1722e8b39537 (patch) | |
tree | 5126ca9154fbfdb2d9bd9d14ea8dd3cab50e6132 | |
parent | eeb683d977cb8f40910c32b719ccc9223d869280 (diff) | |
download | bun-94a4cda713195bbaf70b4d888d0d1722e8b39537.tar.gz bun-94a4cda713195bbaf70b4d888d0d1722e8b39537.tar.zst bun-94a4cda713195bbaf70b4d888d0d1722e8b39537.zip |
Add configuration options to extension
-rw-r--r-- | packages/bun-debug-adapter-protocol/src/debugger/adapter.ts | 10 | ||||
-rw-r--r-- | packages/bun-vscode/README.md | 82 | ||||
-rw-r--r-- | packages/bun-vscode/example/example.ts | 2 | ||||
-rw-r--r-- | packages/bun-vscode/package.json | 14 | ||||
-rw-r--r-- | packages/bun-vscode/src/features/debug.ts | 34 |
5 files changed, 137 insertions, 5 deletions
diff --git a/packages/bun-debug-adapter-protocol/src/debugger/adapter.ts b/packages/bun-debug-adapter-protocol/src/debugger/adapter.ts index 2f04185de..8d1953bff 100644 --- a/packages/bun-debug-adapter-protocol/src/debugger/adapter.ts +++ b/packages/bun-debug-adapter-protocol/src/debugger/adapter.ts @@ -183,6 +183,7 @@ export type DebugAdapterEventMap = InspectorEventMap & { "Adapter.response": [DAP.Response]; "Adapter.event": [DAP.Event]; "Adapter.error": [Error]; + "Adapter.reverseRequest": [DAP.Request]; } & { "Process.requested": [unknown]; "Process.spawned": [ChildProcess]; @@ -325,6 +326,15 @@ export class DebugAdapter extends EventEmitter<DebugAdapterEventMap> implements }); } + #reverseRequest<T extends keyof DAP.RequestMap>(command: T, args?: DAP.RequestMap[T]): void { + this.emit("Adapter.reverseRequest", { + type: "request", + seq: 0, + command, + arguments: args, + }); + } + async ["Adapter.request"](request: DAP.Request): Promise<void> { const { command, arguments: args } = request; diff --git a/packages/bun-vscode/README.md b/packages/bun-vscode/README.md index ce7f11b73..c3d94924c 100644 --- a/packages/bun-vscode/README.md +++ b/packages/bun-vscode/README.md @@ -1,8 +1,6 @@ # Bun for Visual Studio Code  - - <img align="right" src="https://user-images.githubusercontent.com/709451/182802334-d9c42afe-f35d-4a7b-86ea-9985f73f20c3.png" height="150px" style="float: right; padding: 30px;"> @@ -20,3 +18,83 @@ At its core is the _Bun runtime_, a fast JavaScript runtime designed as a drop-i <a href="https://github.com/oven-sh/bun/issues/159">Roadmap</a> <br/> </div> + +## Configuration + +### `.vscode/launch.json` + +You can use the following configurations to debug JavaScript and TypeScript files using Bun. + +```jsonc +{ + "version": "0.2.0", + "configurations": [ + { + "type": "bun", + "request": "launch", + "name": "Debug Bun", + + // The path to a JavaScript or TypeScript file to run. + "program": "${file}", + + // The arguments to pass to the program, if any. + "args": [], + + // The working directory of the program. + "cwd": "${workspaceFolder}", + + // The environment variables to pass to the program. + "env": {}, + + // If the environment variables should not be inherited from the parent process. + "strictEnv": false, + + // If the program should be run in watch mode. + // This is equivalent to passing `--watch` to the `bun` executable. + // You can also set this to "hot" to enable hot reloading using `--hot`. + "watchMode": false, + + // If the debugger should stop on the first line of the program. + "stopOnEntry": false, + + // If the debugger should be disabled. (for example, breakpoints will not be hit) + "noDebug": false, + + // The path to the `bun` executable, defaults to your `PATH` environment variable. + "runtime": "bun", + + // The arguments to pass to the `bun` executable, if any. + // Unlike `args`, these are passed to the executable itself, not the program. + "runtimeArgs": [], + }, + { + "type": "bun", + "request": "attach", + "name": "Attach to Bun", + + // The URL of the WebSocket inspector to attach to. + // This value can be retreived by using `bun --inspect`. + "url": "ws://localhost:6499/", + } + ] +} +``` + +### `.vscode/settings.json` + +You can use the following configurations to customize the behavior of the Bun extension. + +```jsonc +{ + // The path to the `bun` executable. + "bun.runtime": "/path/to/bun", + + "bun.debugTerminal": { + // If support for Bun should be added to the default "JavaScript Debug Terminal". + "enabled": true, + + // If the debugger should stop on the first line of the program. + "stopOnEntry": false, + } +} +```
\ No newline at end of file diff --git a/packages/bun-vscode/example/example.ts b/packages/bun-vscode/example/example.ts index 804e6d702..c02228a8f 100644 --- a/packages/bun-vscode/example/example.ts +++ b/packages/bun-vscode/example/example.ts @@ -8,7 +8,7 @@ export default { }; const coolThing: CoolThing = new SuperCoolThing(); coolThing.doCoolThing(); - return new Response("Hello World??"); + return new Response("Hello World!!"); }, }; diff --git a/packages/bun-vscode/package.json b/packages/bun-vscode/package.json index 6a581b633..fc619f919 100644 --- a/packages/bun-vscode/package.json +++ b/packages/bun-vscode/package.json @@ -44,11 +44,23 @@ "configuration": { "title": "Bun", "properties": { - "bun.path": { + "bun.runtime": { "type": "string", "description": "A path to the `bun` executable. By default, the extension looks for `bun` in the `PATH`, but if set, it will use the specified path instead.", "scope": "window", "default": null + }, + "bun.debugTerminal.enabled": { + "type": "boolean", + "description": "If Bun should be added to the JavaScript Debug Terminal.", + "scope": "window", + "default": true + }, + "bun.debugTerminal.stopOnEntry": { + "type": "boolean", + "description": "If Bun should stop on entry when used in the JavaScript Debug Terminal.", + "scope": "window", + "default": false } } }, diff --git a/packages/bun-vscode/src/features/debug.ts b/packages/bun-vscode/src/features/debug.ts index 8d2b3327a..184863aa0 100644 --- a/packages/bun-vscode/src/features/debug.ts +++ b/packages/bun-vscode/src/features/debug.ts @@ -29,6 +29,7 @@ const attachConfiguration: vscode.DebugConfiguration = { request: "attach", name: "Attach Bun", url: "ws://localhost:6499/", + stopOnEntry: false, }; const adapters = new Map<string, FileDebugSession>(); @@ -59,6 +60,7 @@ function RunFileCommand(resource?: vscode.Uri): void { ...runConfiguration, noDebug: true, program: path, + runtime: getRuntime(resource), }); } } @@ -69,11 +71,17 @@ function DebugFileCommand(resource?: vscode.Uri): void { vscode.debug.startDebugging(undefined, { ...debugConfiguration, program: path, + runtime: getRuntime(resource), }); } } function InjectDebugTerminal(terminal: vscode.Terminal): void { + const enabled = getConfig("debugTerminal.enabled"); + if (enabled === false) { + return; + } + const { name, creationOptions } = terminal; if (name !== "JavaScript Debug Terminal") { return; @@ -84,13 +92,16 @@ function InjectDebugTerminal(terminal: vscode.Terminal): void { return; } + const stopOnEntry = getConfig("debugTerminal.stopOnEntry") === true; + const query = stopOnEntry ? "break=1" : "wait=1"; + const { adapter, signal } = new TerminalDebugSession(); const debug = vscode.window.createTerminal({ ...creationOptions, name: "JavaScript Debug Terminal", env: { ...env, - "BUN_INSPECT": `${adapter.url}?wait=1`, + "BUN_INSPECT": `${adapter.url}?${query}`, "BUN_INSPECT_NOTIFY": `${signal.url}`, }, }); @@ -130,12 +141,18 @@ class DebugConfigurationProvider implements vscode.DebugConfigurationProvider { target = debugConfiguration; } + // If the configuration is missing a default property, copy it from the template. for (const [key, value] of Object.entries(target)) { if (config[key] === undefined) { config[key] = value; } } + // If no runtime is specified, get the path from the configuration. + if (request === "launch" && !config["runtime"]) { + config["runtime"] = getRuntime(folder); + } + return config; } } @@ -169,6 +186,9 @@ class FileDebugSession extends DebugSession { this.adapter = new DebugAdapter(url); this.adapter.on("Adapter.response", response => this.sendResponse(response)); this.adapter.on("Adapter.event", event => this.sendEvent(event)); + this.adapter.on("Adapter.reverseRequest", ({ command, arguments: args }) => + this.sendRequest(command, args, 5000, () => {}), + ); adapters.set(url, this); } @@ -234,3 +254,15 @@ function isJavaScript(languageId?: string): boolean { languageId === "typescriptreact" ); } + +function getRuntime(scope?: vscode.ConfigurationScope): string { + const value = getConfig("runtime", scope); + if (typeof value === "string" && value.trim().length > 0) { + return value; + } + return "bun"; +} + +function getConfig<T>(path: string, scope?: vscode.ConfigurationScope): unknown { + return vscode.workspace.getConfiguration("bun", scope).get(path); +} |