2011-08-03 00:46:35 -06:00
|
|
|
" Copyright 2011 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.
|
|
|
|
"
|
|
|
|
" godoc.vim: Vim command to see godoc.
|
2013-06-12 23:07:09 -06:00
|
|
|
"
|
|
|
|
"
|
|
|
|
" Commands:
|
|
|
|
"
|
|
|
|
" :Godoc
|
|
|
|
"
|
|
|
|
" Open the relevant Godoc for either the word[s] passed to the command or
|
|
|
|
" the, by default, the word under the cursor.
|
|
|
|
"
|
|
|
|
" Options:
|
|
|
|
"
|
|
|
|
" g:go_godoc_commands [default=1]
|
|
|
|
"
|
|
|
|
" Flag to indicate whether to enable the commands listed above.
|
2011-08-03 00:46:35 -06:00
|
|
|
|
|
|
|
if exists("g:loaded_godoc")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_godoc = 1
|
|
|
|
|
|
|
|
let s:buf_nr = -1
|
|
|
|
let s:last_word = ''
|
|
|
|
|
2013-06-12 23:07:09 -06:00
|
|
|
if !exists('g:go_godoc_commands')
|
|
|
|
let g:go_godoc_commands = 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if g:go_godoc_commands
|
2013-10-01 16:52:51 -06:00
|
|
|
command! -nargs=* -range -complete=customlist,go#complete#Package Godoc :call s:Godoc(<f-args>)
|
2013-06-12 23:07:09 -06:00
|
|
|
endif
|
|
|
|
|
|
|
|
nnoremap <silent> <Plug>(godoc-keyword) :<C-u>call <SID>Godoc('')<CR>
|
|
|
|
|
2011-08-03 00:46:35 -06:00
|
|
|
function! s:GodocView()
|
|
|
|
if !bufexists(s:buf_nr)
|
|
|
|
leftabove new
|
|
|
|
file `="[Godoc]"`
|
|
|
|
let s:buf_nr = bufnr('%')
|
|
|
|
elseif bufwinnr(s:buf_nr) == -1
|
|
|
|
leftabove split
|
|
|
|
execute s:buf_nr . 'buffer'
|
|
|
|
delete _
|
|
|
|
elseif bufwinnr(s:buf_nr) != bufwinnr('%')
|
|
|
|
execute bufwinnr(s:buf_nr) . 'wincmd w'
|
|
|
|
endif
|
|
|
|
|
|
|
|
setlocal filetype=godoc
|
|
|
|
setlocal bufhidden=delete
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal noswapfile
|
|
|
|
setlocal nobuflisted
|
|
|
|
setlocal modifiable
|
|
|
|
setlocal nocursorline
|
|
|
|
setlocal nocursorcolumn
|
|
|
|
setlocal iskeyword+=:
|
|
|
|
setlocal iskeyword-=-
|
|
|
|
|
|
|
|
nnoremap <buffer> <silent> K :Godoc<cr>
|
|
|
|
|
|
|
|
au BufHidden <buffer> call let <SID>buf_nr = -1
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:GodocWord(word)
|
2013-09-26 20:35:03 -06:00
|
|
|
if !executable('godoc')
|
|
|
|
echohl WarningMsg
|
|
|
|
echo "godoc command not found."
|
|
|
|
echo " install with: go get code.google.com/p/go.tools/cmd/godoc"
|
|
|
|
echohl None
|
2013-10-01 16:52:51 -06:00
|
|
|
return 0
|
2013-09-26 20:35:03 -06:00
|
|
|
endif
|
2011-08-03 00:46:35 -06:00
|
|
|
let word = a:word
|
|
|
|
silent! let content = system('godoc ' . word)
|
|
|
|
if v:shell_error || !len(content)
|
|
|
|
if len(s:last_word)
|
|
|
|
silent! let content = system('godoc ' . s:last_word.'/'.word)
|
|
|
|
if v:shell_error || !len(content)
|
|
|
|
echo 'No documentation found for "' . word . '".'
|
2013-10-01 16:52:51 -06:00
|
|
|
return 0
|
2011-08-03 00:46:35 -06:00
|
|
|
endif
|
|
|
|
let word = s:last_word.'/'.word
|
|
|
|
else
|
|
|
|
echo 'No documentation found for "' . word . '".'
|
2013-10-01 16:52:51 -06:00
|
|
|
return 0
|
2011-08-03 00:46:35 -06:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let s:last_word = word
|
|
|
|
silent! call s:GodocView()
|
|
|
|
setlocal modifiable
|
|
|
|
silent! %d _
|
|
|
|
silent! put! =content
|
|
|
|
silent! normal gg
|
|
|
|
setlocal nomodifiable
|
|
|
|
setfiletype godoc
|
2013-10-01 16:52:51 -06:00
|
|
|
return 1
|
2011-08-03 00:46:35 -06:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:Godoc(...)
|
2013-10-01 16:52:51 -06:00
|
|
|
if !len(a:000)
|
2013-03-26 00:39:46 -06:00
|
|
|
let oldiskeyword = &iskeyword
|
|
|
|
setlocal iskeyword+=.
|
2011-08-03 00:46:35 -06:00
|
|
|
let word = expand('<cword>')
|
2013-03-26 00:39:46 -06:00
|
|
|
let &iskeyword = oldiskeyword
|
2013-10-01 16:52:51 -06:00
|
|
|
let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
|
|
|
|
let words = split(word, '\.\ze[^./]\+$')
|
|
|
|
else
|
|
|
|
let words = a:000
|
2011-08-03 00:46:35 -06:00
|
|
|
endif
|
2013-03-26 00:39:46 -06:00
|
|
|
if !len(words)
|
2011-08-03 00:46:35 -06:00
|
|
|
return
|
|
|
|
endif
|
2013-10-01 16:52:51 -06:00
|
|
|
if s:GodocWord(words[0])
|
|
|
|
if len(words) > 1
|
|
|
|
if search('^\%(const\|var\|type\|\s\+\) ' . words[1] . '\s\+=\s')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
if search('^func ' . words[1] . '(')
|
|
|
|
silent! normal zt
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
echo 'No documentation found for "' . words[1] . '".'
|
2013-03-26 00:39:46 -06:00
|
|
|
endif
|
|
|
|
endif
|
2011-08-03 00:46:35 -06:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" vim:ts=4:sw=4:et
|