diff --git a/fnl/config/dap-breakpoints.fnl b/fnl/config/dap-breakpoints.fnl new file mode 100644 index 0000000..93254fb --- /dev/null +++ b/fnl/config/dap-breakpoints.fnl @@ -0,0 +1,66 @@ +;; Persist nvim-dap breakpoints across sessions, keyed by absolute file path. +;; Saved on toggle/set/clear (bp-save in plugins/init.fnl), on write and on exit; +;; restored when a file with saved breakpoints is read (which loads dap). + +(local path (.. (vim.fn.stdpath :state) "/dap-breakpoints.json")) + +(fn read-all [] + (let [f (io.open path :r)] + (if f + (let [text (f:read "*a") + _ (f:close) + (ok data) (pcall vim.json.decode text)] + (if (and ok (= (type data) :table)) data {})) + {}))) + +(fn write-all [data] + (let [f (io.open path :w)] + (when f + (f:write (vim.json.encode data)) + (f:close)))) + +(fn file-buf? [buf] + (and (vim.api.nvim_buf_is_loaded buf) + (= (. vim.bo buf :buftype) "") + (not= (vim.api.nvim_buf_get_name buf) ""))) + +(fn save [] + ;; Nothing can have changed until dap is loaded + (when (. package.loaded :dap) + (let [data (read-all) + by-buf ((. (require :dap.breakpoints) :get))] + ;; Loaded buffers are authoritative, even with no breakpoints left; + ;; files not open in this session keep what was saved for them + (each [_ buf (ipairs (vim.api.nvim_list_bufs))] + (when (file-buf? buf) + (let [bps (icollect [_ bp (ipairs (or (. by-buf buf) []))] + {:line bp.line + :condition bp.condition + :hitCondition bp.hitCondition + :logMessage bp.logMessage})] + (tset data (vim.api.nvim_buf_get_name buf) + (if (> (length bps) 0) bps nil))))) + (write-all data)))) + +(fn restore [buf] + (let [bps (. (read-all) (vim.api.nvim_buf_get_name buf))] + (when bps + ;; Loads the dap/dap-ui spec, running its config (sign definitions) + (require :dapui) + (let [breakpoints (require :dap.breakpoints) + last-line (vim.api.nvim_buf_line_count buf)] + (each [_ bp (ipairs bps)] + (when (<= bp.line last-line) + (breakpoints.set {:condition bp.condition + :hit_condition bp.hitCondition + :log_message bp.logMessage} + buf bp.line))))))) + +(fn setup [] + (let [group (vim.api.nvim_create_augroup :dap-breakpoints {:clear true})] + (vim.api.nvim_create_autocmd :BufReadPost + {: group :callback #(restore $.buf)}) + (vim.api.nvim_create_autocmd [:BufWritePost :VimLeavePre] + {: group :callback #(save)}))) + +{: setup : save : restore} diff --git a/fnl/config/dap-start.fnl b/fnl/config/dap-start.fnl new file mode 100644 index 0000000..30751f1 --- /dev/null +++ b/fnl/config/dap-start.fnl @@ -0,0 +1,134 @@ +;; : start/continue a dap session, honouring per-file comment directives +;; near the top of the buffer (first `scan-lines` lines), e.g. +;; +;; ;; debug-build: rust-mal -O0 --debug % -o %o +;; ;; debugger: gdb +;; +;; debug-build: shell command run (from the cwd) before a new session starts. +;; `%o` is the program (below); `%`, `%:p:r` etc. expand as in :! +;; (expandcmd). Failure aborts the session and puts the output in the +;; quickfix list. +;; debug-program: the executable to debug [default: %:p:r, the file without +;; its extension]. With this or debug-build, it is launched directly, with +;; no executable/args prompts. +;; debug-args: its arguments, split on whitespace [default: none]. +;; debugger: adapter type (gdb, lldb) of the configuration to launch +;; [default: the filetype's first, when the program is supplied; otherwise +;; dap's chooser]. +;; Read on every press, never trusted/cached: only applied when you hit . +;; +;; : run to cursor. With no session, starts one as does (build +;; included) that stops at the cursor line, skipping other breakpoints until +;; then, like dap's run_to_cursor. + +(local scan-lines 10) + +(fn directive [buf key] + (let [pat (.. (vim.pesc key) ":%s*(.-)%s*$")] + (accumulate [found nil + _ line (ipairs (vim.api.nvim_buf_get_lines buf 0 scan-lines false)) + &until found] + (let [v (line:match pat)] + (if (and v (not= v "")) v nil))))) + +(fn program [buf] + (vim.fn.fnamemodify (vim.fn.expandcmd (or (directive buf "debug-program") "%:p:r")) + ":p")) + +(fn pick-config [buf auto?] + (let [debugger (directive buf "debugger") + ft (. vim.bo buf :filetype) + configs (or (. (require :dap) :configurations ft) []) + config (if debugger + (accumulate [found nil _ c (ipairs configs) &until found] + (if (= c.type debugger) c nil)) + auto? + (. configs 1))] + (when (and debugger (not config)) + (vim.notify (.. "debugger: no " ft " configuration of type " debugger) + vim.log.levels.WARN)) + config)) + +(fn launch [buf] + (let [dap (require :dap) + auto? (or (directive buf "debug-build") (directive buf "debug-program")) + config (pick-config buf auto?)] + (if (and config auto?) + (dap.run (vim.tbl_extend :force config + {:program (program buf) + :args (vim.split (or (directive buf "debug-args") "") "%s+" + {:trimempty true})})) + config (dap.run config) + (dap.continue)))) + +(fn build-then-launch [buf cmd on-fail] + (vim.notify (.. "debug-build: " cmd)) + (vim.system [vim.o.shell vim.o.shellcmdflag cmd] {:text true} + (fn [res] + (vim.schedule + (fn [] + (if (= res.code 0) + (launch buf) + (let [out (.. (or res.stdout "") (or res.stderr ""))] + (vim.fn.setqflist [] " " {:title (.. "debug-build: " cmd) + :lines (vim.split out "\n" {:trimempty true})}) + (vim.cmd.copen) + (when on-fail (on-fail)) + (vim.notify (.. "debug-build failed (exit " res.code ")") + vim.log.levels.ERROR)))))))) + +(fn new-session [buf on-fail] + (let [cmd (directive buf "debug-build")] + (if cmd + ;; %o first: expandcmd would read it as % followed by "o" + (build-then-launch buf (vim.fn.expandcmd + (pick-values 1 (cmd:gsub "%%o" #(vim.fn.fnameescape (program buf))))) + on-fail) + (launch buf)))) + +(fn start [] + (let [dap (require :dap)] + (if (dap.session) + (dap.continue) + (new-session (vim.api.nvim_get_current_buf))))) + +(fn set-all [by-buf] + (let [breakpoints (require :dap.breakpoints)] + (breakpoints.clear) + (each [buf bps (pairs by-buf)] + (each [_ bp (ipairs bps)] + (breakpoints.set {:condition bp.condition + :hit_condition bp.hitCondition + :log_message bp.logMessage} + buf bp.line))))) + +(fn run-to-cursor [] + (let [dap (require :dap) + breakpoints (require :dap.breakpoints) + buf (vim.api.nvim_get_current_buf) + line (. (vim.api.nvim_win_get_cursor 0) 1) + key :config.dap-start.run-to-cursor] + (if (dap.session) + (dap.run_to_cursor) + (let [before (breakpoints.get) + listen (fn [on-init on-stop] + (tset dap.listeners.after.event_initialized key on-init) + (tset dap.listeners.before.event_stopped key on-stop) + (tset dap.listeners.before.event_terminated key on-stop)) + done (fn [] + (listen nil nil) + (set-all before))] + ;; Sent back to the session: an empty list drops the cursor one + (when (= nil (. before buf)) (tset before buf [])) + (breakpoints.clear) + (breakpoints.set {} buf line) + ;; The session reads its initial breakpoints when initialized, so the + ;; local ones (signs, persistence) can come back then; the session + ;; gets them back once it reaches the cursor (or ends) + (listen #(set-all before) + (fn [session] + (done) + (session:set_breakpoints before nil))) + (new-session buf done))))) + +{: start : run-to-cursor} diff --git a/fnl/plugins/init.fnl b/fnl/plugins/init.fnl index 1ef508a..11d151d 100644 --- a/fnl/plugins/init.fnl +++ b/fnl/plugins/init.fnl @@ -4,6 +4,8 @@ (local repo 1) ;; lazy.nvim spec index for the git repo (e.g., "folke/plugin") (local lhs 1) ;; lazy.nvim/which-key spec index for key sequence (local rhs 2) ;; lazy.nvim keys spec index for command/action +;; Run f, then save dap breakpoints so they survive restarts +(fn bp-save [f] (f) ((. (require :config.dap-breakpoints) :save))) [;; Tokyonight - Colorscheme {repo "folke/tokyonight.nvim" @@ -115,7 +117,8 @@ {lhs "t" :group "toggle"} {lhs "l" :group "log"} {lhs "a" :group "AI/Claude"} - {lhs "d" :group "debug"}]))} + {lhs "d" :group "debug"} + {lhs "dr" :group "reverse"}]))} ;; nvim-dap + nvim-dap-ui - Debugging via the Debug Adapter Protocol ;; Adapters: lldb-dap (LLVM) for Rust, C and compiled rust-mal .clj @@ -125,31 +128,60 @@ ;; The lldb one is imported when the cwd has debug/rust_mal_lldb.py. {repo "rcarriga/nvim-dap-ui" :dependencies ["mfussenegger/nvim-dap" "nvim-neotest/nvim-nio"] - :keys [{lhs "" rhs #((. (require :dap) :continue)) :desc "Debug: start/continue"} + ;; Breakpoints persist across sessions (fnl/config/dap-breakpoints.fnl) + :init #((. (require :config.dap-breakpoints) :setup)) + :keys [{lhs "" rhs #((. (require :config.dap-start) :start)) :desc "Debug: start/continue"} {lhs "" rhs #((. (require :dap) :terminate)) :desc "Debug: stop"} {lhs "" rhs #((. (require :dapui) :toggle)) :desc "Debug: toggle UI"} - {lhs "" rhs #((. (require :dap) :run_to_cursor)) :desc "Debug: run to cursor"} - {lhs "" rhs #((. (require :dap) :toggle_breakpoint)) :desc "Debug: toggle breakpoint"} + {lhs "" rhs #((. (require :config.dap-start) :run-to-cursor)) :desc "Debug: run to cursor"} + {lhs "" rhs #(bp-save #((. (require :dap) :toggle_breakpoint))) :desc "Debug: toggle breakpoint"} {lhs "" rhs #((. (require :dap) :step_over)) :desc "Debug: step over"} {lhs "" rhs #((. (require :dap) :step_into)) :desc "Debug: step into"} + ;; F11 is fullscreen in Windows Terminal; some terminals send ctrl+F11 as F35 + {lhs "" rhs #((. (require :dap) :step_into)) :desc "Debug: step into"} + {lhs "" rhs #((. (require :dap) :step_into)) :desc "Debug: step into"} {lhs "" rhs #((. (require :dap) :step_out)) :desc "Debug: step out"} - {lhs "db" rhs #((. (require :dap) :set_breakpoint) (vim.fn.input "Condition: ")) + ;; Time travel (gdb adapter, rust-mal .clj built with --debug/--small-debug). + ;; Many terminals send shift+F10 as F22 and shift+F5 as F17 + {lhs "" rhs #((. (require :dap) :step_back)) :desc "Debug: step back"} + {lhs "" rhs #((. (require :dap) :step_back)) :desc "Debug: step back"} + {lhs "" rhs #((. (require :dap) :reverse_continue)) :desc "Debug: reverse continue"} + {lhs "" rhs #((. (require :dap) :reverse_continue)) :desc "Debug: reverse continue"} + ;; Non-function-key alternatives to - + {lhs "dc" rhs #((. (require :config.dap-start) :start)) :desc "Start/continue"} + {lhs "dq" rhs #((. (require :dap) :terminate)) :desc "Stop"} + {lhs "du" rhs #((. (require :dapui) :toggle)) :desc "Toggle UI"} + {lhs "dC" rhs #((. (require :config.dap-start) :run-to-cursor)) :desc "Run to cursor"} + {lhs "dt" rhs #(bp-save #((. (require :dap) :toggle_breakpoint))) :desc "Toggle breakpoint"} + {lhs "dn" rhs #((. (require :dap) :step_over)) :desc "Next (step over)"} + {lhs "ds" rhs #((. (require :dap) :step_into)) :desc "Step into"} + {lhs "do" rhs #((. (require :dap) :step_out)) :desc "Step out"} + ;; Reverse actions: the forward key under an `r` prefix + {lhs "drc" rhs #((. (require :dap) :reverse_continue)) :desc "Reverse continue"} + {lhs "drn" rhs #((. (require :dap) :step_back)) :desc "Reverse next (step back)"} + {lhs "db" rhs #(bp-save #((. (require :dap) :set_breakpoint) (vim.fn.input "Condition: "))) :desc "Conditional breakpoint"} - {lhs "dl" rhs #((. (require :dap) :set_breakpoint) nil nil (vim.fn.input "Log message: ")) + {lhs "dl" rhs #(bp-save #((. (require :dap) :set_breakpoint) nil nil (vim.fn.input "Log message: "))) :desc "Log point"} - {lhs "dB" rhs #((. (require :dap) :clear_breakpoints)) :desc "Clear breakpoints"} - {lhs "dr" rhs #((. (require :dap) :run_last)) :desc "Re-run last session"} - {lhs "dR" rhs #((. (require :dap) :restart)) :desc "Restart session"} + {lhs "dB" rhs #(bp-save #((. (require :dap) :clear_breakpoints))) :desc "Clear breakpoints"} + {lhs "dR" rhs #((. (require :dap) :run_last)) :desc "Re-run last session"} + {lhs "d." rhs #((. (require :dap) :restart)) :desc "Restart session"} {lhs "dk" rhs #((. (require :dap) :up)) :desc "Frame up"} {lhs "dj" rhs #((. (require :dap) :down)) :desc "Frame down"} {lhs "de" rhs #((. (require :dapui) :eval)) :mode [:n :v] :desc "Eval expression"} {lhs "dE" rhs #((. (require :dapui) :eval) (vim.fn.input "Expression: ")) :desc "Eval prompt"} - {lhs "do" rhs #((. (require :dap) :repl :toggle)) :desc "Toggle debug REPL"}] + {lhs "dO" rhs #((. (require :dap) :repl :toggle)) :desc "Toggle debug REPL"}] :config (fn [] (local dap (require :dap)) (local dapui (require :dapui)) - (dapui.setup) + ;; /double-click on a Stacks frame jumps to it; expands + (dapui.setup {:mappings {:open ["o" "" "<2-LeftMouse>"] + :expand [""]}}) + ;; Jump into the last code window rather than a dap-ui split. Not + ;; usevisible: it leaves the cursor alone when the line is on + ;; screen, rather than at the column of the form being evaluated + (set dap.defaults.fallback.switchbuf "usetab,uselast") ;; Open/close the UI with the session (set dap.listeners.after.event_initialized.dapui #(dapui.open)) diff --git a/lazy-lock.json b/lazy-lock.json index fb92ed2..d0c83f6 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,19 +1,19 @@ { "blink.cmp": { "branch": "main", "commit": "78336bc89ee5365633bcf754d93df01678b5c08f" }, "claudecode.nvim": { "branch": "main", "commit": "2390c6e45c4789072c293ac69de051d169668b29" }, - "conjure": { "branch": "main", "commit": "349256ada7f73801f6e832abec1ac8eac32bf788" }, - "gitsigns.nvim": { "branch": "main", "commit": "8d79f2410c76e62b92e51c28c82e28c1c5a3daeb" }, + "conjure": { "branch": "main", "commit": "b23c76a8c874fe7ac5ad95255efc66173b049088" }, + "gitsigns.nvim": { "branch": "main", "commit": "fd780920613e541cbdfac41bf711583ed49f9249" }, "hop.nvim": { "branch": "master", "commit": "08ddca799089ab96a6d1763db0b8adc5320bf050" }, "lazy.nvim": { "branch": "main", "commit": "306a05526ada86a7b30af95c5cc81ffba93fef97" }, - "lean.nvim": { "branch": "main", "commit": "950eff1ae00db2b2f35996cb916c1a47254c3c96" }, + "lean.nvim": { "branch": "main", "commit": "ba5663698b76677c22f5602cdbb37ab4bade1072" }, "marks.nvim": { "branch": "master", "commit": "f353e8c08c50f39e99a9ed474172df7eddd89b72" }, "mason-tool-installer.nvim": { "branch": "main", "commit": "443f1ef8b5e6bf47045cb2217b6f748a223cf7dc" }, "mason.nvim": { "branch": "main", "commit": "2a6940af80375532e5e9e7c1f2fc6319a1b7a69d" }, - "nfnl": { "branch": "main", "commit": "a4c47a1e7e52aaf234b358f73135acf783054e35" }, + "nfnl": { "branch": "main", "commit": "7a4ba217167fee57e9bf175405636ced49aa9f83" }, "nvim-dap": { "branch": "master", "commit": "cfa2d58f4537aca6ca83e2de1a0d9f1491121264" }, "nvim-dap-ui": { "branch": "master", "commit": "cc9dd33aade7f20bae414d0cba163bc60d4d4b43" }, "nvim-nio": { "branch": "master", "commit": "edcc181a875301dd21840189aa2f2f9ad69fc172" }, - "nvim-treesitter": { "branch": "main", "commit": "f603a2f4da48728f80257fb5fbb90145fd1dc173" }, + "nvim-treesitter": { "branch": "main", "commit": "ca581c3436fc2b236847f5b739ebc263739e5ca6" }, "plenary.nvim": { "branch": "master", "commit": "74b06c6c75e4eeb3108ec01852001636d85a932b" }, "roslyn.nvim": { "branch": "main", "commit": "bf1bd4e70bc7068d1b4aebf4ce03cc19a86615be" }, "snacks.nvim": { "branch": "main", "commit": "882c996cf28183f4d63640de0b4c02ec886d01f2" }, @@ -21,7 +21,7 @@ "telescope.nvim": { "branch": "master", "commit": "40aedd8a68c78a656a10a8d62d80c54af59420fb" }, "tokyonight.nvim": { "branch": "main", "commit": "cdc07ac78467a233fd62c493de29a17e0cf2b2b6" }, "vim-repeat": { "branch": "master", "commit": "65846025c15494983dafe5e3b46c8f88ab2e9635" }, - "vim-sexp": { "branch": "master", "commit": "1bf50921308f0188d1c1ecf8a712cc72ab2775e5" }, + "vim-sexp": { "branch": "master", "commit": "b2ff4532c5913f539fe96fd8425e0e150ab21997" }, "vim-sexp-mappings-for-regular-people": { "branch": "master", "commit": "4debb74b0a3e530f1b18e5b7dff98a40b2ad26f1" }, "vim-surround": { "branch": "master", "commit": "3d188ed2113431cf8dac77be61b842acb64433d9" }, "which-key.nvim": { "branch": "main", "commit": "3aab2147e74890957785941f0c1ad87d0a44c15a" } diff --git a/lua/bootstrap.lua b/lua/bootstrap.lua index fd9a0c2..1639567 100644 --- a/lua/bootstrap.lua +++ b/lua/bootstrap.lua @@ -51,9 +51,11 @@ return { dependencies = { "nvim-lua/plenary.nvim" }, keys = { { "ff", "Telescope find_files", desc = "Find files" }, + { "fF", "Telescope find_files no_ignore=true hidden=true", desc = "Find files (incl. ignored)" }, { "fg", "Telescope live_grep", desc = "Live grep" }, { "fb", "Telescope buffers", desc = "Buffers" }, { "fh", "Telescope help_tags", desc = "Help tags" }, + { "fk", "Telescope keymaps", desc = "Keymaps" }, }, }, }