Skip to content

Commit 5a7cecb

Browse files
committed
debug: non blocking messages in Vim
Do not block while waiting on responses from delve so that users can continue to interact Vim. Fixes #2915
1 parent eb89837 commit 5a7cecb

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

autoload/go/debug.vim

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,19 @@ function! s:call_jsonrpc(method, ...) abort
9797
let l:ch = s:state['ch']
9898
if has('nvim')
9999
call chansend(l:ch, l:req_json)
100-
while len(s:state.data) == 0
101-
sleep 50m
102-
if get(s:state, 'ready', 0) == 0
103-
return
104-
endif
105-
endwhile
106-
let resp_json = s:state.data[0]
107-
let s:state.data = s:state.data[1:]
108100
else
109101
call ch_sendraw(l:ch, req_json)
110-
let l:resp_raw = ch_readraw(l:ch)
111-
let resp_json = json_decode(l:resp_raw)
112102
endif
113103

104+
while len(s:state.data) == 0
105+
sleep 50m
106+
if get(s:state, 'ready', 0) == 0
107+
return
108+
endif
109+
endwhile
110+
let resp_json = s:state.data[0]
111+
let s:state.data = s:state.data[1:]
112+
114113
if go#util#HasDebug('debugger-commands')
115114
let g:go_debug_commands = add(go#config#DebugCommands(), {
116115
\ 'request': l:req_json,
@@ -521,10 +520,10 @@ function! s:out_cb(ch, msg) abort
521520
let s:state['message'] += [a:msg]
522521

523522
if stridx(a:msg, go#config#DebugAddress()) != -1
524-
if has('nvim')
525-
let s:state['data'] = []
526-
let l:state = {'databuf': ''}
523+
let s:state['data'] = []
524+
let l:state = {'databuf': ''}
527525

526+
if has('nvim')
528527
" explicitly bind callback to state so that within it, self will
529528
" always refer to state. See :help Partial for more information.
530529
let l:state.on_data = function('s:on_data', [], l:state)
@@ -535,7 +534,10 @@ function! s:out_cb(ch, msg) abort
535534
return
536535
endif
537536
else
538-
let l:ch = ch_open(go#config#DebugAddress(), {'mode': 'raw', 'timeout': 20000})
537+
" explicitly bind callback to state so that within it, self will
538+
" always refer to state. See :help Partial for more information.
539+
let l:state.on_data = function('s:on_data', [], l:state)
540+
let l:ch = ch_open(go#config#DebugAddress(), {'mode': 'raw', 'timeout': 20000, 'callback': l:state.on_data})
539541
if ch_status(l:ch) !=# 'open'
540542
call go#util#EchoError("could not connect to debugger")
541543
call go#job#Stop(s:state['job'])
@@ -561,11 +563,10 @@ function! s:out_cb(ch, msg) abort
561563
endif
562564
endfunction
563565

564-
function! s:on_data(ch, data, event) dict abort
565-
let l:data = self.databuf
566-
for l:msg in a:data
567-
let l:data .= l:msg
568-
endfor
566+
" s:on_data's third optional argument is provided, but not used, so that the
567+
" same function can be used for Vim's 'callback' and Neovim's 'data'.
568+
function! s:on_data(ch, data, ...) dict abort
569+
let l:data = s:message(self.databuf, a:data)
569570

570571
try
571572
let l:res = json_decode(l:data)
@@ -579,6 +580,19 @@ function! s:on_data(ch, data, event) dict abort
579580
endtry
580581
endfunction
581582

583+
function! s:message(buf, data) abort
584+
let l:data = a:buf
585+
if has('nvim')
586+
for l:msg in a:data
587+
let l:data .= l:msg
588+
endfor
589+
590+
return l:data
591+
endif
592+
593+
return printf('%s%s', a:buf, a:data)
594+
endfunction
595+
582596
" Start the debug mode. The first argument is the package name to compile and
583597
" debug, anything else will be passed to the running program.
584598
function! go#debug#Start(is_test, ...) abort

0 commit comments

Comments
 (0)