1
0
Fork 0
mirror of synced 2024-11-17 22:45:34 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/doc.vim

200 lines
5.1 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.
2018-12-17 06:28:27 -05:00
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
2014-10-31 17:30:24 -04:00
let s:buf_nr = -1
2016-12-27 09:46:49 -05: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')
2018-06-14 06:31:12 -04:00
let [l:json_out, l:err] = s:gogetdoc(1)
if l:err
2016-12-27 09:46:49 -05:00
call go#util#EchoError(json_out)
return
endif
2016-06-26 07:12:36 -04:00
2016-12-27 09:46:49 -05:00
let out = json_decode(json_out)
if type(out) != type({})
call go#util#EchoError("gogetdoc output is malformed")
endif
2016-06-26 07:12:36 -04:00
2016-12-27 09:46:49 -05:00
let import = out["import"]
let name = out["name"]
2017-02-11 08:01:38 -05:00
let decl = out["decl"]
2017-03-07 12:04:28 -05:00
2018-06-14 06:31:12 -04:00
let godoc_url = go#config#DocUrl()
let godoc_url .= "/" . import
2019-05-17 10:09:13 -04:00
if decl !~ '^package'
let anchor = name
if decl =~ '^func ('
let anchor = substitute(decl, '^func ([^ ]\+ \*\?\([^)]\+\)) ' . name . '(.*', '\1', '') . "." . name
endif
let godoc_url .= "#" . anchor
2016-12-27 09:46:49 -05:00
endif
2016-06-26 07:12:36 -04:00
2019-03-08 06:04:56 -05:00
call go#util#OpenBrowser(godoc_url)
2016-12-27 09:46:49 -05:00
return
2016-06-26 07:12:36 -04:00
endif
2015-07-13 06:22:46 -04:00
2016-06-26 07:12:36 -04:00
let pkgs = s:godocWord(a:000)
if empty(pkgs)
return
endif
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
let pkg = pkgs[0]
let exported_name = pkgs[1]
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
" example url: https://godoc.org/github.com/fatih/set#Set
2018-06-14 06:31:12 -04:00
let godoc_url = go#config#DocUrl() . "/" . pkg . "#" . exported_name
2019-03-08 06:04:56 -05:00
call go#util#OpenBrowser(godoc_url)
2014-10-31 17:30:24 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! go#doc#Open(newmode, mode, ...) abort
2017-09-02 06:43:18 -04:00
" With argument: run "godoc [arg]".
2016-06-26 07:12:36 -04:00
if len(a:000)
2018-12-17 06:28:27 -05:00
let [l:out, l:err] = go#util#Exec(['go', 'doc'] + a:000)
else " Without argument: run gogetdoc on cursor position.
2018-06-14 06:31:12 -04:00
let [l:out, l:err] = s:gogetdoc(0)
2017-09-02 06:43:18 -04:00
if out == -1
return
endif
2016-06-26 07:12:36 -04:00
endif
2014-10-31 17:30:24 -04:00
2018-06-14 06:31:12 -04:00
if l:err
2016-06-26 07:12:36 -04:00
call go#util#EchoError(out)
return
endif
2016-04-12 04:31:09 -04:00
2016-06-26 07:12:36 -04:00
call s:GodocView(a:newmode, a:mode, out)
endfunction
2016-12-27 09:46:49 -05:00
function! s:GodocView(newposition, position, content) abort
2016-06-26 07:12:36 -04:00
" reuse existing buffer window if it exists otherwise create a new one
let is_visible = bufexists(s:buf_nr) && bufwinnr(s:buf_nr) != -1
2016-06-26 07:12:36 -04:00
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
" if window was not visible then resize it
if !is_visible
if a:position == "split"
" cap window height to 20, but resize it for smaller contents
2018-06-14 06:31:12 -04:00
let max_height = go#config#DocMaxHeight()
let content_height = len(split(a:content, "\n"))
if content_height > max_height
exe 'resize ' . max_height
else
exe 'resize ' . content_height
endif
2017-03-07 12:04:28 -05:00
else
" 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'
2017-03-07 12:04:28 -05:00
endif
2016-06-26 07:12:36 -04: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
2018-11-01 06:03:42 -04:00
" close easily with enter
2016-06-26 07:12:36 -04:00
noremap <buffer> <silent> <CR> :<C-U>close<CR>
noremap <buffer> <silent> <Esc> :<C-U>close<CR>
2018-11-01 06:03:42 -04:00
" make sure any key that sends an escape as a prefix (e.g. the arrow keys)
" don't cause the window to close.
nnoremap <buffer> <silent> <Esc>[ <Esc>[
2016-04-12 04:31:09 -04:00
endfunction
2014-10-31 17:30:24 -04:00
2016-12-27 09:46:49 -05:00
function! s:gogetdoc(json) abort
2018-06-14 06:31:12 -04:00
let l:cmd = [
\ 'gogetdoc',
\ '-tags', go#config#BuildTags(),
\ '-pos', expand("%:p:gs!\\!/!") . ':#' . go#util#OffsetCursor()]
2016-12-27 09:46:49 -05:00
if a:json
2018-06-14 06:31:12 -04:00
let l:cmd += ['-json']
2016-12-27 09:46:49 -05:00
endif
if &modified
2018-06-14 06:31:12 -04:00
let l:cmd += ['-modified']
return go#util#Exec(l:cmd, go#util#archive())
2016-12-27 09:46:49 -05:00
endif
2018-06-14 06:31:12 -04:00
return go#util#Exec(l:cmd)
2016-12-27 09:46:49 -05:00
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
2018-12-17 06:28:27 -05:00
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et