github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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  if exists("g:loaded_godoc")
     8    finish
     9  endif
    10  let g:loaded_godoc = 1
    11  
    12  let s:buf_nr = -1
    13  let s:last_word = ''
    14  
    15  function! s:GodocView()
    16    if !bufexists(s:buf_nr)
    17      leftabove new
    18      file `="[Godoc]"`
    19      let s:buf_nr = bufnr('%')
    20    elseif bufwinnr(s:buf_nr) == -1
    21      leftabove split
    22      execute s:buf_nr . 'buffer'
    23      delete _
    24    elseif bufwinnr(s:buf_nr) != bufwinnr('%')
    25      execute bufwinnr(s:buf_nr) . 'wincmd w'
    26    endif
    27  
    28    setlocal filetype=godoc
    29    setlocal bufhidden=delete
    30    setlocal buftype=nofile
    31    setlocal noswapfile
    32    setlocal nobuflisted
    33    setlocal modifiable
    34    setlocal nocursorline
    35    setlocal nocursorcolumn
    36    setlocal iskeyword+=:
    37    setlocal iskeyword-=-
    38  
    39    nnoremap <buffer> <silent> K :Godoc<cr>
    40  
    41    au BufHidden <buffer> call let <SID>buf_nr = -1
    42  endfunction
    43  
    44  function! s:GodocWord(word)
    45    let word = a:word
    46    silent! let content = system('godoc ' . word)
    47    if v:shell_error || !len(content)
    48      if len(s:last_word)
    49        silent! let content = system('godoc ' . s:last_word.'/'.word)
    50        if v:shell_error || !len(content)
    51          echo 'No documentation found for "' . word . '".'
    52          return
    53        endif
    54        let word = s:last_word.'/'.word
    55      else
    56        echo 'No documentation found for "' . word . '".'
    57        return
    58      endif
    59    endif
    60    let s:last_word = word
    61    silent! call s:GodocView()
    62    setlocal modifiable
    63    silent! %d _
    64    silent! put! =content
    65    silent! normal gg
    66    setlocal nomodifiable
    67    setfiletype godoc
    68  endfunction
    69  
    70  function! s:Godoc(...)
    71    let word = join(a:000, ' ')
    72    if !len(word)
    73      let oldiskeyword = &iskeyword
    74      setlocal iskeyword+=.
    75      let word = expand('<cword>')
    76      let &iskeyword = oldiskeyword
    77    endif
    78    let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
    79    let words = split(word, '\.')
    80    if !len(words)
    81      return
    82    endif
    83    call s:GodocWord(words[0])
    84    if len(words) > 1
    85      if search('^\%(const\|var\|type\|\s\+\) ' . words[1] . '\s\+=\s')
    86        return
    87      endif
    88      if search('^func ' . words[1] . '(')
    89        return
    90      endif
    91      echo 'No documentation found for "' . word . '".'
    92    endif
    93  endfunction
    94  
    95  command! -nargs=* -range -complete=customlist,go#complete#Package Godoc :call s:Godoc(<q-args>)
    96  nnoremap <silent> <Plug>(godoc-keyword) :<C-u>call <SID>Godoc('')<CR>
    97  
    98  " vim:ts=4:sw=4:et