2023-11-17 22:02:07 +00:00
|
|
|
local M = {}
|
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
local dap = require("dap")
|
|
|
|
local dap_ui = require("dap.ui")
|
|
|
|
local dap_ui_widgets = require("dap.ui.widgets")
|
2023-11-23 12:09:45 +00:00
|
|
|
|
|
|
|
local function query_launch()
|
2024-03-05 03:10:57 +00:00
|
|
|
local command = vim.fn.input("Launch> ", vim.fn.getcwd() .. "/", "file")
|
|
|
|
vim.api.nvim_echo({ { "", "None" } }, false, {})
|
2023-11-23 12:09:45 +00:00
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
local parts = vim.split(command, "%s+", { trimempty = true })
|
2023-11-23 12:09:45 +00:00
|
|
|
if not parts[1] then
|
2024-03-05 03:10:57 +00:00
|
|
|
vim.api.nvim_err_writeln("Invalid command specification.")
|
2023-11-23 12:09:45 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-03-05 02:55:49 +00:00
|
|
|
return parts[1], { unpack(parts, 2, #parts) }
|
2023-11-23 12:09:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Adaptation of https://github.com/mfussenegger/nvim-dap/blob/e154fdb6d70b3765d71f296e718b29d8b7026a63/lua/dap.lua#L413.
|
|
|
|
local function select_config_and_run()
|
2024-03-05 03:10:57 +00:00
|
|
|
local filetype = vim.api.nvim_buf_get_option(0, "filetype")
|
2023-11-23 12:09:45 +00:00
|
|
|
local configs = dap.configurations[filetype] or {}
|
|
|
|
assert(
|
|
|
|
vim.tbl_islist(configs),
|
|
|
|
string.format(
|
2024-03-05 03:10:57 +00:00
|
|
|
"`dap.configurations.%s` must be a list of configurations, got %s",
|
2023-11-23 12:09:45 +00:00
|
|
|
filetype,
|
|
|
|
vim.inspect(configs)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if not configs[1] then
|
2024-03-05 03:10:57 +00:00
|
|
|
local msg = "No configuration found for `%s`. You need to add configs " ..
|
|
|
|
"to `dap.configurations.%s` (See `:h dap-configuration`)"
|
2023-11-23 12:09:45 +00:00
|
|
|
vim.api.nvim_err_writeln(string.format(msg, filetype, filetype))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local opts = {}
|
|
|
|
opts.filetype = opts.filetype or filetype
|
|
|
|
|
|
|
|
dap_ui.pick_if_many(
|
|
|
|
configs,
|
2024-03-05 03:10:57 +00:00
|
|
|
"Configuration: ",
|
2024-03-05 02:55:49 +00:00
|
|
|
function(c) -- Label function
|
2023-11-23 12:09:45 +00:00
|
|
|
return c.name
|
|
|
|
end,
|
2024-03-05 02:55:49 +00:00
|
|
|
function(c) -- Callback
|
2023-11-23 12:09:45 +00:00
|
|
|
if not c then
|
2024-03-05 03:10:57 +00:00
|
|
|
vim.api.nvim_err_writeln("No configuration selected.")
|
2023-11-23 12:09:45 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local copy = vim.deepcopy(c)
|
2024-03-05 03:10:57 +00:00
|
|
|
if copy.request == "launch" then
|
2023-11-23 12:09:45 +00:00
|
|
|
local program, args = query_launch()
|
|
|
|
copy.program = program
|
|
|
|
copy.args = args
|
|
|
|
end
|
|
|
|
dap.run(copy, opts)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Setup buffer-local DAP mappings. This function is expected to be called in
|
|
|
|
-- a filetype plugin, e.g. `nvim/after/ftplugin/c.lua`.
|
2023-11-17 22:02:07 +00:00
|
|
|
function M.buffer_map()
|
2024-03-05 02:55:49 +00:00
|
|
|
local function sidebar_new(widget)
|
|
|
|
return dap_ui_widgets.sidebar(widget, { width = 32 }, '')
|
2023-11-17 22:02:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local sidebars = {
|
|
|
|
frames = sidebar_new(dap_ui_widgets.frames),
|
|
|
|
scopes = sidebar_new(dap_ui_widgets.scopes),
|
|
|
|
threads = sidebar_new(dap_ui_widgets.threads),
|
|
|
|
}
|
|
|
|
|
2024-03-05 02:55:49 +00:00
|
|
|
local function sidebar_is_open(sb)
|
|
|
|
return sb.win and vim.api.nvim_win_is_valid(sb.win)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sidebar_any_open()
|
2023-11-17 22:02:07 +00:00
|
|
|
for _, sb in pairs(sidebars) do
|
|
|
|
if sidebar_is_open(sb) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2024-03-05 02:55:49 +00:00
|
|
|
local function sidebar_toggle(sidebar)
|
2023-11-17 22:02:07 +00:00
|
|
|
if sidebar_is_open(sidebar) then
|
|
|
|
sidebar.close({ mode = 'toggle' })
|
|
|
|
else
|
|
|
|
local win_id = vim.fn.win_getid()
|
2024-03-05 02:55:49 +00:00
|
|
|
vim.cmd.wincmd('t') -- Move to topleft-most window.
|
2024-03-05 03:10:57 +00:00
|
|
|
vim.cmd(sidebar_any_open() and "leftabove split" or "vertical topleft split")
|
2023-11-17 22:02:07 +00:00
|
|
|
sidebar.open()
|
|
|
|
vim.fn.win_gotoid(win_id)
|
2023-11-23 14:04:20 +00:00
|
|
|
-- Update state of windows.
|
2024-03-05 03:10:57 +00:00
|
|
|
vim.api.nvim_win_set_option(sidebar.win, "colorcolumn", "")
|
|
|
|
vim.api.nvim_win_set_option(sidebar.win, "list", false)
|
|
|
|
vim.api.nvim_win_set_option(sidebar.win, "wrap", false)
|
|
|
|
vim.api.nvim_win_set_option(sidebar.win, "winfixwidth", true)
|
2024-03-05 02:55:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sidebar_only(sidebar)
|
|
|
|
for _, sb in pairs(sidebars) do
|
|
|
|
if sb ~= sidebar and sidebar_is_open(sb) then
|
2024-03-05 03:10:57 +00:00
|
|
|
sb.close({ mode = "toggle" })
|
2024-03-05 02:55:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if not sidebar_is_open(sidebar) then
|
|
|
|
sidebar_toggle(sidebar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function find_bufnr_by_pattern(pattern)
|
|
|
|
for _, bufnr in pairs(vim.api.nvim_list_bufs()) do
|
|
|
|
if vim.fn.bufname(bufnr):match(pattern) then
|
|
|
|
return bufnr
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_bufnr_open(bufnr)
|
|
|
|
if not bufnr then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local windows = vim.fn.win_findbuf(bufnr)
|
|
|
|
for _, _ in pairs(windows) do
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function repl_is_open()
|
|
|
|
return is_bufnr_open(find_bufnr_by_pattern("^%[dap%-repl]"))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function term_is_open()
|
|
|
|
return is_bufnr_open(find_bufnr_by_pattern("^%[dap%-terminal]"))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function repl_open(opts)
|
|
|
|
if repl_is_open() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local height = opts.height or 10
|
|
|
|
local win_id = vim.fn.win_getid()
|
|
|
|
vim.cmd.wincmd('b') -- Move to bottomright-most window.
|
|
|
|
dap.repl.open({}, term_is_open() and
|
2024-03-05 22:49:25 +00:00
|
|
|
string.format("vertical rightbelow %dsplit", height) or
|
2024-03-05 03:10:57 +00:00
|
|
|
string.format("rightbelow %dsplit", height))
|
|
|
|
vim.api.nvim_win_set_option(0, "winfixheight", true)
|
2024-03-05 02:55:49 +00:00
|
|
|
vim.fn.win_gotoid(win_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function term_open(opts)
|
|
|
|
if term_is_open() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local height = opts.height or 10
|
|
|
|
local win_id = vim.fn.win_getid()
|
|
|
|
vim.cmd.wincmd('b') -- Move to bottomright-most window.
|
|
|
|
vim.cmd(repl_is_open() and
|
2024-03-05 22:49:25 +00:00
|
|
|
string.format("vertical rightbelow %dsplit", height) or
|
2024-03-05 03:10:57 +00:00
|
|
|
string.format("rightbelow %dsplit", height))
|
|
|
|
vim.api.nvim_win_set_option(0, "winfixheight", true)
|
2024-03-05 02:55:49 +00:00
|
|
|
vim.api.nvim_win_set_buf(0, find_bufnr_by_pattern("^%[dap%-terminal]"))
|
|
|
|
vim.fn.win_gotoid(win_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function repl_close()
|
|
|
|
dap.repl.close()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function term_close()
|
|
|
|
if not term_is_open() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local bufnr = find_bufnr_by_pattern("^%[dap%-terminal]")
|
|
|
|
local windows = vim.fn.win_findbuf(bufnr)
|
|
|
|
for _, win in pairs(windows) do
|
|
|
|
vim.api.nvim_win_close(win, --[[force=]] true)
|
2023-11-17 22:02:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-05 02:55:49 +00:00
|
|
|
local function repl_toggle(opts)
|
|
|
|
if repl_is_open() then repl_close() else repl_open(opts) end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function term_toggle(opts)
|
|
|
|
if term_is_open() then term_close() else term_open(opts) end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_nnoremap(key, func)
|
2024-03-05 03:10:57 +00:00
|
|
|
local input = string.format("<localleader>%s", key)
|
|
|
|
vim.keymap.set("n", input, func, { buffer = true })
|
2024-03-05 02:55:49 +00:00
|
|
|
end
|
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
set_nnoremap("<localleader>", select_config_and_run)
|
|
|
|
set_nnoremap("b", dap.toggle_breakpoint)
|
|
|
|
set_nnoremap("c", function()
|
|
|
|
if dap.status() == "" then
|
|
|
|
vim.api.nvim_err_writeln("No active session.")
|
2023-11-23 12:09:45 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
dap.continue()
|
|
|
|
end)
|
2024-03-05 02:55:49 +00:00
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
set_nnoremap("d", dap.down)
|
|
|
|
set_nnoremap("i", dap.step_into)
|
|
|
|
set_nnoremap("n", dap.step_over)
|
|
|
|
set_nnoremap("o", dap.step_out)
|
|
|
|
set_nnoremap("q", dap.close)
|
|
|
|
set_nnoremap("r", dap.run_to_cursor)
|
|
|
|
set_nnoremap("u", dap.up)
|
|
|
|
set_nnoremap("x", dap.clear_breakpoints)
|
|
|
|
|
|
|
|
set_nnoremap("wf", function() sidebar_toggle(sidebars.frames) end)
|
|
|
|
set_nnoremap("wh", function() sidebar_toggle(sidebars.threads) end)
|
|
|
|
set_nnoremap("wr", function() repl_toggle({ height = 10 }) end)
|
|
|
|
set_nnoremap("ws", function() sidebar_toggle(sidebars.scopes) end)
|
|
|
|
set_nnoremap("wt", function() term_toggle({ height = 10 }) end)
|
|
|
|
|
|
|
|
set_nnoremap("wF", function() sidebar_only(sidebars.frames) end)
|
|
|
|
set_nnoremap("wH", function() sidebar_only(sidebars.threads) end)
|
|
|
|
set_nnoremap("wR", function()
|
2024-03-05 02:55:49 +00:00
|
|
|
term_close()
|
|
|
|
repl_open({ height = 10 })
|
2023-11-17 22:02:07 +00:00
|
|
|
end)
|
2024-03-05 03:10:57 +00:00
|
|
|
set_nnoremap("wS", function() sidebar_only(sidebars.scopes) end)
|
|
|
|
set_nnoremap("wT", function()
|
2024-03-05 02:55:49 +00:00
|
|
|
repl_close()
|
|
|
|
term_open({ height = 10 })
|
2023-11-17 22:02:07 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|