Skip to content

Commit 067d7c3

Browse files
committed
always use gocode's vim format
1 parent fb2986d commit 067d7c3

File tree

1 file changed

+32
-71
lines changed

1 file changed

+32
-71
lines changed

autoload/go/complete.vim

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let s:sock_type = (has('win32') || has('win64')) ? 'tcp' : 'unix'
22

3-
function! s:gocodeCommand(cmd, options, args) abort
3+
function! s:gocodeCommand(cmd, args) abort
44
let bin_path = go#path#CheckBinPath("gocode")
55
if empty(bin_path)
66
return []
@@ -10,21 +10,21 @@ function! s:gocodeCommand(cmd, options, args) abort
1010

1111
let cmd = [bin_path]
1212
let cmd = extend(cmd, ['-sock', socket_type])
13-
let cmd = extend(cmd, a:options)
13+
let cmd = extend(cmd, ['-f', 'vim'])
1414
let cmd = extend(cmd, [a:cmd])
1515
let cmd = extend(cmd, a:args)
1616

1717
return cmd
1818
endfunction
1919

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

2626
try
27-
let cmd = s:gocodeCommand(a:cmd, a:options, a:args)
27+
let cmd = s:gocodeCommand(a:cmd, a:args)
2828
" gocode can sometimes be slow, so redraw now to avoid waiting for gocode
2929
" to return before redrawing automatically.
3030
redraw
@@ -91,7 +91,6 @@ function! go#complete#Info(auto) abort
9191
endif
9292
endfunction
9393

94-
9594
function! s:async_info(auto)
9695
if exists("s:async_info_job")
9796
call job_stop(s:async_info_job)
@@ -135,38 +134,8 @@ function! s:async_info(auto)
135134
return
136135
endif
137136

138-
" first line is: Charcount,,NumberOfCandidates, i.e: 8,,1
139-
" following lines are candiates, i.e: func foo(name string),,foo(
140-
141-
" no candidates are found
142-
if len(self.messages) == 1
143-
return s:info_complete(self.auto, "")
144-
endif
145-
146-
" only one candidate is found
147-
if len(self.messages) == 2
148-
let result = split(self.messages[1], ',,')[0]
149-
return s:info_complete(self.auto, result)
150-
endif
151-
152-
" too many candidates are available, pick one that matches the word under
153-
" the cursor
154-
let infos = []
155-
for info in self.messages[1:]
156-
call add(infos, split(info, ',,')[0])
157-
endfor
158-
159-
let wordMatch = '\<' . expand("<cword>") . '\>'
160-
" escape single quotes in wordMatch before passing it to filter
161-
let wordMatch = substitute(wordMatch, "'", "''", "g")
162-
let filtered = filter(infos, "v:val =~ '".wordMatch."'")
163-
164-
let result = ""
165-
if len(filtered) == 1
166-
let result = filtered[0]
167-
endif
168-
169-
return s:info_complete(self.auto, result)
137+
let result = s:info_filter(self.auto, join(self.messages, "\n"))
138+
call s:info_complete(self.auto, result)
170139
endfunction
171140

172141
let offset = go#util#OffsetCursor()+1
@@ -177,9 +146,7 @@ function! s:async_info(auto)
177146
\ "GOROOT": go#util#env("goroot")
178147
\ }
179148

180-
" TODO(bc): refactor to use `-f vim` instead of `-f=godit`.
181149
let cmd = s:gocodeCommand('autocomplete',
182-
\ ['-f=godit'],
183150
\ [expand('%:p'), offset])
184151

185152
" TODO(bc): Don't write the buffer to a file; pass the buffer directrly to
@@ -208,58 +175,53 @@ function! s:sync_info(auto)
208175
" auto is true if we were called by g:go_auto_type_info's autocmd
209176
let offset = go#util#OffsetCursor()+1
210177

211-
" TODO(bc): refactor to use `-f vim` instead of `-f=godit`.
212178
let result = s:sync_gocode('autocomplete',
213-
\ ['-f=godit'],
179+
\ ['-f=vim'],
214180
\ [expand('%:p'), offset],
215181
\ go#util#GetLines())
216182

217-
" first line is: Charcount,,NumberOfCandidates, i.e: 8,,1
218-
" following lines are candiates, i.e: func foo(name string),,foo(
219-
let out = split(result, '\n')
183+
let result = s:info_filter(a:auto, result)
184+
call s:info_complete(a:auto, result)
185+
endfunction
220186

221-
" no candidates are found
222-
if len(out) == 1
223-
return s:info_complete(a:auto, "")
187+
function! s:info_filter(auto, result) abort
188+
if empty(a:result)
189+
return ""
224190
endif
225191

226-
" only one candidate is found
227-
if len(out) == 2
228-
let result = split(out[1], ',,')[0]
229-
return s:info_complete(a:auto, result)
192+
let l:result = eval(a:result)
193+
if len(l:result) != 2
194+
return ""
230195
endif
231196

232-
" too many candidates are available, pick one that maches the word under the
233-
" cursor
234-
let infos = []
235-
for info in out[1:]
236-
call add(infos, split(info, ',,')[0])
237-
endfor
197+
let l:candidates = l:result[1]
198+
if len(l:candidates) == 1
199+
" When gocode panics in vim mode, it returns
200+
" [0, [{'word': 'PANIC', 'abbr': 'PANIC PANIC PANIC', 'info': 'PANIC PANIC PANIC'}]]
201+
if a:auto && l:candidates[0].info ==# "PANIC PANIC PANIC"
202+
return ""
203+
endif
238204

205+
return l:candidates[0].info
206+
endif
207+
208+
let filtered = []
239209
let wordMatch = '\<' . expand("<cword>") . '\>'
240210
" escape single quotes in wordMatch before passing it to filter
241211
let wordMatch = substitute(wordMatch, "'", "''", "g")
242-
let filtered = filter(infos, "v:val =~ '".wordMatch."'")
212+
let filtered = filter(l:candidates, "v:val.info =~ '".wordMatch."'")
243213

244-
let result = ""
245-
if len(filtered) == 1
246-
let result = filtered[0]
214+
if len(l:filtered) != 1
215+
return ""
247216
endif
248217

249-
return s:info_complete(a:auto, result)
218+
return l:filtered[0].info
250219
endfunction
251220

252221
function! s:info_complete(auto, result) abort
253222
if !empty(a:result)
254-
" if auto, and the result is a PANIC by gocode, hide it
255-
if a:auto && a:result ==# 'PANIC PANIC PANIC'
256-
return ""
257-
endif
258-
259223
echo "vim-go: " | echohl Function | echon a:result | echohl None
260224
endif
261-
262-
return a:result
263225
endfunction
264226

265227
function! s:trim_bracket(val) abort
@@ -282,7 +244,7 @@ function! go#complete#Complete(findstart, base) abort
282244

283245
return s:completions[1]
284246
endif
285-
endf
247+
endfunction
286248

287249
function! go#complete#ToggleAutoTypeInfo() abort
288250
if get(g:, "go_auto_type_info", 0)
@@ -295,5 +257,4 @@ function! go#complete#ToggleAutoTypeInfo() abort
295257
call go#util#EchoProgress("auto type info enabled")
296258
endfunction
297259

298-
299260
" vim: sw=2 ts=2 et

0 commit comments

Comments
 (0)