Skip to content

Commit

Permalink
Merge pull request #1926 from bhcleek/complete-job-api
Browse files Browse the repository at this point in the history
Use vim-go's job api for autocompletion (and enable async for Neovim)
  • Loading branch information
bhcleek authored Aug 31, 2018
2 parents f464255 + 893b04c commit 4b76af3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
75 changes: 28 additions & 47 deletions autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -67,59 +67,35 @@ function! go#complete#GetInfo() abort
endfunction

function! go#complete#Info() abort
if go#util#has_job(1)
if go#util#has_job(1) || has('nvim')
return s:async_info(1)
else
return s:sync_info(1)
endif
endfunction

function! s:async_info(echo)
if exists("s:async_info_job")
call job_stop(s:async_info_job)
unlet s:async_info_job
endif

let state = {
\ 'exited': 0,
\ 'exit_status': 0,
\ 'closed': 0,
\ 'messages': [],
\ 'echo': a:echo
\ }

function! s:callback(chan, msg) dict
let l:msg = a:msg
if &encoding != 'utf-8'
let l:msg = iconv(l:msg, 'utf-8', &encoding)
endif
call add(self.messages, l:msg)
endfunction

function! s:exit_cb(job, exitval) dict
let self.exit_status = a:exitval
let self.exited = 1

if self.closed
call self.complete()
endif
endfunction
let state = {'echo': a:echo}

function! s:close_cb(ch) dict
let self.closed = 1
if self.exited
call self.complete()
function! s:complete(job, exit_status, messages) abort dict
if a:exit_status != 0
return
endif
endfunction

function state.complete() dict
if self.exit_status != 0
return
if &encoding != 'utf-8'
let i = 0
while i < len(a:messages)
let a:messages[i] = iconv(a:messages[i], 'utf-i', &encoding)
let i += 1
endwhile
endif

let result = s:info_filter(self.echo, join(self.messages, "\n"))
let result = s:info_filter(self.echo, join(a:messages, "\n"))
call s:info_complete(self.echo, result)
endfunction
" 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
Expand All @@ -131,23 +107,28 @@ function! s:async_info(echo)
\ "GOROOT": go#util#env("goroot")
\ }

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

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 directrly to
" 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('%')}
" should work.
let options = {
" doesn't work.
call extend(opts, {
\ 'env': env,
\ 'in_io': 'file',
\ 'in_name': s:gocodeFile(),
\ 'callback': funcref("s:callback", [], state),
\ 'exit_cb': funcref("s:exit_cb", [], state),
\ 'close_cb': funcref("s:close_cb", [], state)
\ }
\ })

let s:async_info_job = job_start(cmd, options)
call go#job#Start(cmd, opts)
endfunction

function! s:gocodeFile()
Expand Down
2 changes: 1 addition & 1 deletion autoload/go/def_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func! Test_Jump_leaves_lists() abort
call setqflist(copy(expected), 'r' )

let l:bufnr = bufnr('%')
call cursor(6, 3)
call cursor(6, 7)
call go#def#Jump('')

let start = reltime()
Expand Down
2 changes: 1 addition & 1 deletion autoload/go/guru.vim
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ function! go#guru#DescribeInfo() abort
return
endif

call go#util#EchoInfo(info)
echo "vim-go: " | echohl Function | echon info | echohl None
endfunction

let args = {
Expand Down

0 comments on commit 4b76af3

Please sign in to comment.