Skip to content

Commit 66b04e8

Browse files
authored
Merge pull request #1308 from fatih/improve-fmt-commands
fmt: change go_fmt_options's type to dict
2 parents 00ab89e + e6533ef commit 66b04e8

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

autoload/go/fmt.vim

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@ if !exists("g:go_fmt_command")
99
let g:go_fmt_command = "gofmt"
1010
endif
1111

12-
if !exists("g:go_goimports_bin")
13-
let g:go_goimports_bin = "goimports"
12+
if !exists('g:go_fmt_options')
13+
let g:go_fmt_options = ''
1414
endif
1515

1616
if !exists('g:go_fmt_fail_silently')
1717
let g:go_fmt_fail_silently = 0
1818
endif
1919

20-
if !exists('g:go_fmt_options')
21-
let g:go_fmt_options = ''
22-
endif
23-
2420
if !exists("g:go_fmt_experimental")
2521
let g:go_fmt_experimental = 0
2622
endif
@@ -70,7 +66,7 @@ function! go#fmt#Format(withGoimport) abort
7066

7167
let bin_name = g:go_fmt_command
7268
if a:withGoimport == 1
73-
let bin_name = g:go_goimports_bin
69+
let bin_name = "goimports"
7470
endif
7571

7672
let out = go#fmt#run(bin_name, l:tmpname, expand('%'))
@@ -172,7 +168,15 @@ function! s:fmt_cmd(bin_name, source, target)
172168
" start constructing the command
173169
let cmd = [bin_path]
174170
call add(cmd, "-w")
175-
call extend(cmd, split(g:go_fmt_options, " "))
171+
172+
" add the options for binary (if any). go_fmt_options was by default of type
173+
" string, however to allow customization it's now a dictionary of binary
174+
" name mapping to options.
175+
let opts = g:go_fmt_options
176+
if type(g:go_fmt_options) == type({})
177+
let opts = has_key(g:go_fmt_options, a:bin_name) ? g:go_fmt_options[a:bin_name] : ""
178+
endif
179+
call extend(cmd, split(opts, " "))
176180

177181
if a:bin_name == "goimports"
178182
" lazy check if goimports support `-srcdir`. We should eventually remove

doc/vim-go.txt

+16-3
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,23 @@ used >
11071107
<
11081108
*'g:go_fmt_options'*
11091109

1110-
Use this option to add additional options to the |'g:go_fmt_command'|. Default
1111-
is empty. >
1112-
1110+
Use this option to add additional options to the |'g:go_fmt_command'|. It's
1111+
value type can be either a string or a dictionary. This is due backwards
1112+
compatibility. The string version will be removed in the future so please use
1113+
the dictionary version. Default is empty.
1114+
>
11131115
let g:go_fmt_options = ''
1116+
1117+
or
1118+
1119+
let g:go_fmt_options = {}
1120+
<
1121+
The dictionary version allows you to define options for multiple binaries:
1122+
>
1123+
let g:go_fmt_options = {
1124+
\ 'gofmt': '-s',
1125+
\ 'goimports': '-local mycompany.com',
1126+
\ }
11141127
<
11151128
*'g:go_fmt_fail_silently'*
11161129

0 commit comments

Comments
 (0)