github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/misc/vim/ftplugin/go/fmt.vim (about) 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 if exists("b:did_ftplugin_go_fmt") 22 finish 23 endif 24 25 26 if !exists("g:go_fmt_commands") 27 let g:go_fmt_commands = 1 28 endif 29 30 31 if g:go_fmt_commands 32 command! -buffer Fmt call s:GoFormat() 33 endif 34 35 function! s:GoFormat() 36 let view = winsaveview() 37 silent %!gofmt 38 if v:shell_error 39 let errors = [] 40 for line in getline(1, line('$')) 41 let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)') 42 if !empty(tokens) 43 call add(errors, {"filename": @%, 44 \"lnum": tokens[2], 45 \"col": tokens[3], 46 \"text": tokens[4]}) 47 endif 48 endfor 49 if empty(errors) 50 % | " Couldn't detect gofmt error format, output errors 51 endif 52 undo 53 if !empty(errors) 54 call setloclist(0, errors, 'r') 55 endif 56 echohl Error | echomsg "Gofmt returned error" | echohl None 57 endif 58 call winrestview(view) 59 endfunction 60 61 let b:did_ftplugin_go_fmt = 1 62 63 " vim:ts=4:sw=4:et