Skip to content

lsp-dart#

flutter_logo dart_logo

MELPA MELPA stable CI Gitter

Emacs Dart IDE using lsp-mode to connect to Dart Analysis Server.

flutter_logo LSP works great with Flutter, but there are also other packages that may help to improve your development, for more info see Additional packages.

Quickstart#

The following has a example to setup lsp-dart.

;; Install use-package
(condition-case nil
    (require 'use-package)
  (file-error
   (require 'package)
   (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
   (package-initialize)
   (package-refresh-contents)
   (package-install 'use-package)
   (require 'use-package)))

(use-package lsp-mode :ensure t)
(use-package lsp-dart 
  :ensure t 
  :hook (dart-mode . lsp))

;; Optional packages
(use-package projectile :ensure t) ;; project management
(use-package yasnippet
  :ensure t
  :config (yas-global-mode)) ;; snipets
(use-package lsp-ui :ensure t) ;; UI for LSP
(use-package company :ensure t) ;; Auto-complete

;; Optional Flutter packages
(use-package hover :ensure t) ;; run app from desktop without emulator

Features#

Besides the lsp-mode features, lsp-dart implements the custom methods features from the dart_analysis_server.

Closing labels#

closing-labels

Tree views#

lsp-dart uses lsp-treemacs for rendering some tree views features.

lsp-dart-show-outline ⚠*

outline

lsp-dart-show-flutter-outline ⚠*

flutter-outline

Run/Debug tests#

lsp-dart-run-all-tests - Run all tests from project.

lsp-dart-run-test-file - Run all tests from current test buffer.

lsp-dart-run-test-at-point - Run single test at point.

lsp-dart-debug-test-at-point - Debug single test at point.

lsp-dart-visit-last-test - Go to last ran test.

lsp-dart-run-last-test - Run last ran test.

lsp-dart-debug-last-test - Debug last ran test.

Running a test interactively:

test

Flutter colors#

Display the flutter colors on left fringe.

flutter-fringe-colors

You can disable the feature setting lsp-dart-flutter-fringe-colors to nil.

Flutter widget guides#

Display widget guide lines from parent to child widgets on flutter. ⚠*

flutter-widget-guides

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

Debug#

For debugging, lsp-dart uses dap-mode. You only need to run dap-dart-setup one time to setup the debugger to your Emacs and dap-debug to start the debug.

debug

Flutter#

lsp-dart support Flutter debugging too via dap-debug with an option to select the device to debug.

flutter-debug

After the debug session has started, you can run:

lsp-dart-flutter-hot-restart - Trigger Flutter hot restart on the debug sessions. lsp-dart-flutter-hot-reload - Trigger Flutter hot reload on the debug sessions.

Custom templates#

You can register a custom template for debugging with dap-register-debug-template, check the following example:

(use-package lsp-dart
  :ensure t
  :hook (dart-mode . lsp)
  :init
  (dap-register-debug-template "Flutter :: Custom debug"
                               (list :flutterPlatform "x86_64"
                                     :program "lib/main_debug.dart"
                                     :args '("--flavor" "customer_a"))))
flutterPlatform,:program, and :args are all optional.

A full list of supported parameters can be found in the source of the function lsp-dart-dap--populate-flutter-start-file-args in lsp-dart-dap.el. Several of these parameters can be configured by setting one of the Supported settings with matching names.

DevTools#

You can also open the Dart DevTools on the current debug session with lsp-dart-open-devtools.

⚠* Features only available for Dart SDK version 2.8.0 or above.#

Supported settings#

Variable Description Default
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
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
lsp-dart-server-command analysis_server executable to use Check source file
lsp-dart-extra-library-directories Extra libs to analyze besides Dart SDK libs '()
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
lsp-dart-suggest-from-unimported-libraries Completion will not include symbols that are not already imported into the current file. t
lsp-dart-closing-labels Enable the closing labels feature on server lsp t
lsp-dart-closing-labels-prefix The prefix string to be concatenated with the closing label ""
lsp-dart-closing-labels-size The font size factor to be multiplied by the closing labels font size 0.9
lsp-dart-outline Enable the outline tree view feature on server lsp t
lsp-dart-outline-position-params The outline tree position params. Left side
lsp-dart-flutter-outline Wheter to enable the Flutter outline tree view feature on server lsp t
lsp-dart-flutter-outline-position-params The Flutter outline tree position params Left side
lsp-dart-flutter-fringe-colors Enable the Flutter colors on fringe. t
lsp-dart-flutter-widget-guides Enable the Flutter widget guide lines from parent to child widgets t
lsp-dart-main-code-lens Enable the Run|Debug code lens on main methods. t
lsp-dart-test-code-lens Enable the Run|Debug code lens on tests. t
lsp-dart-dap-extension-version The debugger extension version. 3.10.1
lsp-dart-dap-debugger-path The debugger extension path Check source file
lsp-dart-dap-dart-debugger-program The command to execute the debugger extension on dart projects. Check source file
lsp-dart-dap-dart-test-debugger-program The command to execute the test debugger extension on dart projects. Check source file
lsp-dart-dap-flutter-debugger-program The command to execute the debugger extension on flutter projects. Check source file
lsp-dart-dap-flutter-test-debugger-program The command to execute the test debugger extension on flutter projects. Check source file
lsp-dart-dap-debug-external-libraries Whether to enable the debug on external libraries nil
lsp-dart-dap-debug-sdk-libraries Whether to enable the debug on Dart SDK libraries nil
lsp-dart-dap-vm-additional-args Additional args for dart debugging VM when the debugging. ""
lsp-dart-dap-flutter-track-widget-creation Whether to pass –track-widget-creation to Flutter apps. Required to support 'Inspect Widget'. t
lsp-dart-dap-flutter-structured-errors Whether to use Flutter’s structured error support for improve error display. t
lsp-dart-dap-flutter-verbose-log Whether to enable verbose logs from Flutter DAP nil
lsp-dart-devtools-theme The devtools theme when openning via lsp-dart-dap-open-devtools "dark"
lsp-dart-devtools-hide-options What to hide when openning DevTools via lsp-dart-dap-open-devtools debugger

Additional packages#

  • lsp-ui : Flycheck, documentation and code actions support.
  • lsp-treemacs : lsp-mode GUI controls implemented using treemacs.
  • company-capf : Completion backend support.
  • flutter.el : Tool to run emulator from emacs.
  • hover.el : Tool to run flutter mobile apps from desktop without the need of an emulator.

FAQ#

❔ Emacs can't find my package, it keeps searching up until the dart root package folder.

🔹 Configure projectile to find the package pubspec.yaml and set the folder as project root:

(with-eval-after-load 'projectile
  (add-to-list 'projectile-project-root-files-bottom-up "pubspec.yaml")
  (add-to-list 'projectile-project-root-files-bottom-up "BUILD"))

❔ LSP :: No LSP server for dart-mode(check *lsp-log*).

🔹 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/.

Community#

All feedback and suggestions are very welcome!

You can open a issue or for a quick anwser, send a message on Gitter.


Last update: June 4, 2020