From e63b2cb4590e93ac1c1a70e009108b5dbcc055d6 Mon Sep 17 00:00:00 2001 From: Hank Donnay Date: Thu, 5 Dec 2019 09:25:22 -0500 Subject: [PATCH 1/2] fmt: respect buffer-level go_fmt_options This commit enables looking at the buffer-level fmt_options variable. An example use is provided in the documentation. Signed-off-by: Hank Donnay --- autoload/go/config.vim | 2 +- doc/vim-go.txt | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/autoload/go/config.vim b/autoload/go/config.vim index 5f793caad4..d921d44a8d 100644 --- a/autoload/go/config.vim +++ b/autoload/go/config.vim @@ -351,7 +351,7 @@ function! go#config#FmtCommand() abort endfunction function! go#config#FmtOptions() abort - return get(g:, "go_fmt_options", {}) + return get(b:, "go_fmt_options", get(g:, "go_fmt_options", {})) endfunction function! go#config#FmtFailSilently() abort diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 8412178fd0..1fb2ebe4aa 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -1339,6 +1339,16 @@ The dictionary version allows you to define options for multiple binaries: \ 'gofmt': '-s', \ 'goimports': '-local mycompany.com', \ } +< + *'b:go_fmt_options'* + +This option is identical to |'g:go_fmt_options'|, but a buffer-level setting. +If present, it's used instead of the global setting. > + + au BufRead,BufNewFile *.go let b:go_fmt_options = { + \ 'goimports': '-local ' . + \ trim(system('{cd '. shellescape(expand('%:h')) .' && go list -m;}')), + \ } < *'g:go_fmt_fail_silently'* From be9535c2b7826759e703cddd99bb3e522a39bc1f Mon Sep 17 00:00:00 2001 From: Billie Cleek Date: Sat, 7 Dec 2019 08:46:42 -0800 Subject: [PATCH 2/2] doc: clarify the example for b:go_fmt_options --- doc/vim-go.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 1fb2ebe4aa..5259d9f9d8 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -1343,9 +1343,12 @@ The dictionary version allows you to define options for multiple binaries: *'b:go_fmt_options'* This option is identical to |'g:go_fmt_options'|, but a buffer-level setting. -If present, it's used instead of the global setting. > +If present, it's used instead of the global setting. By default it is not set. - au BufRead,BufNewFile *.go let b:go_fmt_options = { +As an example, the following autocmd will configure goimports to put imports +of packages from the current module in their own group: +> + autocmd FileType go let b:go_fmt_options = { \ 'goimports': '-local ' . \ trim(system('{cd '. shellescape(expand('%:h')) .' && go list -m;}')), \ }