aboutsummaryrefslogtreecommitdiffstats
path: root/nvim/.config/nvim/lua/plugins/debug.lua
blob: 668323ec9f509a930039a856fb7b50e1d1d438ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
-- debug.lua
--
-- Configures the DAP plugin to debug code.

---@class DapConfig
---@field name string Configuration name
---@field type string Debug adapter type
---@field request string Launch type ('launch' or 'attach')
---@field program function|string Path to executable or function returning path
---@field cwd string Working directory
---@field stopOnEntry boolean Whether to stop at entry point
---@field args table Command line arguments
---@field runInTerminal boolean Whether to run in terminal

---@class DapAdapter
---@field type string Adapter type ('server', 'executable', etc.)
---@field port string Port configuration
---@field executable table Executable configuration with command and args

-- Go debugging adapter setup
-- Configures delve debugger for Go with platform-specific settings
---@return nil
local function setup_go_adapter()
  require('dap-go').setup {
    delve = {
      -- On Windows delve must be run attached or it crashes
      detached = vim.fn.has 'win32' == 0,
    },
  }
end

-- Python debugging adapter setup
-- Configures Python debugger using python3 interpreter
---@return nil
local function setup_python_adapter()
  require('dap-python').setup 'python3'
end

-- CodeLLDB adapter setup
-- Configures LLDB-based debugger for native languages (C, C++, Rust, Zig)
---@param dap table # DAP module instance
---@return nil
local function setup_codelldb_adapter(dap)
  ---@type DapAdapter
  dap.adapters.codelldb = {
    type = 'server',
    port = '${port}',
    executable = {
      command = vim.fn.exepath 'codelldb',
      args = { '--port', '${port}' },
    },
  }
end

-- Build and find the newly-built Zig executable by running
-- 'zig build' and attempting to locate the resulting executable.
-- If the executable is unable to be automatically found, the user
-- will be prompted to give the path to the executable.
--
---@return string|nil # Path to executable or nil if build the failed
local function build_and_find_zig_executable()
  local build_result = vim.fn.system 'zig build'
  if vim.v.shell_error ~= 0 then
    vim.notify('Build failed: ' .. build_result, vim.log.levels.ERROR)
    return nil
  end

  local exe_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':t')
  local possible_paths = { 'zig-out/bin/' .. exe_name }

  for _, path in ipairs(possible_paths) do
    local full_path = vim.fn.getcwd() .. '/' .. path
    if vim.fn.executable(full_path) == 1 then
      return full_path
    end
  end

  return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/zig-out/bin/', 'file')
end

-- Build and find the Zig test executable by running
-- 'zig build test' and prompting the user for the test
-- executable location.
--
---@return string # Path to test executable
local function build_and_find_zig_test()
  vim.fn.system 'zig build test'
  return vim.fn.input('Path to test executable: ', vim.fn.getcwd() .. '/zig-cache/o/', 'file')
end

-- Zig debugging configuration
-- Sets up debugging configurations for Zig programs and tests
---@param dap table # DAP module instance
---@return nil
local function setup_zig_configuration(dap)
  ---@type DapConfig[]
  dap.configurations.zig = {
    {
      name = 'Build and Launch Zig Program',
      type = 'codelldb',
      request = 'launch',
      program = build_and_find_zig_executable,
      cwd = '${workspaceFolder}',
      stopOnEntry = false,
      args = {},
      runInTerminal = false,
    },
    {
      name = 'Launch Zig Test',
      type = 'codelldb',
      request = 'launch',
      program = build_and_find_zig_test,
      cwd = '${workspaceFolder}',
      stopOnEntry = false,
      args = {},
      runInTerminal = false,
    },
  }
end

-- Language adapters and configurations
-- Sets up debugging support for all supported languages
---@param dap table # DAP module instance
---@return nil
local function setup_language_adapters(dap)
  setup_go_adapter()
  setup_python_adapter()
  setup_codelldb_adapter(dap)
  setup_zig_configuration(dap)
end

-- Key mappings setup
-- Configures function keys and leader key combinations for debugging actions
---@param dap table # DAP module instance
---@param dapui table # DAP UI module instance
---@return nil
local function setup_keymaps(dap, dapui)
  ---@type table<string, {[1]: function, [2]: string}>
  local keymaps = {
    ['<F5>'] = { dap.continue, 'Debug: Start/Continue' },
    ['<F1>'] = { dap.step_into, 'Debug: Step Into' },
    ['<F2>'] = { dap.step_over, 'Debug: Step Over' },
    ['<F3>'] = { dap.step_out, 'Debug: Step Out' },
    ['<F7>'] = { dapui.toggle, 'Debug: See last session result' },
    ['<leader>tb'] = { dap.toggle_breakpoint, 'Debug: Toggle Breakpoint' },
    ['<leader>B'] = {
      function()
        dap.set_breakpoint(vim.fn.input 'Breakpoint condition: ')
      end,
      'Debug: Set Breakpoint',
    },
  }

  for key, config in pairs(keymaps) do
    vim.keymap.set('n', key, config[1], { desc = config[2] })
  end
end

-- Event listeners setup
-- Configures automatic UI opening/closing based on debug session events
---@param dap table # DAP module instance
---@param dapui table # DAP UI module instance
---@return nil
local function setup_dap_listeners(dap, dapui)
  local dapui_config = 'dapui_config'
  dap.listeners.after.event_initialized[dapui_config] = dapui.open
  dap.listeners.before.event_terminated[dapui_config] = dapui.close
  dap.listeners.before.event_exited[dapui_config] = dapui.close
end

-- DAP UI configuration
-- Sets up the debugging user interface with custom icons and controls
---@param dapui table # DAP UI module instance
---@return nil
local function setup_dapui_config(dapui)
  dapui.setup {
    icons = {
      expanded = '▾',
      collapsed = '▸',
      current_frame = '*',
    },
    controls = {
      icons = {
        pause = '⏸',
        play = '▶',
        step_into = '⏎',
        step_over = '⏭',
        step_out = '⏮',
        step_back = 'b',
        run_last = '▶▶',
        terminate = '⏹',
        disconnect = '⏏',
      },
    },
  }
end

-- Mason DAP configuration
-- Automatically installs and configures debug adapters through Mason
---@return nil
local function setup_mason_dap()
  require('mason-nvim-dap').setup {
    automatic_installation = true,
    handlers = {},
    ensure_installed = { 'delve', 'python', 'codelldb' },
  }
end

-- Main configuration orchestrator
-- Sets up all debugging components in the correct order
---@return nil
local function setup_dap()
  local dap = require 'dap'
  local dapui = require 'dapui'

  setup_mason_dap()
  setup_dapui_config(dapui)
  setup_dap_listeners(dap, dapui)
  setup_language_adapters(dap)
  setup_keymaps(dap, dapui)
end

-- Plugin specification
-- Configures nvim-dap with all necessary dependencies for debugging
---@return table # Plugin specification for lazy.nvim
return {
  'mfussenegger/nvim-dap',
  dependencies = {
    'rcarriga/nvim-dap-ui', -- Beautiful debugger UI
    'nvim-neotest/nvim-nio', -- Required dependency for nvim-dap-ui
    'williamboman/mason.nvim', -- Package manager for debug adapters
    'jay-babu/mason-nvim-dap.nvim', -- Mason integration for DAP
    'leoluz/nvim-dap-go', -- Go debugging support
    'mfussenegger/nvim-dap-python', -- Python debugging support
  },
  config = function()
    setup_dap()
  end,
}