Skip to content
Open
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
51 changes: 11 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,28 @@ Looking for an IDE experience? See the [Dart Tools][] page.
[Dart tools]: https://dart.dev/tools
[vim-lsc]: https://github.com/natebosch/vim-lsc

## Commands

### :DartFmt

![](https://raw.github.com/dart-lang/dart-vim-plugin/master/DartFmt.gif)

## Installation

Install as a typical vim plugin using your favorite approach. If you don't have
a preference [vim-plug][] is a good place to start. Below are examples for
common choices, be sure to read the docs for each option.

### [vim-plug][]

[vim-plug]: https://github.com/junegunn/vim-plug

```vimscript
call plug#begin()
"... <snip other plugins>
Plug 'dart-lang/dart-vim-plugin'

call plug#end()
```

Then invoke `:PlugInstall` to install the plugin.

### [pathogen][]

[pathogen]: https://github.com/tpope/vim-pathogen

Clone the repository into your pathogen directory.
a preference, use Vim's built-in package support:

```sh
mkdir -p ~/.vim/bundle && cd ~/.vim/bundle && \
git clone https://github.com/dart-lang/dart-vim-plugin
mkdir -p ~/.vim/pack/dart-lang/start
git clone https://github.com/dart-lang/dart-vim-plugin ~/.vim/pack/dart-lang/start/dart-vim-plugin
```

Ensure your `.vimrc` contains the line `execute pathogen#infect()`

### [vundle][]
Examples for [vim-plug][], [pathogen][], and [vundle][] can be found on the [installation wiki][].

[vim-plug]: https://github.com/junegunn/vim-plug
[pathogen]: https://github.com/tpope/vim-pathogen
[vundle]: https://github.com/VundleVim/Vundle.vim
[installation wiki]: https://github.com/dart-lang/dart-vim-plugin/wiki/Installation

```vimscript
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
"... <snip other plugins>
Plugin 'dart-lang/dart-vim-plugin'
## Commands

call vundle#end()
```
### :DartFmt

![](https://raw.github.com/dart-lang/dart-vim-plugin/master/DartFmt.gif)

## Configuration

Expand Down
49 changes: 35 additions & 14 deletions autoload/dart.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,54 @@ function! dart#fmt(...) abort
let l:dartfmt = s:FindDartFmt()
if empty(l:dartfmt) | return | endif
let buffer_content = getline(1, '$')
let l:cmd = extend(l:dartfmt, ['--stdin-name', shellescape(expand('%'))])
let l:cmd = extend(l:dartfmt, ['--stdin-name', 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'
let lines = lines[:-2]
let l:stdout_data = []
let l:stderr_data = []
let l:options = {
\ 'out_cb': { ch, msg -> add(l:stdout_data, msg) },
\ 'err_cb': { ch, msg -> add(l:stderr_data, msg) },
\ 'close_cb': { ch ->
\ s:formatResult(l:stdout_data, l:stderr_data, l:buffer_content)} }
if has('patch-8.1.350')
let options['noblock'] = v:true
endif
if buffer_content == lines
let l:job = job_start(l:cmd, l:options)
call ch_sendraw(job_getchannel(l:job), join(l:buffer_content, "\n"))
call ch_close_in(job_getchannel(l:job))
endfunction

function! s:formatResult(stdout, stderr, buffer_content) abort
if a:buffer_content == a:stdout
call s:clearQfList('dartfmt')
return
endif
if 0 == v:shell_error
if !empty(a:stdout)
let win_view = winsaveview()
silent keepjumps call setline(1, lines)
if line('$') > len(lines)
silent keepjumps execute string(len(lines)+1).',$ delete'
silent keepjumps call setline(1, a:stdout)
if line('$') > len(a:stdout)
silent keepjumps execute string(len(a:stdout)+1).',$ delete'
endif
call winrestview(win_view)
call s:clearQfList('dartfmt')
else
let errors = lines[2:]
let error_format = '%Aline %l\, column %c of %f: %m,%C%.%#'
call s:cexpr(error_format, errors, 'dartfmt')
let l:has_diagnostic = v:false
for l:line in a:stderr
if l:line =~# '^line \d\+, column \d\+ of '
let l:has_diagnostic = v:true
break
endif
endfor

if l:has_diagnostic
let l:format = '%Aline %l\, column %c of %f: %m,%C%.%#,%-G%.%#'
else
let l:format = '%m'
endif
call s:cexpr(l:format, a:stderr, 'dartfmt')
endif
endfunction

Expand Down