Skip to content

Commit

Permalink
refactor(DAP): improve user experience (ayamir#777)
Browse files Browse the repository at this point in the history
* fix: make dap for c/cpp/rust/python works.

* refactor: use cpptools to debug c/cpp/rust.

* refactor: make dap servers configurable.

* refactor: move dap servers config to subdir.

* refactor: use mason-nvim-dap.nvim to manage dap servers.

* perf: add note for each dap server.

* fixup! refactor: make debug functionality better

* fixup! fixup! refactor: make debug functionality better

* fix typo

* re-format code

* feat: simplify `utils/dap.lua`

* fixup! add checks for Windows NT (`codelldb`)

* fixup! fixup! remove redundant comment

* fixup! fix path expansion

* fix: expand current file path to exe prompt.

---------

Co-authored-by: Jint-lzxy <[email protected]>
  • Loading branch information
2 people authored and singlemancombat committed Jun 13, 2023
1 parent 2053b31 commit 61fde8b
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 47 deletions.
65 changes: 33 additions & 32 deletions lazy-lock.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"Comment.nvim": { "branch": "master", "commit": "c8043290f2d77f61025494d839d88e414608c460" },
"LuaSnip": { "branch": "master", "commit": "51ebb4b6637290e1b8e0fb0d6f38b605d3c24940" },
"Comment.nvim": { "branch": "master", "commit": "176e85eeb63f1a5970d6b88f1725039d85ca0055" },
"LuaSnip": { "branch": "master", "commit": "a13af80734eb28f744de6c875330c9d3c24b5f3b" },
"accelerated-jk.nvim": { "branch": "main", "commit": "8fb5dad4ccc1811766cebf16b544038aeeb7806f" },
"alpha-nvim": { "branch": "main", "commit": "1838ae926e8d49fe5330d1498ee8289ae2c340bc" },
"alpha-nvim": { "branch": "main", "commit": "9e33db324b8bb7a147bce9ea5496686ee859461d" },
"ascii.nvim": { "branch": "master", "commit": "f4d165c7384aade71d828fb08502d8ceda7dca89" },
"auto-session": { "branch": "main", "commit": "e3234c489397b46a2b7949d6169d61b69a30c015" },
"auto-session": { "branch": "main", "commit": "d91509611981759419ee2fe5e2ef436d5ec8d0eb" },
"autoclose.nvim": { "branch": "main", "commit": "2321727fd10a6b34624723bc4747d8c09c10b7a4" },
"ayu": { "branch": "master", "commit": "762ff24bd429fbb1c1e20b13043b4c8f0266bcf1" },
"better-escape.nvim": { "branch": "master", "commit": "7031dc734add47bb71c010e0551829fa5799375f" },
Expand All @@ -26,57 +26,58 @@
"copilot.lua": { "branch": "master", "commit": "77e3a4907928f0813024e573b882dc879dfc0c6b" },
"crates.nvim": { "branch": "main", "commit": "3648f8787656d7572740560331553abdaa8cb982" },
"csv.vim": { "branch": "master", "commit": "15ff93edf5b26c466affbb356e0696b7d6a3b499" },
"diffview.nvim": { "branch": "main", "commit": "6ca4cce071d527fa16c27781f98b843774ae84a7" },
"diffview.nvim": { "branch": "main", "commit": "0ad3e4f834093412ebbf317b7eaa9c59568824b9" },
"edge": { "branch": "master", "commit": "358cb6688ac577470a4eafcb53bdd63899dfc937" },
"fidget.nvim": { "branch": "main", "commit": "0ba1e16d07627532b6cae915cc992ecac249fb97" },
"friendly-snippets": { "branch": "main", "commit": "9025d37189f26b6ca0b965a9e5eec3e69c37d2e9" },
"fzy-lua-native": { "branch": "master", "commit": "45148b3f70d244e8b8b5db60df447a2b7ac2de9d" },
"gitsigns.nvim": { "branch": "main", "commit": "f868d82a36f7f7f5e110eb0a9659993984f59875" },
"go.nvim": { "branch": "master", "commit": "8a0498ee48a26f928b1dc1c02fb3d84d648a1c63" },
"fidget.nvim": { "branch": "main", "commit": "f1c375ba68839eaa4a65efdf2aa078c0da0548fe" },
"friendly-snippets": { "branch": "main", "commit": "49ca2a0e0e26427b550b1f64272d7fe7e4d7d51b" },
"fzy-lua-native": { "branch": "master", "commit": "820f745b7c442176bcc243e8f38ef4b985febfaf" },
"gitsigns.nvim": { "branch": "main", "commit": "4bbfb06cf706d14912fd1962a40280c1b1138965" },
"go.nvim": { "branch": "master", "commit": "a8095eb334495caec3099b717cc7f5b1fbc3e628" },
"guihua.lua": { "branch": "master", "commit": "ab8b1f09603cc268770efd057115035dc6cfa83d" },
"hop.nvim": { "branch": "v2", "commit": "90db1b2c61b820e230599a04fedcd2679e64bd07" },
"indent-blankline.nvim": { "branch": "master", "commit": "7075d7861f7a6bbf0de0298c83f8a13195e6ec01" },
"kanagawa": { "branch": "master", "commit": "14a7524a8b259296713d4d77ef3c7f4dec501269" },
"lazy.nvim": { "branch": "main", "commit": "f145e6f42a56306c5536e9efbfe41f7efbec285d" },
"lazy.nvim": { "branch": "main", "commit": "10d4371745f88837c78c8daab00c5be6e48abea4" },
"lsp_signature.nvim": { "branch": "master", "commit": "17ff7a405fea8376b015b8ea7910d2e59958bf68" },
"lspsaga.nvim": { "branch": "main", "commit": "c475ace5b8882631b351ef7c3e8078ca9ebbb751" },
"lspsaga.nvim": { "branch": "main", "commit": "4f075452c466df263e69ae142f6659dcf9324bf6" },
"lualine.nvim": { "branch": "master", "commit": "05d78e9fd0cdfb4545974a5aa14b1be95a86e9c9" },
"markdown-preview.nvim": { "branch": "master", "commit": "02cc3874738bc0f86e4b91f09b8a0ac88aef8e96" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "5230617372e656d4a2e1e236e03bf7e7b4b97273" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "d381fcb78d7a562c3244e1c8f76406954649db36" },
"mason-null-ls.nvim": { "branch": "main", "commit": "54d702020bf94e4eefd357f0b738317af30217eb" },
"mason.nvim": { "branch": "main", "commit": "7d7efc738e08fc5bee822857db45cb6103f0b0c1" },
"mason-nvim-dap.nvim": { "branch": "main", "commit": "e4d56b400e9757b1dc77d620fd3069396e92d5fc" },
"mason.nvim": { "branch": "main", "commit": "4be1226f48bc2011024110f37b17f5ee468df58f" },
"navigator.lua": { "branch": "master", "commit": "f936d926b9d7a1656f634bed6a4e12e2e82c86e5" },
"neodim": { "branch": "master", "commit": "89c16fccccfdb5b7e6ad3a6e8286736debc11705" },
"neodim": { "branch": "master", "commit": "9477da03b93f1984a81fee3b92e6ac7c6ada6aa4" },
"neoscroll.nvim": { "branch": "master", "commit": "d7601c26c8a183fa8994ed339e70c2d841253e93" },
"nightfox": { "branch": "main", "commit": "77aa7458d2b725c2d9ff55a18befe1b891ac473e" },
"nord.nvim": { "branch": "master", "commit": "fab04b2dd4b64f4b1763b9250a8824d0b5194b8f" },
"nordic.nvim": { "branch": "main", "commit": "bd4cf4e4ca8b6a8b63e2e6e38d2112a59be45494" },
"nui.nvim": { "branch": "main", "commit": "d5a82aae64426a805e19d8ef5a379292f9dc55d3" },
"null-ls.nvim": { "branch": "main", "commit": "c89333e034a8daba654ebfcf9a4ec9f87765f01e" },
"nui.nvim": { "branch": "main", "commit": "9970bb107d96e5a506ba2732c4a61237aad6993c" },
"null-ls.nvim": { "branch": "main", "commit": "a138b14099e9623832027ea12b4631ddd2a49256" },
"nvim-bufdel": { "branch": "main", "commit": "96c4f7ab053ddab0025bebe5f7c71e4795430e47" },
"nvim-cmp": { "branch": "main", "commit": "fc0f694af1a742ada77e5b1c91ff405c746f4a26" },
"nvim-cmp": { "branch": "main", "commit": "b8c2a62b3bd3827aa059b43be3dd4b5c45037d65" },
"nvim-colorizer.lua": { "branch": "master", "commit": "dde3084106a70b9a79d48f426f6d6fec6fd203f7" },
"nvim-dap": { "branch": "master", "commit": "7c1d47cf7188fc31acdf951f9eee22da9d479152" },
"nvim-dap-ui": { "branch": "master", "commit": "c020f660b02772f9f3d11f599fefad3268628a9e" },
"nvim-dap-virtual-text": { "branch": "master", "commit": "57f1dbd0458dd84a286b27768c142e1567f3ce3b" },
"nvim-jdtls": { "branch": "master", "commit": "8597d57fb40d4ad503cf3acb2fdcfe1b0d8a193d" },
"nvim-lspconfig": { "branch": "master", "commit": "664de12a7816dfa348bb475607edfa4fe0122025" },
"nvim-metals": { "branch": "main", "commit": "1b9053139342ed9ab47b29e5a1076fcbd971a565" },
"nvim-jdtls": { "branch": "master", "commit": "aebfc22877ab143f907e90889e3903557183a22c" },
"nvim-lspconfig": { "branch": "master", "commit": "fefba589c56a5568a089299e36a4c8242502faaa" },
"nvim-metals": { "branch": "main", "commit": "34a82230e3b9c2a09defec79abd444da4902a1f1" },
"nvim-notify": { "branch": "master", "commit": "ea9c8ce7a37f2238f934e087c255758659948e0f" },
"nvim-scrollview": { "branch": "main", "commit": "e733fcfca7fa1d8879186d21a8d3726a0217aa56" },
"nvim-scrollview": { "branch": "main", "commit": "cb5e6a7692453624beb122f3d154c266a24d9195" },
"nvim-tmux-navigation": { "branch": "main", "commit": "543f090a45cef28156162883d2412fffecb6b750" },
"nvim-tree.lua": { "branch": "master", "commit": "f5d970d4506f385b29534252d8c15a782fa53034" },
"nvim-tree.lua": { "branch": "master", "commit": "f873625d0636889af4cd47a01e486beb865db205" },
"nvim-treehopper": { "branch": "master", "commit": "5a28bff46c05d28bdb4bcaef67e046eb915a9390" },
"nvim-treesitter": { "branch": "master", "commit": "1b5a7334bb9862abafcf6676d2a2a6973d15ae3a" },
"nvim-treesitter-context": { "branch": "master", "commit": "e2ea37627c0681421ccf4a3cf19d68bb958e1817" },
"nvim-treesitter-textobjects": { "branch": "master", "commit": "23e883b99228f8d438254e5ef8c897e5e60e75d1" },
"nvim-treesitter": { "branch": "master", "commit": "2847aac9d40516c8fc29ab4864e665da09fe14d6" },
"nvim-treesitter-context": { "branch": "master", "commit": "d0f03149ed6ad61c0bc7968efb15f6124477112d" },
"nvim-treesitter-textobjects": { "branch": "master", "commit": "2d6d3c7e49a24f6ffbbf7898241fefe9784f61bd" },
"nvim-ts-autotag": { "branch": "main", "commit": "40615e96075c743ef47aaf9de966dc348bec6459" },
"nvim-ts-context-commentstring": { "branch": "main", "commit": "0bf8fbc2ca8f8cdb6efbd0a9e32740d7a991e4c3" },
"nvim-ts-rainbow": { "branch": "master", "commit": "85279555783702b4bed6518e4494ec96d1ae1dfe" },
"nvim-ts-rainbow": { "branch": "master", "commit": "8312b513ce930e7669a1721befbe56f2e1853301" },
"nvim-web-devicons": { "branch": "master", "commit": "2a125024a137677930efcfdf720f205504c97268" },
"paint.nvim": { "branch": "main", "commit": "6ce64212804f425073c61ab0d9c2b034f0435260" },
"papercolor": { "branch": "master", "commit": "9051480ad9129ff4ab4fffb38b44779b9081626f" },
"plenary.nvim": { "branch": "master", "commit": "499e0743cf5e8075cd32af68baa3946a1c76adf1" },
"plenary.nvim": { "branch": "master", "commit": "36aaceb6e93addd20b1b18f94d86aecc552f30c4" },
"project.nvim": { "branch": "main", "commit": "8c6bad7d22eef1b71144b401c9f74ed01526a4fb" },
"rust-tools.nvim": { "branch": "master", "commit": "71d2cf67b5ed120a0e31b2c8adb210dd2834242f" },
"smartyank.nvim": { "branch": "master", "commit": "7e3905578f646503525b2f7018b8afd17861018c" },
Expand All @@ -87,18 +88,18 @@
"suda.vim": { "branch": "master", "commit": "08abd39dfe1cee681b8ce3e7321da5fa03e045c1" },
"sweetie": { "branch": "master", "commit": "4d938ccce2bcc4fb9846c06cd309b0a0488836b7" },
"tabout.nvim": { "branch": "master", "commit": "0d275c8d25f32457e67b5c66d6ae43f26a61bce5" },
"telescope-frecency.nvim": { "branch": "master", "commit": "0a4a521471141ae3c94ff0ebcffcaff3c3aad147" },
"telescope-frecency.nvim": { "branch": "master", "commit": "3ddaaa0a6084592eed451ded5fcd279e4a60c122" },
"telescope-fzf-native.nvim": { "branch": "main", "commit": "9bc8237565ded606e6c366a71c64c0af25cd7a50" },
"telescope-live-grep-args.nvim": { "branch": "master", "commit": "0f75ea809c46af8997c64f49c52e3c641d887885" },
"telescope-undo.nvim": { "branch": "main", "commit": "3dec002ea3e7952071d26fbb5d01e2038a58a554" },
"telescope-zoxide": { "branch": "main", "commit": "68966349aa1b8e9ade403e18479ecf79447389a7" },
"telescope.nvim": { "branch": "master", "commit": "6d3fbffe426794296a77bb0b37b6ae0f4f14f807" },
"telescope.nvim": { "branch": "master", "commit": "00cf15074a2997487813672a75f946d2ead95eb0" },
"toggleterm.nvim": { "branch": "main", "commit": "cf146a267a6a7db62b1e2aff40414b20081048a1" },
"tokyonight": { "branch": "main", "commit": "161114bd39b990995e08dbf941f6821afbdcd666" },
"trouble.nvim": { "branch": "main", "commit": "23c1327b85bd51258f9f8bd8f77f024acef6de69" },
"trouble.nvim": { "branch": "main", "commit": "2af0dd9767526410c88c628f1cbfcb6cf22dd683" },
"vim-cool": { "branch": "master", "commit": "80536b9f2e23292708a64f2e7bcf5e596f9faf24" },
"vim-easy-align": { "branch": "master", "commit": "12dd6316974f71ce333e360c0260b4e1f81169c3" },
"vim-fugitive": { "branch": "master", "commit": "5f0d280b517cacb16f59316659966c7ca5e2bea2" },
"vim-fugitive": { "branch": "master", "commit": "43f18ab9155c853a84ded560c6104e6300ad41da" },
"vim-illuminate": { "branch": "master", "commit": "a2907275a6899c570d16e95b9db5fd921c167502" },
"vim-jsonnet": { "branch": "master", "commit": "4ebc6619ddce5d032a985b42a9864154c3d20e4a" },
"vim-matchup": { "branch": "master", "commit": "3625e099b09bff2db5f04082cb09ba679525980c" },
Expand Down
10 changes: 10 additions & 0 deletions lua/core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ settings["null_ls_deps"] = {
-- "markdownlint",
}

-- Set the Debug Adapter Protocol (DAP) clients that will be installed and configured during bootstrap here.
-- Check the below link for all supported DAPs:
-- https://github.com/jay-babu/mason-nvim-dap.nvim/blob/main/lua/mason-nvim-dap/mappings/source.lua
---@type string[]
settings["dap_deps"] = {
"codelldb", -- C-Family
"delve", -- Go
"python", -- Python (debugpy)
}

return settings
16 changes: 8 additions & 8 deletions lua/modules/configs/completion/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
return function()
local diagnostics_virtual_text = require("core.settings").diagnostics_virtual_text
local diagnostics_level = require("core.settings").diagnostics_level
local is_windows = require("core.global").is_windows

local nvim_lsp = require("lspconfig")
local mason = require("mason")
local mason_registry = require("mason-registry")
local mason_lspconfig = require("mason-lspconfig")
require("lspconfig.ui.windows").default_options.border = "single"
require("lspconfig.ui.windows").default_options.border = "rounded"

local icons = {
ui = require("modules.utils.icons").get("ui", true),
Expand Down Expand Up @@ -34,8 +35,6 @@ return function()
},
})

