summary refs log tree commit diff
path: root/.vim/lua/internal/dap.lua
diff options
context:
space:
mode:
Diffstat (limited to '.vim/lua/internal/dap.lua')
-rw-r--r--.vim/lua/internal/dap.lua162
1 files changed, 162 insertions, 0 deletions
diff --git a/.vim/lua/internal/dap.lua b/.vim/lua/internal/dap.lua
new file mode 100644
index 0000000..aa419ea
--- /dev/null
+++ b/.vim/lua/internal/dap.lua
@@ -0,0 +1,162 @@
+-- vim: fdm=marker
+
+local dap = require('dap')
+dap.defaults.fallback.terminal_win_cmd = 'belowright 10new'
+dap.defaults.fallback.terminal_win_cmd = 'belowright 10new'
+
+-- open repl when session starts
+dap.listeners.after['event_initialized']['me'] = function()
+  dap.repl.toggle()
+end
+
+-- signs
+vim.fn.sign_define({{name='DapBreakpoint', text='🞱', texthl='DapBreakpoint', linehl='DapBreakpointLn'}})
+vim.fn.sign_define({{name='DapStopped', text='→', texthl='DapStopped', linehl='DapStoppedLn'}})
+
+-- keymaps
+vim.keymap.set('n', '<leader>dd', dap.toggle_breakpoint, {desc='toggle breakpoint'}) -- convenience
+vim.keymap.set('n', '<leader>db', dap.toggle_breakpoint, {desc='toggle breakpoint'})
+vim.keymap.set('n', '<leader>dc', dap.continue, {desc='continue'})
+vim.keymap.set('n', '<leader>do', dap.step_over, {desc='step over'})
+vim.keymap.set('n', '<leader>di', dap.step_into, {desc='step into'})
+vim.keymap.set('n', '<leader>dO', dap.step_out, {desc='step out'})
+vim.keymap.set('n', '<leader>dR', dap.repl.toggle, {desc='toggle REPL'})
+vim.keymap.set('n', '<leader>dL', dap.run_last, {desc='run last'})
+vim.keymap.set('n', '<leader>dt', dap.terminate, {desc='terminate'})
+
+local widgets = require('dap.ui.widgets')
+vim.keymap.set('n', '<leader>dh', widgets.hover, {desc='hover'})
+vim.keymap.set('n', '<leader>dB', require('telescope').extensions.dap.list_breakpoints, {desc='list breakpoints'})
+
+-- Common
+local bin_from_var_or_pick = function()
+  local ok, retval = pcall(vim.api.nvim_buf_get_var, 0, 'dap_bin')
+  if ok then return retval end
+  local bin = nil
+  vim.ui.input({prompt = 'DAP Binary: '}, function(choice) bin = choice end)
+  return bin
+end
+
+-- Go {{{
+dap.adapters.go_dlv_local = function(callback, config) -- {{{
+  local stdout = vim.loop.new_pipe()
+  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(exit)
+    stdout:close()
+    handle:close()
+    if exit ~= 0 then vim.notify('dlv exited with exit code ' .. exit) 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)
+  vim.defer_fn(function() callback({type = 'server', host = '127.0.0.1', port = port}) end, 100)
+end -- }}}
+
+dap.adapters.go_dlv_remote = {
+  type = 'server',
+  host = '127.0.0.1',
+  port = 38697,
+}
+
+dap.configurations.go = {
+  {
+    name = 'Debug (local)',
+    type = 'go_dlv_local',
+    request = 'launch',
+    program = '${file}',
+  },
+  {
+    name = 'Debug (remote)',
+    type = 'go_dlv_remote',
+    request = 'launch',
+    mode = 'exec',
+    program = bin_from_var_or_pick,
+  },
+}
+-- end of Go }}}
+
+-- C/C++/Rust {{{
+-- dap.adapters.cppdbg = {
+--   id = 'cppdbg',
+--   type = 'executable',
+--   command = vim.env.HOME .. '/.local/share/nvim/dap/ms-vscode.cpptools/extension/debugAdapters/bin/OpenDebugAD7',
+-- }
+dap.adapters.lldb = {
+  type = 'executable',
+  command = vim.trim(vim.fn.system('command -v lldb-vscode')),
+  name = 'lldb',
+}
+
+-- dap.configurations.c = {
+--   {
+--     name = 'Launch binary',
+--     type = 'cppdbg',
+--     request = 'launch',
+--     program = function()
+--       local file = vim.g.dapbin
+--       if file then return file end
+--       -- TODO: assuming meson
+--       file = vim.trim(vim.fn.system(
+--         [[jq -r '..|select(.type?=="executable")|.filename|.[0]' ]] ..
+--         vim.lsp.buf.list_workspace_folders()[1] ..
+--         [[/build/meson-info/intro-targets.json]]
+--       ))
+--       local fd = vim.loop.fs_open(file, "r", 438)
+--       if file and fd then
+--         vim.loop.fs_close(fd)
+--         return file
+--       end
+--       vim.ui.input('binary: ', function(result) file = result end)
+--       return file
+--     end,
+--     cwd = '${workspaceFolder}',
+--     stopOnEntry = true,
+--   },
+--   {
+--     name = 'Attach to gdbserver',
+--     type = 'cppdbg',
+--     request = 'launch',
+--     MIMode = 'gdb',
+--     miDebuggerServerAddress = 'localhost:1234',
+--     miDebuggerPath = '/usr/bin/gdb',
+--     cwd = '${workspaceFolder}',
+--     program = function()
+--       local file = vim.g.dapbin
+--       if file then return file end
+--       vim.ui.input('binary: ', function(result) file = result end)
+--       return file
+--     end,
+--   },
+-- }
+
+dap.configurations.c = {
+  {
+    name = 'Launch',
+    type = 'lldb',
+    request = 'launch',
+    program = bin_from_var_or_pick,
+    args = {},
+    cwd = '${workspaceFolder}',
+    stopOnEntry = false,
+  },
+  {
+    name = 'Attach',
+    type = 'lldb',
+    request = 'attach',
+    pid = require('dap.utils').pick_process,
+    args = {},
+  },
+}
+dap.configurations.cpp = dap.configurations.c
+dap.configurations.rust = dap.configurations.c
+
+-- end of C/C++/Rust }}}