Skip to content

Commit

Permalink
complete: enable autobuild and propose-builtins
Browse files Browse the repository at this point in the history
This will hopefully make it more easier to use completion for newcomers.
It's totally optional and can be disabled if wished.

Closes #815
  • Loading branch information
fatih committed May 15, 2016
1 parent 5c74881 commit b0e00f6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
48 changes: 33 additions & 15 deletions autoload/go/complete.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
if !exists("g:go_gocode_bin")
let g:go_gocode_bin = "gocode"
endif


fu! s:gocodeCurrentBuffer()
function! s:gocodeCurrentBuffer()
let buf = getline(1, '$')
if &encoding != 'utf-8'
let buf = map(buf, 'iconv(v:val, &encoding, "utf-8")')
Expand All @@ -17,17 +12,17 @@ fu! s:gocodeCurrentBuffer()
call writefile(buf, file)

return file
endf
endfunction

fu! s:gocodeCommand(cmd, preargs, args)
function! s:gocodeCommand(cmd, preargs, args)
for i in range(0, len(a:args) - 1)
let a:args[i] = go#util#Shellescape(a:args[i])
endfor
for i in range(0, len(a:preargs) - 1)
let a:preargs[i] = go#util#Shellescape(a:preargs[i])
endfor

let bin_path = go#path#CheckBinPath(g:go_gocode_bin)
let bin_path = go#path#CheckBinPath("gocode")
if empty(bin_path)
return
endif
Expand All @@ -49,20 +44,43 @@ fu! s:gocodeCommand(cmd, preargs, args)
endif
return result
endif
endf
endfunction

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

let s:optionsEnabled = 0
function! s:gocodeEnableOptions()
if s:optionsEnabled
return
endif

let bin_path = go#path#CheckBinPath("gocode")
if empty(bin_path)
return
endif

let s:optionsEnabled = 1

call go#util#System(printf('%s set propose-builtins %s', go#util#Shellescape(bin_path), s:toBool(get(g:, 'go_gocode_propose_builtins', 1))))
call go#util#System(printf('%s set autobuild %s', go#util#Shellescape(bin_path), s:toBool(get(g:, 'go_gocode_autobuild', 1))))
endfunction

function! s:toBool(val)
if a:val | return 'true ' | else | return 'false' | endif
endfunction

function! s:gocodeAutocomplete()
call s:gocodeEnableOptions()

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

function! go#complete#GetInfo()
let offset = go#util#OffsetCursor()+1
Expand Down Expand Up @@ -120,7 +138,7 @@ function! s:trim_bracket(val)
return a:val
endfunction

fu! go#complete#Complete(findstart, base)
function! 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()
Expand Down
17 changes: 16 additions & 1 deletion doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ CTRL-t
:GoImpl f *Foo io.Writer
:GoImpl T io.ReadWriteCloser
<


===============================================================================
MAPPINGS *go-mappings*
Expand Down Expand Up @@ -1243,10 +1244,24 @@ By default it is set to edit.
*g:go_gorename_prefill*

Specifies whether |:GoRename| prefills the new identifier name with the
word under the cursor. By default is is enabled.
word under the cursor. By default it is enabled.
>
let g:go_gorename_prefill = 1
<
*g:go_gocode_autobuild*

Specifies whether `gocode` should automatically build out-of-date packages
when their source fiels are modified, in order to obtahin the freshes
autocomplete results for them. By default it is enabled.
>
let g:go_gocode_autobuild = 1
<
*g:go_gocode_propose_builtins*

Specifies whether `gocode` should add built-in types, functions and constants
to an autocompletion proposals. By default it is enabled.
>
let g:go_gocode_propose_builtins = 1
===============================================================================
Expand Down

0 comments on commit b0e00f6

Please sign in to comment.