Skip to content

Commit 329fe2d

Browse files
committed
Improve debug output + add new lsp-dart-run command
Fixes #115
1 parent 0e2dbd1 commit 329fe2d

File tree

3 files changed

+75
-33
lines changed

3 files changed

+75
-33
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ Display widget guide lines from parent to child widgets on flutter. [:warning:*]
104104

105105
You can disable the feature setting `lsp-dart-flutter-widget-guides` to `nil`.
106106

107-
### Debug
107+
### Run or Debug
108+
109+
To run your app you can use `lsp-dart-run` which will use `dap-mode` but for running without debug. to pass custom arguments it's possible to pass a prefix-argument with arguments separated by space.
108110

109111
For debugging, `lsp-dart` uses [`dap-mode`](https://github.com/emacs-lsp/dap-mode#dart).
110112
You only need to run `lsp-dart-dap-setup` one time to setup the debugger to your Emacs and `dap-debug` to start the debug.
@@ -176,7 +178,7 @@ lsp-dart supports running Flutter and Dart commands as following:
176178
| `lsp-dart-show-todos` | Whether to generate diagnostics for TODO comments. If unspecified, diagnostics will not be generated. | `nil` |
177179
| `lsp-dart-complete-function-calls` | Completes functions/methods with their required parameters. | `t` |
178180
| `lsp-dart-extra-library-directories` | Extra libs to analyze besides Dart SDK libs | `'()` |
179-
| `lsp-dart-only-analyze-projects-with-open-files` | Analysis will only be performed for projects that have open files rather than the root workspace folder | `nil` |
181+
| `lsp-dart-only-analyze-projects-with-open-files` | Analysis will only be performed for projects that have open files rather than the root workspace folder | `nil` |
180182
| `lsp-dart-suggest-from-unimported-libraries` | Completion will not include symbols that are not already imported into the current file. | `t` |
181183
| `lsp-dart-closing-labels` | Enable the closing labels feature on server lsp | `t` |
182184
| `lsp-dart-closing-labels-prefix` | The prefix string to be concatenated with the closing label | `""` |
@@ -205,7 +207,7 @@ lsp-dart supports running Flutter and Dart commands as following:
205207
| `lsp-dart-dap-vm-additional-args` | Additional args for dart debugging VM when the debugging. | `""` |
206208
| `lsp-dart-dap-flutter-track-widget-creation` | Whether to pass –track-widget-creation to Flutter apps. Required to support 'Inspect Widget'. | `t` |
207209
| `lsp-dart-dap-flutter-structured-errors` | Whether to use Flutter’s structured error support for improve error display. | `t` |
208-
| `lsp-dart-dap-flutter-verbose-log` | Whether to enable verbose logs from Flutter DAP | `nil` |
210+
| `lsp-dart-dap-only-essential-log` | Whether to logs only essential log | `nil` |
209211
| `lsp-dart-dap-flutter-hot-reload-on-save` | When enabled, every buffer save triggers a `lsp-dart-dap-flutter-hot-reload` | `nil` |
210212
| `lsp-dart-dap-flutter-hot-restart-on-save` | When enabled, every buffer save triggers a `lsp-dart-dap-flutter-hot-restart` | `nil` |
211213
| `lsp-dart-devtools-theme` | The devtools theme when openning via `lsp-dart-dap-open-devtools` | `"dark"` |

lsp-dart-dap.el

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ Required to support 'Inspect Widget'."
117117
:group 'lsp-dart
118118
:type 'string)
119119

120-
(defcustom lsp-dart-dap-flutter-verbose-log nil
121-
"Whether to enable logs from Flutter DAP."
120+
(defcustom lsp-dart-dap-only-essential-log nil
121+
"Whether to log only essential log from debugger."
122122
:group 'lsp-dart
123123
:type 'boolean)
124124

@@ -139,6 +139,13 @@ Required to support 'Inspect Widget'."
139139
"Log MSG with ARGS adding lsp-dart-dap prefix."
140140
(apply #'lsp-dart-custom-log "[DAP]" msg args))
141141

142+
(defun lsp-dart-dap--flutter-debugger-args (conf)
143+
"Add capabilities args on CONF checking dart SDK version."
144+
(-> conf
145+
(dap--put-if-absent :flutterSdkPath (lsp-dart-get-flutter-sdk-dir))
146+
(dap--put-if-absent :flutterTrackWidgetCreation lsp-dart-dap-flutter-track-widget-creation)
147+
(dap--put-if-absent :useFlutterStructuredErrors lsp-dart-dap-flutter-structured-errors)))
148+
142149
(defun lsp-dart-dap--capabilities-debugger-args (conf)
143150
"Add capabilities args on CONF checking dart SDK version."
144151
(-> conf
@@ -147,21 +154,18 @@ Required to support 'Inspect Widget'."
147154

148155
(defun lsp-dart-dap--base-debugger-args (conf)
149156
"Return the base args for debugging merged with CONF."
150-
(let ((conf conf))
151-
(dap--put-if-absent conf :request "launch")
152-
(dap--put-if-absent conf :dartSdkPath (lsp-dart-get-sdk-dir))
153-
(dap--put-if-absent conf :maxLogLineLength lsp-dart-dap-max-log-line-length)
154-
(dap--put-if-absent conf :cwd (lsp-dart-get-project-root))
155-
(dap--put-if-absent conf :vmAdditionalArgs lsp-dart-dap-vm-additional-args)
156-
(dap--put-if-absent conf :vmServicePort lsp-dart-dap-vm-service-port)
157-
(dap--put-if-absent conf :debugExternalLibraries lsp-dart-dap-debug-external-libraries)
158-
(dap--put-if-absent conf :debugSdkLibraries lsp-dart-dap-debug-sdk-libraries)
159-
(dap--put-if-absent conf :evaluateGettersInDebugViews lsp-dart-dap-evaluate-getters-in-debug-views)
160-
(dap--put-if-absent conf :evaluateToStringInDebugViews lsp-dart-dap-evaluate-tostring-in-debug-views)
161-
(dap--put-if-absent conf :flutterSdkPath (lsp-dart-get-flutter-sdk-dir))
162-
(dap--put-if-absent conf :flutterTrackWidgetCreation lsp-dart-dap-flutter-track-widget-creation)
163-
(dap--put-if-absent conf :useFlutterStructuredErrors lsp-dart-dap-flutter-structured-errors)
164-
(lsp-dart-dap--capabilities-debugger-args conf)))
157+
(dap--put-if-absent conf :request "launch")
158+
(dap--put-if-absent conf :dartSdkPath (lsp-dart-get-sdk-dir))
159+
(dap--put-if-absent conf :maxLogLineLength lsp-dart-dap-max-log-line-length)
160+
(dap--put-if-absent conf :cwd (lsp-dart-get-project-root))
161+
(dap--put-if-absent conf :vmAdditionalArgs lsp-dart-dap-vm-additional-args)
162+
(dap--put-if-absent conf :vmServicePort lsp-dart-dap-vm-service-port)
163+
(dap--put-if-absent conf :debugExternalLibraries lsp-dart-dap-debug-external-libraries)
164+
(dap--put-if-absent conf :debugSdkLibraries lsp-dart-dap-debug-sdk-libraries)
165+
(dap--put-if-absent conf :evaluateGettersInDebugViews lsp-dart-dap-evaluate-getters-in-debug-views)
166+
(dap--put-if-absent conf :evaluateToStringInDebugViews lsp-dart-dap-evaluate-tostring-in-debug-views)
167+
(lsp-dart-dap--flutter-debugger-args conf)
168+
(lsp-dart-dap--capabilities-debugger-args conf))
165169

166170
(defun lsp-dart-dap--enable-mode (&rest _)
167171
"Enable `lsp-dart-dap-mode'."
@@ -186,6 +190,7 @@ Required to support 'Inspect Widget'."
186190
(dap--put-if-absent :type "dart")
187191
(dap--put-if-absent :name "Dart")
188192
(dap--put-if-absent :dap-server-path lsp-dart-dap-dart-debugger-program)
193+
(dap--put-if-absent :output-filter-function #'lsp-dart-dap--output-filter-function)
189194
(dap--put-if-absent :program (lsp-dart-get-project-entrypoint))))
190195

191196
(dap-register-debug-provider "dart" 'lsp-dart-dap--populate-dart-start-file-args)
@@ -221,21 +226,28 @@ Call CALLBACK when the device is chosen and started successfully."
221226
(lambda (devices)
222227
(if (seq-empty-p devices)
223228
(lsp-dart-log "No devices found. Try to create a device first via `flutter emulators` command")
224-
(let ((chosen-device (dap--completing-read "Select a device to use: "
229+
(let ((chosen-device (dap--completing-read "Select a device to debug/run: "
225230
devices
226231
(-lambda ((&FlutterDaemonDevice :id :name :is-device? :platform-type platform))
227232
(lsp-dart-dap--device-label id name is-device? platform))
228233
nil
229234
t)))
230235
(lsp-dart-flutter-daemon-launch chosen-device callback))))))
231236

237+
(defun lsp-dart-dap--output-filter-function (msg)
238+
"Output filter for dap-mode when parsing MSG."
239+
(if lsp-dart-dap-only-essential-log
240+
msg
241+
""))
242+
232243
(defun lsp-dart-dap--populate-flutter-start-file-args (conf)
233244
"Populate CONF with the required arguments for Flutter debug."
234245
(let ((pre-conf (-> conf
235246
lsp-dart-dap--base-debugger-args
236247
(dap--put-if-absent :type "flutter")
237248
(dap--put-if-absent :flutterMode "debug")
238249
(dap--put-if-absent :dap-server-path lsp-dart-dap-flutter-debugger-program)
250+
(dap--put-if-absent :output-filter-function #'lsp-dart-dap--output-filter-function)
239251
(dap--put-if-absent :program (lsp-dart-get-project-entrypoint)))))
240252
(lambda (start-debugging-callback)
241253
(lsp-dart-dap--flutter-get-or-start-device
@@ -274,12 +286,24 @@ Call CALLBACK when the device is chosen and started successfully."
274286
(when lsp-dart-dap--flutter-progress-reporter
275287
(progress-reporter-update lsp-dart-dap--flutter-progress-reporter)))
276288

289+
(defun lsp-dart-dap--parse-log-message (raw)
290+
"Parse log RAW from debugger events."
291+
(let ((msg (--> raw
292+
(replace-regexp-in-string (regexp-quote "<== ") "" it nil)
293+
(replace-regexp-in-string (regexp-quote "==> ") "" it nil)
294+
(replace-regexp-in-string (regexp-quote "\^M") "" it nil))))
295+
(condition-case nil
296+
(if (booleanp (lsp--read-json msg))
297+
nil)
298+
('error msg))))
299+
277300
(cl-defmethod dap-handle-event ((_event (eql dart.log)) _session params)
278301
"Handle debugger uris EVENT for SESSION with PARAMS."
279-
(when lsp-dart-dap-flutter-verbose-log
280-
(-let* (((&hash "message") params)
281-
(msg (replace-regexp-in-string (regexp-quote "\n") "" message nil 'literal)))
282-
(lsp-dart-dap-log msg))))
302+
(unless lsp-dart-dap-only-essential-log
303+
(when-let (dap-session (dap--cur-session))
304+
(-let* (((&hash "message") params))
305+
(when-let (msg (lsp-dart-dap--parse-log-message message))
306+
(dap--print-to-output-buffer dap-session msg))))))
283307

284308
(cl-defmethod dap-handle-event ((_event (eql dart.progressStart)) _session params)
285309
"Handle debugger uris EVENT for SESSION with PARAMS."
@@ -355,23 +379,25 @@ Call CALLBACK when the device is chosen and started successfully."
355379

356380
;; Public
357381

358-
(defun lsp-dart-dap-run-dart (path)
359-
"Start Dart application without debugging from PATH."
382+
(defun lsp-dart-dap-run-dart (&optional path args)
383+
"Start Dart application without debugging.
384+
Run program PATH if not nil passing ARGS if not nil."
360385
(-> (list :type "dart"
361386
:name "Dart Run"
362387
:program path
363-
:noDebug t
364-
:shouldConnectDebugger nil)
388+
:args args
389+
:noDebug t)
365390
lsp-dart-dap--populate-dart-start-file-args
366391
dap-start-debugging))
367392

368-
(defun lsp-dart-dap-run-flutter (path)
369-
"Start Flutter app without debugging from PATH."
393+
(defun lsp-dart-dap-run-flutter (&optional path args)
394+
"Start Flutter app without debugging.
395+
Run program PATH if not nil passing ARGS if not nil."
370396
(-> (list :type "flutter"
371397
:name "Flutter Run"
372398
:program path
373-
:noDebug t
374-
:shouldConnectDebugger nil)
399+
:args args
400+
:noDebug t)
375401
lsp-dart-dap--populate-flutter-start-file-args
376402
(funcall #'dap-start-debugging)))
377403

lsp-dart.el

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ If the version number could not be determined, signal an error."
194194
project-entrypoint-string)
195195
"\n"))))
196196

197+
;;;###autoload
198+
(defun lsp-dart-run (&optional args)
199+
"Run application without debug mode.
200+
201+
ARGS is an optional space-delimited string of the same flags passed to
202+
`flutter` when running from CLI. Call with a prefix to be prompted for
203+
args."
204+
(interactive
205+
(list (when current-prefix-arg
206+
(split-string (read-string "Args: ") " "))))
207+
(if (lsp-dart-flutter-project-p)
208+
(lsp-dart-dap-run-flutter nil args)
209+
(lsp-dart-dap-run-dart nil args)))
210+
197211

198212
;;;###autoload(with-eval-after-load 'lsp-mode (require 'lsp-dart))
199213

0 commit comments

Comments
 (0)