github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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(<q-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    let word = a:word
    70    silent! let content = system('godoc ' . word)
    71    if v:shell_error || !len(content)
    72      if len(s:last_word)
    73        silent! let content = system('godoc ' . s:last_word.'/'.word)
    74        if v:shell_error || !len(content)
    75          echo 'No documentation found for "' . word . '".'
    76          return
    77        endif
    78        let word = s:last_word.'/'.word
    79      else
    80        echo 'No documentation found for "' . word . '".'
    81        return
    82      endif
    83    endif
    84    let s:last_word = word
    85    silent! call s:GodocView()
    86    setlocal modifiable
    87    silent! %d _
    88    silent! put! =content
    89    silent! normal gg
    90    setlocal nomodifiable
    91    setfiletype godoc
    92  endfunction
    93  
    94  function! s:Godoc(...)
    95    let word = join(a:000, ' ')
    96    if !len(word)
    97      let oldiskeyword = &iskeyword
    98      setlocal iskeyword+=.
    99      let word = expand('<cword>')
   100      let &iskeyword = oldiskeyword
   101    endif
   102    let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
   103    let words = split(word, '\.')
   104    if !len(words)
   105      return
   106    endif
   107    call s:GodocWord(words[0])
   108    if len(words) > 1
   109      if search('^\%(const\|var\|type\|\s\+\) ' . words[1] . '\s\+=\s')
   110        return
   111      endif
   112      if search('^func ' . words[1] . '(')
   113        return
   114      endif
   115      echo 'No documentation found for "' . word . '".'
   116    endif
   117  endfunction
   118  
   119  " vim:ts=4:sw=4:et