Skip to content

Commit

Permalink
fmt: change go_fmt_options's type to dict
Browse files Browse the repository at this point in the history
* add the ability to specificy binary specific options
* remove `go_imports_bin` settings as it's subject to confusing

Thanks to @cespare for the idea and @hori-ryota for initial code snippet

Fixes #1212
  • Loading branch information
fatih committed May 31, 2017
1 parent d7df625 commit 8f27fc1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
20 changes: 12 additions & 8 deletions autoload/go/fmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ if !exists("g:go_fmt_command")
let g:go_fmt_command = "gofmt"
endif

if !exists("g:go_goimports_bin")
let g:go_goimports_bin = "goimports"
if !exists('g:go_fmt_options')
let g:go_fmt_options = ''
endif

if !exists('g:go_fmt_fail_silently')
let g:go_fmt_fail_silently = 0
endif

if !exists('g:go_fmt_options')
let g:go_fmt_options = ''
endif

if !exists("g:go_fmt_experimental")
let g:go_fmt_experimental = 0
endif
Expand Down Expand Up @@ -70,7 +66,7 @@ function! go#fmt#Format(withGoimport) abort

let bin_name = g:go_fmt_command
if a:withGoimport == 1
let bin_name = g:go_goimports_bin
let bin_name = "goimports"
endif

let out = go#fmt#run(bin_name, l:tmpname, expand('%'))
Expand Down Expand Up @@ -172,7 +168,15 @@ function! s:fmt_cmd(bin_name, source, target)
" start constructing the command
let cmd = [bin_path]
call add(cmd, "-w")
call extend(cmd, split(g:go_fmt_options, " "))

" add the options for binary (if any). go_fmt_options was by default of type
" string, however to allow customization it's now a dictionary of binary
" name mapping to options.
let opts = g:go_fmt_options
if type(g:go_fmt_options) == type({})
let opts = has_key(g:go_fmt_options, a:bin_name) ? g:go_fmt_options[a:bin_name] : ""
endif
call extend(cmd, split(opts, " "))

if a:bin_name == "goimports"
" lazy check if goimports support `-srcdir`. We should eventually remove
Expand Down
19 changes: 16 additions & 3 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1107,10 +1107,23 @@ used >
<
*'g:go_fmt_options'*

Use this option to add additional options to the |'g:go_fmt_command'|. Default
is empty. >
Use this option to add additional options to the |'g:go_fmt_command'|. It's
value type can be either a string or a dictionary. This is due backwards
compatibility. The string version will be removed in the future so please use
the dictionary version. Default is empty.
>
let g:go_fmt_options = ''
or
let g:go_fmt_options = {}
<
The dictionary version allows you to define options for multiple binaries:
>
let g:go_fmt_options = {
\ 'gofmt': '-s',
\ 'goimports': '-local mycompany.com',
\ }
<
*'g:go_fmt_fail_silently'*

Expand Down

0 comments on commit 8f27fc1

Please sign in to comment.