2014-10-31 21:30:24 +00: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.
|
|
|
|
|
|
|
|
let s:buf_nr = -1
|
|
|
|
|
|
|
|
if !exists("g:go_doc_command")
|
2016-06-26 11:12:36 +00:00
|
|
|
let g:go_doc_command = "godoc"
|
2014-10-31 21:30:24 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
if !exists("g:go_doc_options")
|
2016-06-26 11:12:36 +00:00
|
|
|
let g:go_doc_options = ""
|
2014-10-31 21:30:24 +00:00
|
|
|
endif
|
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
function! go#doc#OpenBrowser(...) abort
|
|
|
|
" check if we have gogetdoc as it gives us more and accurate information.
|
|
|
|
" Only supported if we have json_decode as it's not worth to parse the plain
|
|
|
|
" non-json output of gogetdoc
|
|
|
|
let bin_path = go#path#CheckBinPath('gogetdoc')
|
|
|
|
if !empty(bin_path) && exists('*json_decode')
|
|
|
|
let json_out = s:gogetdoc(1)
|
|
|
|
if go#util#ShellError() != 0
|
|
|
|
call go#util#EchoError(json_out)
|
|
|
|
return
|
|
|
|
endif
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
let out = json_decode(json_out)
|
|
|
|
if type(out) != type({})
|
|
|
|
call go#util#EchoError("gogetdoc output is malformed")
|
|
|
|
endif
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
let import = out["import"]
|
|
|
|
let name = out["name"]
|
2017-02-11 13:01:38 +00:00
|
|
|
let decl = out["decl"]
|
2017-03-07 17:04:28 +00:00
|
|
|
|
2017-02-11 13:01:38 +00:00
|
|
|
let godoc_url = "https://godoc.org/" . import
|
|
|
|
if decl !~ "^package"
|
2017-03-07 17:04:28 +00:00
|
|
|
let godoc_url .= "#" . name
|
2016-12-27 14:46:49 +00:00
|
|
|
endif
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
echo godoc_url
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
call go#tool#OpenBrowser(godoc_url)
|
|
|
|
return
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2015-07-13 10:22:46 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
let pkgs = s:godocWord(a:000)
|
|
|
|
if empty(pkgs)
|
|
|
|
return
|
|
|
|
endif
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
let pkg = pkgs[0]
|
|
|
|
let exported_name = pkgs[1]
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
" 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)
|
2014-10-31 21:30:24 +00:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
function! go#doc#Open(newmode, mode, ...) abort
|
2016-06-26 11:12:36 +00:00
|
|
|
if len(a:000)
|
|
|
|
" check if we have 'godoc' and use it automatically
|
|
|
|
let bin_path = go#path#CheckBinPath('godoc')
|
|
|
|
if empty(bin_path)
|
|
|
|
return
|
2016-06-11 13:56:50 +00:00
|
|
|
endif
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
let command = printf("%s %s", bin_path, join(a:000, ' '))
|
2016-10-02 11:37:21 +00:00
|
|
|
let out = go#util#System(command)
|
2016-06-26 11:12:36 +00:00
|
|
|
else
|
2016-12-27 14:46:49 +00:00
|
|
|
let out = s:gogetdoc(0)
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
if go#util#ShellError() != 0
|
|
|
|
call go#util#EchoError(out)
|
|
|
|
return
|
|
|
|
endif
|
2016-04-12 08:31:09 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
call s:GodocView(a:newmode, a:mode, out)
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
function! s:GodocView(newposition, position, content) abort
|
2016-06-26 11:12:36 +00:00
|
|
|
" reuse existing buffer window if it exists otherwise create a new one
|
|
|
|
if !bufexists(s:buf_nr)
|
|
|
|
execute a:newposition
|
|
|
|
sil file `="[Godoc]"`
|
|
|
|
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
|
|
|
|
|
2017-03-07 17:04:28 +00:00
|
|
|
if a:position == "split"
|
2017-07-06 12:57:35 +00:00
|
|
|
" cap window height to 20, but resize it for smaller contents
|
|
|
|
let max_height = get(g:, "go_doc_max_height", 20)
|
2017-03-07 17:04:28 +00:00
|
|
|
let content_height = len(split(a:content, "\n"))
|
|
|
|
if content_height > max_height
|
|
|
|
exe 'resize ' . max_height
|
|
|
|
else
|
|
|
|
exe 'resize ' . content_height
|
|
|
|
endif
|
2016-06-26 11:12:36 +00:00
|
|
|
else
|
2017-03-07 17:04:28 +00:00
|
|
|
" set a sane maximum width for vertical splits. In this case the minimum
|
|
|
|
" that fits the godoc for package http without extra linebreaks and line
|
|
|
|
" numbers on
|
|
|
|
exe 'vertical resize 84'
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
setlocal filetype=godoc
|
|
|
|
setlocal bufhidden=delete
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal noswapfile
|
|
|
|
setlocal nobuflisted
|
|
|
|
setlocal nocursorline
|
|
|
|
setlocal nocursorcolumn
|
|
|
|
setlocal iskeyword+=:
|
|
|
|
setlocal iskeyword-=-
|
|
|
|
|
|
|
|
setlocal modifiable
|
|
|
|
%delete _
|
|
|
|
call append(0, split(a:content, "\n"))
|
|
|
|
sil $delete _
|
|
|
|
setlocal nomodifiable
|
|
|
|
sil normal! gg
|
|
|
|
|
|
|
|
" close easily with <esc> or enter
|
|
|
|
noremap <buffer> <silent> <CR> :<C-U>close<CR>
|
|
|
|
noremap <buffer> <silent> <Esc> :<C-U>close<CR>
|
2016-04-12 08:31:09 +00:00
|
|
|
endfunction
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
function! s:gogetdoc(json) abort
|
|
|
|
" check if we have 'gogetdoc' and use it automatically
|
|
|
|
let bin_path = go#path#CheckBinPath('gogetdoc')
|
|
|
|
if empty(bin_path)
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
let cmd = [bin_path]
|
|
|
|
|
|
|
|
let offset = go#util#OffsetCursor()
|
|
|
|
let fname = expand("%:p:gs!\\!/!")
|
|
|
|
let pos = shellescape(fname.':#'.offset)
|
|
|
|
|
|
|
|
let cmd += ["-pos", pos]
|
|
|
|
if a:json
|
|
|
|
let cmd += ["-json"]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let command = join(cmd, " ")
|
|
|
|
|
|
|
|
if &modified
|
|
|
|
" gogetdoc supports the same archive format as guru for dealing with
|
|
|
|
" modified buffers.
|
|
|
|
" use the -modified flag
|
|
|
|
" write each archive entry on stdin as:
|
|
|
|
" filename followed by newline
|
|
|
|
" file size followed by newline
|
|
|
|
" file contents
|
|
|
|
let in = ""
|
2017-02-11 13:01:38 +00:00
|
|
|
let content = join(go#util#GetLines(), "\n")
|
2016-12-27 14:46:49 +00:00
|
|
|
let in = fname . "\n" . strlen(content) . "\n" . content
|
|
|
|
let command .= " -modified"
|
|
|
|
let out = go#util#System(command, in)
|
|
|
|
else
|
|
|
|
let out = go#util#System(command)
|
|
|
|
endif
|
|
|
|
|
|
|
|
return out
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" 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) abort
|
|
|
|
if !executable('godoc')
|
|
|
|
let msg = "godoc command not found."
|
|
|
|
let msg .= " install with: go get golang.org/x/tools/cmd/godoc"
|
|
|
|
call go#util#EchoWarning(msg)
|
|
|
|
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
|
|
|
|
|
|
|
|
if !len(words)
|
|
|
|
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
|
|
|
|
|
|
|
|
function! s:godocNotFound(content) abort
|
|
|
|
if len(a:content) == 0
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
return a:content =~# '^.*: no such file or directory\n$'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
" vim: sw=2 ts=2 et
|