├── README.md ├── autoload └── go │ ├── env.vim │ └── complete.vim └── ftplugin └── go └── godoc.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-godoc 2 | 3 | :Godoc command 4 | 5 | ## Usage 6 | 7 | ``` 8 | :Godoc word 9 | ``` 10 | 11 | ## License 12 | 13 | MIT 14 | 15 | ## Author 16 | 17 | Yasuhiro Matsumoto (a.k.a. mattn) 18 | -------------------------------------------------------------------------------- /autoload/go/env.vim: -------------------------------------------------------------------------------- 1 | let s:cache = {} 2 | 3 | function! go#env#cache_clear(key) 4 | call remove(s:cache, a:key) 5 | endfunction 6 | 7 | function! go#env#root() 8 | if has_key(s:cache, 'root') 9 | return s:cache['root'] 10 | endif 11 | if executable('go') 12 | let root = substitute(system('go env GOROOT'), '\n', '', 'g') 13 | if v:shell_error 14 | echomsg '''go env GOROOT'' failed' 15 | return '' 16 | endif 17 | else 18 | let root = $GOROOT 19 | endif 20 | let s:cache['root'] = root 21 | return root 22 | endfunction 23 | -------------------------------------------------------------------------------- /autoload/go/complete.vim: -------------------------------------------------------------------------------- 1 | " Copyright 2011 The Go Authors. All rights reserved. 2 | " Use of this source code is governed by a BSD-style 3 | " license that can be found in the LICENSE file. 4 | " 5 | " This file provides a utility function that performs auto-completion of 6 | " package names, for use by other commands. 7 | 8 | let s:goos = $GOOS 9 | let s:goarch = $GOARCH 10 | 11 | if len(s:goos) == 0 12 | if exists('g:golang_goos') 13 | let s:goos = g:golang_goos 14 | elseif has('win32') || has('win64') 15 | let s:goos = 'windows' 16 | elseif has('macunix') 17 | let s:goos = 'darwin' 18 | else 19 | let s:goos = '*' 20 | endif 21 | endif 22 | 23 | if len(s:goarch) == 0 24 | if exists('g:golang_goarch') 25 | let s:goarch = g:golang_goarch 26 | else 27 | let s:goarch = '*' 28 | endif 29 | endif 30 | 31 | function! go#complete#PackageMembers(package, member) 32 | silent! let content = system('godoc ' . a:package) 33 | if v:shell_error || !len(content) 34 | return [] 35 | endif 36 | let lines = filter(split(content, "\n"),"v:val !~ '^\\s\\+$'") 37 | try 38 | let mx1 = '^\s\+\(\S+\)\s\+=\s\+.*' 39 | let mx2 = '^\%(const\|var\|type\|func\) \([A-Z][^ (]\+\).*' 40 | let candidates = 41 | \ map(filter(copy(lines), 'v:val =~ mx1'), 'substitute(v:val, mx1, "\\1", "")') 42 | \ + map(filter(copy(lines), 'v:val =~ mx2'), 'substitute(v:val, mx2, "\\1", "")') 43 | return filter(candidates, '!stridx(v:val, a:member)') 44 | catch 45 | return [] 46 | endtry 47 | endfunction 48 | 49 | function! go#complete#Package(ArgLead, CmdLine, CursorPos) 50 | let dirs = [] 51 | 52 | let words = split(a:CmdLine, '\s\+', 1) 53 | if len(words) > 2 54 | " Complete package members 55 | return go#complete#PackageMembers(words[1], words[2]) 56 | endif 57 | 58 | let goroot = go#env#root() 59 | 60 | if len(goroot) != 0 && isdirectory(goroot) 61 | let dirs += [goroot] 62 | endif 63 | 64 | let pathsep = ':' 65 | if s:goos == 'windows' 66 | let pathsep = ';' 67 | endif 68 | let workspaces = split($GOPATH, pathsep) 69 | if workspaces != [] 70 | let dirs += workspaces 71 | endif 72 | 73 | if len(dirs) == 0 74 | " should not happen 75 | return [] 76 | endif 77 | 78 | let ret = {} 79 | for dir in dirs 80 | " this may expand to multiple lines 81 | let root = split(expand(dir . '/pkg/' . s:goos . '_' . s:goarch), "\n") 82 | call add(root, expand(dir . '/src')) 83 | for r in root 84 | for i in split(globpath(r, a:ArgLead.'*'), "\n") 85 | if isdirectory(i) 86 | let i .= '/' 87 | elseif i !~ '\.a$' 88 | continue 89 | endif 90 | let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g') 91 | let ret[i] = i 92 | endfor 93 | endfor 94 | endfor 95 | return sort(keys(ret)) 96 | endfunction 97 | -------------------------------------------------------------------------------- /ftplugin/go/godoc.vim: -------------------------------------------------------------------------------- 1 | " Copyright 2011 The Go Authors. All rights reserved. 2 | " Use of this source code is governed by a BSD-style 3 | " license that can be found in the LICENSE file. 4 | " 5 | " godoc.vim: Vim command to see godoc. 6 | " 7 | " 8 | " Commands: 9 | " 10 | " :Godoc 11 | " 12 | " Open the relevant Godoc for either the word[s] passed to the command or 13 | " the, by default, the word under the cursor. 14 | " 15 | " Options: 16 | " 17 | " g:go_godoc_commands [default=1] 18 | " 19 | " Flag to indicate whether to enable the commands listed above. 20 | 21 | if exists("g:loaded_godoc") 22 | finish 23 | endif 24 | let g:loaded_godoc = 1 25 | 26 | let s:buf_nr = -1 27 | let s:last_word = '' 28 | 29 | if !exists('g:go_godoc_commands') 30 | let g:go_godoc_commands = 1 31 | endif 32 | 33 | if g:go_godoc_commands 34 | command! -nargs=* -range -complete=customlist,go#complete#Package Godoc :call s:Godoc() 35 | endif 36 | 37 | nnoremap (godoc-keyword) :call Godoc('') 38 | 39 | function! s:GodocView() 40 | if !bufexists(s:buf_nr) 41 | leftabove new 42 | file `="[Godoc]"` 43 | let s:buf_nr = bufnr('%') 44 | elseif bufwinnr(s:buf_nr) == -1 45 | leftabove split 46 | execute s:buf_nr . 'buffer' 47 | delete _ 48 | elseif bufwinnr(s:buf_nr) != bufwinnr('%') 49 | execute bufwinnr(s:buf_nr) . 'wincmd w' 50 | endif 51 | 52 | setlocal filetype=godoc 53 | setlocal bufhidden=delete 54 | setlocal buftype=nofile 55 | setlocal noswapfile 56 | setlocal nobuflisted 57 | setlocal modifiable 58 | setlocal nocursorline 59 | setlocal nocursorcolumn 60 | setlocal iskeyword+=: 61 | setlocal iskeyword-=- 62 | 63 | nnoremap K :Godoc 64 | 65 | au BufHidden call let buf_nr = -1 66 | endfunction 67 | 68 | function! s:GodocNotFound(content) 69 | if !len(a:content) 70 | return 1 71 | endif 72 | 73 | return a:content =~# '^.*: no such file or directory\n' 74 | endfunction 75 | 76 | function! s:GodocWord(word) 77 | if !executable('go') 78 | echohl WarningMsg 79 | echo "go command not found." 80 | echohl None 81 | return 0 82 | endif 83 | let word = a:word 84 | silent! let content = system('go doc ' . word) 85 | if v:shell_error || !len(content) 86 | if len(s:last_word) 87 | silent! let content = system('go doc ' . s:last_word.'/'.word) 88 | if v:shell_error || s:GodocNotFound(content) 89 | echo 'No documentation found for "' . word . '".' 90 | return 0 91 | endif 92 | let word = s:last_word.'/'.word 93 | else 94 | echo 'No documentation found for "' . word . '".' 95 | return 0 96 | endif 97 | endif 98 | let s:last_word = word 99 | silent! call s:GodocView() 100 | setlocal modifiable 101 | silent! %d _ 102 | silent! put! =content 103 | silent! normal gg 104 | setlocal nomodifiable 105 | setfiletype godoc 106 | return 1 107 | endfunction 108 | 109 | function! s:Godoc(...) 110 | if !len(a:000) 111 | let oldiskeyword = &iskeyword 112 | setlocal iskeyword+=. 113 | let word = expand('') 114 | let &iskeyword = oldiskeyword 115 | let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g') 116 | let words = split(word, '\.\ze[^./]\+$') 117 | else 118 | let words = a:000 119 | endif 120 | if !len(words) 121 | return 122 | endif 123 | if s:GodocWord(words[0]) 124 | if len(words) > 1 125 | if search('^\%(const\|var\|type\|\s\+\) ' . words[1] . '\s\+=\s') 126 | return 127 | endif 128 | if search('^func ' . words[1] . '(') 129 | silent! normal zt 130 | return 131 | endif 132 | echo 'No documentation found for "' . words[1] . '".' 133 | endif 134 | endif 135 | endfunction 136 | 137 | " vim:ts=4:sw=4:et 138 | --------------------------------------------------------------------------------