github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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 if exists("b:did_ftplugin_go_fmt") 16 finish 17 endif 18 19 command! -buffer Fmt call s:GoFormat() 20 21 function! s:GoFormat() 22 let view = winsaveview() 23 silent %!gofmt 24 if v:shell_error 25 let errors = [] 26 for line in getline(1, line('$')) 27 let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)') 28 if !empty(tokens) 29 call add(errors, {"filename": @%, 30 \"lnum": tokens[2], 31 \"col": tokens[3], 32 \"text": tokens[4]}) 33 endif 34 endfor 35 if empty(errors) 36 % | " Couldn't detect gofmt error format, output errors 37 endif 38 undo 39 if !empty(errors) 40 call setloclist(0, errors, 'r') 41 endif 42 echohl Error | echomsg "Gofmt returned error" | echohl None 43 endif 44 call winrestview(view) 45 endfunction 46 47 let b:did_ftplugin_go_fmt = 1 48 49 " vim:ts=4:sw=4:et