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 let g:gofmt_command = get(g:, "gofmt_command", "goimports")
36 command! -buffer Fmt call s:GoFormat()
39 function! s:GoFormat()
40 let view = winsaveview()
41 silent execute "%!" . g:gofmt_command
44 for line in getline(1, line('$'))
45 let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
47 call add(errors, {"filename": @%,
54 % | " Couldn't detect gofmt error format, output errors
58 call setqflist(errors, 'r')
60 echohl Error | echomsg "Gofmt returned error" | echohl None
62 call winrestview(view)
65 let b:did_ftplugin_go_fmt = 1