Skip to content

Commit e8fe257

Browse files
authored
:compilation for dap-debug (#448)
* :compilation for `dap-debug' * Only close window if compilation buffer * Use :dap-compilation * Better documentation * Buffer naming and comint + ansi
1 parent 3b12bf5 commit e8fe257

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

dap-mode.el

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,12 +1647,22 @@ before starting the debug process."
16471647
(push (cons session-name launch-args) dap--debug-configuration)
16481648
(run-hook-with-args 'dap-session-created-hook debug-session)))))
16491649

1650+
(defcustom dap-debug-compilation-keep nil
1651+
"Whether `dap-debug' should keep the compile window on success.
1652+
By default, it is hidden."
1653+
:type 'boolean
1654+
:group 'dap-mode)
1655+
16501656
;;;###autoload
16511657
(defun dap-debug (debug-args)
16521658
"Run debug configuration DEBUG-ARGS.
16531659
16541660
If DEBUG-ARGS is not specified the configuration is generated
1655-
after selecting configuration template."
1661+
after selecting configuration template.
1662+
1663+
:dap-compilation specifies a shell command to be run using
1664+
`compilation-start' before starting the debug session. It could
1665+
be used to compile the project, spin up docker, ...."
16561666
(interactive (list (-> (dap--completing-read "Select configuration template: "
16571667
(-mapcat #'funcall dap-launch-configuration-providers)
16581668
'cl-first nil t)
@@ -1670,10 +1680,29 @@ after selecting configuration template."
16701680
(funcall debug-args))
16711681
(user-error "Have you loaded the `%s' specific dap package?"
16721682
(or (plist-get debug-args :type)
1673-
(user-error "%s does not specify :type" debug-args))))))
1674-
(if (functionp launch-args)
1675-
(funcall launch-args #'dap-start-debugging-noexpand)
1676-
(dap-start-debugging-noexpand launch-args))))
1683+
(user-error "%s does not specify :type" debug-args)))))
1684+
(cb (lambda ()
1685+
(if (functionp launch-args)
1686+
(funcall launch-args #'dap-start-debugging-noexpand)
1687+
(dap-start-debugging-noexpand launch-args)))))
1688+
(-if-let ((&plist :dap-compilation compilation) launch-args)
1689+
(progn
1690+
(cl-remf launch-args :dap-compilation)
1691+
(with-current-buffer (compilation-start compilation t (lambda (&rest _)
1692+
"*DAP compilation*"))
1693+
(let (window)
1694+
(add-hook 'compilation-finish-functions
1695+
(lambda (buf status &rest _)
1696+
(if (string= "finished\n" status)
1697+
(progn
1698+
(when (and (not dap-debug-compilation-keep)
1699+
(window-live-p window)
1700+
(eq buf (window-buffer window)))
1701+
(delete-window window))
1702+
(funcall cb))
1703+
(lsp--error "Compilation step failed"))))
1704+
(setq window (display-buffer (current-buffer))))))
1705+
(funcall cb))))
16771706

16781707
(defun dap-debug-edit-template (&optional debug-args)
16791708
"Edit registered template DEBUG-ARGS.
@@ -1699,11 +1728,11 @@ normally with `dap-debug'"
16991728
((fst snd . rst) debug-args))
17001729
(insert (format "%s %s" fst (prin1-to-string snd)))
17011730
(cl-loop for (k v) on rst by #'cddr
1702-
do (if (not (equal k :program-to-start))
1703-
(progn
1704-
(insert "\n")
1705-
(--dotimes column (insert " "))
1706-
(insert (format "%s %s" k (prin1-to-string v)))))))
1731+
do (if (not (equal k :program-to-start))
1732+
(progn
1733+
(insert "\n")
1734+
(--dotimes column (insert " "))
1735+
(insert (format "%s %s" k (prin1-to-string v)))))))
17071736
(insert "))"))
17081737
(pop-to-buffer "*DAP Templates*")
17091738
(goto-char (point-max)))

docs/page/features.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ DAP are just like in VSCode and even support variables. See:
105105

106106
- [launch.json](https://code.visualstudio.com/docs/editor/debugging)
107107
- [launch.json variables](https://code.visualstudio.com/docs/editor/variables-reference)
108+
109+
## Compiling the project before debugging
110+
111+
Many modern IDEs, for example Eclipse and VSCode (`preLaunchTask`), provide
112+
functionality to compile the project before starting the debug session.
113+
114+
`dap-mode` also has such a feature: the `:dap-compilation` property of launch
115+
configurations ("dap-compilation" in `launch.json`) specifies a shell command
116+
that needs to execute successfully before the debug session is started.
117+
118+
In the future, we want to support VSCode's `preLaunchTask` instead, but
119+
currently there is no tasks.json-compatible task runner for Emacs.

0 commit comments

Comments
 (0)