local is_win = require("core.global").is_windows

-- Additional plugins for pylsp
mason_registry:on(
"package:install:success",
Expand All @@ -45,9 +44,10 @@ return function()
end

local venv = vim.fn.stdpath("data") .. "/mason/packages/python-lsp-server/venv"
local python = is_win and venv .. "/Scripts/python.exe" or venv .. "/bin/python"
local black = is_win and venv .. "/Scripts/black.exe" or venv .. "/bin/black"
local ruff = is_win and venv .. "/Scripts/ruff.exe" or venv .. "/bin/ruff"
local python = is_windows and venv .. "/Scripts/python.exe" or venv .. "/bin/python"
local black = is_windows and venv .. "/Scripts/black.exe" or venv .. "/bin/black"
local ruff = is_windows and venv .. "/Scripts/ruff.exe" or venv .. "/bin/ruff"

require("plenary.job")
:new({
command = python,
Expand Down Expand Up @@ -125,7 +125,7 @@ return function()

---A handler to setup all servers defined under `completion/servers/*.lua`
---@param lsp_name string
local function mason_handler(lsp_name)
local function mason_lsp_handler(lsp_name)
local ok, custom_handler = pcall(require, "completion.servers." .. lsp_name)
if not ok then
-- Default to use factory config for server(s) that doesn't include a spec
Expand All @@ -151,7 +151,7 @@ return function()
end
end

mason_lspconfig.setup_handlers({ mason_handler })
mason_lspconfig.setup_handlers({ mason_lsp_handler })

-- Setup lsps that are not supported by `mason.nvim` but supported by `nvim-lspconfig` here.
if vim.fn.executable("dart") == 1 then
Expand Down
29 changes: 29 additions & 0 deletions lua/modules/configs/tool/dap/clients/codelldb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/C-C---Rust-(via--codelldb)
return function()
local dap = require("dap")
local utils = require("modules.utils.dap")
local is_windows = require("core.global").is_windows

dap.adapters.codelldb = {
type = "server",
port = "${port}",
executable = {
command = vim.fn.exepath("codelldb"), -- Find codelldb on $PATH
args = { "--port", "${port}" },
detached = is_windows and false or true,
},
}
dap.configurations.c = {
{
name = "Launch the debugger",
type = "codelldb",
request = "launch",
program = utils.input_exec_path(),
args = utils.input_args(),
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
}
dap.configurations.cpp = dap.configurations.c
dap.configurations.rust = dap.configurations.c
end
66 changes: 66 additions & 0 deletions lua/modules/configs/tool/dap/clients/delve.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#go-using-delve-directly
-- https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_dap.md
return function()
local dap = require("dap")
local utils = require("modules.utils.dap")

dap.adapters.go = function(callback)
local stdout = vim.loop.new_pipe(false)
local handle
local pid_or_err
local port = 28695
local opts = {
stdio = { nil, stdout },
args = { "dap", "-l", "127.0.0.1:" .. port },
detached = true,
}
handle, pid_or_err = vim.loop.spawn(vim.fn.exepath("dlv"), opts, function(code)
stdout:close()
handle:close()
if code ~= 0 then
vim.notify(
string.format('"dlv" exited with code: %d, please check your configs for correctness.', code),
vim.log.levels.WARN,
{ title = "[go] DAP Warning!" }
)
end
end)
assert(handle, "Error running dlv: " .. tostring(pid_or_err))
stdout:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require("dap.repl").append(chunk)
end)
end
end)
-- Wait for delve to start
vim.defer_fn(function()
callback({ type = "server", host = "127.0.0.1", port = port })
end, 100)
end
dap.configurations.go = {
{ type = "go", name = "Debug", request = "launch", program = "${file}" },
{
type = "go",
name = "Debug with args",
request = "launch",
program = "${file}",
args = utils.input_args(),
},
{
type = "go",
name = "Debug test", -- configuration for debugging test files
request = "launch",
mode = "test",
program = "${file}",
}, -- works with go.mod packages and sub packages
{
type = "go",
name = "Debug test (go.mod)",
request = "launch",
mode = "test",
program = "./${relativeFileDirname}",
},
}
end
37 changes: 37 additions & 0 deletions lua/modules/configs/tool/dap/clients/lldb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#ccrust-via-lldb-vscode
return function()
local dap = require("dap")
local utils = require("modules.utils.dap")

dap.adapters.lldb = {
type = "executable",
command = "lldb-vscode",
name = "lldb",
}
dap.configurations.c = {
{
name = "Launch",
type = "lldb",
request = "launch",
program = utils.input_exec_path(),
cwd = "${workspaceFolder}",
args = utils.input_args(),
env = utils.get_env(),

-- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting:
--
-- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
--
-- Otherwise you might get the following error:
--
-- Error on launch: Failed to attach to the target process
--
-- But you should be aware of the implications:
-- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html
runInTerminal = false,
},
}

dap.configurations.cpp = dap.configurations.c
dap.configurations.rust = dap.configurations.c
end
Loading

0 comments on commit 61fde8b

Please sign in to comment.