Skip to content

Commit

Permalink
doc: use gogetdoc to display docs in a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Apr 10, 2016
1 parent 5ce6529 commit f478b53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 56 deletions.
84 changes: 28 additions & 56 deletions autoload/go/doc.vim
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
" 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

Expand All @@ -33,10 +17,9 @@ endif
" ie: github.com/fatih/set and New
function! s:godocWord(args)
if !executable('godoc')
echohl WarningMsg
echo "godoc command not found."
echo " install with: go get golang.org/x/tools/cmd/godoc"
echohl None
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

Expand Down Expand Up @@ -94,47 +77,24 @@ function! go#doc#OpenBrowser(...)
endfunction

function! go#doc#Open(newmode, mode, ...)
let pkgs = s:godocWord(a:000)
if empty(pkgs)
" check if we have 'gogetdoc' and use it automatically
let bin_path = go#path#CheckBinPath('gogetdoc')
if empty(bin_path)
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)
if v:shell_error || s:godocNotFound(content)
echo 'No documentation found for "' . pkg . '".'
return -1
endif

call s:GodocView(a:newmode, a:mode, content)
let offset = go#util#OffsetCursor()
let fname = expand("%:p")

if exported_name == ''
silent! normal! gg
return -1
endif
let command = printf("%s -pos %s:#%s", bin_path, fname, offset)

" jump to the specified name
if search('^func ' . exported_name . '(')
silent! normal! zt
return -1
endif

if search('^type ' . exported_name)
silent! normal! zt
return -1
endif

if search('^\%(const\|var\|type\|\s\+\) ' . pkg . '\s\+=\s')
silent! normal! zt
return -1
let out = system(command)
if v:shell_error != 0
call go#util#EchoError(out)
return
endif

" nothing found, jump to top
silent! normal! gg
call s:GodocView(a:newmode, a:mode, out)
endfunction

function! s:GodocView(newposition, position, content)
Expand All @@ -150,6 +110,15 @@ function! s:GodocView(newposition, position, content)
execute bufwinnr(s:buf_nr) . 'wincmd w'
endif

" cap buffer height to 20, but resize it for smaller contents
let max_height = 20
let content_height = len(split(a:content, "\n"))
if content_height > max_height
exe 'resize ' . max_height
else
exe 'resize ' . content_height
endif

setlocal filetype=godoc
setlocal bufhidden=delete
setlocal buftype=nofile
Expand All @@ -165,7 +134,10 @@ function! s:GodocView(newposition, position, content)
call append(0, split(a:content, "\n"))
sil $delete _
setlocal nomodifiable
endfunction

" close easily with <esc> or enter
noremap <buffer> <silent> <CR> :<C-U>close<CR>
noremap <buffer> <silent> <Esc> :<C-U>close<CR>
endfunction

" vim:ts=4:sw=4:et
" vim:ts=2:sw=2:et
1 change: 1 addition & 0 deletions plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let s:packages = [
\ "github.com/jstemmer/gotags",
\ "github.com/klauspost/asmfmt/cmd/asmfmt",
\ "github.com/fatih/motion",
\ "github.com/zmb3/gogetdoc",
\ ]

" These commands are available on any filetypes
Expand Down

0 comments on commit f478b53

Please sign in to comment.