From 219f2a59b8e4166675d8f557e7e6bdb15b3cafc6 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Tue, 15 Jun 2021 15:23:04 -0700 Subject: [PATCH 1/2] Move from dartfmt to dart format --- autoload/dart.vim | 19 +++++-------------- doc/dart.txt | 6 +++--- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/autoload/dart.vim b/autoload/dart.vim index 2dff18c..75f4e07 100644 --- a/autoload/dart.vim +++ b/autoload/dart.vim @@ -26,10 +26,12 @@ function! s:clearQfList(reason) abort endfunction function! dart#fmt(...) abort - let l:dartfmt = s:FindDartFmt() - if type(l:dartfmt) != type('') | return | endif + if !executable('dart') + call s:error('Cannot find `dart` executable') + return + endif let buffer_content = getline(1, '$') - let l:cmd = [l:dartfmt, '--stdin-name', shellescape(expand('%'))] + let l:cmd = ['dart', 'format', '--stdin-name', shellescape(expand('%'))] if exists('g:dartfmt_options') call extend(l:cmd, g:dartfmt_options) endif @@ -59,17 +61,6 @@ function! dart#fmt(...) abort endif endfunction -function! s:FindDartFmt() abort - 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 - endif - call s:error('Cannot find a `dartfmt` command') -endfunction - function! dart#analyzer(q_args) abort call s:error('DartAnalyzer support has been removed. '. \'If this broke your workflow please comment on '. 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* From 75550b60f7305a1229dfa4a4c18123942dd2c735 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Wed, 11 Aug 2021 17:43:06 -0700 Subject: [PATCH 2/2] Scrape SDK version to pick format command If the `dart` command exists, parse the output of `dart --version` and for SDK version `2.14` and higher use `dart format`. On older SDKs fallback to the existing behavior. There will be some `2.14.0-dev` SDKs which are not compatible with this check, but they aren't likely be to in use anymore for local development. --- autoload/dart.vim | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/autoload/dart.vim b/autoload/dart.vim index 75f4e07..bee79c9 100644 --- a/autoload/dart.vim +++ b/autoload/dart.vim @@ -26,12 +26,10 @@ function! s:clearQfList(reason) abort endfunction function! dart#fmt(...) abort - if !executable('dart') - call s:error('Cannot find `dart` executable') - return - endif + let l:dartfmt = s:FindDartFmt() + if empty(l:dartfmt) | return | endif let buffer_content = getline(1, '$') - let l:cmd = ['dart', 'format', '--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 @@ -61,6 +59,33 @@ function! dart#fmt(...) abort endif endfunction +function! s:FindDartFmt() abort + 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 + endif + call s:error('Cannot find a `dartfmt` command') + return [] +endfunction + function! dart#analyzer(q_args) abort call s:error('DartAnalyzer support has been removed. '. \'If this broke your workflow please comment on '.