Merge pull request #4708 from he-la/develop

Fix +debug/start for dap-based debuggers.
This commit is contained in:
Henrik Lissner 2021-03-05 20:14:11 -05:00 committed by GitHub
commit a724771d1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 33 deletions

View file

@ -1,28 +1,74 @@
;;; tools/debugger/autoload/debugger.el -*- lexical-binding: t; -*- ;;; tools/debugger/autoload/debugger.el -*- lexical-binding: t; -*-
(defvar +debugger--last nil) (defvar-local +debugger--last-config nil
"Configuration of the last debugging session of buffer.")
(put '+debugger--last-config 'permanent-local t) ; don't kill on mode change
(defun +debugger-list-for-dap () (defun +debugger--get-last-config ()
(when (and (bound-and-true-p lsp-mode) "Get last debugging configuration.
(bound-and-true-p lsp--buffer-deferred)
(require 'dap-mode nil t)
dap-mode)
(mapcar #'car dap-debug-template-configurations)))
(defun +debugger-list-for-realgud () If in a project, returns the configuration of the last debugging session in the
(cl-loop for (sym . plist) in +debugger--realgud-alist project, if any. Else, returns the last debugging configuration of the current
for sym-name = (symbol-name sym) buffer, if any."
for modes = (plist-get plist :modes) (if (doom-project-p)
if (or (null modes) (apply #'derived-mode-p modes)) (doom-store-get (doom-project-root) "+debugger")
collect sym)) +debugger--last-config))
(defun +debugger--set-config (config)
"Remember this debugging configuration for `+debugger/start-last'.
(defun +debugger-list-available () If in a project, sets the project's debugging session configuration. Else, sets
"TODO" the debugging configuration of the current buffer."
(append (+debugger-list-for-dap) (if (doom-project-p)
(+debugger-list-for-realgud) (doom-store-put (doom-project-root) config
nil)) (lambda (key _cfg) (file-directory-p key))
"+debugger")
(setq +debugger--last-config config)))
(defun +debugger--list-for-dap ()
(and (or (bound-and-true-p lsp-mode)
(bound-and-true-p lsp--buffer-deferred))
(require 'dap-mode nil t)
dap-mode
(mapcar (lambda (c) (cons 'dap c))
(apply #'append (mapcar #'funcall dap-launch-configuration-providers)))))
(defun +debugger--list-for-realgud ()
(mapcar (lambda (c) (cons 'realgud (list (symbol-name c))))
(cl-loop for (sym . plist) in +debugger--realgud-alist
for sym-name = (symbol-name sym)
for modes = (plist-get plist :modes)
if (or (null modes) (apply #'derived-mode-p modes))
collect sym)))
;; Based on dap--completing-read and dap-debug
(defun +debugger-completing-read ()
"Completing read for debug configuration.
Presents both dap and realgud configurations, and returns a list of the form
\('dap ...) or ('realgud ...) containing the corresponding debug configuration
infromation."
(let ((result (mapcar (lambda (c) (cons (cadr c) c))
(append (+debugger--list-for-dap)
(+debugger--list-for-realgud))))
(completion (completing-read "Start debugger: " (mapcar #'car result) nil t)))
(if (or (null completion) (string-empty-p completion))
(user-error "No debugging configuration specified.")
(let ((configuration (cdr (assoc completion result))))
(if (eq (car configuration) 'dap)
;; get dap debugging arguments
(let* ((debug-args (dap-variables-expand-in-launch-configuration
(copy-tree (cddr configuration))))
(launch-args (or (catch 'is-nil
(funcall (or (gethash
(or (plist-get debug-args :type)
(throw 'is-nil nil)) dap--debug-providers)
(throw 'is-nil nil)) debug-args))
(user-error "Have you loaded the `%s' specific dap package?"
(or (plist-get debug-args :type)
(user-error "%s does not specify :type" debug-args))))))
(cons 'dap launch-args))
(cons 'realgud (intern (cadr configuration))))))))
;; ;;
;;; Interactive commands ;;; Interactive commands
@ -31,9 +77,20 @@
(defun +debugger/start-last () (defun +debugger/start-last ()
"Relaunch the last debugger session." "Relaunch the last debugger session."
(interactive) (interactive)
(unless +debugger--last (let ((configuration (+debugger--get-last-config)))
(user-error "No last debugger to invoke")) (unless configuration
(call-interactively +debugger--last)) (user-error "No last debugger%s to invoke"
(if (doom-project-p)
" of this project"
"")))
(let ((launch-args (cdr configuration)))
(if (eq (car configuration) 'dap)
;; start dap configuration
(if (functionp launch-args)
(funcall launch-args #'dap-start-debugging-noexpand)
(dap-start-debugging-noexpand launch-args))
;; else start realgud configuration:
(call-interactively launch-args)))))
;;;###autoload ;;;###autoload
(defun +debugger/start (arg) (defun +debugger/start (arg)
@ -42,24 +99,16 @@
Launches the last used debugger, if one exists. Otherwise, you will be prompted Launches the last used debugger, if one exists. Otherwise, you will be prompted
for what debugger to use. If the prefix ARG is set, prompt anyway." for what debugger to use. If the prefix ARG is set, prompt anyway."
(interactive "P") (interactive "P")
(if (or arg (null +debugger--last)) (when (or arg (null (+debugger--get-last-config)))
(let ((debugger (intern-soft (completing-read "Start debugger: " (+debugger-list-available))))) (+debugger--set-config (+debugger-completing-read)))
(unless debugger (+debugger/start-last))
(user-error "No debugging session to quit"))
(unless (fboundp debugger)
(user-error "Couldn't find debugger backend %S" debugger))
(setq-local +debugger--last debugger)
(if (assoc debugger dap-debug-template-configurations)
(dap-debug debugger)
(call-interactively debugger)))
(+debugger/start-last)))
;;;###autoload ;;;###autoload
(defun +debugger/quit () (defun +debugger/quit ()
"Quit the active debugger, if any." "Quit the active debugger, if any."
(interactive) (interactive)
(cond ((and (fboundp 'dap--cur-session) (dap--cur-session)) (cond ((and (fboundp 'dap--cur-session) (dap--cur-session))
(dap-disconnect)) (dap-disconnect (dap--cur-session)))
((and (fboundp 'realgud-get-cmdbuf) (realgud-get-cmdbuf)) ((and (fboundp 'realgud-get-cmdbuf) (realgud-get-cmdbuf))
(let ((buf (realgud-get-cmdbuf))) (let ((buf (realgud-get-cmdbuf)))
(ignore-errors (ignore-errors

View file

@ -38,6 +38,12 @@
(setq gdb-show-main t (setq gdb-show-main t
gdb-many-windows t) gdb-many-windows t)
(use-package! projectile-variable
:defer t
:commands (projectile-variable-put
projectile-variable-get
projectile-variable-alist
projectile-variable-plist))
(use-package! realgud (use-package! realgud
:defer t :defer t