Skip to content

Commit cb42eb0

Browse files
committed
Update test without repainting buffer
1 parent 3000144 commit cb42eb0

File tree

2 files changed

+84
-58
lines changed

2 files changed

+84
-58
lines changed

autoload/OmniSharp/testrunner.vim

Lines changed: 83 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function! OmniSharp#testrunner#FoldText() abort
4848
elseif line =~# '^ \f'
4949
" File
5050
let filename = trim(line)
51-
let fullpath = fnamemodify(filename, ':p')
5251
let displayname = matchlist(filename, '^\f\{-}\([^/\\]\+\)\.csx\?$')[1]
5352
" Position the cursor so that search() is relative to the fold, not the
5453
" actual cursor position
@@ -57,7 +56,7 @@ function! OmniSharp#testrunner#FoldText() abort
5756
let projectline = search('^;', 'bcnWz')
5857
call winrestview(winview)
5958
let projectkey = matchlist(getline(projectline), '^\S\+')[0]
60-
let ntests = len(s:tests[projectkey].files[fullpath].tests)
59+
let ntests = len(s:tests[projectkey].files[filename].tests)
6160
return printf(' %s [%d]', displayname, ntests)
6261
elseif line =~# '^<'
6362
return printf(' Error details (%d lines)', v:foldend - v:foldstart + 1)
@@ -106,7 +105,7 @@ function! OmniSharp#testrunner#Remove() abort
106105
let s:tests[projectkey].visible = 0
107106
elseif line =~# '^ \f'
108107
" File selected
109-
let filename = fnamemodify(trim(line), ':p')
108+
let filename = trim(line)
110109
let projectline = search('^;', 'bcnWz')
111110
let projectkey = matchlist(getline(projectline), '^\S\+')[0]
112111
let s:tests[projectkey].files[filename].visible = 0
@@ -225,23 +224,9 @@ function! s:buffer.focus() abort
225224
return v:false
226225
endfunction
227226

228-
function! s:buffer.bannerlines() abort
229-
let lines = []
230-
let delimiter = get(g:, 'OmniSharp_testrunner_banner_delimeter', '')
231-
call add(lines, '`' . repeat(delimiter, 80))
232-
call add(lines, '` OmniSharp Test Runner')
233-
call add(lines, '` ' . repeat(delimiter, 76))
234-
call add(lines, '` <F1> Toggle this menu (:help omnisharp-test-runner for more)')
235-
call add(lines, '` <F5> Run test or tests in file under cursor')
236-
call add(lines, '` <F6> Debug test under cursor')
237-
call add(lines, '` <CR> Navigate to test or stack trace')
238-
call add(lines, '`' . repeat(delimiter, 80))
239-
return lines
240-
endfunction
241-
242227
function! s:buffer.paint() abort
243228
if get(g:, 'OmniSharp_testrunner_banner', 1)
244-
let lines = self.bannerlines()
229+
let lines = self.paintbanner()
245230
else
246231
let lines = []
247232
endif
@@ -271,33 +256,10 @@ function! s:buffer.paint() abort
271256
for testfile in sort(keys(s:tests[key].files))
272257
if !s:tests[key].files[testfile].visible | continue | endif
273258
let tests = s:tests[key].files[testfile].tests
274-
call add(lines, ' ' . fnamemodify(testfile, ':.'))
259+
call add(lines, ' ' . testfile)
275260
for name in sort(keys(tests), {a,b -> tests[a].lnum > tests[b].lnum})
276261
let test = tests[name]
277-
if test.state ==# 'hidden' | continue | endif
278-
let state = s:utils.state2char[test.state]
279-
call add(lines, printf('%s %s', state, name))
280-
if state ==# '-' && !has_key(test, 'spintimer')
281-
call s:spinner.start(test, len(lines))
282-
endif
283-
for messageline in get(test, 'message', [])
284-
call add(lines, '> ' . trim(messageline, ' ', 2))
285-
endfor
286-
for stacktraceline in get(test, 'stacktrace', [])
287-
let line = trim(stacktraceline.text)
288-
if has_key(stacktraceline, 'filename')
289-
let line = '__ ' . line . ' ___ ' . stacktraceline.filename . ' __ '
290-
else
291-
let line = '_._ ' . line . ' _._ '
292-
endif
293-
if has_key(stacktraceline, 'lnum')
294-
let line .= 'line ' . stacktraceline.lnum
295-
endif
296-
call add(lines, '> ' . line)
297-
endfor
298-
for outputline in get(test, 'output', [])
299-
call add(lines, '// ' . trim(outputline, ' ', 2))
300-
endfor
262+
call extend(lines, self.painttest(test, len(lines) + 1))
301263
endfor
302264
call add(lines, '__')
303265
endfor
@@ -316,6 +278,51 @@ function! s:buffer.paint() abort
316278
endif
317279
endfunction
318280

