Skip to content

Commit 383761e

Browse files
committed
Add hot-reload/restart on save
1 parent eaa1983 commit 383761e

File tree

2 files changed

+73
-43
lines changed

2 files changed

+73
-43
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ After the debug session has started, you can run:
123123

124124
`lsp-dart-flutter-hot-reload` - Trigger Flutter hot reload on the debug sessions.
125125

126+
For hot-reload/restart on buffer save you can enable `lsp-dart-dap-flutter-hot-reload-on-save` or `lsp-dart-dap-flutter-hot-restart-on-save`.
127+
126128
#### Custom templates
127129

128130
You can register a custom template for debugging with `dap-register-debug-template`, check the following example:
@@ -151,7 +153,7 @@ You can also open the [Dart DevTools](https://dart.dev/tools/dart-devtools) on t
151153

152154
### Commands
153155

154-
`lsp-dart` support running Flutter and Dart commands as following:
156+
lsp-dart supports running Flutter and Dart commands as following:
155157

156158
`lsp-dart-pub-get` - Run `pub get` or `flutter pub get` on project root.
157159

@@ -163,13 +165,13 @@ You can also open the [Dart DevTools](https://dart.dev/tools/dart-devtools) on t
163165

164166
## Supported settings
165167

166-
| Variable | Description | Default |
167-
|:--------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------|
168-
| `lsp-dart-sdk-dir` | The optional Dart SDK path. If nil and in a flutter project, it will try to find the dart SDK from Flutter SDK cache dir, otherwise it will search for a dart executable in `$PATH`. | `nil` |
169-
| `lsp-dart-flutter-sdk-dir` | The optional Flutter SDK path. If nil, it will try to find the Flutter SDK from the `flutter` executable in `$PATH` and if not found, it will try in `$FLUTTER_ROOT`. | `nil` |
170-
| `lsp-dart-server-command` | `analysis_server` executable to use | Check source file |
171-
| `lsp-dart-enable-sdk-formatter` | Whether to enable server formatting. | `t` |
172-
| `lsp-dart-line-length` | Line length used by server formatter. | 80 |
168+
| Variable | Description | Default |
169+
|:-------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------|
170+
| `lsp-dart-sdk-dir` | The optional Dart SDK path. If nil and in a flutter project, it will try to find the dart SDK from Flutter SDK cache dir, otherwise it will search for a dart executable in `$PATH`. | `nil` |
171+
| `lsp-dart-flutter-sdk-dir` | The optional Flutter SDK path. If nil, it will try to find the Flutter SDK from the `flutter` executable in `$PATH` and if not found, it will try in `$FLUTTER_ROOT`. | `nil` |
172+
| `lsp-dart-server-command` | `analysis_server` executable to use | Check source file |
173+
| `lsp-dart-enable-sdk-formatter` | Whether to enable server formatting. | `t` |
174+
| `lsp-dart-line-length` | Line length used by server formatter. | 80 |
173175
| `lsp-dart-extra-library-directories` | Extra libs to analyze besides Dart SDK libs | `'()` |
174176
| `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 | `t` |
175177
| `lsp-dart-suggest-from-unimported-libraries` | Completion will not include symbols that are not already imported into the current file. | `t` |
@@ -178,7 +180,7 @@ You can also open the [Dart DevTools](https://dart.dev/tools/dart-devtools) on t
178180
| `lsp-dart-closing-labels-size` | The font size factor to be multiplied by the closing labels font size | 0.9 |
179181
| `lsp-dart-outline` | Enable the outline tree view feature on server lsp | `t` |
180182
| `lsp-dart-outline-position-params` | The outline tree position params. | Left side |
181-
| `lsp-dart-flutter-outline` | Whether to enable the Flutter outline tree view feature on server lsp | `t` |
183+
| `lsp-dart-flutter-outline` | Whether to enable the Flutter outline tree view feature on server lsp | `t` |
182184
| `lsp-dart-flutter-outline-position-params` | The Flutter outline tree position params | Left side |
183185
| `lsp-dart-flutter-fringe-colors` | Enable the Flutter colors on fringe. | `t` |
184186
| `lsp-dart-flutter-widget-guides` | Enable the Flutter widget guide lines from parent to child widgets | `t` |
@@ -196,14 +198,15 @@ You can also open the [Dart DevTools](https://dart.dev/tools/dart-devtools) on t
196198
| `lsp-dart-dap-flutter-track-widget-creation` | Whether to pass –track-widget-creation to Flutter apps. Required to support 'Inspect Widget'. | `t` |
197199
| `lsp-dart-dap-flutter-structured-errors` | Whether to use Flutter’s structured error support for improve error display. | `t` |
198200
| `lsp-dart-dap-flutter-verbose-log` | Whether to enable verbose logs from Flutter DAP | `nil` |
201+
| `lsp-dart-dap-flutter-hot-reload-on-save` | When enabled, every buffer save triggers a `lsp-dart-dap-flutter-hot-reload` | `nil` |
202+
| `lsp-dart-dap-flutter-hot-restart-on-save` | When enabled, every buffer save triggers a `lsp-dart-dap-flutter-hot-restart` | `nil` |
199203
| `lsp-dart-devtools-theme` | The devtools theme when openning via `lsp-dart-dap-open-devtools` | `"dark"` |
200204
| `lsp-dart-devtools-hide-options` | What to hide when openning DevTools via `lsp-dart-dap-open-devtools` | `debugger` |
201205

202206
## Additional packages
203207
* [lsp-ui](https://github.com/emacs-lsp/lsp-ui) : Flycheck, documentation and code actions support.
204208
* [lsp-treemacs](https://github.com/emacs-lsp/lsp-treemacs) : `lsp-mode` GUI controls implemented using treemacs.
205-
* [company-capf](https://github.com/company-mode/company-mode) : Completion backend support.
206-
* [flutter.el](https://github.com/amake/flutter.el) : Tool to run emulator from emacs.
209+
* [company-capf](https://github.com/company-mode/company-mode) : Completion back-end support.
207210
* [hover.el](https://github.com/ericdallo/hover.el) : Tool to run flutter mobile apps from desktop without the need of an emulator.
208211

209212
## FAQ
@@ -219,9 +222,10 @@ You can also open the [Dart DevTools](https://dart.dev/tools/dart-devtools) on t
219222

220223
:grey_question: `LSP :: No LSP server for dart-mode(check *lsp-log*).`
221224

222-
:small_blue_diamond: Try to set the `lsp-dart-sdk-dir` to the Dart SDK dir instalation or if you are using Flutter, `<your-flutter-dir>/bin/cache/dart-sdk/`.
225+
:small_blue_diamond: Try to set the `lsp-dart-sdk-dir` to the Dart SDK dir installation or if you are using Flutter, `<your-flutter-dir>/bin/cache/dart-sdk/`.
223226

224227
## Community
228+
225229
All feedback and suggestions are very welcome!
226230

227-
You can [open a issue](https://github.com/emacs-lsp/lsp-dart/issues/new/choose) or for a quick anwser, send a message on [Gitter](https://gitter.im/emacs-lsp/lsp-mode).
231+
You can [open a issue](https://github.com/emacs-lsp/lsp-dart/issues/new/choose) or for a quick answer, send a message on [Gitter](https://gitter.im/emacs-lsp/lsp-mode).

lsp-dart-dap.el

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ Required to support 'Inspect Widget'."
116116
:group 'lsp-dart
117117
:type 'boolean)
118118

119+
(defcustom lsp-dart-dap-flutter-hot-reload-on-save nil
120+
"Send hot reload to flutter during the debug."
121+
:group 'lsp-dart
122+
:type 'boolean)
123+
124+
(defcustom lsp-dart-dap-flutter-hot-restart-on-save nil
125+
"Send hot restart to flutter during the debug."
126+
:group 'lsp-dart
127+
:type 'boolean)
128+
119129

120130
;;; Internal
121131

@@ -250,6 +260,12 @@ Call CALLBACK when the device is chosen and started successfully."
250260
(defvar lsp-dart-dap--flutter-progress-reporter nil)
251261
(defvar lsp-dart-dap--flutter-progress-reporter-timer nil)
252262

263+
(defconst lsp-dart-dap--debug-prefix
264+
(concat (propertize "[LSP Dart] "
265+
'face 'font-lock-keyword-face)
266+
(propertize "[DAP] "
267+
'face 'font-lock-function-name-face)))
268+
253269
(defun lsp-dart-dap--cancel-flutter-progress (_debug-session)
254270
"Cancel the Flutter progress timer for DEBUG-SESSION."
255271
(setq lsp-dart-dap--flutter-progress-reporter nil)
@@ -273,35 +289,23 @@ Call CALLBACK when the device is chosen and started successfully."
273289

274290
(cl-defmethod dap-handle-event ((_event (eql dart.progressStart)) _session params)
275291
"Handle debugger uris EVENT for SESSION with PARAMS."
276-
(let ((prefix (concat (propertize "[LSP Dart] "
277-
'face 'font-lock-keyword-face)
278-
(propertize "[DAP] "
279-
'face 'font-lock-function-name-face))))
280-
(setq lsp-dart-dap--flutter-progress-reporter
281-
(make-progress-reporter (concat prefix (gethash "message" params))))
282-
(setq lsp-dart-dap--flutter-progress-reporter-timer
283-
(run-with-timer 0.2 0.2 #'lsp-dart-dap--flutter-tick-progress-update))))
292+
(setq lsp-dart-dap--flutter-progress-reporter
293+
(make-progress-reporter (concat lsp-dart-dap--debug-prefix (gethash "message" params))))
294+
(setq lsp-dart-dap--flutter-progress-reporter-timer
295+
(run-with-timer 0.2 0.2 #'lsp-dart-dap--flutter-tick-progress-update)))
284296

285297
(cl-defmethod dap-handle-event ((_event (eql dart.progressEnd)) _session _params)
286298
"Handle debugger uris EVENT for SESSION with PARAMS."
287-
(when lsp-dart-dap--flutter-progress-reporter
288-
(progress-reporter-done lsp-dart-dap--flutter-progress-reporter))
289-
(lsp-dart-dap-log "Loading app..."))
299+
(lsp-dart-dap--cancel-flutter-progress (dap--cur-session)))
290300

291301
(cl-defmethod dap-handle-event ((_event (eql dart.progressUpdate)) _session params)
292302
"Handle debugger uris EVENT for SESSION with PARAMS."
293-
(message "update - receive %s" params)
294-
(-let (((&hash "message") params)
295-
(prefix (concat (propertize "[LSP Dart] "
296-
'face 'font-lock-keyword-face)
297-
(propertize "[DAP] "
298-
'face 'font-lock-function-name-face))))
303+
(-let (((&hash "message") params))
299304
(when (and message lsp-dart-dap--flutter-progress-reporter)
300-
(progress-reporter-force-update lsp-dart-dap--flutter-progress-reporter nil (concat prefix message)))))
305+
(progress-reporter-force-update lsp-dart-dap--flutter-progress-reporter nil (concat lsp-dart-dap--debug-prefix message)))))
301306

302307
(cl-defmethod dap-handle-event ((_event (eql dart.flutter.firstFrame)) _session _params)
303308
"Handle debugger uris EVENT for SESSION with PARAMS."
304-
(lsp-dart-dap--cancel-flutter-progress (dap--cur-session))
305309
(lsp-dart-dap-log "App ready!"))
306310

307311
(cl-defmethod dap-handle-event ((_event (eql dart.hotRestartRequest)) _session _params)
@@ -312,20 +316,35 @@ Call CALLBACK when the device is chosen and started successfully."
312316
"Ignore this event.")
313317
(cl-defmethod dap-handle-event ((_event (eql dart.navigate)) _session _params)
314318
"Ignore this event.")
315-
(cl-defmethod dap-handle-event ((_event (eql dart.testRunNotification)) _session _params)
316-
"Ignore this event.")
317-
318319
(cl-defmethod dap-handle-event ((_event (eql dart.serviceExtensionAdded)) _session _params)
319320
"Ignore this event.")
320321
(cl-defmethod dap-handle-event ((_event (eql dart.serviceRegistered)) _session _params)
321322
"Ignore this event.")
322-
(cl-defmethod dap-handle-event ((_event (eql dart.flutter.updateIsWidgetCreationTracked)) _session _params)
323+
(cl-defmethod dap-handle-event ((_event (eql dart.flutter.serviceExtensionStateChanged)) _session _params)
323324
"Ignore this event.")
324325
(cl-defmethod dap-handle-event ((_event (eql dart.flutter.updatePlatformOverride)) _session _params)
325326
"Ignore this event.")
326-
(cl-defmethod dap-handle-event ((_event (eql dart.flutter.serviceExtensionStateChanged)) _session _params)
327+
(cl-defmethod dap-handle-event ((_event (eql dart.flutter.updateIsWidgetCreationTracked)) _session _params)
328+
"Ignore this event.")
329+
330+
(cl-defmethod dap-handle-event ((_event (eql dart.testRunNotification)) _session _params)
327331
"Ignore this event.")
328332

333+
(defun lsp-dart-dap--flutter-hot-reload ()
334+
"Hot reload current Flutter debug session."
335+
(dap-request (dap--cur-session) "hotReload"))
336+
337+
(defun lsp-dart-dap--flutter-hot-restart ()
338+
"Hot restart current Flutter debug session."
339+
(dap-request (dap--cur-session) "hotRestart"))
340+
341+
(defun lsp-dart-dap--on-save ()
342+
"Run when `after-save-hook' is triggered."
343+
(if lsp-dart-dap-flutter-hot-restart-on-save
344+
(lsp-dart-dap--flutter-hot-restart)
345+
(when lsp-dart-dap-flutter-hot-reload-on-save
346+
(lsp-dart-dap--flutter-hot-reload))))
347+
329348

330349
;; Public
331350

@@ -389,19 +408,26 @@ Call CALLBACK when the device is chosen and started successfully."
389408

390409
;; Public Interface
391410

392-
;;;###autoload
393411
(defun lsp-dart-dap-flutter-hot-restart ()
394412
"Hot restart current Flutter debug session."
395413
(interactive)
396-
(seq-doseq (debug-session (dap--get-sessions))
397-
(dap-request debug-session "hotRestart")))
414+
(lsp-dart-dap--flutter-hot-restart))
398415

399-
;;;###autoload
400416
(defun lsp-dart-dap-flutter-hot-reload ()
401417
"Hot reload current Flutter debug session."
402418
(interactive)
403-
(seq-doseq (debug-session (dap--get-sessions))
404-
(dap-request debug-session "hotReload")))
419+
(lsp-dart-dap--flutter-hot-reload))
420+
421+
(define-minor-mode lsp-dart-dap-mode
422+
"Mode for when debugging Dart/Flutter code."
423+
nil nil nil
424+
(cond
425+
(lsp-dart-dap-mode
426+
(add-hook 'after-save-hook #'lsp-dart-dap--on-save))
427+
(t
428+
(remove-hook 'after-save-hook #'lsp-dart-dap--on-save))))
429+
430+
(add-hook 'dap-session-created-hook #'lsp-dart-dap-mode)
405431

406432
(provide 'lsp-dart-dap)
407433
;;; lsp-dart-dap.el ends here

0 commit comments

Comments
 (0)