Skip to content

Commit

Permalink
Enable DartFmt Options with g:dartfmt_options (#108)
Browse files Browse the repository at this point in the history
- Use f-args instead of q-args and use varags for the funciton so all
  arguments are lists.
- Add a missing `shellescape` in case there are characters in the
  filename that need to be escaped.
- Extend the default args with the dartfmt_options args, and the
  manually supplied arguments to the command.
  • Loading branch information
David Chen authored Jun 25, 2020
1 parent 70bc2f2 commit b9fd9d2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Enable Dart style guide syntax (like 2-space indentation) with

Enable DartFmt execution on buffer save with `let g:dart_format_on_save = 1`

Configure DartFmt options with `let g:dartfmt_options`
(discover formatter options with `dartfmt -h`)

## FAQ

### Why doesn't the plugin indent identically to `dartfmt`?
Expand Down
14 changes: 9 additions & 5 deletions autoload/dart.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ function! s:clearQfList(reason) abort
endif
endfunction

function! dart#fmt(q_args) abort
let cmd = s:FindDartFmt()
if type(cmd) != type('') | return | endif
function! dart#fmt(...) abort
let l:dartfmt = s:FindDartFmt()
if type(l:dartfmt) != type('') | return | endif
let buffer_content = getline(1, '$')
let args = '--stdin-name '.expand('%').' '.a:q_args
let lines = systemlist(printf('%s %s', cmd, args), join(buffer_content, "\n"))
let l:cmd = [l:dartfmt, '--stdin-name', shellescape(expand('%'))]
if exists('g:dartfmt_options')
call extend(l:cmd, g:dartfmt_options)
endif
call extend(l:cmd, a:000)
let lines = systemlist(join(l:cmd), join(buffer_content, "\n"))
" TODO(https://github.com/dart-lang/sdk/issues/38507) - Remove once the
" tool no longer emits this line on SDK upgrades.
if lines[-1] ==# 'Isolate creation failed'
Expand Down
5 changes: 5 additions & 0 deletions doc/dart.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Set to any value (set to `2` by convention) to set tab and width behavior to
match the Dart style guide - spaces only with an indent of 2. Also sets
`formatoptions += t` to auto wrap text.

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`)


SYNTAX HIGHLIGHTING *dart-syntax*

This plugin uses narrow highlight groups to allow selectively disabling the
Expand Down
4 changes: 2 additions & 2 deletions plugin/dart.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ set cpo&vim
function! s:FormatOnSave()
" Dart code formatting on save
if get(g:, 'dart_format_on_save', 0)
call dart#fmt('')
call dart#fmt()
endif
endfunction

augroup dart-vim-plugin
autocmd!
autocmd BufWritePre *.dart call s:FormatOnSave()
autocmd FileType dart command! -buffer -nargs=? DartFmt call dart#fmt(<q-args>)
autocmd FileType dart command! -buffer -nargs=? DartFmt call dart#fmt(<f-args>)
autocmd FileType dart command! -buffer DartToggleFormatOnSave call dart#ToggleFormatOnSave()
autocmd FileType dart command! -buffer -nargs=? Dart2Js call dart#tojs(<q-args>)
autocmd FileType dart command! -buffer -nargs=? DartAnalyzer call dart#analyzer(<q-args>)
Expand Down

0 comments on commit b9fd9d2

Please sign in to comment.