Skip to content

complete: drop support for gocode #2686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 1 addition & 244 deletions autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,253 +2,10 @@
let s:cpo_save = &cpo
set cpo&vim

function! s:gocodeCommand(cmd, args) abort
let l:gocode_bin = "gocode"
let l:gomod = go#util#gomod()
if filereadable(l:gomod)
let l:gocode_bin = "gocode-gomod"
endif

let bin_path = go#path#CheckBinPath(l:gocode_bin)
if empty(bin_path)
return []
endif

let socket_type = go#config#GocodeSocketType()

let cmd = [bin_path]
let cmd = extend(cmd, ['-sock', socket_type])
let cmd = extend(cmd, ['-f', 'vim'])

if go#config#GocodeProposeBuiltins()
let cmd = extend(cmd, ['-builtin'])
endif

if go#config#GocodeProposeSource()
let cmd = extend(cmd, ['-source'])
else
let cmd = extend(cmd, ['-fallback-to-source', '-cache'])
endif

if go#config#GocodeUnimportedPackages()
let cmd = extend(cmd, ['-unimported-packages'])
endif

let cmd = extend(cmd, [a:cmd])
let cmd = extend(cmd, a:args)

return cmd
endfunction

function! s:sync_gocode(cmd, args, input) abort
" We might hit cache problems, as gocode doesn't handle different GOPATHs
" well. See: https://github.com/nsf/gocode/issues/239
let old_goroot = $GOROOT
let $GOROOT = go#util#env("goroot")

try
let cmd = s:gocodeCommand(a:cmd, a:args)
" gocode can sometimes be slow, so redraw now to avoid waiting for gocode
" to return before redrawing automatically.
redraw

let [l:result, l:err] = go#util#Exec(cmd, a:input)
finally
let $GOROOT = old_goroot
endtry

if l:err != 0
return "[0, []]"
endif

if &encoding != 'utf-8'
let l:result = iconv(l:result, 'utf-8', &encoding)
endif

return l:result
endfunction

