feat: add command to load launch.json config for debugging

main
Ian Mancini 2 years ago
parent 52a60ebfd9
commit 26de444472

@ -1,50 +1,50 @@
local dap = require("dap") local dap = require("dap")
require("dap").adapters["pwa-node"] = { require("dap").adapters["pwa-node"] = {
type = "server", type = "server",
host = "localhost", host = "localhost",
port = "${port}", port = "${port}",
executable = { executable = {
command = vim.env.HOME .. "/.asdf/installs/nodejs/lts/bin/node", command = vim.env.HOME .. "/.asdf/installs/nodejs/lts/bin/node",
args = { args = {
vim.fn.stdpath("data") .. "/mason/packages/js-debug-adapter/js-debug/src/dapDebugServer.js", vim.fn.stdpath("data") .. "/mason/packages/js-debug-adapter/js-debug/src/dapDebugServer.js",
"${port}", "${port}",
}, },
}, },
} }
for _, language in ipairs({ "typescript", "javascript" }) do for _, language in ipairs({ "typescript", "javascript" }) do
dap.configurations[language] = { dap.configurations[language] = {
{ {
type = "pwa-node", type = "pwa-node",
request = "launch", request = "launch",
name = "Launch file", name = "Launch file",
program = "${file}", program = "${file}",
cwd = "${workspaceFolder}", cwd = "${workspaceFolder}",
}, },
{ {
type = "pwa-node", type = "pwa-node",
request = "attach", request = "attach",
name = "Attach", name = "Attach",
processId = require("dap.utils").pick_process, processId = require("dap.utils").pick_process,
cwd = "${workspaceFolder}", cwd = "${workspaceFolder}",
}, },
{ {
type = "pwa-node", type = "pwa-node",
request = "launch", request = "launch",
name = "Debug Jest Tests", name = "Debug Jest Tests",
-- trace = true, -- include debugger info -- trace = true, -- include debugger info
runtimeExecutable = "node", runtimeExecutable = "node",
runtimeArgs = { runtimeArgs = {
"./node_modules/jest/bin/jest.js", "./node_modules/jest/bin/jest.js",
"--runInBand", "--runInBand",
}, },
rootPath = "${workspaceFolder}", rootPath = "${workspaceFolder}",
cwd = "${workspaceFolder}", cwd = "${workspaceFolder}",
console = "integratedTerminal", console = "integratedTerminal",
internalConsoleOptions = "neverOpen", internalConsoleOptions = "neverOpen",
}, },
} }
end end
require("dapui").setup() require("dapui").setup()
@ -52,60 +52,69 @@ require("dapui").setup()
local wk = require("which-key") local wk = require("which-key")
wk.register({ wk.register({
d = { d = {
name = "Debug", name = "Debug",
d = { d = {
function() function()
require("dapui").toggle() require("dapui").toggle()
end, end,
"Toggle debugger UI", "Toggle debugger UI",
}, },
b = { b = {
function() function()
require("dap").toggle_breakpoint() require("dap").toggle_breakpoint()
end, end,
"Toggle breakpoint", "Toggle breakpoint",
}, },
c = { c = {
function() function()
require("dap").continue() require("dap").continue()
end, end,
"Launch or resume session", "Launch or resume session",
}, },
o = { o = {
function() function()
require("dap").step_over() require("dap").step_over()
end, end,
"Step over", "Step over",
}, },
i = { i = {
function() function()
require("dap").step_into() require("dap").step_into()
end, end,
"Step into", "Step into",
}, },
p = { p = {
function() function()
local config_dir = vim.loop.cwd() .. "/.vscode" require("dap.ext.vscode").load_launchjs(
local config_file = config_dir .. "/launch.json" nil,
if not vim.fn.filereadable(config_file) then { ["pwa-node"] = { "typescriptreact", "typescript", "javascript", "javascriptreact", "json" } }
vim.ui.input( )
{ prompt = "Debugger config not found at " .. config_file .. ". Create? [Y/n]" }, end,
function(input) "Load project config",
if input == "" or input == "y" or input == "Y" then },
vim.fn.mkdir(config_dir) P = {
end function()
end local config_dir = vim.loop.cwd() .. "/.vscode"
) local config_file = config_dir .. "/launch.json"
end if not vim.fn.filereadable(config_file) then
vim.ui.input(
{ prompt = "Debugger config not found at " .. config_file .. ". Create? [Y/n]" },
function(input)
if input == "" or input == "y" or input == "Y" then
vim.fn.mkdir(config_dir)
end
end
)
end
if vim.fn.filereadable(config_file) then if vim.fn.filereadable(config_file) then
vim.cmd("edit " .. config_file) vim.cmd("edit " .. config_file)
end end
end, end,
"Open project config", "Open project config",
}, },
}, },
}, { }, {
prefix = "<leader>", prefix = "<leader>",
}) })

Loading…
Cancel
Save