Skip to content

Move from dartfmt to dart format #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions autoload/dart.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than return with an error, would it be worth using the fallback in this case?

Copy link
Member

@fishythefish fishythefish Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error might be better? IIRC, it's difficult/tedious to distinguish between stdout and stderr in vim, so diagnostics emitted by dart format/dartfmt get inserted at the top of the file. In particular, if we fail to detect the Dart version and fall back to dartfmt when dart format is expected, the UX will be worse than not formatting at all.

Nate can confirm though - I'm not sure if the linked issues have changed how this works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it - error sgtm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't expect any existing SDK releases to hit this. If the version string format changes in the future SDK releases, the fallback won't be the right behavior.

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
Expand Down
6 changes: 3 additions & 3 deletions doc/dart.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -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*
Expand Down