summaryrefslogtreecommitdiffstats
path: root/lua/conf/plugins/dap.lua
blob: 5abe1b386bbc2b8d541b81bbc425a32d3d1c7856 (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
local misc = require("core.misc")
local map = misc.map

--- select a program to execute
---@return thread coroutine containing the picker
local function select_program()
  return coroutine.create(function(coro)
    ---@diagnostic disable-next-line: param-type-mismatch
    local entries = vim.fn.readdir(".", [[v:val !~ '^\.']])
    vim.ui.select(entries, { prompt = "Select the executable to run:" },
      function(choice)
        coroutine.resume(coro, choice)
      end)
  end)
end

local keymap_restore = {}
--- make the default hover binding work for nvim-dap instead of lsp
local function set_hover_bind()
  for _, buf in pairs(vim.api.nvim_list_bufs()) do
    local keymaps = vim.api.nvim_buf_get_keymap(buf, 'n')
    for _, keymap in pairs(keymaps) do
      if keymap.lhs == "K" then
        table.insert(keymap_restore, keymap)
        vim.api.nvim_buf_del_keymap(buf, 'n', 'K')
      end
    end
  end
  vim.keymap.set('n', 'K', require("dap.ui.widgets").hover,
    { silent = true })
end

--- revert the hover bind back to whatever it was
local function unset_hover_bind()
  for _, keymap in pairs(keymap_restore) do
    if keymap.rhs then
      vim.api.nvim_buf_set_keymap(keymap.buffer, keymap.mode, keymap.lhs,
        keymap.rhs, { silent = keymap.silent == 1 })
    elseif keymap.callback then
      vim.keymap.set(keymap.mode, keymap.lhs, keymap.callback,
        { buffer = keymap.buffer, silent = keymap.silent == 1 })
    end
  end
  keymap_restore = {}
end

return { "mfussenegger/nvim-dap",
  requires = {
    "mason-org/mason.nvim",
    "nvim-telescope/telescope.nvim"
  },
  disable = not vim.fn.has("nvim-0.9.5"),
  branch = "0.10.0",
  function()
    local dap = require("dap")

    -- define codelldb
    dap.adapters.codelldb = {
      type = "executable",
      command = "codelldb",
    }

    -- define the c configuration for codelldb
    dap.configurations.c = {
      {
        name = "Launch file",
        type = "codelldb",
        request = "launch",
        program = select_program,

        cwd = "${workspaceFolder}",
        stopOnEntry = false
      }
    }

    -- and define them for cpp, zig, and rust
    dap.configurations.cpp = dap.configurations.c
    dap.configurations.zig = dap.configurations.c
    dap.configurations.rust = dap.configurations.c

    -- keybinds
    map("n", "<Leader>ec", dap.continue, { desc = "dap continue " })
    map("n", "<Leader>el", dap.run_last, { desc = "dap run last" })
    map("n", "<Leader>et", function()
      dap.terminate()
      unset_hover_bind()
    end, { desc = "dap terminate " })
    map("n", "<Leader>eb", require("dap.breakpoints").toggle, { desc = "dap toggle breakpoint" })
    map("n", "<Leader>e]", dap.step_over, { desc = "dap step over" })
    map("n", "<Leader>e[", dap.step_back, { desc = "dap step back" })
    map("n", "<Leader>er", dap.repl.toggle, { desc = "dap repl toggle" })
    map("n", "<Leader>eR", dap.restart, { desc = "dap restart" })

    -- events
    dap.listeners.after['event_initialized']['me'] = set_hover_bind
    dap.listeners.after['event_terminated']['me'] = unset_hover_bind
  end
}