Skip to content

Commit

Permalink
plugin: add vim-godef plugin
Browse files Browse the repository at this point in the history
  vim-godef:a679733999fce6a566704a4df15f6813a900e8a9

Signed-off-by: lilei <[email protected]>
  • Loading branch information
lilei committed Dec 2, 2019
1 parent 6505f25 commit b134fc5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
43 changes: 43 additions & 0 deletions vim/bundle/vim-godef/README.md
Original file line number Diff line number Diff line change
@@ -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.
59 changes: 59 additions & 0 deletions vim/bundle/vim-godef/plugin/godef.vim
Original file line number Diff line number Diff line change
@@ -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 <buffer> gd :call GodefUnderCursor()<cr>
command! -range -nargs=1 Godef :call Godef(<q-args>)
1 change: 1 addition & 0 deletions vim/bundles.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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!

0 comments on commit b134fc5

Please sign in to comment.