diff --git a/vim/bundle/vim-godef/README.md b/vim/bundle/vim-godef/README.md new file mode 100644 index 0000000..b587471 --- /dev/null +++ b/vim/bundle/vim-godef/README.md @@ -0,0 +1,43 @@ +## You probably want to be using https://github.com/fatih/vim-go instead + + +This plugin adds godef support to vim. + +The `godef` tool from Roger Peppe parses Go code and returns the location of +the definition of a symbol. It can be installed with + + go get -v github.com/rogpeppe/godef + go install -v github.com/rogpeppe/godef + +To install the vim-godef plugin, clone this vim-godef repository and (from it) copy `plugin/godef.vim` to `~/.vim/plugin` . + +Or, if you're using pathogen, on Linux: + + git clone https://github.com/dgryski/vim-godef ~/.vim/bundle/vim-godef + +or on Windows: + + git clone https://github.com/dgryski/vim-godef %USERPROFILE%\vimfiles\bundle\vim-godef + +This modules overrides the `gd` (go to local definition) command to open a new +window at the definition of the symbol under the cursor. Setting + + g:godef_split=0 + +(that is, entering the command `let g:godef_split=0` or inserting it into your ~/.vimrc) +will reuse the current window, and + + g:godef_split=2 + +will open the definition in a new tab, and + + g:godef_split=3 + +will use a vertical split instead of the default horizontal split. + +If you want jumps to the same file to move your current cursor instead of splitting, use + + g:godef_same_file_in_same_window=1 + +This also adds a `:Godef ` which will work for global types, methods, +constants, and variables in the current package. diff --git a/vim/bundle/vim-godef/plugin/godef.vim b/vim/bundle/vim-godef/plugin/godef.vim new file mode 100644 index 0000000..157e684 --- /dev/null +++ b/vim/bundle/vim-godef/plugin/godef.vim @@ -0,0 +1,59 @@ +" needs https://github.com/rogpeppe/rog-go/tree/master/exp/cmd/godef/godef.go + +if !exists("g:godef_command") + let g:godef_command = "godef" +endif + +if !exists("g:godef_split") + let g:godef_split = 1 +endif + +if !exists("g:godef_same_file_in_same_window") + let g:godef_same_file_in_same_window=0 +endif + +if !exists("g:godef_system_function") + let g:godef_system_function="system" +endif + +function! GodefUnderCursor() + let pos = getpos(".")[1:2] + if &encoding == 'utf-8' + let offs = line2byte(pos[0]) + pos[1] - 2 + else + let c = pos[1] + let buf = line('.') == 1 ? "" : (join(getline(1, pos[0] - 1), "\n") . "\n") + let buf .= c == 1 ? "" : getline(pos[0])[:c-2] + let offs = len(iconv(buf, &encoding, "utf-8")) + endif + silent call Godef("-o=" . offs) +endfunction + +function! Godef(arg) + let out=call(g:godef_system_function, [g:godef_command . " -f=" . expand("%:p") . " -i " . shellescape(a:arg), join(getbufline(bufnr('%'), 1, '$'), "\n")]) + + let old_errorformat = &errorformat + let &errorformat = "%f:%l:%c" + + if out =~ 'godef: ' + let out=substitute(out, '\n$', '', '') + echom out + elseif g:godef_same_file_in_same_window == 1 && (out) =~ "^".expand("%:p") + let x=stridx(out, ":") + let out=expand("%").strpart(out, x, len(out)-x) + lexpr out + else + if g:godef_split == 1 + split + elseif g:godef_split == 2 + tabnew + elseif g:godef_split == 3 + vsplit + endif + lexpr out + end + let &errorformat = old_errorformat +endfunction + +autocmd FileType go nnoremap gd :call GodefUnderCursor() +command! -range -nargs=1 Godef :call Godef() diff --git a/vim/bundles.vim b/vim/bundles.vim index 869039d..a2bf19d 100644 --- a/vim/bundles.vim +++ b/vim/bundles.vim @@ -20,6 +20,7 @@ Bundle 'scrooloose/nerdcommenter' Bundle 'vim-scripts/DoxygenToolkit.vim' Bundle 'davidhalter/jedi-vim' Bundle 'fatih/vim-go' +Bundle 'dgryski/vim-godef' filetype plugin indent on " required!