]> Sergey Matveev's repositories - dotfiles.git/blob - vim/.vim/ftplugin/go/fmt.vim
Initial
[dotfiles.git] / vim / .vim / ftplugin / go / fmt.vim
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.
4 "
5 " fmt.vim: Vim command to format Go files with gofmt.
6 "
7 " This filetype plugin add a new commands for go buffers:
8 "
9 "   :Fmt
10 "
11 "       Filter the current Go buffer through gofmt.
12 "       It tries to preserve cursor position and avoids
13 "       replacing the buffer with stderr output.
14 "
15 " Options:
16 "
17 "   g:go_fmt_commands [default=1]
18 "
19 "       Flag to indicate whether to enable the commands listed above.
20 "
21 "   g:gofmt_command [default="gofmt"]
22 "
23 "       Flag naming the gofmt executable to use.
24 "
25 if exists("b:did_ftplugin_go_fmt")
26     finish
27 endif
28
29 if !exists("g:go_fmt_commands")
30     let g:go_fmt_commands = 1
31 endif
32
33 if !exists("g:gofmt_command")
34     let g:gofmt_command = "gofmt"
35 endif
36
37 if g:go_fmt_commands
38     command! -buffer Fmt call s:GoFormat()
39 endif
40
41 function! s:GoFormat()
42     let view = winsaveview()
43     silent execute "%!" . g:gofmt_command
44     if v:shell_error
45         let errors = []
46         for line in getline(1, line('$'))
47             let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
48             if !empty(tokens)
49                 call add(errors, {"filename": @%,
50                                  \"lnum":     tokens[2],
51                                  \"col":      tokens[3],
52                                  \"text":     tokens[4]})
53             endif
54         endfor
55         if empty(errors)
56             % | " Couldn't detect gofmt error format, output errors
57         endif
58         undo
59         if !empty(errors)
60             call setqflist(errors, 'r')
61         endif
62         echohl Error | echomsg "Gofmt returned error" | echohl None
63     endif
64     call winrestview(view)
65 endfunction
66
67 let b:did_ftplugin_go_fmt = 1
68
69 " vim:ts=4:sw=4:et