Skip to content

Commit

Permalink
refactor(DAP): improve user experience (#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
ayamir and Jint-lzxy authored Jun 13, 2023
1 parent 24c5c22 commit 53727fc
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 164 deletions.
10 changes: 10 additions & 0 deletions lua/core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ settings["null_ls_deps"] = {
"vint",
}

-- 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 @@ -129,7 +129,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 @@ -155,7 +155,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
64 changes: 64 additions & 0 deletions lua/modules/configs/tool/dap/clients/python.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#python
return function()
local dap = require("dap")
local debugpy = vim.fn.exepath("debugpy-adapter")

local function is_empty(s)
return s == nil or s == ""
end

dap.adapters.python = function(callback, config)
if config.request == "attach" then
---@diagnostic disable-next-line: undefined-field
local port = (config.connect or config).port
---@diagnostic disable-next-line: undefined-field
local host = (config.connect or config).host or "127.0.0.1"
callback({
type = "server",
port = assert(port, "`connect.port` is required for a python `attach` configuration"),
host = host,
options = { source_filetype = "python" },
})
else
callback({
type = "executable",
command = debugpy,
options = { source_filetype = "python" },
})
end
end
dap.configurations.python = {
{
-- The first three options are required by nvim-dap
type = "python", -- the type here established the link to the adapter definition: `dap.adapters.python`
request = "launch",
name = "Launch file",
-- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options
program = "${file}", -- This configuration will launch the current file if used.
pythonPath = function()
if not is_empty(vim.env.CONDA_PREFIX) then
return vim.env.CONDA_PREFIX .. "/bin/python"
else
return "python3"
end
end,
},
}

-- NOTE: This setting is for people using venv
-- pythonPath = function()
-- -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
-- -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
-- -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
-- local cwd, venv = vim.fn.getcwd(), os.getenv("VIRTUAL_ENV")
-- if venv and vim.fn.executable(venv .. "/bin/python") == 1 then
-- return venv .. "/bin/python"
-- elseif vim.fn.executable(cwd .. "/venv/bin/python") == 1 then
-- return cwd .. "/venv/bin/python"
-- elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then
-- return cwd .. "/.venv/bin/python"
-- else
-- return "python3"
-- end
-- end,
end
46 changes: 0 additions & 46 deletions lua/modules/configs/tool/dap/dap-debugpy.lua

This file was deleted.

65 changes: 0 additions & 65 deletions lua/modules/configs/tool/dap/dap-dlv.lua

This file was deleted.

Loading

0 comments on commit 53727fc

Please sign in to comment.