Skip to content

Commit

Permalink
fixup! refactor: make debug functionality better
Browse files Browse the repository at this point in the history
  • Loading branch information
Jint-lzxy committed Jun 12, 2023
1 parent c21e1cf commit b84b2e2
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 137 deletions.
6 changes: 3 additions & 3 deletions lua/core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ 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:
-- 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/C++/Rust
"codelldb", -- C-Family
"delve", -- Go
"python", -- Python (debugpy)
}
Expand Down
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 @@ -132,7 +132,7 @@ return function()
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 lsp server(s) that doesn't include a spec
-- Default to use factory config for 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\nLsp server settings under `completion/servers` must return\neither a fun(opts) or a table (got '%s' instead)",
"Failed to setup [%s].\n\nServer definition under `completion/servers` must return\neither a fun(opts) or a table (got '%s' instead)",
lsp_name,
type(custom_handler)
),
Expand Down
25 changes: 13 additions & 12 deletions lua/modules/configs/tool/dap/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
return function()
local icons = { dap = require("modules.utils.icons").get("dap") }
local colors = require("modules.utils").get_palette()

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

local icons = { dap = require("modules.utils.icons").get("dap") }
local colors = require("modules.utils").get_palette()

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

---A handler to setup all servers defined under `tool/dap/servers/*.lua`
---A handler to setup all clients defined under `tool/dap/clients/*.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)
local ok, custom_handler = pcall(require, "tool.dap.clients." .. dap_name)
if not ok then
-- Default to use factory config for dap server(s) that doesn't include a spec
-- Default to use factory config for clients(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.
-- Case where the protocol requires its own setup
-- Make sure to set
-- * dap.adpaters.<dap_name> = { your config }
-- * dap.configurations.<lang> = { your config }
-- See `codelldb.lua` for a concrete 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)",
"Failed to setup [%s].\n\nClient definition under `tool/dap/clients` must return\na fun(opts) (got '%s' instead)",
config.name,
type(custom_handler)
),
Expand All @@ -65,6 +65,7 @@ return function()

mason_dap.setup({
ensure_installed = require("core.settings").dap_deps,
automatic_installation = true,
handlers = { mason_dap_handler },
})
end
21 changes: 9 additions & 12 deletions lua/modules/configs/tool/dap/servers/codelldb.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/C-C---Rust-(via--codelldb)
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()
local utils = require("modules.utils.dap")

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

dap.configurations.cpp = {
dap.configurations.c = {
{
name = "Launch file",
name = "Launch the debugger",
type = "codelldb",
request = "launch",
program = path_to_program,
program = utils.input_exec_path(),
env = utils.get_env(),
args = utils.input_args(),
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
}
dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
dap.configurations.cpp = dap.configurations.c
dap.configurations.rust = dap.configurations.c
end
48 changes: 0 additions & 48 deletions lua/modules/configs/tool/dap/servers/cppdbg.lua

This file was deleted.

13 changes: 5 additions & 8 deletions lua/modules/configs/tool/dap/servers/delve.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
-- 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 dlv = vim.fn.exepath("dlv")
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 = 38697
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(dlv, opts, function(code)
handle, pid_or_err = vim.loop.spawn(vim.fn.exepath("dlv"), opts, function(code)
stdout:close()
handle:close()
if code ~= 0 then
Expand All @@ -38,18 +39,14 @@ return 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,
args = utils.input_args(),
},
{
type = "go",
Expand Down
24 changes: 9 additions & 15 deletions lua/modules/configs/tool/dap/servers/lldb.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#ccrust-via-lldb-vscode
return function()
local dap = require("dap")
local dap_utils = require("modules.configs.tool.dap.utils")

-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#ccrust-via-lldb-vscode
local lldb_abspath = vim.fn.exepath("lldb-vscode")
local path_to_program = dap_utils.path_to_program()
local get_env = dap_utils.get_env()
local input_args = dap_utils.input_args()
local utils = require("modules.utils.dap")

dap.adapters.lldb = {
type = "executable",
command = lldb_abspath,
command = "lldb-vscode",
name = "lldb",
}
dap.configurations.cpp = {
dap.configurations.c = {
{
name = "Launch",
type = "lldb",
request = "launch",
program = path_to_program,
program = utils.input_exec_path(),
cwd = "${workspaceFolder}",
env = get_env,
args = input_args,
stopOnEntry = false,
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
Expand All @@ -38,6 +32,6 @@ return function()
},
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
dap.configurations.cpp = dap.configurations.c
dap.configurations.rust = dap.configurations.c
end
18 changes: 7 additions & 11 deletions lua/modules/configs/tool/dap/servers/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,27 @@ return function()
local dap = require("dap")
local debugpy = vim.fn.exepath("debugpy-adapter")

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

dap.adapters.python = function(cb, config)
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"
cb({
callback({
type = "server",
port = assert(port, "`connect.port` is required for a python `attach` configuration"),
host = host,
options = {
source_filetype = "python",
},
options = { source_filetype = "python" },
})
else
cb({
callback({
type = "executable",
command = debugpy,
options = {
source_filetype = "python",
},
options = { source_filetype = "python" },
})
end
end
Expand All @@ -40,7 +36,7 @@ return function()
-- 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 isempty(vim.env.CONDA_PREFIX) then
if not is_empty(vim.env.CONDA_PREFIX) then
return vim.env.CONDA_PREFIX .. "/bin/python"
else
return "python3"
Expand Down
20 changes: 0 additions & 20 deletions lua/modules/configs/tool/dap/utils.lua

This file was deleted.

Loading

0 comments on commit b84b2e2

Please sign in to comment.