Mastodon hachyterm.io

Here’s how to get language support for the Dart language in (Neo)Vim.

Tools Needed

I assume that you installed (Neo)Vim and the Dart language. For example, Arch has a community package that easily installs dart with your package manager of choice.

Additional tools:

1. Install dartfmt

pub global activate dart_style

You need to add the pub bin directory to your environment variables so that you can run the programs from every directory.

With bash or zsh:

export PATH="~/.pub-cache/bin:$PATH"

With fish:

set -U fish_user_paths ~/.pub-cache/bin $fish_user_paths

2. Install LSP Client for (Neo)Vim

2.1. ALE

If you want to install ALE and Vim Polyglot with minpac as package manager (add to your ~.vimrc or ~/.config/nvim/init.vim):

call minpac#add('dense-analysis/ale')
call minpac#add('sheerun/vim-polyglot')

Don’t forget to load your packages from the Vim command line!

:call minpac#update()

If you use a different package manager, please consult their installation instructions.

Add the following configuration options to your ALE setup (add to your ~.vimrc or ~/.config/nvim/init.vim):


" ALE
let g:ale_fixers = {
\   'dart': ['dartfmt'],
\}

2.2. Language Server

If you want to use ALE for linting your files, you have to tell ALE which language server to use.
For now, ALE works with the dart_language_server. But the plugin is obsolete (as of 2020).

There is an open issue on ALE’s GitHub page to add the official dart analysis server (pull request pending).

The Dart SDK already ships with a language server. Thus, it makes sense to use it as you get it for free when you install Dart.

Install a dedicated language server protocol plugin for Vim/NeoVim.

Dart works well with the vim-lsc and vim-lsc-dart plugins by Nate Bosch.

Install those with your favorite plugin manager. For example with minpac:

call minpac#add('natebosch/vim-lsc')
call minpac#add('natebosch/vim-lsc-dart')

Apply the default configuration (this goes into ~/.vimrc or ~/.config/nvim/init.vim or similar):

let g:lsc_auto_map = v:true

Now, you can open a Dart file and use K to show hover information (similar to VS Code et al.). For more options, see :h lsc.

3. Usage

Run the dart formatter with :ALEFix inside Vim or configure ALE to run it automatically.

Run the language server support with :LSClient<command> (see :h lsc).

Further Reading