Skip to content

Commit

Permalink
refactor autocmd events
Browse files Browse the repository at this point in the history
Refactor ftdetect/gofiletype.vim so that it _only_ sets filetype; it no
longer sets fileformat or fileencoding. fileformat and fileencoding are
now set by autocmd events configured in plugin/go.vim

Move most functions called by autocmd events into autoload/go/auto.vim.
The only exceptions are s:gofiletype_pre and s:gofiletype_post in
plugin/go.vim.

Consistently use `setfiletype`; previously `set filetype` was used for
gohtmltmpl and gomod files.

Change s:gofiletype_pre in plugin/go.vim so that it does not set the
filetype; it's already set in ftdetect/go.vim and setting it again may
cause problems for any user that needs to set it to something else.

Move the configuration of all autocmd events that occur after the
FileType event into ftplugin files and clear them in undo_ftplugin so
that they will not execute if the filetype is changed.
  • Loading branch information
bhcleek committed Jan 5, 2019
1 parent 94b97d3 commit 1ed9b52
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 130 deletions.
79 changes: 79 additions & 0 deletions 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
38 changes: 6 additions & 32 deletions ftdetect/gofiletype.vim
Expand Up @@ -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 <path>'.
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('$'))
Expand All @@ -52,7 +26,7 @@ fun! s:gomod()
endif

if l:l =~# '^module .\+'
set filetype=gomod
setfiletype gomod
endif

break
Expand Down
12 changes: 11 additions & 1 deletion ftplugin/asm.vim
Expand Up @@ -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 * <buffer>'"

setlocal formatoptions-=t

Expand All @@ -20,6 +21,15 @@ setlocal noexpandtab

command! -nargs=0 AsmFmt call go#asmfmt#Format()

" Autocommands
" ============================================================================

augroup vim-go-asm-buffer
autocmd! * <buffer>

autocmd BufWritePre <buffer> call go#auto#asmfmt_autosave()
augroup end

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
40 changes: 39 additions & 1 deletion ftplugin/go.vim
Expand Up @@ -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 * <buffer>'"

setlocal formatoptions-=t

Expand Down Expand Up @@ -72,6 +73,43 @@ if go#config#AutoTypeInfo() || go#config#AutoSameids()
let &l:updatetime= get(g:, "go_updatetime", 800)
endif

" Autocommands
" ============================================================================
"
augroup vim-go-buffer
autocmd! * <buffer>

autocmd CursorHold <buffer> call go#auto#auto_type_info()
autocmd CursorHold <buffer> 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 <buffer> call go#auto#echo_go_info()
endif

autocmd BufWritePre <buffer> call go#auto#fmt_autosave()
autocmd BufWritePost <buffer> 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 <buffer> call go#guru#ClearSameIds()

autocmd BufEnter <buffer>
\ if go#config#AutodetectGopath() && !exists('b:old_gopath')
\| let b:old_gopath = exists('$GOPATH') ? $GOPATH : -1
\| let $GOPATH = go#path#Detect()
\| endif
autocmd BufLeave <buffer>
\ 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
unlet s:cpo_save
Expand Down
12 changes: 11 additions & 1 deletion ftplugin/gomod.vim
Expand Up @@ -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 * <buffer>'"

setlocal formatoptions-=t

setlocal comments=s1:/*,mb:*,ex:*/,://
setlocal commentstring=//\ %s

" Autocommands
" ============================================================================

augroup vim-go-gomod-buffer
autocmd! * <buffer>

autocmd BufWritePre <buffer> call go#auto#modfmt_autosave()
augroup end

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
117 changes: 22 additions & 95 deletions plugin/go.vim
Expand Up @@ -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
Expand Down

0 comments on commit 1ed9b52

Please sign in to comment.