1 " Copyright 2011 The Go Authors. All rights reserved.
2 " Use of this source code is governed by a BSD-style
3 " license that can be found in the LICENSE file.
5 " fmt.vim: Vim command to format Go files with gofmt.
7 " This filetype plugin add a new commands for go buffers:
11 " Filter the current Go buffer through gofmt.
12 " It tries to preserve cursor position and avoids
13 " replacing the buffer with stderr output.
17 " g:go_fmt_commands [default=1]
19 " Flag to indicate whether to enable the commands listed above.
21 " g:gofmt_command [default="gofmt"]
23 " Flag naming the gofmt executable to use.
25 if exists("b:did_ftplugin_go_fmt")
29 if !exists("g:go_fmt_commands")
30 let g:go_fmt_commands = 1
33 if !exists("g:gofmt_command")
34 let g:gofmt_command = "goimports"
38 command! -buffer Fmt call s:GoFormat()
41 function! s:GoFormat()
42 let view = winsaveview()
43 silent execute "%!" . g:gofmt_command
46 for line in getline(1, line('$'))
47 let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
49 call add(errors, {"filename": @%,
56 % | " Couldn't detect gofmt error format, output errors
60 call setqflist(errors, 'r')
62 echohl Error | echomsg "Gofmt returned error" | echohl None
64 call winrestview(view)
67 let b:did_ftplugin_go_fmt = 1