]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/dap.lua
nvim: import neotest setup from lazyvim
[dotfiles.git] / nvim / lua / plugins / dap.lua
1 ---@param config {args?:string[]|fun():string[]?}
2 local function get_args(config)
3 local args = type(config.args) == "function" and (config.args() or {}) or config.args or {}
4 config = vim.deepcopy(config)
5 ---@cast args string[]
6 config.args = function()
7 local new_args = vim.fn.input("Run with args: ", table.concat(args, " ")) --[[@as string]]
8 return vim.split(vim.fn.expand(new_args) --[[@as string]], " ")
9 end
10 return config
11 end
12
13 return {
14 "mfussenegger/nvim-dap",
15
16 dependencies = {
17 -- fancy UI for the debugger
18 {
19 "rcarriga/nvim-dap-ui",
20 -- stylua: ignore
21 keys = {
22 { "<leader>du", function() require("dapui").toggle({ }) end, desc = "Dap UI" },
23 { "<leader>de", function() require("dapui").eval() end, desc = "Eval", mode = {"n", "v"} },
24 },
25 opts = {},
26 config = function(_, opts)
27 local dap = require("dap")
28 local dapui = require("dapui")
29 dapui.setup(opts)
30 dap.listeners.after.event_initialized["dapui_config"] = function()
31 dapui.open({})
32 end
33 dap.listeners.before.event_terminated["dapui_config"] = function()
34 dapui.close({})
35 end
36 dap.listeners.before.event_exited["dapui_config"] = function()
37 dapui.close({})
38 end
39 end,
40 },
41
42 -- virtual text for the debugger
43 {
44 "theHamsta/nvim-dap-virtual-text",
45 opts = {},
46 },
47
48 -- which key integration
49 {
50 "folke/which-key.nvim",
51 opts = {
52 defaults = {
53 ["<leader>d"] = { name = "+debug" },
54 },
55 },
56 },
57
58 -- mason.nvim integration
59 {
60 "jay-babu/mason-nvim-dap.nvim",
61 dependencies = "mason.nvim",
62 cmd = { "DapInstall", "DapUninstall" },
63 opts = {
64 -- Makes a best effort to setup the various debuggers with
65 -- reasonable debug configurations
66 automatic_installation = true,
67
68 -- You can provide additional configuration to the handlers,
69 -- see mason-nvim-dap README for more information
70 handlers = {},
71 },
72 },
73 {
74 "jbyuki/one-small-step-for-vimkind",
75 -- stylua: ignore
76 keys = {
77 { "<leader>daL", function() require("osv").launch({ port = 8086 }) end, desc = "Adapter Lua Server" },
78 { "<leader>dal", function() require("osv").run_this() end, desc = "Adapter Lua" },
79 },
80 config = function()
81 local dap = require("dap")
82 dap.adapters.nlua = function(callback, config)
83 local adapter = {
84 type = "server",
85 host = config.host or "127.0.0.1",
86 port = config.port or 8086,
87 }
88 if config.start_neovim then
89 local dap_run = dap.run
90 dap.run = function(c)
91 adapter.port = c.port
92 adapter.host = c.host
93 end
94 require("osv").run_this()
95 dap.run = dap_run
96 end
97 callback(adapter)
98 end
99 dap.configurations.lua = {
100 {
101 type = "nlua",
102 request = "attach",
103 name = "Run this file",
104 start_neovim = {},
105 },
106 {
107 type = "nlua",
108 request = "attach",
109 name = "Attach to running Neovim instance (port 8086)",
110 port = 8086,
111 },
112 }
113 end,
114 },
115 },
116
117 -- stylua: ignore
118 keys = {
119 { "<leader>dB", function() require("dap").set_breakpoint(vim.fn.input('Breakpoint condition: ')) end, desc = "Breakpoint Condition" },
120 { "<leader>db", function() require("dap").toggle_breakpoint() end, desc = "Toggle Breakpoint" },
121 { "<leader>dc", function() require("dap").continue() end, desc = "Continue" },
122 { "<leader>da", function() require("dap").continue({ before = get_args }) end, desc = "Run with Args" },
123 { "<leader>dC", function() require("dap").run_to_cursor() end, desc = "Run to Cursor" },
124 { "<C-T>", function() require("dap").run_to_cursor() end, desc = "Run to Cursor" },
125 { "<leader>dg", function() require("dap").goto_() end, desc = "Go to line (no execute)" },
126 { "<leader>di", function() require("dap").step_into() end, desc = "Step Into" },
127 { "<C-S>", function() require("dap").step_into() end, desc = "Step Into" },
128 { "<leader>dj", function() require("dap").down() end, desc = "Down" },
129 { "<leader>dk", function() require("dap").up() end, desc = "Up" },
130 { "<leader>dl", function() require("dap").run_last() end, desc = "Run Last" },
131 { "<leader>do", function() require("dap").step_out() end, desc = "Step Out" },
132 { "<C-F>", function() require("dap").step_out() end, desc = "Step Over" },
133 { "<leader>dO", function() require("dap").step_over() end, desc = "Step Over" },
134 { "<C-N>", function() require("dap").step_over() end, desc = "Step Over" },
135 { "<leader>dp", function() require("dap").pause() end, desc = "Pause" },
136 { "<leader>dr", function() require("dap").repl.toggle() end, desc = "Toggle REPL" },
137 { "<leader>ds", function() require("dap").session() end, desc = "Session" },
138 { "<leader>dt", function() require("dap").terminate() end, desc = "Terminate" },
139 { "<leader>dw", function() require("dap.ui.widgets").hover() end, desc = "Widgets" },
140 },
141
142 config = function()
143 local Config = require("lazyvim.config")
144 vim.api.nvim_set_hl(0, "DapStoppedLine", { default = true, link = "Visual" })
145
146 for name, sign in pairs(Config.icons.dap) do
147 sign = type(sign) == "table" and sign or { sign }
148 vim.fn.sign_define(
149 "Dap" .. name,
150 { text = sign[1], texthl = sign[2] or "DiagnosticInfo", linehl = sign[3], numhl = sign[3] }
151 )
152 end
153 end,
154 }