diff --git a/autoload/dart.vim b/autoload/dart.vim index 2dff18c..bee79c9 100644 --- a/autoload/dart.vim +++ b/autoload/dart.vim @@ -27,9 +27,9 @@ endfunction function! dart#fmt(...) abort let l:dartfmt = s:FindDartFmt() - if type(l:dartfmt) != type('') | return | endif + if empty(l:dartfmt) | return | endif let buffer_content = getline(1, '$') - let l:cmd = [l:dartfmt, '--stdin-name', shellescape(expand('%'))] + let l:cmd = extend(l:dartfmt, ['--stdin-name', shellescape(expand('%'))]) if exists('g:dartfmt_options') call extend(l:cmd, g:dartfmt_options) endif @@ -60,14 +60,30 @@ function! dart#fmt(...) abort endfunction function! s:FindDartFmt() abort - if executable('dartfmt') | return 'dartfmt' | endif + if executable('dart') + let l:version_text = system('dart --version') + let l:match = matchlist(l:version_text, + \ '\vDart SDK version: (\d+)\.(\d+)\.\d+.*') + if empty(l:match) + call s:error('Unable to determine dart version') + return [] + endif + let l:major = l:match[1] + let l:minor = l:match[2] + if l:major > 2 || l:major == 2 && l:minor >= 14 + return ['dart', 'format'] + endif + endif + " Legacy fallback for Dart SDK pre 2.14 + if executable('dartfmt') | return ['dartfmt'] | endif if executable('flutter') let l:flutter_cmd = resolve(exepath('flutter')) let l:bin = fnamemodify(l:flutter_cmd, ':h') let l:dartfmt = l:bin.'/cache/dart-sdk/bin/dartfmt' - if executable(l:dartfmt) | return l:dartfmt | endif + if executable(l:dartfmt) | return [l:dartfmt] | endif endif call s:error('Cannot find a `dartfmt` command') + return [] endfunction function! dart#analyzer(q_args) abort diff --git a/doc/dart.txt b/doc/dart.txt index 132b3b5..3713f42 100644 --- a/doc/dart.txt +++ b/doc/dart.txt @@ -23,12 +23,12 @@ argument. If there are any errors they will be shown in the quickfix window. *:DartFmt* -Runs dartfmt and passes the current buffer content through stdin. If the +Runs `dart format` and passes the current buffer content through stdin. If the format is successful replaces the current buffer content with the formatted result. If the format is unsuccessful errors are shown in the quickfix window. This command does not use the file content on disk so it is safe to run with unwritten changes. -Options may be passed to `dartfmt` by setting |g:dartfmt_options| or passing +Options may be passed to `dart format` by setting |g:dartfmt_options| or passing arguments to the `:DartFmt` command. *:DartAnalyzer* @@ -55,7 +55,7 @@ match the Dart style guide - spaces only with an indent of 2. Also sets *g:dartfmt_options* Configure DartFmt options with `let g:dartfmt_options`, for example, enable auto syntax fixes with `let g:dartfmt_options = ['--fix']` -(discover formatter options with `dartfmt -h`) +(discover formatter options with `dart format -h`) SYNTAX HIGHLIGHTING *dart-syntax*