281+
function! s:buffer.paintbanner() abort
282+
let lines = []
283+
let delimiter = get(g:, 'OmniSharp_testrunner_banner_delimeter', '')
284+
call add(lines, '`' . repeat(delimiter, 80))
285+
call add(lines, '` OmniSharp Test Runner')
286+
call add(lines, '` ' . repeat(delimiter, 76))
287+
call add(lines, '` <F1> Toggle this menu (:help omnisharp-test-runner for more)')
288+
call add(lines, '` <F5> Run test or tests in file under cursor')
289+
call add(lines, '` <F6> Debug test under cursor')
290+
call add(lines, '` <CR> Navigate to test or stack trace')
291+
call add(lines, '`' . repeat(delimiter, 80))
292+
return lines
293+
endfunction
294+
295+
function! s:buffer.painttest(test, lnum) abort
296+
if a:test.state ==# 'hidden'
297+
return []
298+
endif
299+
let lines = []
300+
let state = s:utils.state2char[a:test.state]
301+
call add(lines, printf('%s %s', state, a:test.name))
302+
if state ==# '-' && !has_key(a:test, 'spintimer')
303+
call s:spinner.start(a:test, a:lnum)
304+
endif
305+
for messageline in get(a:test, 'message', [])
306+
call add(lines, '> ' . trim(messageline, ' ', 2))
307+
endfor
308+
for stacktraceline in get(a:test, 'stacktrace', [])
309+
let line = trim(stacktraceline.text)
310+
if has_key(stacktraceline, 'filename')
311+
let line = '__ ' . line . ' ___ ' . stacktraceline.filename . ' __ '
312+
else
313+
let line = '_._ ' . line . ' _._ '
314+
endif
315+
if has_key(stacktraceline, 'lnum')
316+
let line .= 'line ' . stacktraceline.lnum
317+
endif
318+
call add(lines, '> ' . line)
319+
endfor
320+
for outputline in get(a:test, 'output', [])
321+
call add(lines, '// ' . trim(outputline, ' ', 2))
322+
endfor
323+
return lines
324+
endfunction
325+
319326

320327
function! OmniSharp#testrunner#SetBreakpoints() abort
321328
if !OmniSharp#util#HasVimspector()
@@ -397,19 +404,33 @@ function! s:UpdateState(bufnr, state, ...) abort
397404
endif
398405
endif
399406
endfor
400-
401-
let tests[testname].state = a:state
402-
let tests[testname].message = get(opts, 'message', [])
403-
let tests[testname].stacktrace = stacktrace
404-
let tests[testname].output = get(opts, 'output', [])
407+
let test = tests[testname]
408+
let test.state = a:state
409+
let test.message = get(opts, 'message', [])
410+
let test.stacktrace = stacktrace
411+
let test.output = get(opts, 'output', [])
412+
413+
call setbufvar(s:runner.bufnr, '&modifiable', 1)
414+
let lines = getbufline(s:runner.bufnr, 1, '$')
415+
let pattern = '^ ' . substitute(filename, '/', '\\/', 'g')
416+
let fileline = match(lines, pattern) + 1
417+
let pattern = '^[-|*!] ' . testname
418+
let testline = match(lines, pattern, fileline) + 1
419+
420+
let patterns = ['^[-|*!] \S', '^__$', '^$']
421+
let endline = min(
422+
\ filter(
423+
\ map(
424+
\ patterns,
425+
\ {_,pattern -> match(lines, pattern, testline)}),
426+
\ {_,matchline -> matchline >= testline}))
427+
let testlines = s:buffer.painttest(test, testline)
428+
call deletebufline(s:runner.bufnr, testline, endline)
429+
call appendbufline(s:runner.bufnr, testline - 1, testlines)
430+
call setbufvar(s:runner.bufnr, '&modifiable', 0)
431+
call setbufvar(s:runner.bufnr, '&modified', 0)
405432
endif
406433
endfor
407-
let l:winid = win_getid()
408-
let l:focused = s:buffer.focus()
409-
call s:buffer.paint()
410-
if l:focused
411-
call win_gotoid(l:winid)
412-
endif
413434
endfunction
414435

