-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vim-godef:a679733999fce6a566704a4df15f6813a900e8a9 Signed-off-by: lilei <[email protected]>
- Loading branch information
lilei
committed
Dec 2, 2019
1 parent
6505f25
commit b134fc5
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters