Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust imports on save #2797

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions autoload/go/auto.vim
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,28 @@ function! s:handler(timer_id)
endfunction

function! go#auto#fmt_autosave()
if !(go#config#FmtAutosave() && isdirectory(expand('%:p:h')) && expand('<afile>:p') == expand('%:p'))
if !(isdirectory(expand('%:p:h')) && expand('<afile>:p') == expand('%:p'))
return
endif

if !(go#config#FmtAutosave() || go#config#ImportsAutosave())
return
endif

if go#config#ImportsAutosave() && !(go#config#FmtAutosave() && go#config#FmtCommand() == 'goimports')
call go#fmt#Format(1)

" return early when the imports mode is goimports, because there's no need
" to format again when goimports was run
if go#config#ImportsMode() == 'goimports'
return
endif
endif

if !go#config#FmtAutosave()
return
endif

" Go code formatting on save
call go#fmt#Format(-1)
endfunction

Expand Down
4 changes: 4 additions & 0 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ function! go#config#FmtAutosave() abort
return get(g:, "go_fmt_autosave", 1)
endfunction

function! go#config#ImportsAutosave() abort
return get(g:, 'go_imports_autosave', 1)
endfunction

function! go#config#SetFmtAutosave(value) abort
let g:go_fmt_autosave = a:value
endfunction
Expand Down
7 changes: 6 additions & 1 deletion autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ function! go#lsp#Imports() abort
let l:handler = go#promise#New(function('s:handleCodeAction', [], l:state), 10000, '')
let l:state.handleResult = l:handler.wrapper
let l:state.error = l:handler.wrapper
" let l:state.handleError = function('s:handleCodeActionError', [l:fname], l:state)
let l:state.handleError = function('s:handleCodeActionError', [l:fname], l:state)
let l:msg = go#lsp#message#CodeActionImports(l:fname)
call l:lsp.sendMessage(l:msg, l:state)

Expand All @@ -1374,6 +1374,11 @@ function! s:handleFormat(msg) abort dict
endfunction

function! s:handleCodeAction(msg) abort dict
if type(a:msg) is type('')
call self.handleError(a:msg)
return
endif

if a:msg is v:null
return
endif
Expand Down
8 changes: 7 additions & 1 deletion doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,13 @@ it's causing problems on some Vim versions. This has no effect if

<

*'g:go_imports_mode'*
*'g:go_imports_autosave'*

Use this option to auto |:GoImports| on save. By default it's enabled.
>
let g:go_imports_autosave = 1
<
*'g:go_imports_mode'*

Use this option to define which tool is used to adjust imports. Valid options
are `goimports` and `gopls`. By default `goimports` is used.
Expand Down