summaryrefslogtreecommitdiffstats
path: root/ftplugin/go/fmt.vim
diff options
context:
space:
mode:
Diffstat (limited to 'ftplugin/go/fmt.vim')
-rw-r--r--ftplugin/go/fmt.vim63
1 files changed, 63 insertions, 0 deletions
diff --git a/ftplugin/go/fmt.vim b/ftplugin/go/fmt.vim
new file mode 100644
index 00000000..5447d457
--- /dev/null
+++ b/ftplugin/go/fmt.vim
@@ -0,0 +1,63 @@
+" Copyright 2011 The Go Authors. All rights reserved.
+" Use of this source code is governed by a BSD-style
+" license that can be found in the LICENSE file.
+"
+" fmt.vim: Vim command to format Go files with gofmt.
+"
+" This filetype plugin add a new commands for go buffers:
+"
+" :Fmt
+"
+" Filter the current Go buffer through gofmt.
+" It tries to preserve cursor position and avoids
+" replacing the buffer with stderr output.
+"
+" Options:
+"
+" g:go_fmt_commands [default=1]
+"
+" Flag to indicate whether to enable the commands listed above.
+"
+if exists("b:did_ftplugin_go_fmt")
+ finish
+endif
+
+
+if !exists("g:go_fmt_commands")
+ let g:go_fmt_commands = 1
+endif
+
+
+if g:go_fmt_commands
+ command! -buffer Fmt call s:GoFormat()
+endif
+
+function! s:GoFormat()
+ let view = winsaveview()
+ silent %!gofmt
+ if v:shell_error
+ let errors = []
+ for line in getline(1, line('$'))
+ let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
+ if !empty(tokens)
+ call add(errors, {"filename": @%,
+ \"lnum": tokens[2],
+ \"col": tokens[3],
+ \"text": tokens[4]})
+ endif
+ endfor
+ if empty(errors)
+ % | " Couldn't detect gofmt error format, output errors
+ endif
+ undo
+ if !empty(errors)
+ call setloclist(0, errors, 'r')
+ endif
+ echohl Error | echomsg "Gofmt returned error" | echohl None
+ endif
+ call winrestview(view)
+endfunction
+
+let b:did_ftplugin_go_fmt = 1
+
+" vim:ts=4:sw=4:et