forked from emacs-lsp/dap-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dap-ui.el
1106 lines (987 loc) · 47.9 KB
/
dap-ui.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; dap-ui.el --- Debug Adapter Protocol UI -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Ivan Yonchovski
;; Author: Ivan Yonchovski <[email protected]>
;; Keywords: languages, debug
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;; URL: https://github.com/yyoncho/dap-mode
;; Package-Requires: ((emacs "25.1") (tree-mode "1.1.1.1") (bui "1.1.0"))
;; Version: 0.2
;;; Commentary:
;; DAP Windows/overlays
;;; Code:
(require 'dap-mode)
(require 'wid-edit)
(require 'dash)
(require 'bui)
(require 'comint)
(require 'compile)
(require 'gdb-mi)
(require 'lsp-treemacs)
(defcustom dap-ui-stack-frames-loaded nil
"Stack frames loaded."
:type 'hook
:group 'dap-ui)
(defcustom dap-ui-breakpoints-ui-list-displayed-hook nil
"List of functions to run when breakpoints list is displayed."
:type 'hook
:group 'dap-ui)
(defface dap-ui-compile-errline
'((t (:inherit compilation-error)))
"Face used for marking the line on which an error occurs."
:group 'dap-ui)
(defface dap-ui-sessions-active-session-face
'((t :weight bold))
"Face used for marking current session in sessions list."
:group 'dap-ui)
(defface dap-ui-sessions-terminated-face
'((t :inherit shadow))
"Face used for marking terminated session."
:group 'dap-ui)
(defface dap-ui-sessions-terminated-active-face
'((t :inherit shadow :weight bold))
"Face used for marking terminated session."
:group 'dap-ui)
(defface dap-ui-sessions-running-face
'((t))
"Face used for marking terminated session."
:group 'dap-ui)
(defface dap-ui-locals-scope-face
'((t :inherit font-lock-function-name-face :weight bold :underline t))
"Face used for scopes in locals view."
:group 'dap-ui)
(defface dap-ui-locals-variable-leaf-face
'((t :inherit font-lock-builtin-face :italic t))
"Face used for variables that does not have nested items."
:group 'dap-ui)
(defface dap-ui-locals-variable-face
'((t :inherit font-lock-builtin-face :weight bold))
"Face used for marking terminated session."
:group 'dap-ui)
(defface dap-ui-sessions-thread-face
'((t))
"Face used for threads in sessions view."
:group 'dap-ui)
(defface dap-ui-sessions-thread-active-face
'((t :weight bold))
"Face used for threads in sessions view."
:group 'dap-ui)
(defface dap-ui-sessions-stack-frame-face
'((t :inherit font-lock-builtin-face))
"Face used for threads in sessions view."
:group 'dap-ui)
(defface dap-ui-pending-breakpoint-face
'((t ()))
"Face used for marking lines with a pending breakpoints."
:group 'dap-ui)
(defface dap-ui-verified-breakpoint-face
'((t ()))
"Face used for marking lines with a verified breakpoints."
:group 'dap-ui)
(defface dap-ui-marker-face
'((t :inherit highlight))
"Face used for marking the current point of execution."
:group 'dap-ui)
(defface dap-ui-compile-errline
'((t (:inherit compilation-error)))
"Face used for marking the line on which an error occurs."
:group 'dap-ui)
(defface dap-ui-breakpoint-verified-fringe
'((t
:foreground "dark green"
:weight bold))
"Face for enabled breakpoint icon in fringe."
:group 'dap-ui)
(defcustom dap-ui-default-fetch-count 30
"Default number of variables to load in inspect variables view for array variables."
:group 'dap-ui
:type 'number)
(defconst dap-ui--locals-buffer "*dap-ui-locals*")
(defconst dap-ui--sessions-buffer "*dap-ui-sessions*")
(defconst dap-ui--debug-window-buffer "*debug-window*")
(defconst dap-ui--expressions-buffer "*dap-ui-expressions*")
(defconst dap-ui--breakpoints-buffer "*dap-ui-breakpoints*")
(defvar dap-ui-buffer-configurations
`((,dap-ui--locals-buffer . ((side . right) (slot . 1) (window-width . 0.20)))
(,dap-ui--expressions-buffer . ((side . right) (slot . 2) (window-width . 0.20)))
(,dap-ui--sessions-buffer . ((side . right) (slot . 3) (window-width . 0.20)))
(,dap-ui--breakpoints-buffer . ((side . left) (slot . 2) (window-width . ,treemacs-width)))
(,dap-ui--debug-window-buffer . ((side . bottom) (slot . 3) (window-width . 0.20)))))
(defvar-local dap-ui--locals-request-id 0
"The locals request id that is currently active.")
(defun dap-ui-session--calculate-face (debug-session)
"Calculate the face of DEBUG-SESSION based on its state."
(cond
((and (eq debug-session (dap--cur-session))
(not (dap--session-running debug-session))) 'dap-ui-sessions-terminated-active-face)
((eq debug-session (dap--cur-session)) 'dap-ui-sessions-active-session-face)
((not (dap--session-running debug-session)) 'dap-ui-sessions-terminated-face)
(t 'dap-ui-sessions-running-face)))
(defun dap-ui--make-overlay (beg end tooltip-text visuals &optional mouse-face buf)
"Allocate a DAP UI overlay in range BEG and END.
TOOLTIP-TEXT, VISUALS, MOUSE-FACE will be used for the overlay.
BUF is the active buffer."
(let ((ov (make-overlay beg end buf t t)))
(overlay-put ov 'face (plist-get visuals :face))
(overlay-put ov 'mouse-face mouse-face)
(overlay-put ov 'help-echo tooltip-text)
(overlay-put ov 'dap-ui-overlay t)
(overlay-put ov 'priority (plist-get visuals :priority))
(let ((char (plist-get visuals :char)))
(if (window-system)
(when char
(overlay-put ov 'before-string
(propertize char
'display
(list 'left-fringe
(plist-get visuals :bitmap)
(plist-get visuals :fringe)))))))
ov))
(defun dap-ui--make-overlay-at (file point msg visuals)
"Create an overlay highlighting the given POINT in FILE.
VISUALS and MSG will be used for the overlay."
(-when-let (buf (find-buffer-visiting file))
(with-current-buffer buf
;; If point is provided, use it to define region
(when (integer-or-marker-p point)
(save-excursion
(goto-char point)
(dap-ui--make-overlay (point-at-bol) (point-at-eol) msg visuals nil buf))))))
(defvar-local dap-ui--breakpoint-overlays nil)
(defvar-local dap-ui--cursor-overlay nil)
(defun dap-ui--clear-breakpoint-overlays ()
"Remove all overlays that ensime-debug has created."
(mapc #'delete-overlay dap-ui--breakpoint-overlays)
(setq dap-ui--breakpoint-overlays '()))
(defun dap-ui--breakpoint-visuals (breakpoint breakpoint-dap)
"Calculate visuals for a BREAKPOINT based on the data comming from DAP server.
BREAKPOINT-DAP - nil or the data comming from DAP."
(list :face (if (and breakpoint-dap (gethash "verified" breakpoint-dap))
'dap-ui-verified-breakpoint-face
'dap-ui-pending-breakpoint-face)
:char "."
:bitmap (cond
((plist-get breakpoint :condition) 'filled-rectangle)
((plist-get breakpoint :log-message) 'right-arrow)
((plist-get breakpoint :hit-condition) 'hollow-rectangle)
(t 'breakpoint))
:fringe (if (and breakpoint-dap (gethash "verified" breakpoint-dap))
'dap-ui-breakpoint-verified-fringe
'breakpoint-disabled)
:priority 1))
(defun dap-ui--refresh-breakpoints ()
"Refresh breakpoints in FILE-NAME.
DEBUG-SESSION the new breakpoints for FILE-NAME."
(dap-ui--clear-breakpoint-overlays)
(-map (-lambda ((bp . remote-bp))
(push (dap-ui--make-overlay-at buffer-file-name
(dap-breakpoint-get-point bp)
"Breakpoint"
(dap-ui--breakpoint-visuals bp remote-bp))
dap-ui--breakpoint-overlays))
(-zip-fill
nil
(gethash buffer-file-name (dap--get-breakpoints))
(-some->> (dap--cur-session) dap--debug-session-breakpoints (gethash buffer-file-name))))
(save-mark-and-excursion
(dap-ui--stack-frame-changed (dap--cur-session))))
(defun dap-ui--clear-marker-overlay (&rest _rest)
"Clear marker overlay for DEBUG-SESSION."
(--map
(with-current-buffer it
(when dap-ui--cursor-overlay
(delete-overlay dap-ui--cursor-overlay)
(setq-local dap-ui--cursor-overlay nil)))
(dap--buffer-list)))
(defun dap-ui--set-debug-marker (file point)
"Set debug marker for DEBUG-SESSION in FILE at POINT."
(dap-ui--clear-marker-overlay)
(setq-local dap-ui--cursor-overlay
(dap-ui--make-overlay-at
file point
"Debug Marker"
(list :face 'dap-ui-marker-face
:char ">"
:bitmap 'right-triangle
:fringe 'dap-ui-compile-errline
:priority 2))))
(defun dap-ui--stack-frame-changed (debug-session)
"Handler for `dap-stack-frame-changed-hook'.
DEBUG-SESSION is the debug session triggering the event."
(when debug-session
(-if-let* (((stack-frame &as &hash "source" "line" "column")
(dap--debug-session-active-frame debug-session))
(path (dap--get-path-for-frame stack-frame))
(buffer (find-buffer-visiting path)))
(with-current-buffer buffer
(goto-char (point-min))
(forward-line (1- line))
(forward-char column)
(dap-ui--set-debug-marker path (point)))
(dap-ui--clear-marker-overlay))))
(defun dap-ui--after-open ()
"Handler for `lsp-after-open-hook'."
(dap-ui--refresh-breakpoints)
(-when-let* ((debug-session (dap--cur-session))
(path (-some->> debug-session
dap--debug-session-active-frame
(gethash "source")
(gethash "path"))))
(when (string= buffer-file-name path)
(dap-ui--stack-frame-changed debug-session))))
(defvar dap-ui-mode-map
(let ((map (make-sparse-keymap)))
(easy-menu-define dap-ui-mode-menu map
"Menu for DAP"
`("DAP Debug"
["Debug" dap-debug]
["Create Debug Template" dap-debug-edit-template]
["Debug last session" dap-debug-last]
("Recent Sessions"
:filter ,(lambda (_)
(-map (-lambda ((name . debug-args))
(vector name (lambda ()
(interactive)
(dap-debug debug-args))))
dap--debug-configuration))
:active dap--debug-configuration)
"--"
["Sessions" dap-ui-sessions]
["Locals" dap-ui-locals]
["Expressions" dap-ui-expressions]
["Sources" dapui-loaded-sources]
["Output" dap-go-to-output-buffer]
["Breakpoints" dap-ui-breakpoints]
"---"
["Toggle Controls" dap-ui-controls-mode]
["Toggle Mouse Hover" dap-tooltip-mode]))
map)
"Keymap for DAP mode.")
;;;###autoload
(define-minor-mode dap-ui-mode
"Displaying DAP visuals."
:init-value nil
:global t
:keymap dap-ui-mode-map
:require 'dap-ui
(cond
(dap-ui-mode
(add-hook 'dap-breakpoints-changed-hook 'dap-ui--refresh-breakpoints)
(add-hook 'dap-continue-hook 'dap-ui--clear-marker-overlay)
(add-hook 'dap-stack-frame-changed-hook 'dap-ui--stack-frame-changed)
(add-hook 'lsp-after-open-hook 'dap-ui--after-open))
(t
(remove-hook 'dap-breakpoints-changed-hook 'dap-ui--refresh-breakpoints)
(remove-hook 'dap-continue-hook 'dap-ui--clear-marker-overlay)
(remove-hook 'dap-stack-frame-changed-hook 'dap-ui--stack-frame-changed)
(remove-hook 'lsp-after-open-hook 'dap-ui--after-open))))
(defun dap-ui--show-buffer (buf)
"Show BUF according to defined rules."
(when-let (win (display-buffer-in-side-window buf
(or (-> buf
buffer-name
(assoc dap-ui-buffer-configurations)
cl-rest)
'((side . right)
(slot . 1)
(window-width . 0.20)))))
(set-window-dedicated-p win t)
(select-window win)))
;; breakpoints
(defun dap-ui--breakpoints-entries ()
"Get breakpoints entries."
(let ((id 0)
result)
(apply 'append
(maphash
(lambda (file-name breakpoints)
(let ((session-breakpoints (-some->> (dap--cur-session)
dap--debug-session-breakpoints
(gethash file-name))))
(with-temp-buffer
(insert-file-contents file-name)
(mapc
(-lambda (((&plist :point :condition :hit-condition :log-message) . remote-bp))
(push `((id ,(setq id (1+ id)))
(file-name . (,file-name ,point))
(line . ,(line-number-at-pos point))
(verified . ,(if (and remote-bp (gethash "verified" remote-bp))
"y"
"n"))
(hit-condition . ,hit-condition)
(log-message . ,log-message)
(condition . ,condition))
result))
(-zip-fill nil breakpoints session-breakpoints)))))
(dap--get-breakpoints)))
(or result (vector))))
(define-button-type 'dap-ui-breakpoint-position
:supertype 'bui
'face 'bui-file-name
'help-echo "Go to breakpoint"
'action (lambda (btn)
(find-file (button-get btn 'file))
(goto-char (button-get btn 'point))))
(defun dap-ui--get-file-info (file-data &optional _)
"Used to render FILE-DATA in breakpoints' list."
(list (f-filename (cl-first file-data))
:type 'dap-ui-breakpoint-position
'file (cl-first file-data)
'point (cl-second file-data)))
(bui-define-interface dap-ui-breakpoints-ui list
:buffer-name "*Breakpoints*"
:get-entries-function 'dap-ui--breakpoints-entries
:format '((file-name dap-ui--get-file-info 30 t)
(line nil 8 bui-list-sort-numerically-2)
(verified nil 8 t)
(condition nil 25 t)
(hit-condition nil 20 t)
(log-message nil 15 t))
:sort-key '(file-name))
(defun dap-ui-breakpoints-goto ()
"Go to breakpoint under cursor."
(interactive)
(--when-let (bui-list-current-entry)
(-let (((file point) (alist-get 'file-name it)))
(find-file file)
(goto-char point))))
(defun dap-ui-breakpoints-delete (breakpoint)
"Delete BREAKPOINT on the current line."
(interactive (list (bui-list-current-entry)))
(-when-let* (((&alist 'file-name (file-name ui-list-point)) breakpoint)
(file-breakpoints (gethash file-name (dap--get-breakpoints)))
(existing-breakpoint (cl-find-if
(-lambda ((&plist :point)) (= ui-list-point point))
file-breakpoints)))
(-some-> existing-breakpoint (plist-get :marker) (set-marker nil))
(dap--breakpoints-changed (cl-remove existing-breakpoint file-breakpoints) file-name)))
(defun dap-ui-breakpoints-delete-selected ()
"Delete breakpoint on the current line."
(interactive)
(->> (bui-list-get-marked 'general)
(-map 'cl-first)
(bui-entries-by-ids (bui-current-entries))
(-map 'dap-ui-breakpoints-delete)))
(let ((map dap-ui-breakpoints-ui-list-mode-map))
(define-key map (kbd "RET") 'dap-ui-breakpoints-goto)
(define-key map (kbd "d") 'dap-ui-breakpoints-delete)
(define-key map (kbd "D") 'dap-ui-breakpoints-delete-selected))
(defun dap-ui-refresh-breakpoints-list ()
"Refresh breakpoints' list."
(with-current-buffer "*Breakpoints*"
(let ((workspace lsp--cur-workspace))
(bui-revert nil t)
(setq-local lsp--cur-workspace workspace)
(run-hooks 'dap-ui-breakpoints-ui-list-displayed-hook))))
(defun dap-ui--brekapoints-list-cleanup ()
"Cleanup when buffer list has been deleted."
(remove-hook 'dap-breakpoints-changed-hook 'dap-ui-refresh-breakpoints-list))
(add-hook
'dap-ui-breakpoints-ui-list-mode-hook
(lambda ()
(add-hook 'dap-breakpoints-changed-hook 'dap-ui-refresh-breakpoints-list)
(add-hook 'kill-buffer-hook 'dap-ui--brekapoints-list-cleanup nil t)))
;;;###autoload
(defun dap-ui-breakpoints-list ()
"List breakpoints."
(interactive)
(let ((workspaces lsp--buffer-workspaces))
(bui-get-display-entries 'dap-ui-breakpoints-ui 'list)
(setq-local lsp--buffer-workspaces workspaces)
(add-hook 'bui-after-redisplay-hook
(lambda () (setq-local lsp--buffer-workspaces workspaces)))
(run-hooks 'dap-ui-breakpoints-ui-list-displayed-hook)))
;; dap-ui posframe stuff
(defvar dap-ui--control-images-root-dir (f-join (f-dirname (or load-file-name buffer-file-name)) "icons/vscode"))
(defvar dap-ui--control-buffer " *dap-ui*")
(defun dap-ui--create-command (image command hover-text)
(propertize " "
'display `(image :type png
:file ,(f-join dap-ui--control-images-root-dir image)
:ascent center
:background ,(face-attribute 'fringe :background nil t))
'local-map (--doto (make-sparse-keymap)
(define-key it [mouse-1] command))
'help-echo hover-text))
(declare-function posframe-show "ext:posframe")
(declare-function posframe-hide "ext:posframe")
(defun dap-ui--update-controls (&rest _)
(let* ((session (dap--cur-session))
(stopped? (and session (dap--debug-session-active-frame session)))
(running? (and session (dap--session-running session))))
(if running?
(let ((content (s-concat
(dap-ui--create-command "continue.png" #'dap-continue "Continue")
(dap-ui--create-command (if stopped?
"step-over.png"
"step-over-disabled.png")
(when stopped? #'dap-next)
(if stopped? "Step over"
"Session not stopped?"))
(dap-ui--create-command (if stopped? "step-out.png"
"step-out-disabled.png")
(when stopped? #'dap-step-out)
(if stopped? "Step out"
"Session not stopped? "))
(dap-ui--create-command (if stopped? "step-into.png"
"step-into-disabled.png")
(when stopped? #'dap-step-in)
(if stopped? "Step in"
"Session not stopped?"))
(dap-ui--create-command "disconnect.png" #'dap-disconnect "Disconnect")
(dap-ui--create-command "restart.png" #'dap-debug-restart "Restart")))
(posframe-mouse-banish nil)
(pos-frame (-first
(lambda (frame)
(let ((buffer-info (frame-parameter frame 'posframe-buffer)))
(or (equal dap-ui--control-buffer (car buffer-info))
(equal dap-ui--control-buffer (cdr buffer-info)))))
(frame-list))))
(when (eq (selected-frame) pos-frame)
(select-frame (frame-parent pos-frame)))
(posframe-show dap-ui--control-buffer
:string content
:poshandler #'posframe-poshandler-frame-top-center))
(posframe-hide dap-ui--control-buffer))))
;;;###autoload
(define-minor-mode dap-ui-controls-mode
"Displaying DAP visuals."
:init-value nil
:global t
:require 'dap-ui
(unless (require 'posframe nil t)
(error "Displaying DAP controls requires that the posframe Emacs package is installed"))
(cond
(dap-ui-controls-mode
(add-hook 'dap-session-changed-hook 'dap-ui--update-controls)
(add-hook 'dap-terminated-hook 'dap-ui--update-controls )
(add-hook 'dap-session-changed-hook 'dap-ui--update-controls)
(add-hook 'dap-continue-hook 'dap-ui--update-controls)
(add-hook 'dap-stack-frame-changed-hook 'dap-ui--update-controls)
(setq posframe-mouse-banish nil)
(dap-ui--update-controls))
(t
(remove-hook 'dap-session-changed-hook 'dap-ui--update-controls)
(remove-hook 'dap-terminated-hook 'dap-ui--update-controls )
(remove-hook 'dap-session-changed-hook 'dap-ui--update-controls)
(remove-hook 'dap-continue-hook 'dap-ui--update-controls)
(remove-hook 'dap-stack-frame-changed-hook 'dap-ui--update-controls)
(setq posframe-mouse-banish t)
(posframe-hide dap-ui--control-buffer))))
;; sessions
(defmacro dap-ui-define-action (name keys &rest body)
(declare (doc-string 3) (indent 2))
`(defun ,name (&rest args)
,(format "Code action %s" name)
(interactive)
(if-let (node (treemacs-node-at-point))
(-let [,(cons '&plist keys) (button-get node :item)]
,@body)
(treemacs-pulse-on-failure "No node at point"))))
(dap-ui-define-action dap-ui-session-select (:session)
(dap--switch-to-session session))
(dap-ui-define-action dap-ui-thread-select (:session :thread-id)
(setf (dap--debug-session-thread-id session) thread-id)
(dap--switch-to-session session)
(dap--select-thread-id session thread-id))
(dap-ui-define-action dap-ui-delete-session (:session)
(dap-delete-session session))
(dap-ui-define-action dap-ui-disconnect (:session)
(dap-disconnect session))
(dap-ui-define-action dap-ui-continue (:session :thread-id)
(dap-continue session thread-id))
(dap-ui-define-action dap-ui-restart-frame (:session :stack-frame)
(dap-restart-frame session (gethash "id" stack-frame)))
(dap-ui-define-action dap-ui-select-stack-frame (:session :thread-id :stack-frame)
(setf (dap--debug-session-thread-id session) thread-id
(dap--debug-session-active-frame session) stack-frame)
(dap--switch-to-session session))
(dap-ui-define-action dap-ui-thread-stop (:session :thread-id)
(dap-stop-thread-1 session thread-id))
(defvar dap-ui-session-mode-map
(-doto (make-sparse-keymap)
(define-key (kbd "X") #'dap-ui-disconnect)
(define-key (kbd "D") #'dap-ui-delete-session)
(define-key (kbd "C") #'dap-ui-continue)
(define-key (kbd "S") #'dap-ui-thread-stop)))
(define-minor-mode dap-ui-sessions-mode
"UI Session list minor mode."
:init-value nil
:group dap-ui
:keymap dap-ui-session-mode-map)
(defun dap-ui--sessions-tree ()
(->>
"debug-sessions"
(lsp-workspace-get-metadata)
(reverse)
(-map
(-lambda ((session &as &dap-session 'name 'thread-states))
(list
:label (propertize name 'face (dap-ui-session--calculate-face session))
:key name
:session session
:ret-action #'dap-ui-session-select
:icon (if (dap--session-running session)
'session-started
'session-terminated)
:actions (if (dap--session-running session)
`(["Select" dap-ui-session-select]
["Disconnect" dap-ui-disconnect]
["Delete Session" dap-ui-delete-session]
"--"
["Delete All Sessions" dap-delete-all-sessions])
`(["Select" dap-ui-session-select]
["Delete Session" dap-ui-delete-session]
"--"
["Delete All Sessions" dap-delete-all-sessions]))
:children-async
(when (dap--session-running session)
(lambda (_node callback)
(dap--send-message
(dap--make-request "threads")
(-lambda ((&hash? "body" (&hash? "threads")))
(funcall
callback
(-map
(-lambda ((thread &as &hash "name" "id"))
(let* ((status (s-capitalize (gethash id thread-states "running")))
(stopped? (not (string= (s-downcase status) "running"))))
(list
:label (concat (propertize name
'face (if (and (eq session (dap--cur-session))
(eq id (dap--debug-session-thread-id session)))
'dap-ui-sessions-thread-active-face
'dap-ui-sessions-thread-face))
(when status (propertize (concat " " status) 'face 'lsp-lens-face)))
:key (format "%s" id)
:icon (if stopped? 'thread-stopped 'thread-running)
:ret-action #'dap-ui-thread-select
:session session
:thread-id id
:actions (if stopped?
`(["Select" dap-ui-thread-select]
["Continue" dap-ui-continue]
"--"
["Delete All Sessions" dap-delete-all-sessions])
`(["Select" dap-ui-thread-select]
["Stop thread" dap-ui-thread-stop]
"--"
["Delete All Sessions" dap-delete-all-sessions]))
:children-async
(when stopped?
(-lambda (_node callback)
(dap--send-message
(dap--make-request "stackTrace" (list :threadId id))
(-lambda ((&hash? "body" (&hash? "stackFrames" stack-frames)))
(funcall
callback
(-map
(-lambda ((stack-frame &as &hash "name" "line" "source"))
(let* ((current-session (dap--cur-session))
(icon (if (and
(equal session current-session)
(eq id (dap--debug-session-thread-id current-session))
(equal stack-frame (dap--debug-session-active-frame current-session)))
'stack-stopped
'stack)))
(list
:session session
:actions (if stopped?
`(["Select" dap-ui-select-stack-frame]
["Continue" dap-ui-continue]
["Restart Frame" dap-ui-restart-frame]
"--"
["Delete All Sessions" dap-delete-all-sessions])
`(["Select" dap-ui-select-stack-frame]
["Stop thread" dap-ui-thread-stop]
"--"
["Delete All Sessions" dap-delete-all-sessions]))
:thread-id id
:stack-frame stack-frame
:ret-action #'dap-ui-select-stack-frame
:label (if source
(concat (propertize
name
'face (if (and (eq session (dap--cur-session))
(eq id (dap--debug-session-thread-id session))
(equal name (-some->> current-session
dap--debug-session-active-frame
(gethash "name"))))
'dap-ui-sessions-thread-active-face
'dap-ui-sessions-thread-face))
(propertize (format " (%s:%s)" (or (gethash "name" source)
(gethash "path" source))
line)
'face 'lsp-lens-face))
(concat name (propertize "(Unknown source)" 'face 'lsp-lens-face)))
:key name
:icon icon)))
stack-frames)))
session))))))
threads)))
session))))))))
(defun dap-ui-sessions--refresh (&rest _)
(save-excursion
(lsp-treemacs-wcb-unless-killed dap-ui--sessions-buffer
(setq-local lsp-treemacs-tree (dap-ui--sessions-tree))
(lsp-treemacs-generic-refresh))))
(defun dap-ui-sessions--cleanup-hooks ()
"Remove UI sessions related hooks."
(remove-hook 'dap-terminated-hook #'dap-ui-sessions--refresh)
(remove-hook 'dap-session-changed-hook #'dap-ui-sessions--refresh)
(remove-hook 'dap-continue-hook #'dap-ui-sessions--refresh)
(remove-hook 'dap-stack-frame-changed-hook 'dap-ui-sessions--refresh))
;;;###autoload
(defun dap-ui-sessions ()
"Show currently active sessions."
(interactive)
(dap-ui--show-buffer
(lsp-treemacs-render
(dap-ui--sessions-tree)
" Debug Sessions " nil
dap-ui--sessions-buffer
'(["Delete All Sessions" dap-delete-all-sessions])))
(dap-ui-sessions-mode)
(add-hook 'dap-terminated-hook #'dap-ui-sessions--refresh)
(add-hook 'dap-session-changed-hook #'dap-ui-sessions--refresh)
(add-hook 'dap-continue-hook #'dap-ui-sessions--refresh)
(add-hook 'dap-stack-frame-changed-hook #'dap-ui-sessions--refresh)
(add-hook 'kill-buffer-hook #'dap-ui-sessions--cleanup-hooks nil t))
;; locals
(dap-ui-define-action dap-ui-set-variable-value (:session :variables-reference :value :name)
(dap--send-message
(dap--make-request "setVariable"
(list :variablesReference variables-reference
:name name
:value (read-string (format "Enter value for %s: " name ) value)))
(-lambda (result))
session))
(defun dap-ui-render-variables (debug-session variables-reference _node)
(when (dap--session-running debug-session)
(->>
variables-reference
(dap-request debug-session "variables" :variablesReference)
(gethash "variables")
(-map (-lambda ((&hash "value" "name"
"indexedVariables" indexed-variables
"variablesReference" variables-reference))
`(:label ,(concat (propertize (format "%s" name)
'face 'font-lock-variable-name-face)
": "
value)
:icon variable
:value ,value
:session ,debug-session
:variables-reference ,variables-reference
:name ,name
,@(list :actions '(["Set value" dap-ui-set-variable-value]))
:key ,name
,@(when (and variables-reference (not (zerop variables-reference)))
(list :children (-partial #'dap-ui-render-variables
debug-session
variables-reference)))))))))
(defvar dap-ui--locals-timer nil)
(defun dap-ui-locals--refresh (&rest _)
(save-excursion
(setq dap-ui--locals-timer nil)
(with-current-buffer (get-buffer-create dap-ui--locals-buffer)
(or (-some--> (dap--cur-session)
(dap--debug-session-active-frame it)
(gethash "id" it)
(dap-request (dap--cur-session) "scopes" :frameId it)
(gethash "scopes" it)
(-map (-lambda ((&hash "name" "variablesReference" variables-reference))
(list :key name
:label name
:icon 'scope
:children (-partial #'dap-ui-render-variables
(dap--cur-session)
variables-reference)))
it)
(lsp-treemacs-render it " Locals " nil dap-ui--locals-buffer)
(or it t))
(lsp-treemacs-render
'((:label "Nothing to display..."
:key "foo"
:icon :empty))
" Locals :: no locals info "
nil
dap-ui--locals-buffer)))))
(defun dap-ui-locals--refresh-schedule (&rest _)
(lsp-treemacs-wcb-unless-killed dap-ui--locals-buffer
(when dap-ui--locals-timer (cancel-timer dap-ui--locals-timer))
(setq-local dap-ui--locals-timer (run-with-idle-timer 0.2 nil #'dap-ui-locals--refresh))))
(defun dap-ui-locals--cleanup-hooks ()
(remove-hook 'dap-terminated-hook #'dap-ui-locals--refresh-schedule)
(remove-hook 'dap-session-changed-hook #'dap-ui-locals--refresh-schedule)
(remove-hook 'dap-continue-hook #'dap-ui-locals--refresh-schedule)
(remove-hook 'dap-stack-frame-changed-hook #'dap-ui-locals--refresh-schedule))
;;;###autoload
(defun dap-ui-locals ()
(interactive)
(dap-ui--show-buffer (get-buffer-create dap-ui--locals-buffer))
(dap-ui-locals--refresh)
(with-current-buffer dap-ui--locals-buffer
(add-hook 'dap-terminated-hook #'dap-ui-locals--refresh-schedule)
(add-hook 'dap-session-changed-hook #'dap-ui-locals--refresh-schedule)
(add-hook 'dap-continue-hook #'dap-ui-locals--refresh-schedule)
(add-hook 'dap-stack-frame-changed-hook #'dap-ui-locals--refresh-schedule)
(add-hook 'kill-buffer-hook #'dap-ui-locals--cleanup-hooks nil t)))
;; watch expressions
(defcustom dap-ui-expressions nil
"The watch expressions."
:type '(repeat string)
:group 'dap-ui)
(defun dap-ui-expressions-add (expression)
(interactive (list (read-string
"Add watch expression: "
(cond
((region-active-p) (buffer-substring-no-properties
(region-beginning)
(region-end)))
(t (symbol-at-point))))))
(when (-contains? dap-ui-expressions expression)
(user-error "\"%s\" is already watched." expression))
(add-to-list 'dap-ui-expressions expression)
(dap-ui-expressions)
(dap-ui-expressions-refresh))
(defun dap-ui-expressions-remove (expression)
(interactive (list (completing-read
"Select expression to remove: "
dap-ui-expressions
nil
t)))
(unless (-contains? dap-ui-expressions expression)
(user-error "\"%s\" is not present." expression))
(setq dap-ui-expressions (remove expression dap-ui-expressions))
(dap-ui-expressions-refresh))
(dap-ui-define-action dap-ui-expressions-mouse-remove (:expression)
(dap-ui-expressions-remove expression))
(defun dap-ui-expressions-refresh ()
(interactive)
(save-excursion
(with-current-buffer (get-buffer-create dap-ui--expressions-buffer)
(lsp-treemacs-render
(let ((debug-session (dap--cur-session)))
(-map
(if-let ((active-frame-id (when (dap--session-running debug-session)
(-some->> debug-session
dap--debug-session-active-frame
(gethash "id")))))
(lambda (expression)
(condition-case err
(-let [(&hash "result" "variablesReference" variables-reference)
(dap-request
(dap--cur-session)
"evaluate"
:expression expression
:frameId active-frame-id)]
`(:key ,expression
:expression ,expression
:label ,(concat (propertize (format "%s: " expression) 'face 'font-lock-variable-name-face)
result)
:icon expression
,@(when (and variables-reference (not (zerop variables-reference)))
(list :children (-partial #'dap-ui-render-variables debug-session variables-reference)))
:actions (["Remove" dap-ui-expressions-mouse-remove]
"--"
["Add" dap-ui-expressions-add]
["Refresh" dap-ui-expressions-refresh])))
(error `(:key ,expression
:label ,(concat (propertize (format "%s: " expression) 'face 'font-lock-variable-name-face)
(propertize (error-message-string err) 'face 'error))
:icon failed-expression
:actions (["Remove" dap-ui-expressions-mouse-remove]
"--"
["Add" dap-ui-expressions-add]
["Refresh" dap-ui-expressions-refresh])))))
(lambda (expression)
`(:key ,expression
:expression ,expression
:label ,(concat
(propertize (format "%s: " expression) 'face 'font-lock-variable-name-face)
(propertize "not available" 'face 'italic))
:icon expression
:actions (["Remove" dap-ui-expressions-mouse-remove]
"--"
["Add" dap-ui-expressions-add]
["Refresh" dap-ui-expressions-refresh]))))
dap-ui-expressions))
" Expressions "
nil
dap-ui--expressions-buffer
'(["Add" dap-ui-expressions-add]
["Refresh" dap-ui-expressions-refresh])))))
(defvar dap-ui--watches-timer nil)
(defun dap-ui-expressions--refresh-schedule (&rest _)
(lsp-treemacs-wcb-unless-killed dap-ui--expressions-buffer
(when dap-ui--watches-timer (cancel-timer dap-ui--watches-timer))
(setq-local dap-ui--watches-timer (run-with-idle-timer 0.2 nil #'dap-ui-expressions-refresh))))
(defun dap-ui-expressions--cleanup-hooks ()
(remove-hook 'dap-terminated-hook #'dap-ui-expressions--refresh-schedule)
(remove-hook 'dap-session-changed-hook #'dap-ui-expressions--refresh-schedule)
(remove-hook 'dap-continue-hook #'dap-ui-expressions--refresh-schedule)
(remove-hook 'dap-stack-frame-changed-hook #'dap-ui-expressions--refresh-schedule))
(defun dap-ui-expressions ()
(interactive)
(dap-ui--show-buffer (get-buffer-create dap-ui--expressions-buffer))
(dap-ui-expressions-refresh)
(add-hook 'dap-terminated-hook #'dap-ui-expressions--refresh-schedule)
(add-hook 'dap-session-changed-hook #'dap-ui-expressions--refresh-schedule)
(add-hook 'dap-continue-hook #'dap-ui-expressions--refresh-schedule)
(add-hook 'dap-stack-frame-changed-hook #'dap-ui-expressions--refresh-schedule)
(add-hook 'kill-buffer-hook #'dap-ui-expressions--cleanup-hooks nil t))
(make-obsolete 'dap-ui-inspect 'dap-ui-expressions-add "dap-mode 0.2")
(make-obsolete 'dap-ui-inspect-region 'dap-ui-expressions-add "dap-mode 0.2")
(make-obsolete 'dap-ui-inspect-thing-at-point 'dap-ui-expressions-add "dap-mode 0.2")
;; Breakpoints - new
(defvar dap-exception-breakpoints nil)
(dap-ui-define-action dap-ui-breakpoints-toggle (:filter :session :default)
(let ((type (plist-get (dap--debug-session-launch-args session) :type)))
(setf (alist-get
filter
(alist-get type dap-exception-breakpoints nil nil #'string=)
nil nil #'string=)
(not (dap--breakpoint-filter-enabled
filter
type
default))))
(dap--set-exception-breakpoints session #'dap-ui-breakpoints--refresh))
(dap-ui-define-action dap-ui-breakpoints-goto-breakpoint (:file-name :point)
(select-window (get-mru-window (selected-frame) nil))
(find-file file-name)
(goto-char point))
(dap-ui-define-action dap-ui-breakpoint-delete (:file-name :breakpoint)
(dap-breakpoint-delete breakpoint file-name))
(dap-ui-define-action dap-ui-breakpoint-condition (:file-name :breakpoint)
(dap-breakpoint-condition file-name breakpoint))
(dap-ui-define-action dap-ui-breakpoint-hit-condition (:file-name :breakpoint)
(dap-breakpoint-hit-condition file-name breakpoint))
(dap-ui-define-action dap-ui-breakpoint-log-message (:file-name :breakpoint)
(dap-breakpoint-log-message file-name breakpoint))
(defun dap-ui--breakpoints-data ()
(-let (((debug-session &as &dap-session 'launch-args 'initialize-result 'breakpoints all-session-breakpoints)
(or (dap--cur-session)
(make-dap--debug-session)))
(lsp-file-truename-cache (ht)))
(lsp-with-cached-filetrue-name
(append
(when (dap--session-running debug-session)
(-some->> initialize-result
(gethash "body")
(gethash "exceptionBreakpointFilters")
(-map (-lambda ((&hash "label" "filter" "default"))
(list :label (propertize
(format "%s %s"
(if (dap--breakpoint-filter-enabled
filter
(plist-get launch-args :type)
default)