github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/misc/vim/plugin/godoc.vim (about)

     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(<f-args>)
    35  endif
    36  
    37  nnoremap <silent> <Plug>(godoc-keyword) :<C-u>call <SID>Godoc('')<CR>
    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 <buffer> <silent> K :Godoc<cr>
    64  
    65    au BufHidden <buffer> call let <SID>buf_nr = -1
    66  endfunction
    67  
    68  function! s:GodocWord(word)
    69    if !executable('godoc')
    70      echohl WarningMsg
    71      echo "godoc command not found."
    72      echo "  install with: go get code.google.com/p/go.tools/cmd/godoc"
    73      echohl None
    74      return 0
    75    endif
    76    let word = a:word
    77    silent! let content = system('godoc ' . word)
    78    if v:shell_error || !len(content)
    79      if len(s:last_word)
    80        silent! let content = system('godoc ' . s:last_word.'/'.word)
    81        if v:shell_error || !len(content)
    82          echo 'No documentation found for "' . word . '".'
    83          return 0
    84        endif
    85        let word = s:last_word.'/'.word
    86      else
    87        echo 'No documentation found for "' . word . '".'
    88        return 0
    89      endif
    90    endif
    91    let s:last_word = word
    92    silent! call s:GodocView()
    93    setlocal modifiable
    94    silent! %d _
    95    silent! put! =content
    96    silent! normal gg
    97    setlocal nomodifiable
    98    setfiletype godoc
    99    return 1
   100  endfunction
   101  
   102  function! s:Godoc(...)
   103    if !len(a:000)
   104      let oldiskeyword = &iskeyword
   105      setlocal iskeyword+=.
   106      let word = expand('<cword>')
   107      let &iskeyword = oldiskeyword
   108      let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
   109      let words = split(word, '\.\ze[^./]\+$')
   110    else
   111      let words = a:000
   112    endif
   113    if !len(words)
   114      return
   115    endif
   116    if s:GodocWord(words[0])
   117      if len(words) > 1
   118        if search('^\%(const\|var\|type\|\s\+\) ' . words[1] . '\s\+=\s')
   119          return
   120        endif
   121        if search('^func ' . words[1] . '(')
   122          silent! normal zt
   123          return
   124        endif
   125        echo 'No documentation found for "' . words[1] . '".'
   126      endif
   127    endif
   128  endfunction
   129  
   130  " vim:ts=4:sw=4:et