" Copyright 2013 The Go Authors. All rights reserved. " Use of this source code is governed by a BSD-style " license that can be found in the LICENSE file. " " go.vim: Vim filetype plugin for Go. if exists("b:did_ftplugin") finish endif let b:did_ftplugin = 1 " don't spam the user when Vim is started in Vi compatibility mode let s:cpo_save = &cpo set cpo&vim let b:undo_ftplugin = "setl fo< com< cms<" \ . "| exe 'au! vim-go-buffer * '" setlocal formatoptions-=t setlocal comments=s1:/*,mb:*,ex:*/,:// setlocal commentstring=//\ %s setlocal noexpandtab compiler go if go#config#CodeCompletionEnabled() " Set autocompletion setlocal omnifunc=go#complete#Complete endif if get(g:, "go_doc_keywordprg_enabled", 1) " keywordprg doesn't allow to use vim commands, override it nnoremap K :GoDoc endif if get(g:, "go_def_mapping_enabled", 1) " these are default Vim mappings, we're overriding them to make them " useful again for Go source code nnoremap gd :GoDef nnoremap :GoDef nnoremap :GoDef nnoremap g :GoDef nnoremap :call go#def#Jump("split", 0) nnoremap ] :call go#def#Jump("split", 0) nnoremap :call go#def#StackPop(v:count1) endif if get(g:, "go_textobj_enabled", 1) onoremap af :call go#textobj#Function('a') xnoremap af :call go#textobj#Function('a') onoremap if :call go#textobj#Function('i') xnoremap if :call go#textobj#Function('i') onoremap ac :call go#textobj#Comment('a') xnoremap ac :call go#textobj#Comment('a') onoremap ic :call go#textobj#Comment('i') xnoremap ic :call go#textobj#Comment('i') " Remap ]] and [[ to jump betweeen functions as they are useless in Go nnoremap ]] :call go#textobj#FunctionJump('n', 'next') nnoremap [[ :call go#textobj#FunctionJump('n', 'prev') onoremap ]] :call go#textobj#FunctionJump('o', 'next') onoremap [[ :call go#textobj#FunctionJump('o', 'prev') xnoremap ]] :call go#textobj#FunctionJump('v', 'next') xnoremap [[ :call go#textobj#FunctionJump('v', 'prev') endif " Autocommands " ============================================================================ " augroup vim-go-buffer autocmd! * " The file is registered (textDocument/DidOpen) with gopls in plugin/go.vim " on the FileType event. if go#util#has_job() autocmd BufWritePost,FileChangedShellPost call go#lsp#DidChange(expand(':p')) autocmd BufDelete call go#lsp#DidClose(expand(':p')) endif " send the textDocument/didChange notification when idle. go#lsp#DidChange " will not send an event if the buffer hasn't changed since the last " notification. autocmd CursorHold,CursorHoldI call go#lsp#DidChange(expand(':p')) autocmd BufEnter,CursorHold call go#auto#update_autocmd() " 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#complete_done() endif autocmd BufWritePre call go#auto#fmt_autosave() autocmd BufWritePost call go#auto#metalinter_autosave() if !has('textprop') "TODO(bc): how to clear sameids and diagnostics when a non-go buffer is " loaded into a window and the previously loaded buffer is still loaded in " another window? " TODO(bc): only clear when the new buffer isn't the old buffer " clear SameIds when the buffer is unloaded from its last window so that " loading another buffer (especially of a different filetype) in the same " window doesn't highlight the most recently matched identifier's positions. autocmd BufWinLeave call go#guru#ClearSameIds() " clear SameIds when a new buffer is loaded in the window so that the " previous buffer's highlighting isn't used. autocmd BufWinEnter call go#guru#ClearSameIds() " clear diagnostics when the buffer is unloaded from its last window so that " loading another buffer (especially of a different filetype) in the same " window doesn't highlight the previously loaded buffer's diagnostics. autocmd BufWinLeave call go#lsp#ClearDiagnosticHighlights() " clear diagnostics when a new buffer is loaded in the window so that the " previous buffer's diagnostics aren't used. "autocmd BufWinEnter call go#lsp#ClearDiagnosticHighlights() endif 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 unlet s:cpo_save " vim: sw=2 ts=2 et