github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/misc/vim/autoload/go/complete.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  " 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    if executable('go')
    59      let goroot = substitute(system('go env GOROOT'), '\n', '', 'g')
    60      if v:shell_error
    61        echomsg '\'go env GOROOT\' failed'
    62      endif
    63    else
    64      let goroot = $GOROOT
    65    endif
    66  
    67    if len(goroot) != 0 && isdirectory(goroot)
    68      let dirs += [goroot]
    69    endif
    70  
    71    let pathsep = ':'
    72    if s:goos == 'windows'
    73      let pathsep = ';'
    74    endif
    75    let workspaces = split($GOPATH, pathsep)
    76    if workspaces != []
    77      let dirs += workspaces
    78    endif
    79  
    80    if len(dirs) == 0
    81      " should not happen
    82      return []
    83    endif
    84  
    85    let ret = {}
    86    for dir in dirs
    87      " this may expand to multiple lines
    88      let root = split(expand(dir . '/pkg/' . s:goos . '_' . s:goarch), "\n")
    89      call add(root, expand(dir . '/src'))
    90      for r in root
    91        for i in split(globpath(r, a:ArgLead.'*'), "\n")
    92          if isdirectory(i)
    93            let i .= '/'
    94          elseif i !~ '\.a$'
    95            continue
    96          endif
    97          let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g')
    98          let ret[i] = i
    99        endfor
   100      endfor
   101    endfor
   102    return sort(keys(ret))
   103  endfunction