diff options
Diffstat (limited to 'neovim/.config/nvim/lua/config/dap_node.lua')
| -rw-r--r-- | neovim/.config/nvim/lua/config/dap_node.lua | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/neovim/.config/nvim/lua/config/dap_node.lua b/neovim/.config/nvim/lua/config/dap_node.lua new file mode 100644 index 0000000..0fb6890 --- /dev/null +++ b/neovim/.config/nvim/lua/config/dap_node.lua | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | -- JavaScript/TypeScript launch configs, wired to the js-debug-adapter | ||
| 2 | -- (Microsoft's vscode-js-debug, bundled by Mason as a standalone | ||
| 3 | -- multi-session DAP server) that dap.lua's mason-nvim-dap ensure_installed | ||
| 4 | -- installs. | ||
| 5 | -- | ||
| 6 | -- There's no nvim-dap-python equivalent for this: the one community plugin | ||
| 7 | -- for it (mxsdev/nvim-dap-vscode-js) hardcodes the older | ||
| 8 | -- `out/src/vsDebugServer.js` vscode-js-debug build layout, which doesn't | ||
| 9 | -- match what Mason's js-debug-adapter package actually installs | ||
| 10 | -- (`js-debug/src/dapDebugServer.js`) - so this resolves the Mason path | ||
| 11 | -- directly instead, same approach as dap-python.lua's debugpy_path. | ||
| 12 | -- | ||
| 13 | -- Lives under lua/config/ rather than lua/plugins/ (and is required | ||
| 14 | -- eagerly from dap.lua's config()) because it isn't an installable plugin | ||
| 15 | -- itself, just adapter/config wiring - same reason project_run.lua lives | ||
| 16 | -- here instead of there. | ||
| 17 | -- | ||
| 18 | -- js-debug-adapter is a multi-session server: debugging a script that | ||
| 19 | -- spawns a worker thread arrives as a `startDebugging` reverse request | ||
| 20 | -- rather than a second top-level session. nvim-dap's core (dap/session.lua) | ||
| 21 | -- already handles that natively, so no extra plugin is needed for it either. | ||
| 22 | local M = {} | ||
| 23 | |||
| 24 | local function configurations() | ||
| 25 | return { | ||
| 26 | { | ||
| 27 | type = "pwa-node", | ||
| 28 | request = "launch", | ||
| 29 | name = "Launch file", | ||
| 30 | program = "${file}", | ||
| 31 | cwd = "${workspaceFolder}", | ||
| 32 | }, | ||
| 33 | { | ||
| 34 | type = "pwa-node", | ||
| 35 | request = "attach", | ||
| 36 | name = "Attach to process", | ||
| 37 | processId = require("dap.utils").pick_process, | ||
| 38 | cwd = "${workspaceFolder}", | ||
| 39 | }, | ||
| 40 | } | ||
| 41 | end | ||
| 42 | |||
| 43 | function M.setup() | ||
| 44 | local registry = require("mason-registry") | ||
| 45 | local pkg = registry.get_package("js-debug-adapter") | ||
| 46 | |||
| 47 | local function register() | ||
| 48 | local entrypoint = pkg:get_install_path() .. "/js-debug/src/dapDebugServer.js" | ||
| 49 | |||
| 50 | require("dap").adapters["pwa-node"] = { | ||
| 51 | type = "server", | ||
| 52 | host = "localhost", | ||
| 53 | port = "${port}", | ||
| 54 | executable = { | ||
| 55 | command = "node", | ||
| 56 | args = { entrypoint, "${port}" }, | ||
| 57 | }, | ||
| 58 | } | ||
| 59 | |||
| 60 | local dap = require("dap") | ||
| 61 | for _, filetype in ipairs({ "javascript", "typescript", "javascriptreact", "typescriptreact" }) do | ||
| 62 | dap.configurations[filetype] = configurations() | ||
| 63 | end | ||
| 64 | end | ||
| 65 | |||
| 66 | if pkg:is_installed() then | ||
| 67 | register() | ||
| 68 | else | ||
| 69 | -- Same reasoning as dap-python.lua: dap.lua's mason-nvim-dap | ||
| 70 | -- ensure_installed triggers the install, but that's async - on a fresh | ||
| 71 | -- machine (or before it finishes) the entrypoint doesn't exist on disk | ||
| 72 | -- yet, so wire up the adapter once the install actually completes | ||
| 73 | -- instead of pointing it at a path that isn't there yet. | ||
| 74 | vim.notify( | ||
| 75 | "js-debug-adapter not installed yet - dap.lua's ensure_installed should be" | ||
| 76 | .. " installing it now (check :Mason); run :MasonInstall js-debug-adapter" | ||
| 77 | .. " if it doesn't show up there. Debugging will work once it's done.", | ||
| 78 | vim.log.levels.WARN | ||
| 79 | ) | ||
| 80 | registry:on("package:install:success", function(installed_pkg) | ||
| 81 | if installed_pkg.name == "js-debug-adapter" then | ||
| 82 | register() | ||
| 83 | end | ||
| 84 | end) | ||
| 85 | end | ||
| 86 | end | ||
| 87 | |||
| 88 | return M | ||
