From 52a60ebfd9f60e13be2340e88f2d44cec968ceb1 Mon Sep 17 00:00:00 2001 From: Ian Mancini Date: Sun, 14 May 2023 05:40:46 +0200 Subject: [PATCH] feat: add dap (debugging) config --- lua/plugins/debugging.lua | 111 +++++++++++ lua/plugins/init.lua | 10 +- lua/plugins/lsp.lua | 385 +++++++++++++++++++------------------- 3 files changed, 311 insertions(+), 195 deletions(-) create mode 100644 lua/plugins/debugging.lua diff --git a/lua/plugins/debugging.lua b/lua/plugins/debugging.lua new file mode 100644 index 0000000..6d535c9 --- /dev/null +++ b/lua/plugins/debugging.lua @@ -0,0 +1,111 @@ +local dap = require("dap") + +require("dap").adapters["pwa-node"] = { + type = "server", + host = "localhost", + port = "${port}", + executable = { + command = vim.env.HOME .. "/.asdf/installs/nodejs/lts/bin/node", + args = { + vim.fn.stdpath("data") .. "/mason/packages/js-debug-adapter/js-debug/src/dapDebugServer.js", + "${port}", + }, + }, +} + +for _, language in ipairs({ "typescript", "javascript" }) do + dap.configurations[language] = { + { + type = "pwa-node", + request = "launch", + name = "Launch file", + program = "${file}", + cwd = "${workspaceFolder}", + }, + { + type = "pwa-node", + request = "attach", + name = "Attach", + processId = require("dap.utils").pick_process, + cwd = "${workspaceFolder}", + }, + { + type = "pwa-node", + request = "launch", + name = "Debug Jest Tests", + -- trace = true, -- include debugger info + runtimeExecutable = "node", + runtimeArgs = { + "./node_modules/jest/bin/jest.js", + "--runInBand", + }, + rootPath = "${workspaceFolder}", + cwd = "${workspaceFolder}", + console = "integratedTerminal", + internalConsoleOptions = "neverOpen", + }, + } +end + +require("dapui").setup() + +local wk = require("which-key") + +wk.register({ + d = { + name = "Debug", + d = { + function() + require("dapui").toggle() + end, + "Toggle debugger UI", + }, + b = { + function() + require("dap").toggle_breakpoint() + end, + "Toggle breakpoint", + }, + c = { + function() + require("dap").continue() + end, + "Launch or resume session", + }, + o = { + function() + require("dap").step_over() + end, + "Step over", + }, + i = { + function() + require("dap").step_into() + end, + "Step into", + }, + p = { + function() + local config_dir = vim.loop.cwd() .. "/.vscode" + local config_file = config_dir .. "/launch.json" + 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 + vim.cmd("edit " .. config_file) + end + end, + "Open project config", + }, + }, +}, { + prefix = "", +}) diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index bced4a2..1edd03d 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -285,9 +285,13 @@ return require("packer").startup(function() use({ "Eduruiz/vim-blade" }) -- DAP - use("mfussenegger/nvim-dap") - use("Pocco81/dap-buddy.nvim") - use("rcarriga/nvim-dap-ui") + use({ + "mfussenegger/nvim-dap", + { + "rcarriga/nvim-dap-ui", + config = [[require('plugins.debugging')]], + }, + }) -- Test runner use({ diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index cc6fc03..e0e1ce6 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -3,30 +3,31 @@ local wk = require("which-key") require("mason").setup() require("mason-tool-installer").setup({ - ensure_installed = { - "stylua", - "shellcheck", - "shfmt", - "editorconfig-checker", - "eslint_d", - "prettier", - "prettierd", - "flake8", - "black", - }, + ensure_installed = { + "stylua", + "shellcheck", + "shfmt", + "editorconfig-checker", + "eslint_d", + "prettier", + "prettierd", + "flake8", + "black", + "js-debug-adapter", + }, }) require("mason-lspconfig").setup({ - automatic_installation = true, + automatic_installation = true, }) vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { - border = "rounded", - close_events = { "CursorMoved", "BufHidden", "InsertCharPre" }, + border = "rounded", + close_events = { "CursorMoved", "BufHidden", "InsertCharPre" }, }) vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { - border = "rounded", + border = "rounded", }) require("aerial").setup({}) @@ -36,133 +37,133 @@ local capabilities = require("cmp_nvim_lsp").default_capabilities() local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) local lsp_rename_mapping = { - function() - vim.lsp.buf.rename() - end, - "Rename LSP symbol", + function() + vim.lsp.buf.rename() + end, + "Rename LSP symbol", } local lsp_references_mapping = { - "TroubleToggle lsp_references", - "LSP references", + "TroubleToggle lsp_references", + "LSP references", } local on_attach = function(client, bufnr) - vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") - - -- Normal mappings - wk.register({ - c = { - a = { "lua vim.lsp.buf.code_action()", "Code action" }, - r = lsp_rename_mapping, - f = { - "lua vim.lsp.buf.format()", - "Format file", - }, - o = { "AerialToggle!", "Toggle outline" }, - x = { "lua vim.diagnostic.open_float({source= true})", "Show line diagnostics" }, - R = lsp_references_mapping, - t = { - name = "Trouble", - t = { "TroubleToggle", "Trouble Toggle" }, - r = lsp_references_mapping, - w = { "TroubleToggle lsp_workspace_diagnostics", "Workspace diagnostics" }, - d = { "TroubleToggle lsp_document_diagnostics", "Document diagnostics" }, - q = { "TroubleToggle quickfix", "Quickfix" }, - l = { "TroubleToggle loclist", "Location list" }, - }, - g = { - name = "Go to", - D = { "lua vim.lsp.buf.declaration()", "Go to declaration" }, - d = { "lua vim.lsp.buf.definition()", "Go to definition" }, - i = { "lua vim.lsp.buf.implementation()", "Go to implementation" }, - t = { "lua vim.lsp.buf.type_definition()", "Go to type definition" }, - }, - }, - }, { - prefix = "", - buffer = bufnr, - }) - - -- Visual mappings - wk.register({ - c = { - a = { "lua vim.lsp.buf.code_action()", "Range code action" }, - f = { - "lua vim.lsp.buf.format()", - "Range formatting", - }, - }, - }, { - mode = "v", - prefix = "", - buffer = bufnr, - }) - - -- No leader mappings - wk.register({ - gd = { "lua vim.lsp.buf.definition()", "Go to definition" }, - K = { "lua vim.lsp.buf.hover()", "LSP Hover" }, - ["lua vim.lsp.buf.signature_help()", "LSP Signature" }, - [""] = lsp_rename_mapping, - ["[["] = { "AerialPrevUp", "Outline previous" }, - ["]]"] = { "AerialNext", "Outline next" }, - }, { - buffer = bufnr, - }) - - if client.supports_method("textDocument/formatting") then - vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) - vim.api.nvim_create_autocmd("BufWritePre", { - group = augroup, - buffer = bufnr, - callback = function() - if vim.g.format_on_save == 1 then - vim.lsp.buf.format({ bufnr = bufnr }) - end - end, - }) - end + vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") + + -- Normal mappings + wk.register({ + c = { + a = { "lua vim.lsp.buf.code_action()", "Code action" }, + r = lsp_rename_mapping, + f = { + "lua vim.lsp.buf.format()", + "Format file", + }, + o = { "AerialToggle!", "Toggle outline" }, + x = { "lua vim.diagnostic.open_float({source= true})", "Show line diagnostics" }, + R = lsp_references_mapping, + t = { + name = "Trouble", + t = { "TroubleToggle", "Trouble Toggle" }, + r = lsp_references_mapping, + w = { "TroubleToggle lsp_workspace_diagnostics", "Workspace diagnostics" }, + d = { "TroubleToggle lsp_document_diagnostics", "Document diagnostics" }, + q = { "TroubleToggle quickfix", "Quickfix" }, + l = { "TroubleToggle loclist", "Location list" }, + }, + g = { + name = "Go to", + D = { "lua vim.lsp.buf.declaration()", "Go to declaration" }, + d = { "lua vim.lsp.buf.definition()", "Go to definition" }, + i = { "lua vim.lsp.buf.implementation()", "Go to implementation" }, + t = { "lua vim.lsp.buf.type_definition()", "Go to type definition" }, + }, + }, + }, { + prefix = "", + buffer = bufnr, + }) + + -- Visual mappings + wk.register({ + c = { + a = { "lua vim.lsp.buf.code_action()", "Range code action" }, + f = { + "lua vim.lsp.buf.format()", + "Range formatting", + }, + }, + }, { + mode = "v", + prefix = "", + buffer = bufnr, + }) + + -- No leader mappings + wk.register({ + gd = { "lua vim.lsp.buf.definition()", "Go to definition" }, + K = { "lua vim.lsp.buf.hover()", "LSP Hover" }, + ["lua vim.lsp.buf.signature_help()", "LSP Signature" }, + [""] = lsp_rename_mapping, + ["[["] = { "AerialPrevUp", "Outline previous" }, + ["]]"] = { "AerialNext", "Outline next" }, + }, { + buffer = bufnr, + }) + + if client.supports_method("textDocument/formatting") then + vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) + vim.api.nvim_create_autocmd("BufWritePre", { + group = augroup, + buffer = bufnr, + callback = function() + if vim.g.format_on_save == 1 then + vim.lsp.buf.format({ bufnr = bufnr }) + end + end, + }) + end end local settings = { - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, } -- Neovim require("neodev").setup({}) local servers = { - "clangd", - "pyright", - "rust_analyzer", - "gopls", - "tailwindcss", - "jsonls", - "lua_ls", + "clangd", + "pyright", + "rust_analyzer", + "gopls", + "tailwindcss", + "jsonls", + "lua_ls", } for _, lsp in pairs(servers) do - require("lspconfig")[lsp].setup(settings) + require("lspconfig")[lsp].setup(settings) end -- JSON require("lspconfig").jsonls.setup(vim.tbl_deep_extend("force", settings, { - settings = { - json = { - schemas = require("schemastore").json.schemas(), - validate = { - enable = true, - }, - }, - }, + settings = { + json = { + schemas = require("schemastore").json.schemas(), + validate = { + enable = true, + }, + }, + }, })) -- Typescript require("typescript").setup({ - disable_commands = false, - debug = false, - server = settings, + disable_commands = false, + debug = false, + server = settings, }) -- null-ls @@ -171,90 +172,90 @@ local null_ls = require("null-ls") vim.g.format_on_save = 1 function _G.toggle_format_on_save() - if vim.g.format_on_save == 1 then - vim.g.format_on_save = 0 - print("Disabled format on save") - else - vim.g.format_on_save = 1 - print("Enabled format on save") - end + if vim.g.format_on_save == 1 then + vim.g.format_on_save = 0 + print("Disabled format on save") + else + vim.g.format_on_save = 1 + print("Enabled format on save") + end end require("null-ls").setup({ - timeout_ms = 5000, - sources = { - -- General - null_ls.builtins.code_actions.refactoring, - null_ls.builtins.completion.spell, - null_ls.builtins.diagnostics.trail_space, - null_ls.builtins.formatting.trim_newlines, - null_ls.builtins.formatting.trim_whitespace, - - -- Git - null_ls.builtins.code_actions.gitrebase, - null_ls.builtins.code_actions.gitsigns, - - -- GitHub - null_ls.builtins.diagnostics.actionlint, - - -- Shell - null_ls.builtins.code_actions.shellcheck, - null_ls.builtins.diagnostics.shellcheck, - null_ls.builtins.formatting.shfmt, - - -- C/C++ - null_ls.builtins.formatting.clang_format, - null_ls.builtins.formatting.cmake_format, - - -- Web - null_ls.builtins.formatting.prettier, - -- null_ls.builtins.code_actions.eslint_d, - null_ls.builtins.code_actions.eslint, - null_ls.builtins.diagnostics.eslint, - -- null_ls.builtins.formatting.eslint, - null_ls.builtins.diagnostics.stylelint, - null_ls.builtins.formatting.stylelint, - null_ls.builtins.formatting.rustywind, - - -- Markdown - null_ls.builtins.formatting.remark, - - -- Lua - -- null_ls.builtins.diagnostics.luacheck, - null_ls.builtins.formatting.stylua, - - -- Python - null_ls.builtins.diagnostics.flake8, - null_ls.builtins.formatting.black, - null_ls.builtins.formatting.isort, - - -- PHP - null_ls.builtins.diagnostics.phpstan, - - -- Go - null_ls.builtins.formatting.gofmt, - - -- Rust - null_ls.builtins.formatting.rustfmt, - }, + timeout_ms = 5000, + sources = { + -- General + null_ls.builtins.code_actions.refactoring, + null_ls.builtins.completion.spell, + null_ls.builtins.diagnostics.trail_space, + null_ls.builtins.formatting.trim_newlines, + null_ls.builtins.formatting.trim_whitespace, + + -- Git + null_ls.builtins.code_actions.gitrebase, + null_ls.builtins.code_actions.gitsigns, + + -- GitHub + null_ls.builtins.diagnostics.actionlint, + + -- Shell + null_ls.builtins.code_actions.shellcheck, + null_ls.builtins.diagnostics.shellcheck, + null_ls.builtins.formatting.shfmt, + + -- C/C++ + null_ls.builtins.formatting.clang_format, + null_ls.builtins.formatting.cmake_format, + + -- Web + null_ls.builtins.formatting.prettier, + -- null_ls.builtins.code_actions.eslint_d, + null_ls.builtins.code_actions.eslint, + null_ls.builtins.diagnostics.eslint, + -- null_ls.builtins.formatting.eslint, + null_ls.builtins.diagnostics.stylelint, + null_ls.builtins.formatting.stylelint, + null_ls.builtins.formatting.rustywind, + + -- Markdown + null_ls.builtins.formatting.remark, + + -- Lua + -- null_ls.builtins.diagnostics.luacheck, + null_ls.builtins.formatting.stylua, + + -- Python + null_ls.builtins.diagnostics.flake8, + null_ls.builtins.formatting.black, + null_ls.builtins.formatting.isort, + + -- PHP + null_ls.builtins.diagnostics.phpstan, + + -- Go + null_ls.builtins.formatting.gofmt, + + -- Rust + null_ls.builtins.formatting.rustfmt, + }, }) vim.api.nvim_create_autocmd("LspAttach", { - callback = function(args) - vim.bo[args.buf].formatexpr = nil - end, + callback = function(args) + vim.bo[args.buf].formatexpr = nil + end, }) require("nvim-lightbulb").setup({ - autocmd = { - enabled = false, - }, - sign = { - enabled = false, - }, - virtual_text = { - enabled = false, - }, + autocmd = { + enabled = false, + }, + sign = { + enabled = false, + }, + virtual_text = { + enabled = false, + }, }) require("fidget").setup({})