Skip to content

Commit e6788d1

Browse files
authored
Merge pull request #3713 from bhcleek/debug/improvement
debug: improve some logging bits
2 parents 787342d + d2ed711 commit e6788d1

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

autoload/go/debug.vim

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ scriptencoding utf-8
66

77
if !exists('s:state')
88
let s:state = {
9-
\ 'rpcid': 1,
9+
\ 'rpcid': 0,
1010
\ 'running': 0,
1111
\ 'currentThread': {},
1212
\ 'localVars': {},
@@ -64,28 +64,56 @@ function! s:complete(job, exit_status, data) abort
6464
call s:clearState()
6565
endfunction
6666

67-
function! s:logger(prefix, ch, msg) abort
68-
let l:cur_win = bufwinnr('')
69-
let l:log_win = bufwinnr(bufnr('__GODEBUG_OUTPUT__'))
70-
if l:log_win == -1
71-
return
72-
endif
73-
exe l:log_win 'wincmd w'
74-
67+
let s:log = []
68+
let s:logtimer = 0
69+
function! s:logasync(timer) abort
70+
let s:logtimer = 0
7571
try
76-
setlocal modifiable
77-
if getline(1) == ''
78-
call setline('$', a:prefix . a:msg)
79-
else
80-
call append('$', a:prefix . a:msg)
72+
let l:name = '__GODEBUG_OUTPUT__'
73+
let l:log_winid = bufwinid(l:name)
74+
if l:log_winid == -1
75+
if !s:isReady() && !has_key(s:state, 'job')
76+
return
77+
endif
78+
let s:logtimer = timer_start(go#config#DebugLogDelay(), function('s:logasync', []))
79+
return
8180
endif
82-
normal! G
83-
setlocal nomodifiable
81+
82+
try
83+
call setbufvar(l:name, '&modifiable', 1)
84+
for [l:prefix, l:data] in s:log
85+
call remove(s:log, 0)
86+
if getbufline(l:name, 1)[0] == ''
87+
call setbufline(l:name, '$', printf('%s%s', l:prefix, l:data))
88+
else
89+
call appendbufline(l:name, '$', printf('%s%s', l:prefix, l:data))
90+
endif
91+
endfor
92+
93+
" Move the window's cursor position without switching to the window
94+
call win_execute(l:log_winid, 'normal! G')
95+
call setbufvar(l:name, '&modifiable', 0)
96+
finally
97+
endtry
98+
catch
99+
call go#util#EchoError(printf('at %s: %s', v:throwpoint, v:exception))
84100
finally
85-
exe l:cur_win 'wincmd w'
101+
" retry when there's an exception.
102+
if len(s:log) != 0
103+
let s:logtimer = timer_start(go#config#DebugLogDelay(), function('s:logasync', []))
104+
endif
86105
endtry
87106
endfunction
88107

108+
function! s:logger(prefix, ch, data) abort
109+
let l:shouldStart = s:logtimer is 0
110+
let s:log = add(s:log, [a:prefix, a:data])
111+
112+
if l:shouldStart
113+
let s:logtimer = timer_start(go#config#DebugLogDelay(), function('s:logasync', []))
114+
endif
115+
endfunction
116+
89117
" s:call_jsonrpc will call method, passing all of s:call_jsonrpc's optional
90118
" arguments in the rpc request's params field.
91119

@@ -295,7 +323,13 @@ endfunction
295323
function! go#debug#Stop() abort
296324
" Remove all commands and add back the default commands.
297325
for k in map(split(execute('command GoDebug'), "\n")[1:], 'matchstr(v:val, "^\\s*\\zs\\S\\+")')
298-
exe 'delcommand' k
326+
try
327+
if k is 'Name'
328+
continue
329+
endif
330+
execute(printf('delcommand %s', k))
331+
catch
332+
endtry
299333
endfor
300334
command! -nargs=* -complete=customlist,go#package#Complete GoDebugStart call go#debug#Start('debug', <f-args>)
301335
command! -nargs=* -complete=customlist,go#package#Complete GoDebugTest call go#debug#Start('test', <f-args>)
@@ -309,7 +343,10 @@ function! go#debug#Stop() abort
309343

310344
" remove plug mappings
311345
for k in map(split(execute('nmap <Plug>(go-debug-'), "\n"), 'matchstr(v:val, "^n\\s\\+\\zs\\S\\+")')
312-
execute(printf('nunmap %s', k))
346+
try
347+
execute(printf('nunmap %s', k))
348+
catch
349+
endtry
313350
endfor
314351

315352
call s:stop()
@@ -1468,7 +1505,7 @@ function! go#debug#Restart() abort
14681505
call s:stop()
14691506

14701507
let s:state = {
1471-
\ 'rpcid': 1,
1508+
\ 'rpcid': 0,
14721509
\ 'running': 0,
14731510
\ 'currentThread': {},
14741511
\ 'localVars': {},

0 commit comments

Comments
 (0)