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