github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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        echomsg '\'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 pathsep = ':'
    48    if s:goos == 'windows'
    49      let pathsep = ';'
    50    endif
    51    let workspaces = split($GOPATH, pathsep)
    52    if workspaces != []
    53      let dirs += workspaces
    54    endif
    55  
    56    if len(dirs) == 0
    57      " should not happen
    58      return []
    59    endif
    60  
    61    let ret = {}
    62    for dir in dirs
    63      " this may expand to multiple lines
    64      let root = split(expand(dir . '/pkg/' . s:goos . '_' . s:goarch), "\n")
    65      for r in root
    66        for i in split(globpath(r, a:ArgLead.'*'), "\n")
    67          if isdirectory(i)
    68            let i .= '/'
    69          elseif i !~ '\.a$'
    70            continue
    71          endif
    72          let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g')
    73          let ret[i] = i
    74        endfor
    75      endfor
    76    endfor
    77    return sort(keys(ret))
    78  endfunction