|
| 1 | +if exists('g:loaded_gocode') |
| 2 | + finish |
| 3 | +endif |
| 4 | +let g:loaded_gocode = 1 |
| 5 | + |
| 6 | +fu! s:gocodeCurrentBuffer() |
| 7 | + let buf = getline(1, '$') |
| 8 | + if &l:fileformat == 'dos' |
| 9 | + " XXX: line2byte() depend on 'fileformat' option. |
| 10 | + " so if fileformat is 'dos', 'buf' must include '\r'. |
| 11 | + let buf = map(buf, 'v:val."\r"') |
| 12 | + endif |
| 13 | + let file = tempname() |
| 14 | + call writefile(buf, file) |
| 15 | + return file |
| 16 | +endf |
| 17 | + |
| 18 | +fu! s:system(str, ...) |
| 19 | + return (a:0 == 0 ? system(a:str) : system(a:str, join(a:000))) |
| 20 | +endf |
| 21 | + |
| 22 | +fu! s:gocodeCommand(cmd, preargs, args) |
| 23 | + for i in range(0, len(a:args) - 1) |
| 24 | + let a:args[i] = shellescape(a:args[i]) |
| 25 | + endfor |
| 26 | + for i in range(0, len(a:preargs) - 1) |
| 27 | + let a:preargs[i] = shellescape(a:preargs[i]) |
| 28 | + endfor |
| 29 | + let result = s:system(printf('gocode %s %s %s', join(a:preargs), a:cmd, join(a:args))) |
| 30 | + if v:shell_error != 0 |
| 31 | + return "[\"0\", []]" |
| 32 | + else |
| 33 | + return result |
| 34 | + endif |
| 35 | +endf |
| 36 | + |
| 37 | +fu! s:gocodeCurrentBufferOpt(filename) |
| 38 | + return '-in=' . a:filename |
| 39 | +endf |
| 40 | + |
| 41 | +fu! s:gocodeCursor() |
| 42 | + return printf('%d', line2byte(line('.')) + (col('.')-2)) |
| 43 | +endf |
| 44 | + |
| 45 | +fu! s:gocodeAutocomplete() |
| 46 | + let filename = s:gocodeCurrentBuffer() |
| 47 | + let result = s:gocodeCommand('autocomplete', |
| 48 | + \ [s:gocodeCurrentBufferOpt(filename), '-f=vim'], |
| 49 | + \ [expand('%:p'), s:gocodeCursor()]) |
| 50 | + call delete(filename) |
| 51 | + return result |
| 52 | +endf |
| 53 | + |
| 54 | +fu! gocomplete#Complete(findstart, base) |
| 55 | + "findstart = 1 when we need to get the text length |
| 56 | + if a:findstart == 1 |
| 57 | + execute "silent let g:gocomplete_completions = " . s:gocodeAutocomplete() |
| 58 | + return col('.') - g:gocomplete_completions[0] - 1 |
| 59 | + "findstart = 0 when we need to return the list of completions |
| 60 | + else |
| 61 | + return g:gocomplete_completions[1] |
| 62 | + endif |
| 63 | +endf |
0 commit comments