November 19, 2014

Read godoc in vim using Shift K

Add the following to your ~/.vim/ftplugin/go.vim (the key here is to use <cfile> not <cword>)

fun! ReadMan()
  " Assign current file under cursor to a script variable:
  let s:man_word = expand('<cfile>')
  :exe ":Godoc " . s:man_word
endfun
" Map the K key to the ReadMan function:
map K :call ReadMan()<CR>

=======UPDATE========
Had a better way for this:

1. Create a file in your ~/bin called godoc.sh with the following content:
#!/bin/sh
fullpkg=`gawk -v "cmd=$1" '
{ FS="/" }
{
        if ($NF==cmd || $0 ==cmd){
                printf("%s",$0);
                exit;
        }
}' ~/bin/gopkg-list.txt`
echo -n $fullpkg

2. Create a text file in ~/bin/gopkg-list.txt with the following contents:
log/syslog
unicode/utf8
unicode/utf16
testing
hash/crc64
hash/adler32
hash/fnv
hash/crc32
archive/zip
archive/tar
expvar
mime
reflect
crypto/des
crypto/sha1
crypto/subtle
crypto/aes
crypto/md5
crypto/cipher
crypto/rand
crypto/ecdsa
crypto/tls
crypto/sha512
crypto/x509
crypto/sha256
crypto/elliptic
crypto/hmac
crypto/rsa
crypto/x509/pkix
crypto/rc4
crypto/dsa
flag
strings
sync
os/user
os/signal
os/exec
os
fmt
syscall
time
runtime/pprof
runtime/cgo
runtime/debug
runtime/race
net/http/httputil
net/http/cookiejar
net/http/fcgi
net/http/pprof
net/http/cgi
net/http/httptest
net/rpc/jsonrpc
net/mail
net/http
net/textproto
net/url
net/rpc
net/smtp
io/ioutil
math
database/sql/driver
database/sql
mime/multipart
bytes
errors
sort
bufio
encoding
log
path/filepath
index/suffixarray
net
crypto
sync/atomic
go/printer
go/doc
go/scanner
go/ast
go/parser
go/token
go/build
go/format
unicode
image
io
runtime
html/template
regexp
html
container/ring
container/list
container/heap
hash
path
regexp/syntax
text/template/parse
text/tabwriter
text/template
text/scanner
strconv
testing/quick
testing/iotest
compress/flate
compress/zlib
compress/lzw
compress/bzip2
compress/gzip
image/color
image/draw
image/color/palette
image/png
image/gif
image/jpeg
encoding/base64
encoding/base32
encoding/xml
encoding/asn1
encoding/pem
encoding/ascii85
encoding/csv
encoding/hex
encoding/json
encoding/binary
encoding/gob
debug/pe
debug/plan9obj
debug/dwarf
debug/elf
debug/gosym
debug/macho
math/rand
math/big
math/cmplx

3. In your ~/.vim/ftplugin/go.vim, add the following:
fun! ReadMan()
  " Assign current file under cursor to a script variable:
  let s:man_word = expand('<cword>')
  let s:full_word = system('~/bin/godoc.sh '. s:man_word)
  :exe ":Godoc " . s:full_word
endfun
" Map the K key to the ReadMan function:
map K :call ReadMan()<CR>

This will take the keyword under cursor, expanded using the shell script, and call :Godoc on it. This takes care of subpackages

No comments:

Post a Comment