Minor refactor & use doom-store-* API

+ Fixes a shallow comparison error (eq cannot compare strings).
+ Uses the doom-store-* API rather than pull in a new dependency to
  manage project-local variables.
This commit is contained in:
Henrik Lissner 2021-03-05 18:58:18 -05:00
parent 44803038b2
commit cf086d08fa
2 changed files with 33 additions and 35 deletions

View file

@ -1,37 +1,39 @@
;;; tools/debugger/autoload/debugger.el -*- lexical-binding: t; -*- ;;; tools/debugger/autoload/debugger.el -*- lexical-binding: t; -*-
(defvar +debugger-last-configuration nil (defvar-local +debugger--last-config nil
"Configuration of the last debugging session of buffer.") "Configuration of the last debugging session of buffer.")
(make-variable-buffer-local '+debugger-last-configuration) (put '+debugger--last-config 'permanent-local t) ; don't kill on mode change
(defun +debugger-get-configuration () (defun +debugger--get-last-config ()
"Get last debugging configuration. "Get last debugging configuration.
If in a project, returns the configuration of the last debugging session in the If in a project, returns the configuration of the last debugging session in the
project, if any. Else, returns the last debugging configuration of the current project, if any. Else, returns the last debugging configuration of the current
buffer, if any." buffer, if any."
(if (projectile-project-p) (if (doom-project-p)
(projectile-variable-get '+debugger-last-configuration) (doom-store-get (doom-project-root) "+debugger")
+debugger-last-configuration)) +debugger--last-config))
(defun +debugger-set-configuration (configuration) (defun +debugger--set-config (config)
"Set the debugging configuration. "Remember this debugging configuration for `+debugger/start-last'.
If in a project, sets the project's debugging session configuration. Else, sets If in a project, sets the project's debugging session configuration. Else, sets
the debugging configuration of the current buffer." the debugging configuration of the current buffer."
(if (projectile-project-p) (if (doom-project-p)
(projectile-variable-put '+debugger-last-configuration configuration) (doom-store-put (doom-project-root) config
(setq-local +debugger-last-configuration configuration))) (lambda (key _cfg) (file-directory-p key))
"+debugger")
(setq +debugger--last-config config)))
(defun +debugger-list-for-dap () (defun +debugger--list-for-dap ()
(when (and (bound-and-true-p lsp-mode) (and (or (bound-and-true-p lsp-mode)
(bound-and-true-p lsp--buffer-deferred) (bound-and-true-p lsp--buffer-deferred))
(require 'dap-mode nil t) (require 'dap-mode nil t)
dap-mode) dap-mode
(mapcar (lambda (c) (cons 'dap c)) (mapcar (lambda (c) (cons 'dap c))
(apply 'append (mapcar #'funcall dap-launch-configuration-providers))))) (apply #'append (mapcar #'funcall dap-launch-configuration-providers)))))
(defun +debugger-list-for-realgud () (defun +debugger--list-for-realgud ()
(mapcar (lambda (c) (cons 'realgud (list (symbol-name c)))) (mapcar (lambda (c) (cons 'realgud (list (symbol-name c))))
(cl-loop for (sym . plist) in +debugger--realgud-alist (cl-loop for (sym . plist) in +debugger--realgud-alist
for sym-name = (symbol-name sym) for sym-name = (symbol-name sym)
@ -44,21 +46,19 @@ the debugging configuration of the current buffer."
"Completing read for debug configuration. "Completing read for debug configuration.
Presents both dap and realgud configurations, and returns a list of the form Presents both dap and realgud configurations, and returns a list of the form
('dap ...) or ('realgud ...) containing the corresponding debug configuration \('dap ...) or ('realgud ...) containing the corresponding debug configuration
infromation." infromation."
(let* ((configurations (append (let ((result (mapcar (lambda (c) (cons (cadr c) c))
(+debugger-list-for-dap) (append (+debugger--list-for-dap)
(+debugger-list-for-realgud))) (+debugger--list-for-realgud))))
(result (mapcar (lambda (c) (cons (cadr c) c)) configurations)) (completion (completing-read "Start debugger: " (mapcar #'car result) nil t)))
(completion (completing-read "Start debugger: " (mapcar 'car result) nil t))) (if (or (null completion) (string-empty-p completion))
(if (or (null completion) (eq completion ""))
(user-error "No debugging configuration specified.") (user-error "No debugging configuration specified.")
(let ((configuration (cdr (assoc completion result)))) (let ((configuration (cdr (assoc completion result))))
(if (eq (car configuration) 'dap) (if (eq (car configuration) 'dap)
;; get dap debugging arguments ;; get dap debugging arguments
(let* ((debug-args (dap-variables-expand-in-launch-configuration (copy-tree (let* ((debug-args (dap-variables-expand-in-launch-configuration
(cl-rest (copy-tree (cddr configuration))))
(cdr configuration)))))
(launch-args (or (catch 'is-nil (launch-args (or (catch 'is-nil
(funcall (or (gethash (funcall (or (gethash
(or (plist-get debug-args :type) (or (plist-get debug-args :type)
@ -77,10 +77,10 @@ infromation."
(defun +debugger/start-last () (defun +debugger/start-last ()
"Relaunch the last debugger session." "Relaunch the last debugger session."
(interactive) (interactive)
(let ((configuration (+debugger-get-configuration))) (let ((configuration (+debugger--get-last-config)))
(unless configuration (unless configuration
(user-error "No last debugger%s to invoke" (user-error "No last debugger%s to invoke"
(if (projectile-project-p) (if (doom-project-p)
" of this project" " of this project"
""))) "")))
(let ((launch-args (cdr configuration))) (let ((launch-args (cdr configuration)))
@ -99,8 +99,8 @@ infromation."
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-get-configuration))) (when (or arg (null (+debugger--get-last-config)))
(+debugger-set-configuration (+debugger-completing-read))) (+debugger--set-config (+debugger-completing-read)))
(+debugger/start-last)) (+debugger/start-last))
;;;###autoload ;;;###autoload

View file

@ -5,8 +5,6 @@
(when (featurep! :lang javascript) (when (featurep! :lang javascript)
(package! realgud-trepan-ni :pin "6e38cf838c7b47b5f1353d00901b939ffa36d707"))) (package! realgud-trepan-ni :pin "6e38cf838c7b47b5f1353d00901b939ffa36d707")))
(package! projectile-variable :pin "8d348ac70bdd6dc320c13a12941b32b38140e264")
(when (featurep! +lsp) (when (featurep! +lsp)
(package! dap-mode :pin "aa15b9c49b7e09bb23f9a4ff7855122f0eb19976") (package! dap-mode :pin "aa15b9c49b7e09bb23f9a4ff7855122f0eb19976")
(package! posframe :pin "3454a4cb9d218c38f9c5b88798dfb2f7f85ad936")) (package! posframe :pin "3454a4cb9d218c38f9c5b88798dfb2f7f85ad936"))