1
0
Fork 0
mirror of synced 2024-11-27 03:05:36 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/doc.vim

172 lines
3.9 KiB
VimL
Raw Normal View History

2014-10-31 17:30:24 -04: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.
"
"
" 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.
let s:buf_nr = -1
if !exists("g:go_doc_command")
let g:go_doc_command = "godoc"
endif
if !exists("g:go_doc_options")
let g:go_doc_options = ""
endif
" returns the package and exported name. exported name might be empty.
" ie: fmt and Println
" ie: github.com/fatih/set and New
function! s:godocWord(args)
if !executable('godoc')
echohl WarningMsg
echo "godoc command not found."
2015-01-18 07:58:28 -05:00
echo " install with: go get golang.org/x/tools/cmd/godoc"
2014-10-31 17:30:24 -04:00
echohl None
return []
endif
if !len(a:args)
let oldiskeyword = &iskeyword
setlocal iskeyword+=.
let word = expand('<cword>')
let &iskeyword = oldiskeyword
let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
let words = split(word, '\.\ze[^./]\+$')
else
let words = a:args
endif
2015-01-18 07:58:28 -05:00
if !len(words)
2014-10-31 17:30:24 -04:00
return []
endif
let pkg = words[0]
if len(words) == 1
let exported_name = ""
else
let exported_name = words[1]
endif
let packages = go#tool#Imports()
if has_key(packages, pkg)
let pkg = packages[pkg]
endif
return [pkg, exported_name]
endfunction
2015-07-13 06:22:46 -04:00
function! s:godocNotFound(content)
if len(a:content) == 0
return 1
endif
return a:content =~# '^.*: no such file or directory\n$'
endfunction
2014-10-31 17:30:24 -04:00
function! go#doc#OpenBrowser(...)
let pkgs = s:godocWord(a:000)
if empty(pkgs)
return
endif
let pkg = pkgs[0]
let exported_name = pkgs[1]
" example url: https://godoc.org/github.com/fatih/set#Set
let godoc_url = "https://godoc.org/" . pkg . "#" . exported_name
call go#tool#OpenBrowser(godoc_url)
endfunction
2015-01-18 07:58:28 -05:00
function! go#doc#Open(newmode, mode, ...)
2014-10-31 17:30:24 -04:00
let pkgs = s:godocWord(a:000)
if empty(pkgs)
return
endif
let pkg = pkgs[0]
let exported_name = pkgs[1]
let command = g:go_doc_command . ' ' . g:go_doc_options . ' ' . pkg
silent! let content = system(command)
2015-07-13 06:22:46 -04:00
if v:shell_error || s:godocNotFound(content)
2014-10-31 17:30:24 -04:00
echo 'No documentation found for "' . pkg . '".'
return -1
endif
2015-01-18 07:58:28 -05:00
call s:GodocView(a:newmode, a:mode, content)
2014-10-31 17:30:24 -04:00
2015-01-18 07:58:28 -05:00
if exported_name == ''
2016-03-14 06:04:57 -04:00
silent! normal! gg
2015-01-18 07:58:28 -05:00
return -1
endif
2014-10-31 17:30:24 -04:00
2015-01-18 07:58:28 -05:00
" jump to the specified name
2014-10-31 17:30:24 -04:00
if search('^func ' . exported_name . '(')
2016-03-14 06:04:57 -04:00
silent! normal! zt
2014-10-31 17:30:24 -04:00
return -1
endif
if search('^type ' . exported_name)
2016-03-14 06:04:57 -04:00
silent! normal! zt
2014-10-31 17:30:24 -04:00
return -1
endif
if search('^\%(const\|var\|type\|\s\+\) ' . pkg . '\s\+=\s')
2016-03-14 06:04:57 -04:00
silent! normal! zt
2014-10-31 17:30:24 -04:00
return -1
endif
" nothing found, jump to top
2016-03-14 06:04:57 -04:00
silent! normal! gg
2014-10-31 17:30:24 -04:00
endfunction
2015-01-18 07:58:28 -05:00
function! s:GodocView(newposition, position, content)
2014-10-31 17:30:24 -04:00
" reuse existing buffer window if it exists otherwise create a new one
if !bufexists(s:buf_nr)
2015-01-18 07:58:28 -05:00
execute a:newposition
sil file `="[Godoc]"`
2014-10-31 17:30:24 -04:00
let s:buf_nr = bufnr('%')
elseif bufwinnr(s:buf_nr) == -1
execute a:position
execute s:buf_nr . 'buffer'
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 nocursorline
setlocal nocursorcolumn
setlocal iskeyword+=:
setlocal iskeyword-=-
setlocal modifiable
2015-01-18 07:58:28 -05:00
%delete _
2014-10-31 17:30:24 -04:00
call append(0, split(a:content, "\n"))
2015-01-18 07:58:28 -05:00
sil $delete _
2014-10-31 17:30:24 -04:00
setlocal nomodifiable
endfunction
" vim:ts=4:sw=4:et