Skip to content

Commit

Permalink
plugin: add vim-gocode plugin
Browse files Browse the repository at this point in the history
  vim-gocode:9c8a27a130932c801037129016d6afa73a04156a

Signed-off-by: lilei <[email protected]>
  • Loading branch information
lilei committed Dec 2, 2019
1 parent b134fc5 commit 6604557
Show file tree
Hide file tree
Showing 18 changed files with 1,412 additions and 0 deletions.
53 changes: 53 additions & 0 deletions vim/bundle/vim-gocode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
vim-gocode
==========

All credit goes to [nsf/code](http://github.com/nsf/gocode) and Go authors.
All Vim plugins from Go 1.2 are also included.

You should *not* install this plugin with either [fsouza/go.vim](https://github.com/fsouza/go.vim) or [jnwhiteh/vim-golang](https://github.com/jnwhiteh/vim-golang)! It could take unknown effect on your setup.
## Commands


* **:RelPkg** takes no or one argument, a relative package path, and prints it as a full package path. If no argument is given, default to current package.

*Example* `:RelPkg ../pkg/child` in the `$GOPATH/src/github.com/Blackrush/gofus/main.go` file will print `github.com/Blackrush/pkg/child`

* **:GoInstall** takes no or one argument, a relative package path, installs it or prints compilation errors otherwise. If no argument is given, default to current package.

*Example* `:GoInstall ../pkg/child` with current working directory `$GOPATH/src/github.com/Blackrush/gofus`
will try to install the `github.com/Blackrush/pkg/child` package

* **:GoTest** takes no or one argument, a relative package path, tests it and prints its output. If no argument is given, default to current package.

*Example* `:GoTest ../pkg/child` with current working directory `$GOPATH/src/github.com/Blackrush/gofus`
will try to test the `github.com/Blackrush/pkg/child` package

* **:GoImport**, **:GoImportAs** and **:GoDrop** are equivalent of original **:Import**, **:ImportAs** and **:Drop**
but takes all a relative package path to the current working directory

* **:make** — you can use QuickFix to iterate through build errors if any; if file is in subdirectory of $GOPATH/src/, the whole package is build, else — only current file

See [#1](https://github.com/Blackrush/vim-gocode/issues/1) to see future commands implementation.

## Installation

Make sure you have installed [gocode](https://github.com/nsf/gocode) before installing this plugin :

```bash
go get github.com/nsf/gocode
```

### Vundle

Add this line to your ~/.vimrc configuration file :

Bundle 'Blackrush/vim-gocode'

And then run vim :

vim +BundleInstall

### Pathogen

cd ~/.vim/bundle
git clone https://github.com/Blackrush/vim-gocode.git
7 changes: 7 additions & 0 deletions vim/bundle/vim-gocode/addon-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vim-gocode",
"repository": {
"type": "git",
"url": "https://github.com/Blackrush/vim-gocode.git"
}
}
89 changes: 89 additions & 0 deletions vim/bundle/vim-gocode/autoload/go/complete.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
if exists('g:loaded_gocode')
finish
endif
let g:loaded_gocode = 1

fu! s:gocodeCurrentBuffer()
let buf = getline(1, '$')
if &encoding != 'utf-8'
let buf = map(buf, 'iconv(v:val, &encoding, "utf-8")')
endif
if &l:fileformat == 'dos'
" XXX: line2byte() depend on 'fileformat' option.
" so if fileformat is 'dos', 'buf' must include '\r'.
let buf = map(buf, 'v:val."\r"')
endif
let file = tempname()
call writefile(buf, file)
return file
endf

let s:vim_system = get(g:, 'gocomplete#system_function', 'system')

fu! s:system(str, ...)
return call(s:vim_system, [a:str] + a:000)
endf

fu! s:gocodeShellescape(arg)
try
let ssl_save = &shellslash
set noshellslash
return shellescape(a:arg)
finally
let &shellslash = ssl_save
endtry
endf

fu! s:gocodeCommand(cmd, preargs, args)
for i in range(0, len(a:args) - 1)
let a:args[i] = s:gocodeShellescape(a:args[i])
endfor
for i in range(0, len(a:preargs) - 1)
let a:preargs[i] = s:gocodeShellescape(a:preargs[i])
endfor
let result = s:system(printf('gocode %s %s %s', join(a:preargs), a:cmd, join(a:args)))
if v:shell_error != 0
return "[\"0\", []]"
else
if &encoding != 'utf-8'
let result = iconv(result, 'utf-8', &encoding)
endif
return result
endif
endf

fu! s:gocodeCurrentBufferOpt(filename)
return '-in=' . a:filename
endf

fu! s:gocodeCursor()
if &encoding != 'utf-8'
let c = col('.')
let buf = line('.') == 1 ? "" : (join(getline(1, line('.')-1), "\n") . "\n")
let buf .= c == 1 ? "" : getline('.')[:c-2]
return printf('%d', len(iconv(buf, &encoding, "utf-8")))
endif
return printf('%d', line2byte(line('.')) + (col('.')-2))
endf

fu! s:gocodeAutocomplete()
let filename = s:gocodeCurrentBuffer()
let result = s:gocodeCommand('autocomplete',
\ [s:gocodeCurrentBufferOpt(filename), '-f=vim'],
\ [expand('%:p'), s:gocodeCursor()])
call delete(filename)
return result
endf

fu! go#complete#Complete(findstart, base)
"findstart = 1 when we need to get the text length
if a:findstart == 1
execute "silent let g:gocomplete_completions = " . s:gocodeAutocomplete()
return col('.') - g:gocomplete_completions[0] - 1
"findstart = 0 when we need to return the list of completions
else
return g:gocomplete_completions[1]
endif
endf

" vim:sw=4:et
145 changes: 145 additions & 0 deletions vim/bundle/vim-gocode/autoload/go/package.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
" 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.
"
" This file provides a utility function that performs auto-completion of
" package names, for use by other commands.

let s:goos = $GOOS
let s:goarch = $GOARCH

if len(s:goos) == 0
if exists('g:golang_goos')
let s:goos = g:golang_goos
elseif has('win32') || has('win64')
let s:goos = 'windows'
elseif has('macunix')
let s:goos = 'darwin'
else
let s:goos = '*'
endif
endif

if len(s:goarch) == 0
if exists('g:golang_goarch')
let s:goarch = g:golang_goarch
else
let s:goarch = '*'
endif
endif

function! go#package#Paths()
let dirs = []

if exists('$GOROOT')
let goroot = $GOROOT
else
if executable('go')
let goroot = substitute(system('go env GOROOT'), '\n', '', 'g')
if v:shell_error
echomsg '''go env GOROOT'' failed'
endif
endif
endif

if len(goroot) != 0 && isdirectory(goroot)
let dirs += [goroot]
endif

let pathsep = ':'
if s:goos == 'windows'
let pathsep = ';'
endif
let workspaces = split($GOPATH, pathsep)
if workspaces != []
let dirs += workspaces
endif

return dirs
endfunction

function! go#package#FromPath(arg)
let path = fnamemodify(resolve(a:arg), ':p:h')
let dirs = go#package#Paths()

if s:goos == 'windows'
let path = substitute(tolower(path), "\\", "/", 'g')
for dir in dirs
if len(dir)
let dir = substitute(tolower(dir), "\\", "/", 'g')
if stridx(path, dir) == 0
let workspace = dir
endif
endif
endfor
else
for dir in dirs
if len(dir) && stridx(path, dir) == 0
let workspace = dir
endif
endfor
endif

if !exists('workspace')
return -1
endif

let workspace = substitute(workspace, '/$', '', '')
return substitute(path, workspace . '/src/', '', '')
endfunction

function! go#package#CompleteMembers(package, member)
silent! let content = system('godoc ' . a:package)
if v:shell_error || !len(content)
return []
endif
let lines = filter(split(content, "\n"),"v:val !~ '^\\s\\+$'")
try
let mx1 = '^\s\+\(\S+\)\s\+=\s\+.*'
let mx2 = '^\%(const\|var\|type\|func\) \([A-Z][^ (]\+\).*'
let candidates = map(filter(copy(lines), 'v:val =~ mx1'),
\ 'substitute(v:val, mx1, "\\1", "")')
\ + map(filter(copy(lines), 'v:val =~ mx2'),
\ 'substitute(v:val, mx2, "\\1", "")')
return filter(candidates, '!stridx(v:val, a:member)')
catch
return []
endtry
endfunction

function! go#package#Complete(ArgLead, CmdLine, CursorPos)
let words = split(a:CmdLine, '\s\+', 1)
if len(words) > 2
" Complete package members
return go#package#CompleteMembers(words[1], words[2])
endif

let dirs = go#package#Paths()

if len(dirs) == 0
" should not happen
return []
endif

let ret = {}
for dir in dirs
" this may expand to multiple lines
let root = split(expand(dir . '/pkg/' . s:goos . '_' . s:goarch), "\n")
call add(root, expand(dir . '/src'))
for r in root
for i in split(globpath(r, a:ArgLead.'*'), "\n")
if isdirectory(i)
let i .= '/'
elseif i !~ '\.a$'
continue
endif
let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'),
\ '\.a$', '', 'g')
let ret[i] = i
endfor
endfor
endfor
return sort(keys(ret))
endfunction

" vim:sw=4:et
35 changes: 35 additions & 0 deletions vim/bundle/vim-gocode/compiler/go.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
" Copyright 2013 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.
"
" compiler/go.vim: Vim compiler file for Go.

if exists('current_compiler')
finish
endif
let current_compiler = 'go'

if exists(':CompilerSet') != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif

if exists('b:gopackage')
execute 'CompilerSet makeprg=go\ build\ $*\ ' . escape(b:gopackage, ' \')
else
CompilerSet makeprg=go\ build\ $*\ %
endif

let s:save_cpo = &cpo
set cpo-=C

CompilerSet errorformat=
\%-G#\ %.%#,
\%E%f:%l:%c:\ %m,
\%E%f:%l:\ %m,
\%C%*\\s%m,
\%-G%.%#

let &cpo = s:save_cpo
unlet s:save_cpo

" vim:ts=4:sw=4:et
65 changes: 65 additions & 0 deletions vim/bundle/vim-gocode/doc/vim-gocode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
*vim-gocode.txt* Tools for Golang

Compiler plugin, syntax highlight, omni completion (with gocode)
and other stuff for Go programming language

==============================================================================
CONTENTS *vim-gocode-contents*

1. Usage...........................|vim-gocode-usage|

==============================================================================
1. Usage *vim-gocode-usage*

*:Import*
:Import <package> Add <package> to import section of file.

*:ImportAs*
:ImportAs <alias> <package> Add <package> to import section of file with
name <alias>.

*:Drop*
:Drop <package> Remove <package> from import section of file.

*:Fmt*
:Fmt Format file with gofmt.

*:Godoc*
:Godoc [<words>] Open Godoc for <words>. If no arguments given
open Godoc for word under the cursor.

*:RelPkg*
:RelPkg [<rel-path>] Print full package path of <rel-path>. If no
argument is given, default to current package.


*:GoInstall*
:GoInstall [<rel-path>] Install the package at <rel-path> or current
package if no argument is given.

*:GoTest*
:GoTest [<rel-path>] Test the package at <rel-path> or current
package if no argument is given.


*:GoTestVerbose*
:GoTestVerbse [<rel-path>] Verbosely test the pacakge at <re-path> or
current package if no argument is given.


*:GoImport*
:GoImport <rel-path> Same as |:Import| but take a relative package
path to the current working directory.

*:GoImportAs*
:GoImportAs <rel-path> Same as |:ImportAs| but take a relative
package path to the current working directory.


*:GoDrop*
:GoDrop <rel-path> Same as |:Drop| but take a relative package
path to the current working directory.

==============================================================================

vim:tw=78:ts=8:ft=help:norl:
Loading

0 comments on commit 6604557

Please sign in to comment.