Skip to content

Commit

Permalink
Merge pull request #2108 from bhcleek/pure-ftdetect
Browse files Browse the repository at this point in the history
refactor autocmd configuration
  • Loading branch information
bhcleek authored Jan 9, 2019
2 parents 8342cd4 + 1ed9b52 commit 0ab5221
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 196 deletions.
79 changes: 79 additions & 0 deletions autoload/go/auto.vim
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
104 changes: 37 additions & 67 deletions ftplugin/go.vim
Original file line number Diff line number Diff line change
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,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 <buffer> <silent> * :<c-u>call Sameids_search(0)<CR>
" autocmd FileType go nnoremap <buffer> <silent> # :<c-u>call Sameids_search(1)<CR>
" autocmd FileType go nnoremap <buffer> <silent> n :<c-u>call Sameids_repeat(0)<CR>
" autocmd FileType go nnoremap <buffer> <silent> N :<c-u>call Sameids_repeat(1)<CR>
" autocmd FileType go cabbrev nohlsearch <C-r>=Sameids_nohlsearch()<CR>
" 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! * <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
Expand Down
12 changes: 11 additions & 1 deletion ftplugin/gomod.vim
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 0ab5221

Please sign in to comment.