415436
function! OmniSharp#testrunner#StateComplete(location) abort
@@ -449,9 +470,9 @@ function! OmniSharp#testrunner#ToggleBanner() abort
449470
let displayed = getline(1) =~# '`'
450471
call setbufvar(s:runner.bufnr, '&modifiable', 1)
451472
if g:OmniSharp_testrunner_banner && !displayed
452-
call appendbufline(s:runner.bufnr, 0, s:buffer.bannerlines())
473+
call appendbufline(s:runner.bufnr, 0, s:buffer.paintbanner())
453474
elseif !g:OmniSharp_testrunner_banner && displayed
454-
call deletebufline(s:runner.bufnr, 1, len(s:buffer.bannerlines()))
475+
call deletebufline(s:runner.bufnr, 1, len(s:buffer.paintbanner()))
455476
endif
456477
call setbufvar(s:runner.bufnr, '&modifiable', 0)
457478
call setbufvar(s:runner.bufnr, '&modified', 0)
@@ -481,22 +502,27 @@ let s:spinner.steps_utf8 = [
481502
function! s:spinner.spin(test, lnum, timer) abort
482503
if s:utils.state2char[a:test.state] !=# '-'
483504
call timer_stop(a:timer)
484-
return
485505
endif
486506
let lnum = a:lnum + (get(g:, 'OmniSharp_testrunner_banner', 1) ? 8 : 0)
487507
let lines = getbufline(s:runner.bufnr, lnum)
488508
if len(lines) == 0
489509
call timer_stop(a:timer)
490510
return
491511
endif
512+
" TODO: find the test by name, instead of line number
492513
let line = lines[0]
493514
let steps = get(g:, 'OmniSharp_testrunner_spinnersteps',
494515
\ get(g:, 'OmniSharp_testrunner_spinner_ascii')
495516
\ ? self.steps_ascii : self.steps_utf8)
496517
if !has_key(a:test.spinner, 'index')
518+
" Starting
497519
let line .= ' -- ' . steps[0]
498520
let a:test.spinner.index = 0
521+
elseif s:utils.state2char[a:test.state] !=# '-'
522+
" Stopping
523+
let line = substitute(line, ' -- .*$', '', '')
499524
else
525+
" Stepping
500526
let a:test.spinner.index += 1
501527
if a:test.spinner.index >= len(steps)
502528
let a:test.spinner.index = 0
@@ -545,7 +571,6 @@ function! s:utils.findTest() abort
545571
let projectkey = matchlist(getline(projectline), '^\S\+')[0]
546572
let fileline = search('^ \f', 'bcnWz')
547573
let filename = matchlist(getline(fileline), '^ \zs.*$')[0]
548-
let filename = fnamemodify(filename, ':p')
549574
return s:tests[projectkey].files[filename].tests[testname]
550575
endif
551576
return {}

syntax/omnisharptest.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ hi def link ostStackLoc Identifier
7171
hi def link ostOutput Comment
7272

7373
" Highlights for normally concealed elements
74+
hi def link ostBannerPrefix NonText
7475
hi def link ostProjectDelimiter NonText
7576
hi def link ostErrorPrefix NonText
7677
hi def link ostFileDivider NonText

0 commit comments

Comments
 (0)