diff options
Diffstat (limited to 'misc/vim')
| -rw-r--r-- | misc/vim/autoload/go/complete.vim | 48 | ||||
| -rw-r--r-- | misc/vim/ftplugin/go/fmt.vim | 5 | ||||
| -rw-r--r-- | misc/vim/ftplugin/go/godoc.vim | 13 | ||||
| -rw-r--r-- | misc/vim/ftplugin/go/import.vim | 38 | ||||
| -rwxr-xr-x | misc/vim/ftplugin/go/test.sh | 78 | ||||
| -rw-r--r-- | misc/vim/plugin/godoc.vim | 2 | ||||
| -rw-r--r-- | misc/vim/readme.txt | 9 | 
7 files changed, 160 insertions, 33 deletions
| diff --git a/misc/vim/autoload/go/complete.vim b/misc/vim/autoload/go/complete.vim index d4ae3b97f..cc1013b7d 100644 --- a/misc/vim/autoload/go/complete.vim +++ b/misc/vim/autoload/go/complete.vim @@ -29,21 +29,43 @@ if len(s:goarch) == 0  endif  function! go#complete#Package(ArgLead, CmdLine, CursorPos) -  let goroot = $GOROOT -  if len(goroot) == 0 -    " should not occur. -    return [] +  let dirs = [] + +  if executable('go') +      let goroot = substitute(system('go env GOROOT'), '\n', '', 'g') +      if v:shell_error +          echo '\'go env GOROOT\' failed' +      endif +  else +      let goroot = $GOROOT    endif + +  if len(goroot) != 0 && isdirectory(goroot) +    let dirs += [ goroot ] +  endif + +  let workspaces = split($GOPATH, ':') +  if workspaces != [] +      let dirs += workspaces +  endif + +  if len(dirs) == 0 +      " should not happen +      return [] +  endif +    let ret = {} -  let root = expand(goroot.'/pkg/'.s:goos.'_'.s:goarch) -  for i in split(globpath(root, a:ArgLead.'*'), "\n") -    if isdirectory(i) -      let i .= '/' -    elseif i !~ '\.a$' -      continue -    endif -    let i = substitute(substitute(i[len(root)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g') -    let ret[i] = i +  for dir in dirs +    let root = expand(dir . '/pkg/' . s:goos . '_' . s:goarch) +    for i in split(globpath(root, a:ArgLead.'*'), "\n") +      if isdirectory(i) +        let i .= '/' +      elseif i !~ '\.a$' +        continue +      endif +      let i = substitute(substitute(i[len(root)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g') +      let ret[i] = i +    endfor    endfor    return sort(keys(ret))  endfunction diff --git a/misc/vim/ftplugin/go/fmt.vim b/misc/vim/ftplugin/go/fmt.vim index 0ee44cd59..30814fdfd 100644 --- a/misc/vim/ftplugin/go/fmt.vim +++ b/misc/vim/ftplugin/go/fmt.vim @@ -12,6 +12,9 @@  "       It tries to preserve cursor position and avoids  "       replacing the buffer with stderr output.  " +if exists("b:did_ftplugin_go_fmt") +    finish +endif  command! -buffer Fmt call s:GoFormat() @@ -41,4 +44,6 @@ function! s:GoFormat()      call winrestview(view)  endfunction +let b:did_ftplugin_go_fmt = 1 +  " vim:ts=4:sw=4:et diff --git a/misc/vim/ftplugin/go/godoc.vim b/misc/vim/ftplugin/go/godoc.vim deleted file mode 100644 index 55195a674..000000000 --- a/misc/vim/ftplugin/go/godoc.vim +++ /dev/null @@ -1,13 +0,0 @@ -" Copyright 2011 The Go Authors. All rights reserved. -" Use of this source code is governed by a BSD-style -" license that can be found in the LICENSE file. -" -" godoc.vim: Vim command to see godoc. - -if exists("b:did_ftplugin") -    finish -endif - -silent! nmap <buffer> <silent> K <Plug>(godoc-keyword) - -" vim:ts=4:sw=4:et diff --git a/misc/vim/ftplugin/go/import.vim b/misc/vim/ftplugin/go/import.vim index 6705a476b..1d969e460 100644 --- a/misc/vim/ftplugin/go/import.vim +++ b/misc/vim/ftplugin/go/import.vim @@ -12,7 +12,7 @@  "       in the current Go buffer, using proper style and ordering.  "       If {path} is already being imported, an error will be  "       displayed and the buffer will be untouched. -"  +"  "   :ImportAs {localname} {path}  "  "       Same as Import, but uses a custom local name for the package. @@ -32,7 +32,7 @@  " The backslash is the default maplocalleader, so it is possible that  " your vim is set to use a different character (:help maplocalleader).  " -if exists("b:did_ftplugin") +if exists("b:did_ftplugin_go_import")      finish  endif @@ -58,6 +58,12 @@ function! s:SwitchImport(enabled, localname, path)          return      endif +    " Extract any site prefix (e.g. github.com/). +    " If other imports with the same prefix are grouped separately, +    " we will add this new import with them. +    " Only up to and including the first slash is used. +    let siteprefix = matchstr(path, "^[^/]*/") +      let qpath = '"' . path . '"'      if a:localname != ''          let qlocalpath = a:localname . ' ' . qpath @@ -83,16 +89,31 @@ function! s:SwitchImport(enabled, localname, path)              let appendstr = qlocalpath              let indentstr = 1              let appendline = line +            let firstblank = -1 +            let lastprefix = ""              while line <= line("$")                  let line = line + 1                  let linestr = getline(line)                  let m = matchlist(getline(line), '^\()\|\(\s\+\)\(\S*\s*\)"\(.\+\)"\)')                  if empty(m) +                    if siteprefix == "" && a:enabled +                        " must be in the first group +                        break +                    endif +                    " record this position, but keep looking +                    if firstblank < 0 +                        let firstblank = line +                    endif                      continue                  endif                  if m[1] == ')' +                    " if there's no match, add it to the first group +                    if appendline < 0 && firstblank >= 0 +                        let appendline = firstblank +                    endif                      break                  endif +                let lastprefix = matchstr(m[4], "^[^/]*/")                  if a:localname != '' && m[3] != ''                      let qlocalpath = printf('%-' . (len(m[3])-1) . 's %s', a:localname, qpath)                  endif @@ -103,7 +124,16 @@ function! s:SwitchImport(enabled, localname, path)                      let deleteline = line                      break                  elseif m[4] < path -                    let appendline = line +                    " don't set candidate position if we have a site prefix, +                    " we've passed a blank line, and this doesn't share the same +                    " site prefix. +                    if siteprefix == "" || firstblank < 0 || match(m[4], "^" . siteprefix) >= 0 +                        let appendline = line +                    endif +                elseif siteprefix != "" && match(m[4], "^" . siteprefix) >= 0 +                    " first entry of site group +                    let appendline = line - 1 +                    break                  endif              endwhile              break @@ -198,4 +228,6 @@ function! s:Error(s)      echohl Error | echo a:s | echohl None  endfunction +let b:did_ftplugin_go_import = 1 +  " vim:ts=4:sw=4:et diff --git a/misc/vim/ftplugin/go/test.sh b/misc/vim/ftplugin/go/test.sh new file mode 100755 index 000000000..a6e31d8a3 --- /dev/null +++ b/misc/vim/ftplugin/go/test.sh @@ -0,0 +1,78 @@ +#!/bin/bash -e +# +# Copyright 2012 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. +# +# Tests for import.vim. + +cd $(dirname $0) + +cat > base.go <<EOF +package test + +import ( +	"bytes" +	"io" +	"net" + +	"mycorp/foo" +) +EOF + +fail=0 + +# usage: test_one command pattern +# Pattern is a PCRE expression that will match across lines. +test_one() { +  echo 2>&1 -n "$1: " +  vim -e -s -u /dev/null -U /dev/null --noplugin -c "source import.vim" \ +    -c "$1" -c 'wq! test.go' base.go +  # ensure blank lines are treated correctly +  if ! gofmt test.go | cmp test.go; then +    echo 2>&1 "gofmt conflict" +    gofmt test.go | diff -u test.go - | sed "s/^/	/" 2>&1 +    fail=1 +    return +  fi +  if ! grep -P -q "(?s)$2" test.go; then +    echo 2>&1 "$2 did not match" +    cat test.go | sed "s/^/	/" 2>&1 +    fail=1 +    return +  fi +  echo 2>&1 "ok" +} + +# Tests for Import + +test_one "Import baz" '"baz".*"bytes"' +test_one "Import io/ioutil" '"io".*"io/ioutil".*"net"' +test_one "Import myc" '"io".*"myc".*"net"'  # prefix of a site prefix +test_one "Import nat" '"io".*"nat".*"net"' +test_one "Import net/http" '"net".*"net/http".*"mycorp/foo"' +test_one "Import zoo" '"net".*"zoo".*"mycorp/foo"' +test_one "Import mycorp/bar" '"net".*"mycorp/bar".*"mycorp/foo"' +test_one "Import mycorp/goo" '"net".*"mycorp/foo".*"mycorp/goo"' + +# Tests for Drop + +cat > base.go <<EOF +package test + +import ( +	"foo" + +	"something" +	"zoo" +) +EOF + +test_one "Drop something" '\([^"]*"foo"[^"]*"zoo"[^"]*\)' + +rm -f base.go test.go +if [ $fail -gt 0 ]; then +  echo 2>&1 "FAIL" +  exit 1 +fi +echo 2>&1 "PASS" diff --git a/misc/vim/plugin/godoc.vim b/misc/vim/plugin/godoc.vim index fdb496631..a9abb7ae6 100644 --- a/misc/vim/plugin/godoc.vim +++ b/misc/vim/plugin/godoc.vim @@ -72,7 +72,7 @@ function! s:Godoc(...)    if !len(word)      let word = expand('<cword>')    endif -  let word = substitute(word, '[^a-zA-Z0-9\/]', '', 'g') +  let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')    if !len(word)      return    endif diff --git a/misc/vim/readme.txt b/misc/vim/readme.txt index fe15da993..cb3a52073 100644 --- a/misc/vim/readme.txt +++ b/misc/vim/readme.txt @@ -1,9 +1,13 @@  Vim plugins for Go (http://golang.org)  ====================================== -To use all the Vim plugins, add these lines to your vimrc. +To use all the Vim plugins, add these lines to your $HOME/.vimrc. -  set rtp+=$GOROOT/misc/vim +  " Some Linux distributions set filetype in /etc/vimrc. +  " Clear filetype flags before changing runtimepath to force Vim to reload them. +  filetype off +  filetype plugin indent off +  set runtimepath+=$GOROOT/misc/vim    filetype plugin indent on    syntax on @@ -72,5 +76,4 @@ To install godoc plugin:    1. Same as 1 above.    2. Copy or link plugin/godoc.vim to $HOME/.vim/plugin/godoc,       syntax/godoc.vim to $HOME/.vim/syntax/godoc.vim, -     ftplugin/go/godoc.vim to $HOME/.vim/ftplugin/go/godoc.vim.       and autoload/go/complete.vim to $HOME/.vim/autoload/go/complete.vim. | 
