diff --git a/.github/workflows/lint_code.yml b/.github/workflows/lint_code.yml index 2eff9436d..622cbbd4c 100644 --- a/.github/workflows/lint_code.yml +++ b/.github/workflows/lint_code.yml @@ -7,4 +7,4 @@ jobs: - uses: actions/checkout@v3 - uses: lunarmodules/luacheck@v1 with: - args: . --std luajit --globals vim _toggle_lazygit _command_panel --max-line-length 150 --no-config + args: . --std luajit --globals vim _toggle_lazygit _command_panel _debugging --max-line-length 150 --no-config diff --git a/lazy-lock.json b/lazy-lock.json index 72c4fb5ea..2dd673215 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -125,7 +125,7 @@ }, "gitsigns.nvim": { "branch": "main", - "commit": "11b80e7eea249affc8776483272bcfc627b5552a" + "commit": "5d73da785a3c05fd63ac31769079db05169a6ec7" }, "hop.nvim": { "branch": "master", @@ -145,11 +145,11 @@ }, "lsp_signature.nvim": { "branch": "master", - "commit": "58d4e810801da74c29313da86075d6aea537501f" + "commit": "04f0c52c6a04680f29bdecb5b2943591b8c0ff43" }, "lspsaga.nvim": { "branch": "main", - "commit": "23a481f0b142f4498e2fc2dd80e7a404079833e6" + "commit": "557e6fd7fbca325a4bea582576fe36bec903367c" }, "lualine.nvim": { "branch": "master", @@ -205,7 +205,7 @@ }, "nvim-dap": { "branch": "master", - "commit": "0a02ed103e4da1928dc88428248dec7bb5ee6f34" + "commit": "2f28ea843bcdb378b171a66ddcd568516e431d55" }, "nvim-dap-ui": { "branch": "master", @@ -233,7 +233,7 @@ }, "nvim-treesitter": { "branch": "master", - "commit": "736a672afe852dbad4199002c89b7461b285c47f" + "commit": "6aababfca4e3d72cb1e8fbbdac4115a5f88107d0" }, "nvim-treesitter-context": { "branch": "master", @@ -249,7 +249,7 @@ }, "nvim-ts-context-commentstring": { "branch": "main", - "commit": "7f625207f225eea97ef7a6abe7611e556c396d2f" + "commit": "e9062e2dfb9854e6a927370f2d720de354c88524" }, "nvim-web-devicons": { "branch": "master", @@ -257,7 +257,7 @@ }, "obsidian.nvim": { "branch": "main", - "commit": "1534187b4e3eee40053918388460c739d2b76da4" + "commit": "ed7ace4823a57eeea1d296e86861c57eefe292bb" }, "paint.nvim": { "branch": "main", @@ -277,7 +277,7 @@ }, "rainbow-delimiters.nvim": { "branch": "master", - "commit": "5db37600becf689d78dbf066545b0de4d6f0ab94" + "commit": "ec7ade261f961a5299a13451aa18a4dce630d0b3" }, "rust-tools.nvim": { "branch": "master", @@ -289,7 +289,7 @@ }, "sniprun": { "branch": "master", - "commit": "6f9025bf475799bc0fbe8b338b3912f4a701ff81" + "commit": "1f698c5d26afa356f548d4be5a0deb389d0bb387" }, "specs.nvim": { "branch": "main", @@ -333,7 +333,7 @@ }, "telescope.nvim": { "branch": "master", - "commit": "1228f3b15ca3d9b95dcb92efda6a3448871030bd" + "commit": "22735947d84d78f29436c203f0a4b8dc15dda204" }, "toggleterm.nvim": { "branch": "main", @@ -365,7 +365,7 @@ }, "vim-matchup": { "branch": "master", - "commit": "ca8971739e3908973c5912cb3a0ffb6cf7bd83e7" + "commit": "0646093e07b1f89b16536cfb3dc4993e3f0ac653" }, "vim-pencil": { "branch": "master", diff --git a/lua/core/event.lua b/lua/core/event.lua index ce2f26076..10f421866 100644 --- a/lua/core/event.lua +++ b/lua/core/event.lua @@ -15,7 +15,7 @@ end local mapping = require("keymap.completion") vim.api.nvim_create_autocmd("LspAttach", { - group = vim.api.nvim_create_augroup("UserLspConfig", {}), + group = vim.api.nvim_create_augroup("LspKeymapLoader", { clear = true }), callback = function(event) mapping.lsp(event.buf) end, diff --git a/lua/keymap/tool.lua b/lua/keymap/tool.lua index 08b2de2bc..60cee05f2 100644 --- a/lua/keymap/tool.lua +++ b/lua/keymap/tool.lua @@ -149,7 +149,6 @@ local plug_map = { :with_desc("debug: Run/Continue"), ["n|"] = map_callback(function() require("dap").terminate() - require("dapui").close() end) :with_noremap() :with_silent() diff --git a/lua/modules/configs/completion/lspsaga.lua b/lua/modules/configs/completion/lspsaga.lua index 0e8d7e967..d57339333 100644 --- a/lua/modules/configs/completion/lspsaga.lua +++ b/lua/modules/configs/completion/lspsaga.lua @@ -26,38 +26,31 @@ return function() set_sidebar_icons() require("lspsaga").setup({ - scroll_preview = { - scroll_down = "", - scroll_up = "", + -- Breadcrumbs: https://dev.neovim.pro/lspsaga/breadcrumbs/ + symbol_in_winbar = { + enable = true, + separator = " " .. icons.ui.Separator, + hide_keyword = false, + show_file = false, + folder_level = 1, + color_mode = true, + delay = 100, }, - request_timeout = 3000, - finder = { - silent = true, - default = "def+ref+imp", + -- https://dev.neovim.pro/lspsaga/callhierarchy/ + callhierarchy = { layout = "float", - filter = {}, keys = { - shuttle = "[]", - toggle_or_open = "", - jump_to = "e", + edit = "e", vsplit = "v", split = "s", tabe = "t", - tabnew = "n", quit = "q", + shuttle = "[]", + toggle_or_req = "u", close = "", }, }, - definition = { - keys = { - edit = "o", - vsplit = "v", - split = "s", - tabe = "t", - close = "q", - quit = "q", - }, - }, + -- https://dev.neovim.pro/lspsaga/codeaction/ code_action = { num_shortcut = true, show_server_name = true, @@ -67,22 +60,33 @@ return function() exec = "", }, }, - lightbulb = { - enable = false, - sign = true, - sign_priority = 20, - virtual_text = false, + -- https://dev.neovim.pro/lspsaga/definition/ + definition = { + width = 0.6, + height = 0.5, + keys = { + edit = "o", + vsplit = "v", + split = "s", + tabe = "t", + quit = "q", + close = "q", + }, }, + -- https://dev.neovim.pro/lspsaga/diagnostic/ diagnostic = { + show_code_action = true, + jump_num_shortcut = true, max_width = 0.5, max_height = 0.6, text_hl_follow = true, - show_code_action = true, border_follow = true, - diagnostic_only_current = false, - extend_relatedInformation = false, - jump_num_shortcut = true, + extend_relatedInformation = true, show_layout = "float", + show_normal_height = 10, + max_show_width = 0.9, + max_show_height = 0.6, + diagnostic_only_current = false, keys = { exec_action = "r", quit = "q", @@ -90,72 +94,93 @@ return function() quit_in_show = { "q", "" }, }, }, - rename = { - in_select = false, - auto_save = false, + -- https://dev.neovim.pro/lspsaga/finder/ + finder = { + max_height = 0.5, + left_width = 0.3, + right_width = 0.3, + default = "def+ref+imp", + methods = {}, + layout = "float", + filter = {}, + silent = false, keys = { - quit = "", - select = "x", - exec = "", + shuttle = "[]", + toggle_or_open = "", + vsplit = "v", + split = "s", + tabe = "t", + tabnew = "n", + quit = "q", + close = "", }, }, + -- https://dev.neovim.pro/lspsaga/hover/ hover = { max_width = 0.3, max_height = 0.7, open_link = "gl", - open_browser = "silent !" .. require("core.settings").external_browser, + open_cmd = "silent !" .. require("core.settings").external_browser, + }, + -- https://dev.neovim.pro/lspsaga/implement/ + implement = { + enable = true, + sign = true, + virtual_text = false, + priority = 100, + }, + -- https://dev.neovim.pro/lspsaga/lightbulb/ + lightbulb = { + enable = false, + sign = true, + virtual_text = false, + debounce = 10, + sign_priority = 20, }, + -- https://dev.neovim.pro/lspsaga/outline/ outline = { win_position = "right", win_width = 30, auto_preview = false, + detail = false, auto_close = true, close_after_jump = true, - detail = false, layout = "normal", + max_height = 0.5, + left_width = 0.3, keys = { toggle_or_jump = "", - jump = "o", quit = "q", + jump = "o", }, }, - symbol_in_winbar = { - enable = true, - separator = " " .. icons.ui.Separator, - hide_keyword = false, - show_file = false, - color_mode = true, - }, - implement = { - enable = true, - sign = true, - virtual_text = false, - }, - callhierarchy = { - layout = "float", + -- https://dev.neovim.pro/lspsaga/rename/ + rename = { + in_select = false, + auto_save = false, + project_max_width = 0.5, + project_max_height = 0.5, keys = { - edit = "e", - vsplit = "v", - split = "s", - tabe = "t", - quit = "q", - shuttle = "[]", - toggle_or_req = "u", - close = "", + quit = "", + exec = "", + select = "x", }, }, + -- https://dev.neovim.pro/lspsaga/misc/#beacon beacon = { enable = true, frequency = 12, }, + -- https://dev.neovim.pro/lspsaga/misc/#generic-ui-options ui = { - title = true, - devicon = true, border = "single", -- Can be single, double, rounded, solid, shadow. - actionfix = icons.ui.Spell, + devicon = true, + title = true, expand = icons.ui.ArrowClosed, collapse = icons.ui.ArrowOpen, code_action = icons.ui.CodeAction, + actionfix = icons.ui.Spell, + lines = { "┗", "┣", "┃", "━", "┏" }, imp_sign = icons.kind.Implementation, kind = { -- Kind @@ -198,5 +223,10 @@ return function() Value = { icons.kind.Value, "LspKindValue" }, }, }, + -- https://dev.neovim.pro/lspsaga/misc/#scrolling-keymaps + scroll_preview = { + scroll_down = "", + scroll_up = "", + }, }) end diff --git a/lua/modules/configs/editor/rainbow_delims.lua b/lua/modules/configs/editor/rainbow_delims.lua index 037a81397..cb58af559 100644 --- a/lua/modules/configs/editor/rainbow_delims.lua +++ b/lua/modules/configs/editor/rainbow_delims.lua @@ -10,7 +10,7 @@ return function() if errors < 0 then return nil end - return (check_lines and vim.fn.line("$") > 450) and require("rainbow-delimiters").strategy["global"] + return (check_lines and vim.fn.line("$") > 350) and require("rainbow-delimiters").strategy["global"] or require("rainbow-delimiters").strategy["local"] end end @@ -20,6 +20,8 @@ return function() [""] = init_strategy(false), c = init_strategy(true), cpp = init_strategy(true), + vimdoc = init_strategy(true), + vim = init_strategy(true), }, query = { [""] = "rainbow-delimiters", diff --git a/lua/modules/configs/editor/vimtex.lua b/lua/modules/configs/editor/vimtex.lua index b6a21daa3..e50adbf15 100644 --- a/lua/modules/configs/editor/vimtex.lua +++ b/lua/modules/configs/editor/vimtex.lua @@ -1,8 +1,9 @@ return function() - vim.g.vimtex_view_method = "zathura" + vim.g.vimtex_view_method = "skim" vim.g.vimtex_compiler_latexmk_engines = { _ = "-xelatex", } vim.g.tex_comment_nospell = 1 vim.g.vimtex_compiler_progname = "nvr" + vim.g.vimtex_view_skim_sync = 1 end diff --git a/lua/modules/configs/editor/wrapping.lua b/lua/modules/configs/editor/wrapping.lua new file mode 100644 index 000000000..80796a165 --- /dev/null +++ b/lua/modules/configs/editor/wrapping.lua @@ -0,0 +1,5 @@ +return function() + require("wrapping").setup({ + softener = { latex = 0.8 }, + }) +end diff --git a/lua/modules/configs/tool/dap/clients/codelldb.lua b/lua/modules/configs/tool/dap/clients/codelldb.lua index 44eea8237..64124c617 100644 --- a/lua/modules/configs/tool/dap/clients/codelldb.lua +++ b/lua/modules/configs/tool/dap/clients/codelldb.lua @@ -15,13 +15,31 @@ return function() } dap.configurations.c = { { - name = "Launch the debugger", + name = "Debug", + type = "codelldb", + request = "launch", + program = utils.input_exec_path(), + cwd = "${workspaceFolder}", + stopOnEntry = false, + terminal = "integrated", + }, + { + name = "Debug (with args)", type = "codelldb", request = "launch", program = utils.input_exec_path(), args = utils.input_args(), cwd = "${workspaceFolder}", stopOnEntry = false, + terminal = "integrated", + }, + { + name = "Attach to a running process", + type = "codelldb", + request = "attach", + program = utils.input_exec_path(), + stopOnEntry = false, + waitFor = true, }, } dap.configurations.cpp = dap.configurations.c diff --git a/lua/modules/configs/tool/dap/clients/delve.lua b/lua/modules/configs/tool/dap/clients/delve.lua index 719d5950a..6f023445e 100644 --- a/lua/modules/configs/tool/dap/clients/delve.lua +++ b/lua/modules/configs/tool/dap/clients/delve.lua @@ -1,66 +1,100 @@ --- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#go-using-delve-directly --- https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_dap.md +-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#go +-- https://github.com/golang/vscode-go/blob/master/docs/debugging.md return function() local dap = require("dap") local utils = require("modules.utils.dap") - dap.adapters.go = function(callback) - local stdout = vim.loop.new_pipe(false) - local handle - local pid_or_err - local port = 28695 - local opts = { - stdio = { nil, stdout }, - args = { "dap", "-l", "127.0.0.1:" .. port }, - detached = true, - } - handle, pid_or_err = vim.loop.spawn(vim.fn.exepath("dlv"), opts, function(code) - stdout:close() - handle:close() - if code ~= 0 then - vim.notify( - string.format('"dlv" exited with code: %d, please check your configs for correctness.', code), - vim.log.levels.WARN, - { title = "[go] DAP Warning!" } - ) - 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) - -- Wait for delve to start - vim.defer_fn(function() - callback({ type = "server", host = "127.0.0.1", port = port }) - end, 100) + if not require("mason-registry").is_installed("go-debug-adapter") then + vim.notify( + "Automatically installing `go-debug-adapter` for go debugging", + vim.log.levels.INFO, + { title = "nvim-dap" } + ) + + local go_dbg = require("mason-registry").get_package("go-debug-adapter") + go_dbg:install():once( + "closed", + vim.schedule_wrap(function() + if go_dbg:is_installed() then + vim.notify("Successfully installed `go-debug-adapter`", vim.log.levels.INFO, { title = "nvim-dap" }) + end + end) + ) end + + dap.adapters.go = { + type = "executable", + command = "node", + args = { + require("mason-registry").get_package("go-debug-adapter"):get_install_path() + .. "/extension/dist/debugAdapter.js", + }, + } dap.configurations.go = { - { type = "go", name = "Debug", request = "launch", program = "${file}" }, { type = "go", - name = "Debug with args", + name = "Debug (file)", + request = "launch", + cwd = "${workspaceFolder}", + program = utils.input_file_path(), + console = "integratedTerminal", + dlvToolPath = vim.fn.exepath("dlv"), + showLog = true, + showRegisters = true, + stopOnEntry = false, + }, + { + type = "go", + name = "Debug (file with args)", request = "launch", - program = "${file}", + cwd = "${workspaceFolder}", + program = utils.input_file_path(), args = utils.input_args(), + console = "integratedTerminal", + dlvToolPath = vim.fn.exepath("dlv"), + showLog = true, + showRegisters = true, + stopOnEntry = false, }, { type = "go", - name = "Debug test", -- configuration for debugging test files + name = "Debug (executable)", request = "launch", - mode = "test", - program = "${file}", - }, -- works with go.mod packages and sub packages + cwd = "${workspaceFolder}", + program = utils.input_exec_path(), + args = utils.input_args(), + console = "integratedTerminal", + dlvToolPath = vim.fn.exepath("dlv"), + mode = "exec", + showLog = true, + showRegisters = true, + stopOnEntry = false, + }, { type = "go", - name = "Debug test (go.mod)", + name = "Debug (test file)", request = "launch", + cwd = "${workspaceFolder}", + program = utils.input_file_path(), + console = "integratedTerminal", + dlvToolPath = vim.fn.exepath("dlv"), mode = "test", + showLog = true, + showRegisters = true, + stopOnEntry = false, + }, + { + type = "go", + name = "Debug (using go.mod)", + request = "launch", + cwd = "${workspaceFolder}", program = "./${relativeFileDirname}", + console = "integratedTerminal", + dlvToolPath = vim.fn.exepath("dlv"), + mode = "test", + showLog = true, + showRegisters = true, + stopOnEntry = false, }, } end diff --git a/lua/modules/configs/tool/dap/clients/lldb.lua b/lua/modules/configs/tool/dap/clients/lldb.lua index 2064b85c6..8461c77a1 100644 --- a/lua/modules/configs/tool/dap/clients/lldb.lua +++ b/lua/modules/configs/tool/dap/clients/lldb.lua @@ -5,8 +5,7 @@ return function() dap.adapters.lldb = { type = "executable", - command = "lldb-vscode", - name = "lldb", + command = vim.fn.exepath("lldb-vscode"), -- Find lldb-vscode on $PATH } dap.configurations.c = { { diff --git a/lua/modules/configs/tool/dap/clients/python.lua b/lua/modules/configs/tool/dap/clients/python.lua index cbb46ed98..29fda2a0f 100644 --- a/lua/modules/configs/tool/dap/clients/python.lua +++ b/lua/modules/configs/tool/dap/clients/python.lua @@ -1,7 +1,9 @@ -- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#python +-- https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings return function() local dap = require("dap") local debugpy = vim.fn.exepath("debugpy-adapter") + local utils = require("modules.utils.dap") local function is_empty(s) return s == nil or s == "" @@ -9,9 +11,7 @@ return function() dap.adapters.python = function(callback, config) if config.request == "attach" then - ---@diagnostic disable-next-line: undefined-field local port = (config.connect or config).port - ---@diagnostic disable-next-line: undefined-field local host = (config.connect or config).host or "127.0.0.1" callback({ type = "server", @@ -32,9 +32,10 @@ return function() -- The first three options are required by nvim-dap type = "python", -- the type here established the link to the adapter definition: `dap.adapters.python` request = "launch", - name = "Launch file", + name = "Debug", -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options - program = "${file}", -- This configuration will launch the current file if used. + console = "integratedTerminal", + program = utils.input_file_path(), pythonPath = function() if not is_empty(vim.env.CONDA_PREFIX) then return vim.env.CONDA_PREFIX .. "/bin/python" @@ -43,22 +44,26 @@ return function() end end, }, + { + -- NOTE: This setting is for people using venv + type = "python", + request = "launch", + name = "Debug (using venv)", + -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options + console = "integratedTerminal", + program = utils.input_file_path(), + pythonPath = function() + local cwd, venv = vim.fn.getcwd(), os.getenv("VIRTUAL_ENV") + if venv and vim.fn.executable(venv .. "/bin/python") == 1 then + return venv .. "/bin/python" + elseif vim.fn.executable(cwd .. "/venv/bin/python") == 1 then + return cwd .. "/venv/bin/python" + elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then + return cwd .. "/.venv/bin/python" + else + return "python3" + end + end, + }, } - - -- NOTE: This setting is for people using venv - -- pythonPath = function() - -- -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself. - -- -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within. - -- -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable. - -- local cwd, venv = vim.fn.getcwd(), os.getenv("VIRTUAL_ENV") - -- if venv and vim.fn.executable(venv .. "/bin/python") == 1 then - -- return venv .. "/bin/python" - -- elseif vim.fn.executable(cwd .. "/venv/bin/python") == 1 then - -- return cwd .. "/venv/bin/python" - -- elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then - -- return cwd .. "/.venv/bin/python" - -- else - -- return "python3" - -- end - -- end, end diff --git a/lua/modules/configs/tool/dap/dap-keymap.lua b/lua/modules/configs/tool/dap/dap-keymap.lua new file mode 100644 index 000000000..bdaf08d32 --- /dev/null +++ b/lua/modules/configs/tool/dap/dap-keymap.lua @@ -0,0 +1,21 @@ +local M = {} + +local bind = require("keymap.bind") +local map_cmd = bind.map_cmd + +local did_load_debug_mappings = false +local debug_keymap = { + ["nv|K"] = map_cmd("lua require('dapui').eval()") + :with_noremap() + :with_nowait() + :with_desc("Evaluate expression under cursor"), +} + +function M.load_extras() + if not did_load_debug_mappings then + require("modules.utils.keymap").amend("Debugging", "_debugging", debug_keymap) + did_load_debug_mappings = true + end +end + +return M diff --git a/lua/modules/configs/tool/dap/dapui.lua b/lua/modules/configs/tool/dap/dapui.lua index 13896e2d8..b588f0816 100644 --- a/lua/modules/configs/tool/dap/dapui.lua +++ b/lua/modules/configs/tool/dap/dapui.lua @@ -5,14 +5,20 @@ return function() } require("dapui").setup({ - icons = { expanded = icons.ui.ArrowOpen, collapsed = icons.ui.ArrowClosed, current_frame = icons.ui.Indicator }, + force_buffers = true, + icons = { + expanded = icons.ui.ArrowOpen, + collapsed = icons.ui.ArrowClosed, + current_frame = icons.ui.Indicator, + }, mappings = { -- Use a table to apply multiple mappings + edit = "e", expand = { "", "<2-LeftMouse>" }, open = "o", remove = "d", - edit = "e", repl = "r", + toggle = "t", }, layouts = { { @@ -20,18 +26,24 @@ return function() -- Provide as ID strings or tables with "id" and "size" keys { id = "scopes", - size = 0.25, -- Can be float or integer > 1 + size = 0.3, -- Can be float or integer > 1 }, - { id = "breakpoints", size = 0.25 }, - { id = "stacks", size = 0.25 }, - { id = "watches", size = 0.25 }, + { id = "watches", size = 0.3 }, + { id = "stacks", size = 0.3 }, + { id = "breakpoints", size = 0.1 }, + }, + size = 0.3, + position = "right", + }, + { + elements = { + { id = "console", size = 0.55 }, + { id = "repl", size = 0.45 }, }, - size = 40, - position = "left", + position = "bottom", + size = 0.25, }, - { elements = { "repl" }, size = 10, position = "bottom" }, }, - -- Requires Nvim version >= 0.8 controls = { enabled = true, -- Display controls in this session @@ -48,10 +60,13 @@ return function() }, }, floating = { - max_height = nil, - max_width = nil, - mappings = { close = { "q", "" } }, + max_height = nil, -- These can be integers or a float between 0 and 1. + max_width = nil, -- Floats will be treated as percentage of your screen. + border = "single", -- Border style. Can be "single", "double" or "rounded" + mappings = { + close = { "q", "" }, + }, }, - windows = { indent = 1 }, + render = { indent = 1, max_value_lines = 85 }, }) end diff --git a/lua/modules/configs/tool/dap/init.lua b/lua/modules/configs/tool/dap/init.lua index 78b76b0ba..8fff15a4e 100644 --- a/lua/modules/configs/tool/dap/init.lua +++ b/lua/modules/configs/tool/dap/init.lua @@ -5,16 +5,25 @@ return function() local icons = { dap = require("modules.utils.icons").get("dap") } local colors = require("modules.utils").get_palette() + local mappings = require("tool.dap.dap-keymap") - dap.listeners.after.event_initialized["dapui_config"] = function() - dapui.open() + -- Initialize debug hooks + _G._debugging = false + local function debug_init_cb() + _G._debugging = true + mappings.load_extras() + dapui.open({ reset = true }) end - dap.listeners.after.event_terminated["dapui_config"] = function() - dapui.close() - end - dap.listeners.after.event_exited["dapui_config"] = function() - dapui.close() + local function debug_terminate_cb() + if _debugging then + _G._debugging = false + dapui.close() + end end + dap.listeners.after.event_initialized["dapui_config"] = debug_init_cb + dap.listeners.before.event_terminated["dapui_config"] = debug_terminate_cb + dap.listeners.before.event_exited["dapui_config"] = debug_terminate_cb + dap.listeners.before.disconnect["dapui_config"] = debug_terminate_cb -- We need to override nvim-dap's default highlight groups, AFTER requiring nvim-dap for catppuccin. vim.api.nvim_set_hl(0, "DapStopped", { fg = colors.green }) diff --git a/lua/modules/plugins/editor.lua b/lua/modules/plugins/editor.lua index fd7b4eb22..b87866162 100644 --- a/lua/modules/plugins/editor.lua +++ b/lua/modules/plugins/editor.lua @@ -122,5 +122,8 @@ editor["lervag/vimtex"] = { config = require("editor.vimtex"), ft = "tex", } +-- editor["andrewferrier/wrapping.nvim"] = { +-- config = require("editor.wrapping"), +-- } return editor diff --git a/lua/modules/utils/dap.lua b/lua/modules/utils/dap.lua index 7e61d8da5..0ea276c7c 100644 --- a/lua/modules/utils/dap.lua +++ b/lua/modules/utils/dap.lua @@ -9,6 +9,10 @@ function M.input_exec_path() return vim.fn.input('Path to executable (default to "a.out"): ', vim.fn.expand("%:p:h") .. "/a.out", "file") end +function M.input_file_path() + return vim.fn.input("Path to debuggee (default to the current file): ", vim.fn.expand("%:p"), "file") +end + function M.get_env() local variables = {} for k, v in pairs(vim.fn.environ()) do diff --git a/lua/modules/utils/keymap.lua b/lua/modules/utils/keymap.lua new file mode 100644 index 000000000..033e1686a --- /dev/null +++ b/lua/modules/utils/keymap.lua @@ -0,0 +1,150 @@ +local M = {} + +---Shortcut for `nvim_replace_termcodes`. +---@param keys string +---@return string +local function termcodes(keys) + return vim.api.nvim_replace_termcodes(keys, true, true, true) +end + +---Returns if two key sequence are equal or not. +---@param a string +---@param b string +---@return boolean +local function keymap_equals(a, b) + return termcodes(a) == termcodes(b) +end + +---Get map +---@param mode string +---@param lhs string +---@return table +local function get_map(mode, lhs) + for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, mode)) do + if keymap_equals(map.lhs, lhs) then + return { + lhs = map.lhs, + rhs = map.rhs or "", + expr = map.expr == 1, + callback = map.callback, + noremap = map.noremap == 1, + script = map.script == 1, + silent = map.silent == 1, + nowait = map.nowait == 1, + buffer = true, + } + end + end + + for _, map in ipairs(vim.api.nvim_get_keymap(mode)) do + if keymap_equals(map.lhs, lhs) then + return { + lhs = map.lhs, + rhs = map.rhs or "", + expr = map.expr == 1, + callback = map.callback, + noremap = map.noremap == 1, + script = map.script == 1, + silent = map.silent == 1, + nowait = map.nowait == 1, + buffer = false, + } + end + end + + return { + lhs = lhs, + rhs = lhs, + expr = false, + callback = nil, + noremap = true, + script = false, + silent = true, + nowait = false, + buffer = false, + } +end + +---Returns the function constructed from the passed keymap object on call of +---which the original keymapping will be executed. +---@param map table keymap object +---@return function +local function get_fallback(map) + return function() + local keys, fmode + if map.expr then + if map.callback then + keys = map.callback() + else + keys = vim.api.nvim_eval(map.rhs) + end + elseif map.callback then + map.callback() + return + else + keys = map.rhs + end + keys = termcodes(keys) + fmode = map.noremap and "in" or "im" + vim.api.nvim_feedkeys(keys, fmode, false) + end +end + +---@param cond string +---@param mode string +---@param lhs string +---@param rhs function +---@param opts? table +local function amend(cond, mode, lhs, rhs, opts) + local map = get_map(mode, lhs) + local fallback = get_fallback(map) + local options = vim.deepcopy(opts) or {} + options.desc = table.concat({ + "[" .. cond, + (options.desc and ": " .. options.desc or ""), + "]", + (map.desc and " / " .. map.desc or ""), + }) + vim.keymap.set(mode, lhs, function() + rhs(fallback) + end, options) +end + +---Amend the existing keymap. +---@param cond string +---@param mode string | string[] +---@param lhs string +---@param rhs function +---@param opts? table +local function modes_amend(cond, mode, lhs, rhs, opts) + if type(mode) == "table" then + for _, m in ipairs(mode) do + amend(cond, m, lhs, rhs, opts) + end + else + amend(cond, mode, lhs, rhs, opts) + end +end + +---@param cond string +---@param global_flag string +---@param mapping table +function M.amend(cond, global_flag, mapping) + for key, value in pairs(mapping) do + local modes, keymap = key:match("([^|]*)|?(.*)") + if type(value) == "table" then + local rhs = value.cmd + local options = value.options + modes_amend(cond, vim.split(modes, ""), keymap, function(fallback) + if _G[global_flag] then + local fmode = options.noremap and "in" or "im" + vim.api.nvim_feedkeys(termcodes(rhs), fmode, false) + else + fallback() + end + end, options) + end + end +end + +return M