Skip to content

Commit 8f69dc2

Browse files
ericdallokiennq
andauthored
Add auto configuration mode (emacs-lsp#277)
* Add auto configuration mode * Change docs * Fix dap-ui-mode disable Co-authored-by: kiennq <[email protected]> * Fix dap-ui-controls-mode disable Co-authored-by: kiennq <[email protected]> * Add option to specify which features to enable * Remove debug message * Add tags to choices on defcustom * Minor adjustments * Rename consts * Add mode for show and hide windows * Fix docs * Move to minor mode hooks Co-authored-by: kiennq <[email protected]>
1 parent 2730999 commit 8f69dc2

File tree

4 files changed

+103
-39
lines changed

4 files changed

+103
-39
lines changed

dap-mode.el

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,31 @@ The hook will be called with the session file and the new set of breakpoint loca
137137
(defcustom dap-debug-template-configurations nil
138138
"Plist Template configurations for DEBUG/RUN."
139139
:safe #'listp
140+
:group 'dap-mode
140141
:type '(plist))
141142

143+
(defcustom dap-auto-configure-features '(sessions locals breakpoints expressions controls tooltip)
144+
"Windows to auto show on debugging when in dap-ui-auto-configure-mode."
145+
:group 'dap-mode
146+
:type '(set (const :tag "Show sessions popup window when debugging and enable `dap-ui-sessions-mode`" sessions)
147+
(const :tag "Show locals popup window when debugging" locals)
148+
(const :tag "Show breakpoints popup window when debugging and enable `dap-ui-breakpoints-mode`" breakpoints)
149+
(const :tag "Show expressions popup window when debugging" expressions)
150+
(const :tag "Enable `dap-ui-controls-mode` with controls to manage the debug session when debugging" controls)
151+
(const :tag "Enable `dap-tooltip-mode` that enables mouse hover support when debugging" tooltip)))
152+
153+
(defconst dap-features->windows
154+
'((sessions . (dap-ui-sessions . dap-ui--sessions-buffer))
155+
(locals . (dap-ui-locals . dap-ui--locals-buffer))
156+
(breakpoints . (dap-ui-breakpoints . dap-ui--breakpoints-buffer))
157+
(expressions . (dap-ui-expressions . dap-ui--expressions-buffer))))
158+
159+
(defconst dap-features->modes
160+
'((sessions . dap-ui-sessions-mode)
161+
(breakpoints . dap-ui-breakpoints-mode)
162+
(controls . (dap-ui-controls-mode . posframe))
163+
(tooltip . dap-tooltip-mode)))
164+
142165
(defvar dap--debug-configuration nil
143166
"List of the previous configuration that have been executed.")
144167

@@ -1598,6 +1621,36 @@ point is set."
15981621
(interactive)
15991622
(dap-mode t))
16001623

