github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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#Package(ArgLead, CmdLine, CursorPos)
    32    let dirs = []
    33  
    34    if executable('go')
    35        let goroot = substitute(system('go env GOROOT'), '\n', '', 'g')
    36        if v:shell_error
    37            echo '\'go env GOROOT\' failed'
    38        endif
    39    else
    40        let goroot = $GOROOT
    41    endif
    42  
    43    if len(goroot) != 0 && isdirectory(goroot)
    44      let dirs += [ goroot ]
    45    endif
    46  
    47    let workspaces = split($GOPATH, ':')
    48    if workspaces != []
    49        let dirs += workspaces
    50    endif
    51  
    52    if len(dirs) == 0
    53        " should not happen
    54        return []
    55    endif
    56  
    57    let ret = {}
    58    for dir in dirs
    59      let root = expand(dir . '/pkg/' . s:goos . '_' . s:goarch)
    60      for i in split(globpath(root, a:ArgLead.'*'), "\n")
    61        if isdirectory(i)
    62          let i .= '/'
    63        elseif i !~ '\.a$'
    64          continue
    65        endif
    66        let i = substitute(substitute(i[len(root)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g')
    67        let ret[i] = i
    68      endfor
    69    endfor
    70    return sort(keys(ret))
    71  endfunction