Skip to content

Commit 39f6594

Browse files
committed
Add debug for dart and flutter
1 parent 6cb73fb commit 39f6594

File tree

4 files changed

+113
-7
lines changed

4 files changed

+113
-7
lines changed

lsp-dart-dap.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,24 @@
5050
:group 'lsp-dart
5151
:type '(repeat string))
5252

53+
(defcustom lsp-dart-dap-dart-test-debugger-program
54+
`("node" ,(f-join lsp-dart-dap-debugger-path "extension/out/src/debug/dart_test_debug_entry.js"))
55+
"The path to the dart test debugger."
56+
:group 'lsp-dart
57+
:type '(repeat string))
58+
5359
(defcustom lsp-dart-dap-flutter-debugger-program
5460
`("node" ,(f-join lsp-dart-dap-debugger-path "extension/out/src/debug/flutter_debug_entry.js"))
5561
"The path to the Flutter debugger."
5662
:group 'lsp-dart
5763
:type '(repeat string))
5864

65+
(defcustom lsp-dart-dap-flutter-test-debugger-program
66+
`("node" ,(f-join lsp-dart-dap-debugger-path "extension/out/src/debug/flutter_test_debug_entry.js"))
67+
"The path to the dart test debugger."
68+
:group 'lsp-dart
69+
:type '(repeat string))
70+
5971
(defcustom lsp-dart-dap-debug-external-libraries nil
6072
"If non-nil, enable the debug on external libraries."
6173
:group 'lsp-dart
@@ -66,6 +78,11 @@
6678
:group 'lsp-dart
6779
:type 'boolean)
6880

81+
(defcustom lsp-dart-dap-vm-additional-args ""
82+
"Additional args for dart debugging VM."
83+
:group 'lsp-dart
84+
:type 'string)
85+
6986
(defcustom lsp-dart-dap-flutter-track-widget-creation t
7087
"Whether to pass –track-widget-creation to Flutter apps.
7188
Required to support 'Inspect Widget'."
@@ -103,6 +120,8 @@ Required to support 'Inspect Widget'."
103120
"npm" "install" "--prefix" (f-join lsp-dart-dap-debugger-path "extension")
104121
"--no-package-lock" "--silent" "--no-save"))
105122

123+
;; Dart
124+
106125
(dap-utils-github-extension-setup-function
107126
"dap-dart"
108127
"Dart-Code"
@@ -121,13 +140,35 @@ Required to support 'Inspect Widget'."
121140
(dap--put-if-absent :cwd (lsp-dart-project-get-root))
122141
(dap--put-if-absent :program (buffer-file-name))
123142
(dap--put-if-absent :dartPath (lsp-dart-project-dart-command))
143+
(dap--put-if-absent :pubPath (lsp-dart-project-get-pub-command))
144+
(dap--put-if-absent :pubSnapshotPath (lsp-dart-project-pub-snapshot-command))
145+
(dap--put-if-absent :vmAdditionalArgs lsp-dart-dap-vm-additional-args)
124146
(dap--put-if-absent :debugExternalLibraries lsp-dart-dap-debug-external-libraries)
125147
(dap--put-if-absent :debugSdkLibraries lsp-dart-dap-debug-sdk-libraries)))
126148

127149
(dap-register-debug-provider "dart" 'lsp-dart-dap--populate-dart-start-file-args)
128150
(dap-register-debug-template "Dart :: Debug"
129151
(list :type "dart"))
130152

153+
(defun lsp-dart-dap-debug-dart-test (path args)
154+
"Start dart test debugging from PATH with ARGS."
155+
(-> (list :name "Tests"
156+
:type "dart"
157+
:request "launch"
158+
:dap-server-path lsp-dart-dap-dart-test-debugger-program
159+
:noDebug nil
160+
:shouldConnectDebugger t
161+
:cwd (lsp-dart-project-get-root)
162+
:dartPath (lsp-dart-project-dart-command)
163+
:pubPath (lsp-dart-project-get-pub-command)
164+
:pubSnapshotPath (lsp-dart-project-pub-snapshot-command)
165+
:vmAdditionalArgs lsp-dart-dap-vm-additional-args
166+
:debugExternalLibraries lsp-dart-dap-debug-external-libraries
167+
:debugSdkLibraries lsp-dart-dap-debug-sdk-libraries
168+
:program path
169+
:args args)
170+
dap-start-debugging))
171+
131172
;; Flutter
132173

133174
(declare-function all-the-icons-faicon "ext:all-the-icons")
@@ -179,6 +220,7 @@ Call CALLBACK when the device is chosen and started successfully."
179220
(-> pre-conf
180221
(dap--put-if-absent :deviceId device-id)
181222
(dap--put-if-absent :deviceName device-name)
223+
(dap--put-if-absent :vmAdditionalArgs lsp-dart-dap-vm-additional-args)
182224
(dap--put-if-absent :flutterPlatform "default")
183225
(dap--put-if-absent :name (concat "Flutter (" device-name ")")))))))))
184226

@@ -257,6 +299,30 @@ Call CALLBACK when the device is chosen and started successfully."
257299
(cl-defmethod dap-handle-event ((_event (eql dart.navigate)) _session _params)
258300
"Ignore this event.")
259301

302+
(cl-defmethod dap-handle-event ((_event (eql dart.testRunNotification)) _session _params)
303+
"Ignore this event.")
304+
305+
(defun lsp-dart-dap-debug-flutter-test (path args)
306+
"Start dart test debugging from PATH with ARGS."
307+
(-> (list :name "Tests"
308+
:type "flutter"
309+
:request "launch"
310+
:dap-server-path lsp-dart-dap-flutter-test-debugger-program
311+
:noDebug nil
312+
:shouldConnectDebugger t
313+
:cwd (lsp-dart-project-get-root)
314+
:dartPath (lsp-dart-project-dart-command)
315+
:pubPath (lsp-dart-project-get-pub-command)
316+
:pubSnapshotPath (lsp-dart-project-pub-snapshot-command)
317+
:flutterPath (lsp-dart-project-get-flutter-path)
318+
:flutterMode "debug"
319+
:vmAdditionalArgs lsp-dart-dap-vm-additional-args
320+
:debugExternalLibraries lsp-dart-dap-debug-external-libraries
321+
:debugSdkLibraries lsp-dart-dap-debug-sdk-libraries
322+
:program path
323+
:args args)
324+
dap-start-debugging))
325+
260326
;;;###autoload
261327
(defun lsp-dart-dap-flutter-hot-restart ()
262328
"Hot restart current Flutter debug session."

lsp-dart-project.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ flutter cache dir."
7272
file-name-as-directory
7373
(concat "bin/pub")))
7474

75+
(defun lsp-dart-project-pub-snapshot-command ()
76+
"Return the pub snapshot executable path from dart SDK path."
77+
(-> (lsp-dart-project-get-sdk-dir)
78+
file-name-as-directory
79+
(concat "bin/snapshots/pub.dart.snapshot")))
80+
7581
(defun lsp-dart-project-dart-command ()
7682
"Return the dart executable from dart SDK dir."
7783
(expand-file-name "bin/dart" (lsp-dart-project-get-sdk-dir)))

lsp-dart-test-support.el

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
(require 'lsp-mode)
2929

3030
(require 'lsp-dart-project)
31+
(require 'lsp-dart-dap)
3132

3233
(defconst lsp-dart-test-support-tests-buffer-name "*LSP Dart tests*")
3334

@@ -40,6 +41,11 @@
4041
(position nil)
4142
(kind nil))
4243

44+
(defface lsp-dart-code-lens-separator
45+
'((t :height 0.3))
46+
"The face used for separate code lens overlays."
47+
:group 'lsp-dart-test-support)
48+
4349
(defun lsp-dart-test-support--test-kind-p (kind)
4450
"Return non-nil if KIND is a test type."
4551
(or (string= kind "UNIT_TEST_TEST")
@@ -100,9 +106,8 @@ otherwise the dart command."
100106
escaped-str))
101107

102108
(defun lsp-dart-test-support-run (test)
103-
"Run Dart/Flutter test command in a compilation buffer for BUFFER file.
109+
"Run Dart/Flutter test command in a compilation buffer.
104110
If TEST is non nil, it will run only this test."
105-
(interactive)
106111
(lsp-dart-test-support--from-project-root
107112
(let* ((file-name (lsp-dart-test-file-name test))
108113
(buffer (get-file-buffer file-name))
@@ -125,6 +130,22 @@ If TEST is non nil, it will run only this test."
125130
t
126131
(lambda (_) lsp-dart-test-support-tests-buffer-name)))))
127132

133+
(defun lsp-dart-test-support-debug (test)
134+
"Debug Dart/Flutter TEST in a compilation buffer."
135+
(let* ((file-name (lsp-dart-test-file-name test))
136+
(names (lsp-dart-test-names test))
137+
(kind (lsp-dart-test-kind test))
138+
(test-name (lsp-dart-test-support--build-test-name names))
139+
(group-kind? (string= kind "UNIT_TEST_GROUP"))
140+
(regex (concat "^"
141+
(lsp-dart-test-support--escape-test-name test-name)
142+
(unless group-kind? "$")))
143+
(test-arg `("--name" ,regex)))
144+
(lsp-workspace-set-metadata "last-ran-test" test)
145+
(if (lsp-dart-test-support--flutter-test-file-p (get-file-buffer file-name))
146+
(lsp-dart-dap-debug-flutter-test file-name test-arg)
147+
(lsp-dart-dap-debug-dart-test file-name test-arg))))
148+
128149
(defun lsp-dart-test-support--build-overlay (buffer names kind range test-range)
129150
"Build an overlay in BUFFER for a test NAMES of KIND.
130151
RANGE is the overlay range to build.
@@ -138,20 +159,33 @@ TEST-RANGE is the test method range."
138159
(test (make-lsp-dart-test :file-name (buffer-file-name buffer)
139160
:names names
140161
:position beg
141-
:kind kind)))
162+
:kind kind))
163+
(separator (propertize " " 'font-lock-face 'lsp-dart-code-lens-separator)))
142164
(overlay-put overlay 'lsp-dart-test-code-lens t)
143165
(overlay-put overlay 'lsp-dart-test test)
144166
(overlay-put overlay 'lsp-dart-test-overlay-test-range (lsp--range-to-region test-range))
145167
(overlay-put overlay 'before-string
146168
(concat spaces
147-
(propertize "Run\n"
169+
(propertize "Run"
148170
'help-echo "mouse-1: Run this test"
149171
'mouse-face 'lsp-lens-mouse-face
150172
'local-map (-doto (make-sparse-keymap)
151-
(define-key [mouse-1] (lambda ()
173+
(define-key [mouse-1] (lambda ()
152174
(interactive)
153175
(lsp-dart-test-support-run test))))
154-
'font-lock-face 'lsp-lens-face)))))
176+
'font-lock-face 'lsp-lens-face)
177+
separator
178+
(propertize "|" 'font-lock-face 'lsp-lens-face)
179+
separator
180+
(propertize "Debug"
181+
'help-echo "mouse-1: Debug this test"
182+
'mouse-face 'lsp-lens-mouse-face
183+
'local-map (-doto (make-sparse-keymap)
184+
(define-key [mouse-1] (lambda ()
185+
(interactive)
186+
(lsp-dart-test-support-debug test))))
187+
'font-lock-face 'lsp-lens-face)
188+
"\n"))))
155189

156190
(defun lsp-dart-test-support--add-code-lens (buffer items &optional names)
157191
"Add test code lens to BUFFER for ITEMS.

lsp-dart.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
(require 'lsp-mode)
3232

3333
(require 'lsp-dart-project)
34-
(require 'lsp-dart-test-support)
3534
(require 'lsp-dart-dap)
35+
(require 'lsp-dart-test-support)
3636
(require 'lsp-dart-flutter-fringe)
3737
(require 'lsp-dart-flutter-widget-guide)
3838

0 commit comments

Comments
 (0)