function! s:gocodeAutocomplete() abort
" use the offset as is, because the cursor position is the position for
" which autocomplete candidates are needed.
return s:sync_gocode('autocomplete',
\ [expand('%:p'), go#util#OffsetCursor()],
\ go#util#GetLines())
endfunction

" go#complete#GoInfo returns the description of the identifier under the
" cursor.
function! go#complete#GetInfo() abort
let l:mode = go#config#InfoMode()
if l:mode == 'gopls' && go#util#has_job()
return go#lsp#GetInfo()
else
return s:sync_info(0)
endif
endfunction

function! go#complete#Info(showstatus) abort
if go#util#has_job(1)
return s:async_info(1, a:showstatus)
else
return s:sync_info(1)
endif
endfunction

function! s:async_info(echo, showstatus)
let state = {'echo': a:echo}

" explicitly bind complete to state so that within it, self will
" always refer to state. See :help Partial for more information.
let state.complete = function('s:complete', [], state)

" add 1 to the offset, so that the position at the cursor will be included
" in gocode's search
let offset = go#util#OffsetCursor()+1

" We might hit cache problems, as gocode doesn't handle different GOPATHs
" well. See: https://github.com/nsf/gocode/issues/239
let env = {
\ "GOROOT": go#util#env("goroot")
\ }

let opts = {
\ 'bang': 1,
\ 'complete': state.complete,
\ 'for': '_',
\ }

if a:showstatus
let opts.statustype = 'gocode'
endif

let opts = go#job#Options(l:opts)

let cmd = s:gocodeCommand('autocomplete',
\ [expand('%:p'), offset])

" TODO(bc): Don't write the buffer to a file; pass the buffer directly to
" gocode's stdin. It shouldn't be necessary to use {in_io: 'file', in_name:
" s:gocodeFile()}, but unfortunately {in_io: 'buffer', in_buf: bufnr('%')}
" doesn't work.
call extend(opts, {
\ 'env': env,
\ 'in_io': 'file',
\ 'in_name': s:gocodeFile(),
\ })

call go#job#Start(cmd, opts)
endfunction

function! s:complete(job, exit_status, messages) abort dict
if a:exit_status != 0
return
endif

if &encoding != 'utf-8'
let i = 0
while i < len(a:messages)
let a:messages[i] = iconv(a:messages[i], 'utf-8', &encoding)
let i += 1
endwhile
endif

let result = s:info_filter(self.echo, join(a:messages, "\n"))
call s:info_complete(self.echo, result)
endfunction

function! s:gocodeFile()
let file = tempname()
call writefile(go#util#GetLines(), file)
return file
endfunction

function! s:sync_info(echo)
" add 1 to the offset, so that the position at the cursor will be included
" in gocode's search
let offset = go#util#OffsetCursor()+1

let result = s:sync_gocode('autocomplete',
\ [expand('%:p'), offset],
\ go#util#GetLines())

let result = s:info_filter(a:echo, result)
return s:info_complete(a:echo, result)
endfunction

function! s:info_filter(echo, result) abort
if empty(a:result)
return ""
endif

let l:result = eval(a:result)
if len(l:result) != 2
return ""
endif

let l:candidates = l:result[1]
if len(l:candidates) == 1
" When gocode panics in vim mode, it returns
" [0, [{'word': 'PANIC', 'abbr': 'PANIC PANIC PANIC', 'info': 'PANIC PANIC PANIC'}]]
if a:echo && l:candidates[0].info ==# "PANIC PANIC PANIC"
return ""
endif

return l:candidates[0].info
endif

let filtered = []
let wordMatch = '\<' . expand("<cword>") . '\>'
" escape single quotes in wordMatch before passing it to filter
let wordMatch = substitute(wordMatch, "'", "''", "g")
let filtered = filter(l:candidates, "v:val.info =~ '".wordMatch."'")

if len(l:filtered) == 0
return "no matches"
elseif len(l:filtered) > 1
return "ambiguous match"
endif

return l:filtered[0].info
endfunction

function! s:info_complete(echo, result) abort
if a:echo
call go#util#ShowInfo(a:result)
endif

return a:result
endfunction

function! s:trim_bracket(val) abort
let a:val.word = substitute(a:val.word, '[(){}\[\]]\+$', '', '')
return a:val
endfunction

let s:completions = []

function! go#complete#GocodeComplete(findstart, base) abort
"findstart = 1 when we need to get the text length
if a:findstart == 1
let l:completions = []
execute "silent let l:completions = " . s:gocodeAutocomplete()

if len(l:completions) == 0 || len(l:completions) >= 2 && len(l:completions[1]) == 0
" no matches. cancel and leave completion mode.
call go#util#EchoInfo("no matches")
return -3
endif

let s:completions = l:completions[1]
return col('.') - l:completions[0] - 1
"findstart = 0 when we need to return the list of completions
else
let s = getline(".")[col('.') - 1]
if s =~ '[(){}\{\}]'
return map(copy(s:completions), 's:trim_bracket(v:val)')
endif
return s:completions
endif
return go#lsp#GetInfo()
endfunction

function! go#complete#Complete(findstart, base) abort
Expand Down
17 changes: 0 additions & 17 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,6 @@ function! go#config#SetGuruScope(scope) abort
endif
endfunction

function! go#config#GocodeUnimportedPackages() abort
return get(g:, 'go_gocode_unimported_packages', 0)
endfunction

let s:sock_type = (has('win32') || has('win64')) ? 'tcp' : 'unix'
function! go#config#GocodeSocketType() abort
return get(g:, 'go_gocode_socket_type', s:sock_type)
endfunction

function! go#config#GocodeProposeBuiltins() abort
return get(g:, 'go_gocode_propose_builtins', 1)
endfunction

function! go#config#GocodeProposeSource() abort
return get(g:, 'go_gocode_propose_source', 0)
endfunction

function! go#config#EchoCommandInfo() abort
return get(g:, 'go_echo_command_info', 1)
endfunction
Expand Down
4 changes: 1 addition & 3 deletions autoload/go/tool.vim
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ endfunction

function! go#tool#Info(showstatus) abort
let l:mode = go#config#InfoMode()
if l:mode == 'gocode'
call go#complete#Info(a:showstatus)
elseif l:mode == 'guru'
if l:mode == 'guru'
call go#guru#DescribeInfo(a:showstatus)
elseif l:mode == 'gopls'
if !go#config#GoplsEnabled()
Expand Down
43 changes: 4 additions & 39 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ CTRL-t
:GoInstallBinaries [binaries]

Download and install all necessary Go tool binaries such as `godef`,
`goimports`, `gocode`, etc. under |'g:go_bin_path'|. If [binaries] is
`goimports`, `gopls`, etc. under |'g:go_bin_path'|. If [binaries] is
supplied, then only the specified binaries will be installed. The default
is to install everything.

Expand All @@ -512,7 +512,7 @@ CTRL-t
:GoUpdateBinaries [binaries]

Download and update previously installed Go tool binaries such as `godef`,
`goimports`, `gocode`, etc. under |'g:go_bin_path'|. If [binaries] is
`goimports`, `gopls`, etc. under |'g:go_bin_path'|. If [binaries] is
supplied, then only the specified binaries will be updated. The default is
to update everything.

Expand Down Expand Up @@ -1209,11 +1209,6 @@ into the statusline.

Uses `gopls` for autocompletion. By default, it is hooked up to 'omnifunc'.


*go#complete#GocodeComplete()*

Uses `gocode` for autocompletion.

*go#tool#DescribeBalloon()*

Suitable to be used as an expression to show the evaluation balloon. See `help
Expand Down Expand Up @@ -1286,8 +1281,8 @@ updated. By default it's disabled. The delay can be configured with the

Use this option to define the command to be used for |:GoInfo|. By default
`gopls` is used, because it is the fastest and is known to be highly accurate.
One might also use `guru` for its accuracy or `gocode` for its performance.
Valid options are `gocode`, `gopls`, and `guru`.
One might also use `guru` for its accuracy.
Valid options are `gopls` and `guru`.
>
let g:go_info_mode = 'gopls'
<
Expand Down Expand Up @@ -1709,36 +1704,6 @@ same.
\ '? go#util#pascalcase(expand("<cword>"))' .
\ ': go#util#camelcase(expand("<cword>"))'
<
*'g:go_gocode_propose_builtins'*

Specifies whether `gocode` should add built-in types, functions and constants
to code completion proposals. By default it is enabled.
>
let g:go_gocode_propose_builtins = 1
<
*'g:go_gocode_propose_source'*

Specifies whether `gocode` should use source files instead of binary packages
for code completion proposals. When disabled, only identifiers from the
current package and packages that have been installed will proposed.
>
let g:go_gocode_propose_source = 0
<
*'g:go_gocode_unimported_packages'*

Specifies whether `gocode` should include suggestions from unimported
packages. By default it is disabled.
>
let g:go_gocode_unimported_packages = 0
<

*'g:go_gocode_socket_type'*

Specifies whether `gocode` should use a different socket type. By default
`unix` is enabled. Possible values: `unix`, `tcp`
>
let g:go_gocode_socket_type = 'unix'
<

*'g:go_gopls_enabled'*

Expand Down
3 changes: 0 additions & 3 deletions ftplugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ compiler go
if go#config#CodeCompletionEnabled()
" Set autocompletion
setlocal omnifunc=go#complete#Complete
if !go#util#has_job()
setlocal omnifunc=go#complete#GocodeComplete
endif
endif

if get(g:, "go_doc_keywordprg_enabled", 1)
Expand Down
2 changes: 0 additions & 2 deletions plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ let s:packages = {
\ 'dlv': ['github.com/go-delve/delve/cmd/dlv@master'],
\ 'errcheck': ['github.com/kisielk/errcheck@master'],
\ 'fillstruct': ['github.com/davidrjenni/reftools/cmd/fillstruct@master'],
\ 'gocode': ['github.com/mdempsky/gocode@master', {'windows': ['-ldflags', '-H=windowsgui']}],
\ 'gocode-gomod': ['github.com/stamblerre/gocode'],
\ 'godef': ['github.com/rogpeppe/godef@master'],
\ 'gogetdoc': ['github.com/zmb3/gogetdoc@master'],
\ 'goimports': ['golang.org/x/tools/cmd/goimports@master'],
Expand Down