diff --git a/autoload/go/auto.vim b/autoload/go/auto.vim new file mode 100644 index 0000000000..9d7fbd8a70 --- /dev/null +++ b/autoload/go/auto.vim @@ -0,0 +1,79 @@ +" don't spam the user when Vim is started in Vi compatibility mode +let s:cpo_save = &cpo +set cpo&vim + +function! go#auto#template_autocreate() + " create new template from scratch + if get(g:, "go_template_autocreate", 1) && &modifiable + call go#template#create() + endif +endfunction + +function! go#auto#echo_go_info() + if !get(g:, "go_echo_go_info", 1) + return + endif + + if !exists('v:completed_item') || empty(v:completed_item) + return + endif + let item = v:completed_item + + if !has_key(item, "info") + return + endif + + if empty(item.info) + return + endif + + redraws! | echo "vim-go: " | echohl Function | echon item.info | echohl None +endfunction + +function! go#auto#auto_type_info() + " GoInfo automatic update + if get(g:, "go_auto_type_info", 0) + call go#tool#Info(0) + endif +endfunction + +function! go#auto#auto_sameids() + " GoSameId automatic update + if get(g:, "go_auto_sameids", 0) + call go#guru#SameIds(0) + endif +endfunction + +function! go#auto#fmt_autosave() + " Go code formatting on save + if get(g:, "go_fmt_autosave", 1) + call go#fmt#Format(-1) + endif +endfunction + +function! go#auto#metalinter_autosave() + " run gometalinter on save + if get(g:, "go_metalinter_autosave", 0) + call go#lint#Gometa(0, 1) + endif +endfunction + +function! go#auto#modfmt_autosave() + " go.mod code formatting on save + if get(g:, "go_mod_fmt_autosave", 1) + call go#mod#Format() + endif +endfunction + +function! go#auto#asmfmt_autosave() + " Go asm formatting on save + if get(g:, "go_asmfmt_autosave", 0) + call go#asmfmt#Format() + endif +endfunction + +" restore Vi compatibility settings +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: sw=2 ts=2 et diff --git a/ftdetect/gofiletype.vim b/ftdetect/gofiletype.vim index 09f6ac7530..7051f37372 100644 --- a/ftdetect/gofiletype.vim +++ b/ftdetect/gofiletype.vim @@ -4,45 +4,19 @@ let s:cpo_save = &cpo set cpo&vim -" We take care to preserve the user's fileencodings and fileformats, -" because those settings are global (not buffer local), yet we want -" to override them for loading Go files, which are defined to be UTF-8. -let s:current_fileformats = '' -let s:current_fileencodings = '' - -" define fileencodings to open as utf-8 encoding even if it's ascii. -function! s:gofiletype_pre(type) - let s:current_fileformats = &g:fileformats - let s:current_fileencodings = &g:fileencodings - set fileencodings=utf-8 fileformats=unix - let &l:filetype = a:type -endfunction - -" restore fileencodings as others -function! s:gofiletype_post() - let &g:fileformats = s:current_fileformats - let &g:fileencodings = s:current_fileencodings -endfunction - " Note: should not use augroup in ftdetect (see :help ftdetect) -au BufNewFile *.go setfiletype go | if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif -au BufRead *.go call s:gofiletype_pre("go") -au BufReadPost *.go call s:gofiletype_post() - -au BufNewFile *.s setfiletype asm | if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif -au BufRead *.s call s:gofiletype_pre("asm") -au BufReadPost *.s call s:gofiletype_post() - -au BufRead,BufNewFile *.tmpl set filetype=gohtmltmpl +au BufRead,BufNewFile *.go setfiletype go +au BufRead,BufNewFile *.s setfiletype asm +au BufRead,BufNewFile *.tmpl setfiletype gohtmltmpl " remove the autocommands for modsim3, and lprolog files so that their " highlight groups, syntax, etc. will not be loaded. *.MOD is included, so " that on case insensitive file systems the module2 autocmds will not be " executed. -au! BufNewFile,BufRead *.mod,*.MOD +au! BufRead,BufNewFile *.mod,*.MOD " Set the filetype if the first non-comment and non-blank line starts with " 'module '. -au BufNewFile,BufRead go.mod call s:gomod() +au BufRead,BufNewFile go.mod call s:gomod() fun! s:gomod() for l:i in range(1, line('$')) @@ -52,7 +26,7 @@ fun! s:gomod() endif if l:l =~# '^module .\+' - set filetype=gomod + setfiletype gomod endif break diff --git a/ftplugin/asm.vim b/ftplugin/asm.vim index db1f10db5d..9271d22515 100644 --- a/ftplugin/asm.vim +++ b/ftplugin/asm.vim @@ -9,7 +9,8 @@ let b:did_ftplugin = 1 let s:cpo_save = &cpo set cpo&vim -let b:undo_ftplugin = "setl fo< com< cms<" +let b:undo_ftplugin = "setl fo< com< cms< + \ | exe 'au! vim-go-asm-buffer * '" setlocal formatoptions-=t @@ -20,6 +21,15 @@ setlocal noexpandtab command! -nargs=0 AsmFmt call go#asmfmt#Format() +" Autocommands +" ============================================================================ + +augroup vim-go-asm-buffer + autocmd! * + + autocmd BufWritePre call go#auto#asmfmt_autosave() +augroup end + " restore Vi compatibility settings let &cpo = s:cpo_save unlet s:cpo_save diff --git a/ftplugin/go.vim b/ftplugin/go.vim index 7160d8bbc6..bf841031f4 100644 --- a/ftplugin/go.vim +++ b/ftplugin/go.vim @@ -13,7 +13,8 @@ let b:did_ftplugin = 1 let s:cpo_save = &cpo set cpo&vim -let b:undo_ftplugin = "setl fo< com< cms<" +let b:undo_ftplugin = "setl fo< com< cms< + \ | exe 'au! vim-go-buffer * '" setlocal formatoptions-=t @@ -72,73 +73,42 @@ if go#config#AutoTypeInfo() || go#config#AutoSameids() let &l:updatetime= get(g:, "go_updatetime", 800) endif -" NOTE(arslan): experimental, disabled by default, doesn't work well. No -" documentation as well. If anyone feels adventurous, enable the following and -" try to search for Go identifiers ;) +" Autocommands +" ============================================================================ " -" if get(g:, "go_sameid_search_enabled", 0) -" autocmd FileType go nnoremap * :call Sameids_search(0) -" autocmd FileType go nnoremap # :call Sameids_search(1) -" autocmd FileType go nnoremap n :call Sameids_repeat(0) -" autocmd FileType go nnoremap N :call Sameids_repeat(1) -" autocmd FileType go cabbrev nohlsearch =Sameids_nohlsearch() -" endif - -" " mode 0: next 1: prev -" function! Sameids_repeat(mode) -" let matches = getmatches() -" if empty(matches) -" return -" endif -" let cur_offset = go#util#OffsetCursor() - -" " reverse list to make it easy to find the prev occurrence -" if a:mode -" call reverse(matches) -" endif - -" for m in matches -" if !has_key(m, "group") -" return -" endif - -" if m.group != "goSameId" -" return -" endif - -" let offset = go#util#Offset(m.pos1[0], m.pos1[1]) - -" if a:mode && cur_offset > offset -" call cursor(m.pos1[0], m.pos1[1]) -" return -" elseif !a:mode && cur_offset < offset -" call cursor(m.pos1[0], m.pos1[1]) -" return -" endif -" endfor - -" " reached start/end, jump to the end/start -" let initial_match = matches[0] -" if !has_key(initial_match, "group") -" return -" endif - -" if initial_match.group != "goSameId" -" return -" endif - -" call cursor(initial_match.pos1[0], initial_match.pos1[1]) -" endfunction - -" function! Sameids_search(mode) -" call go#guru#SameIds() -" call Sameids_repeat(a:mode) -" endfunction - -" function! Sameids_nohlsearch() -" call go#guru#ClearSameIds() -" return "nohlsearch" -" endfunction +augroup vim-go-buffer + autocmd! * + + autocmd CursorHold call go#auto#auto_type_info() + autocmd CursorHold call go#auto#auto_sameids() + + " Echo the identifier information when completion is done. Useful to see + " the signature of a function, etc... + if exists('##CompleteDone') + autocmd CompleteDone call go#auto#echo_go_info() + endif + + autocmd BufWritePre call go#auto#fmt_autosave() + autocmd BufWritePost call go#auto#metalinter_autosave() + + " clear SameIds when the buffer is unloaded so that loading another buffer + " in the same window doesn't highlight the most recently matched + " identifier's positions. + autocmd BufWinEnter call go#guru#ClearSameIds() + + autocmd BufEnter + \ if go#config#AutodetectGopath() && !exists('b:old_gopath') + \| let b:old_gopath = exists('$GOPATH') ? $GOPATH : -1 + \| let $GOPATH = go#path#Detect() + \| endif + autocmd BufLeave + \ if exists('b:old_gopath') + \| if b:old_gopath isnot -1 + \| let $GOPATH = b:old_gopath + \| endif + \| unlet b:old_gopath + \| endif +augroup end " restore Vi compatibility settings let &cpo = s:cpo_save diff --git a/ftplugin/gomod.vim b/ftplugin/gomod.vim index 107abc6cdb..b78717054d 100644 --- a/ftplugin/gomod.vim +++ b/ftplugin/gomod.vim @@ -9,13 +9,23 @@ let b:did_ftplugin = 1 let s:cpo_save = &cpo set cpo&vim -let b:undo_ftplugin = "setl fo< com< cms<" +let b:undo_ftplugin = "setl fo< com< cms< + \ | exe 'au! vim-go-gomod-buffer * '" setlocal formatoptions-=t setlocal comments=s1:/*,mb:*,ex:*/,:// setlocal commentstring=//\ %s +" Autocommands +" ============================================================================ + +augroup vim-go-gomod-buffer + autocmd! * + + autocmd BufWritePre call go#auto#modfmt_autosave() +augroup end + " restore Vi compatibility settings let &cpo = s:cpo_save unlet s:cpo_save diff --git a/plugin/go.vim b/plugin/go.vim index 947d7e7169..54274835eb 100644 --- a/plugin/go.vim +++ b/plugin/go.vim @@ -212,110 +212,37 @@ endfunction " Autocommands " ============================================================================ " -function! s:echo_go_info() - if !get(g:, "go_echo_go_info", 1) - return - endif - - if !exists('v:completed_item') || empty(v:completed_item) - return - endif - let item = v:completed_item - - if !has_key(item, "info") - return - endif - - if empty(item.info) - return - endif - - redraws! | echo "vim-go: " | echohl Function | echon item.info | echohl None -endfunction - -function! s:auto_type_info() - " GoInfo automatic update - if get(g:, "go_auto_type_info", 0) - call go#tool#Info(0) - endif -endfunction -function! s:auto_sameids() - " GoSameId automatic update - if get(g:, "go_auto_sameids", 0) - call go#guru#SameIds(0) - endif -endfunction - -function! s:fmt_autosave() - " Go code formatting on save - if get(g:, "go_fmt_autosave", 1) - call go#fmt#Format(-1) - endif -endfunction - -function! s:asmfmt_autosave() - " Go asm formatting on save - if get(g:, "go_asmfmt_autosave", 0) - call go#asmfmt#Format() - endif -endfunction - -function! s:modfmt_autosave() - " go.mod code formatting on save - if get(g:, "go_mod_fmt_autosave", 1) - call go#mod#Format() - endif -endfunction - -function! s:metalinter_autosave() - " run gometalinter on save - if get(g:, "go_metalinter_autosave", 0) - call go#lint#Gometa(0, 1) - endif +" We take care to preserve the user's fileencodings and fileformats, +" because those settings are global (not buffer local), yet we want +" to override them for loading Go files, which are defined to be UTF-8. +let s:current_fileformats = '' +let s:current_fileencodings = '' + +" define fileencodings to open as utf-8 encoding even if it's ascii. +function! s:gofiletype_pre() + let s:current_fileformats = &g:fileformats + let s:current_fileencodings = &g:fileencodings + set fileencodings=utf-8 fileformats=unix endfunction -function! s:template_autocreate() - " create new template from scratch - if get(g:, "go_template_autocreate", 1) && &modifiable - call go#template#create() - endif +" restore fileencodings as others +function! s:gofiletype_post() + let &g:fileformats = s:current_fileformats + let &g:fileencodings = s:current_fileencodings endfunction augroup vim-go autocmd! - autocmd CursorHold *.go call s:auto_type_info() - autocmd CursorHold *.go call s:auto_sameids() - - " Echo the identifier information when completion is done. Useful to see - " the signature of a function, etc... - if exists('##CompleteDone') - autocmd CompleteDone *.go call s:echo_go_info() - endif + autocmd BufNewFile *.go if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif + autocmd BufNewFile *.go call go#auto#template_autocreate() + autocmd BufRead *.go call s:gofiletype_pre() + autocmd BufReadPost *.go call s:gofiletype_post() - autocmd BufWritePre *.go call s:fmt_autosave() - autocmd BufWritePre *.mod call s:modfmt_autosave() - autocmd BufWritePre *.s call s:asmfmt_autosave() - autocmd BufWritePost *.go call s:metalinter_autosave() - autocmd BufNewFile *.go call s:template_autocreate() - " clear SameIds when the buffer is unloaded so that loading another buffer - " in the same window doesn't highlight the most recently matched - " identifier's positions. - autocmd BufWinEnter *.go call go#guru#ClearSameIds() - - autocmd BufEnter *.go - \ if go#config#AutodetectGopath() && !exists('b:old_gopath') - \| let b:old_gopath = exists('$GOPATH') ? $GOPATH : -1 - \| let $GOPATH = go#path#Detect() - \| endif - autocmd BufLeave *.go - \ if exists('b:old_gopath') - \| if b:old_gopath isnot -1 - \| let $GOPATH = b:old_gopath - \| endif - \| unlet b:old_gopath - \| endif + autocmd BufNewFile *.s if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif + autocmd BufRead *.s call s:gofiletype_pre() + autocmd BufReadPost *.s call s:gofiletype_post() augroup end " restore Vi compatibility settings