;; 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}