135 lines
5.4 KiB
Fennel
135 lines
5.4 KiB
Fennel
;; <F5>: 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 <F5>.
|
|
;;
|
|
;; <F8>: run to cursor. With no session, starts one as <F5> 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}
|