Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make debug functionality better. #777

Merged
merged 18 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 dap servers that will be installed during bootstrap here.
-- check the below link for all the 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",
"delve",
"python",
}

return settings
8 changes: 4 additions & 4 deletions lua/modules/configs/completion/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ 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
-- Default to use factory config for lsp server(s) that doesn't include a spec
nvim_lsp[lsp_name].setup(opts)
return
elseif type(custom_handler) == "function" then
Expand All @@ -145,7 +145,7 @@ return function()
else
vim.notify(
string.format(
"Failed to setup [%s].\n\nServer definition under `completion/servers` must return\neither a fun(opts) or a table (got '%s' instead)",
"Failed to setup [%s].\n\nLsp server settings under `completion/servers` must return\neither a fun(opts) or a table (got '%s' instead)",
lsp_name,
type(custom_handler)
),
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
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.

38 changes: 0 additions & 38 deletions lua/modules/configs/tool/dap/dap-lldb.lua

This file was deleted.

38 changes: 34 additions & 4 deletions lua/modules/configs/tool/dap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return function()

local dap = require("dap")
local dapui = require("dapui")
local mason_dap = require("mason-nvim-dap")

dap.listeners.after.event_initialized["dapui_config"] = function()
dapui.open()
Expand Down Expand Up @@ -33,8 +34,37 @@ return function()
)
vim.fn.sign_define("DapLogPoint", { text = icons.dap.LogPoint, texthl = "DapLogPoint", linehl = "", numhl = "" })

-- Config lang adaptors
require("tool.dap.dap-lldb")
require("tool.dap.dap-debugpy")
require("tool.dap.dap-dlv")
---A handler to setup all servers defined under `tool/dap/servers/*.lua`
---@param config table
local function mason_dap_handler(config)
local dap_name = config.name
local ok, custom_handler = pcall(require, "modules.configs.tool.dap.servers." .. dap_name)
if not ok then
-- Default to use factory config for dap server(s) that doesn't include a spec
mason_dap.default_setup(config)
return
elseif type(custom_handler) == "function" then
--- Case where dap server requires its own setup
--- Make sure to set
--- dap.adpaters.<dap_name> = {some config}
--- dap.configurations.<lang> = {some config}
--- See `codelldb.lua` for example.
custom_handler(config)
else
vim.notify(
string.format(
"Failed to setup [%s].\n\nDap server settings under `tool/dap/servers` must return\n a fun(opts) (got '%s' instead)",
config.name,
type(custom_handler)
),
vim.log.levels.ERROR,
{ title = "nvim-dap" }
)
end
end

mason_dap.setup({
ensure_installed = require("core.settings").dap_deps,
handlers = { mason_dap_handler },
})
end
32 changes: 32 additions & 0 deletions lua/modules/configs/tool/dap/servers/codelldb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
return function()
local dap = require("dap")
local dap_utils = require("modules.configs.tool.dap.utils")

local codelldb = vim.fn.exepath("codelldb")
local path_to_program = dap_utils.path_to_program()

dap.adapters.codelldb = {
type = "server",
port = "${port}",
executable = {
-- CHANGE THIS to your path!
command = codelldb,
args = { "--port", "${port}" },
-- On windows you may have to uncomment this:
-- detached = false,
},
}

dap.configurations.cpp = {
{
name = "Launch file",
type = "codelldb",
request = "launch",
program = path_to_program,
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
}
dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
end
48 changes: 48 additions & 0 deletions lua/modules/configs/tool/dap/servers/cppdbg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/C-C---Rust-(gdb-via--vscode-cpptools)
return function()
local dap = require("dap")
local dap_utils = require("modules.configs.tool.dap.utils")

local gdb_abspath = vim.fn.exepath("gdb")
local cpptools_abspath = vim.fn.exepath("OpenDebugAD7")
local path_to_program = dap_utils.path_to_program()
local get_env = dap_utils.get_env()
local setupCommands = {
text = "-enable-pretty-printing",
description = "enable pretty printing",
ignoreFailures = false,
}

dap.adapters.cppdbg = {
id = "cppdbg",
type = "executable",
command = cpptools_abspath,
}
dap.configurations.cpp = {
{
name = "Launch file",
type = "cppdbg",
request = "launch",
program = path_to_program,
cwd = "${workspaceFolder}",
env = get_env,
setupCommands = setupCommands,
stopAtEntry = false,
},
{
name = "Attach to gdbserver :1234",
type = "cppdbg",
request = "launch",
MIMode = "gdb",
miDebuggerServerAddress = "localhost:1234",
miDebuggerPath = gdb_abspath,
program = path_to_program,
cwd = "${workspaceFolder}",
env = get_env,
setupCommands = setupCommands,
},
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
end
68 changes: 68 additions & 0 deletions lua/modules/configs/tool/dap/servers/delve.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
return function()
local dap = require("dap")
local dlv = vim.fn.exepath("dlv")

dap.adapters.go = function(callback)
local stdout = vim.loop.new_pipe(false)
local handle
local pid_or_err
local port = 38697
local opts = {
stdio = { nil, stdout },
args = { "dap", "-l", "127.0.0.1:" .. port },
detached = true,
}
handle, pid_or_err = vim.loop.spawn(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
-- https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_dap.md
dap.configurations.go = {
{ type = "go", name = "Debug", request = "launch", program = "${file}" },
{
type = "go",
name = "Debug with args",
request = "launch",
program = "${file}",
args = function()
local argument_string = vim.fn.input("Program arg(s): ")
return vim.fn.split(argument_string, " ", true)
end,
},
{
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
Loading