1624+
1625+
;; Auto configure
1626+
1627+
;;;###autoload
1628+
(define-minor-mode dap-auto-configure-mode
1629+
"Auto configure dap minor mode."
1630+
:init-value nil
1631+
:global t
1632+
:group 'dap-mode
1633+
(cond
1634+
(dap-auto-configure-mode
1635+
(dap-mode 1)
1636+
(dap-ui-mode 1)
1637+
(seq-doseq (feature dap-auto-configure-features)
1638+
(when-let (mode (alist-get feature dap-features->modes))
1639+
(if (consp mode)
1640+
(when (require (cdr mode) nil t)
1641+
(funcall (car mode) 1))
1642+
(funcall mode 1))))
1643+
(dap-ui-many-windows-mode 1))
1644+
(t
1645+
(dap-mode -1)
1646+
(dap-ui-mode -1)
1647+
(seq-doseq (feature dap-auto-configure-features)
1648+
(when-let (mode (alist-get feature dap-features->modes))
1649+
(if (consp mode)
1650+
(funcall (car mode) -1)
1651+
(funcall mode -1))))
1652+
(dap-ui-many-windows-mode -1))))
1653+
16011654
(provide 'dap-mode)
16021655
;;; dap-mode.el ends here
16031656

dap-ui.el

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ DEBUG-SESSION is the debug session triggering the event."
10761076
(define-key (kbd "C L") #'dap-ui-breakpoint-log-message)))
10771077

10781078
(define-minor-mode dap-ui-breakpoints-mode
1079-
"UI Session list minor mode."
1079+
"UI Breakpoints list minor mode."
10801080
:init-value nil
10811081
:group dap-ui
10821082
:keymap dap-ui-breakpoints-mode-map)
@@ -1109,5 +1109,42 @@ DEBUG-SESSION is the debug session triggering the event."
11091109
(add-hook 'dap-breakpoints-changed-hook #'dap-ui-breakpoints--refresh)
11101110
(add-hook 'kill-buffer-hook 'dap-ui-breakpoints--cleanup-hooks nil t))
11111111

1112+
(defun dap-ui--show-many-windows (_session)
1113+
"Show auto configured feature windows."
1114+
(seq-doseq (feature-start-stop dap-auto-configure-features)
1115+
(when-let (start-stop (alist-get feature-start-stop dap-features->windows))
1116+
(funcall (car start-stop)))))
1117+
1118+
(defun dap-ui--hide-many-windows (_session)
1119+
"Hide all debug windows when sessions are dead."
1120+
(seq-doseq (feature-start-stop dap-auto-configure-features)
1121+
(-when-let* ((feature-start-stop (alist-get feature-start-stop dap-features->windows))
1122+
(buffer-name (symbol-value (cdr feature-start-stop))))
1123+
(and (get-buffer buffer-name)
1124+
(kill-buffer buffer-name)))))
1125+
1126+
;;;###autoload
1127+
(defun dap-ui-show-many-windows ()
1128+
"Show auto configured feature windows."
1129+
(interactive)
1130+
(dap-ui--show-many-windows nil))
1131+
1132+
;;;###autoload
1133+
(defun dap-ui-hide-many-windows ()
1134+
"Hide all debug windows when sessions are dead."
1135+
(interactive)
1136+
(dap-ui--hide-many-windows nil))
1137+
1138+
(define-minor-mode dap-ui-many-windows-mode
1139+
"Shows/hide the windows from `dap-auto-configure-features`"
1140+
:global t
1141+
(cond
1142+
(dap-ui-many-windows-mode
1143+
(add-hook 'dap-stopped-hook #'dap-ui--show-many-windows)
1144+
(add-hook 'dap-terminated-hook #'dap-ui--hide-many-windows))
1145+
(t
1146+
(remove-hook 'dap-stopped-hook #'dap-ui--show-many-windows)
1147+
(remove-hook 'dap-terminated-hook #'dap-ui--hide-many-windows))))
1148+
11121149
(provide 'dap-ui)
11131150
;;; dap-ui.el ends here

docs/page/configuration.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ Configuration
33

44
## DAP mode configuration
55

6-
Enable both `dap-mode` and `dap-ui-mode` (requires posframe to be
7-
installed manually, available only for emacs version \>= 26).
6+
For an auto-configuration enable the `dap-auto-configure-mode`. You can configure which features from `dap-mode` do you want with `dap-auto-configure-features`:
87

9-
``` elisp
8+
```elisp
9+
;; Enabling only some features
10+
(setq dap-auto-configure-features '(sessions locals controls tooltip))
11+
```
12+
13+
Or if you want to enable only specific modes instead:
14+
15+
```elisp
1016
(dap-mode 1)
17+
18+
;; The modes above are optional
19+
1120
(dap-ui-mode 1)
1221
;; enables mouse hover support
1322
(dap-tooltip-mode 1)

docs/page/how-to.md

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,6 @@
11
How to
22
======
33

4-
## Display debug windows on session startup
5-
6-
Here it is a code that can be used to automatically display/hide debug windows.
7-
8-
```elisp
9-
(defun my/window-visible (b-name)
10-
"Return whether B-NAME is visible."
11-
(-> (-compose 'buffer-name 'window-buffer)
12-
(-map (window-list))
13-
(-contains? b-name)))
14-
15-
(defun my/show-debug-windows (session)
16-
"Show debug windows."
17-
(let ((lsp--cur-workspace (dap--debug-session-workspace session)))
18-
(save-excursion
19-
;; display locals
20-
(unless (my/window-visible dap-ui--locals-buffer)
21-
(dap-ui-locals))
22-
;; display sessions
23-
(unless (my/window-visible dap-ui--sessions-buffer)
24-
(dap-ui-sessions)))))
25-
26-
(add-hook 'dap-stopped-hook 'my/show-debug-windows)
27-
28-
(defun my/hide-debug-windows (session)
29-
"Hide debug windows when all debug sessions are dead."
30-
(unless (-filter 'dap--session-running (dap--get-sessions))
31-
(and (get-buffer dap-ui--sessions-buffer)
32-
(kill-buffer dap-ui--sessions-buffer))
33-
(and (get-buffer dap-ui--locals-buffer)
34-
(kill-buffer dap-ui--locals-buffer))))
35-
36-
(add-hook 'dap-terminated-hook 'my/hide-debug-windows)
37-
```
38-
394
## Activate minor modes when stepping through code
405

416
You may want to activate minor modes, e.g. `read-only-mode`, when the debugger is active in a buffer. If so, you could use a trick like this:

0 commit comments

Comments
 (0)