Skip to content

Commit e71c760

Browse files
authored
Use buffer as a job output (#94)
* Use buffer as a job output * Rename some variables * Make code coverage thingy happy * Fix 'writing to the wrong buffer' bug * Use copen instead of cwindow after job stops * Fix test on MacOS with cmake 2.8
1 parent e80b6f9 commit e71c760

File tree

5 files changed

+37
-34
lines changed

5 files changed

+37
-34
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
test/tmp
33
.DS_Store
44
*.swp
5+
*.swo

Diff for: autoload/utils/cmake/fileapi.vim

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ function! s:getReplyFolder(build_dir) abort
1616
endfunction
1717

1818
function! s:createFile(filename, content) abort
19-
new
20-
setlocal buftype=nofile bufhidden=hide noswapfile nobuflisted
21-
put=a:content
22-
execute 'w! ' a:filename
23-
q
19+
silent call writefile( [ a:content ], a:filename )
2420
endfunction
2521

2622
function! s:createQuery() abort

Diff for: autoload/utils/common.vim

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
" Executes the command
55
function! utils#common#executeCommand(cmd, ...) abort
6-
" Close quickfix list in order to don't save custom error format
7-
silent! cclose
86
let l:errFormat = get(a:, 1, '')
97

108
if (g:cmake_build_executor ==# 'dispatch') || (g:cmake_build_executor ==# '' && exists(':Dispatch'))
9+
" Close quickfix list to discard custom error format
10+
silent! cclose
1111
call utils#exec#dispatch#run(a:cmd, l:errFormat)
1212
elseif (g:cmake_build_executor ==# 'job') || (g:cmake_build_executor ==# '' && ((has('job') && has('channel')) || has('nvim')))
13+
" job#run behaves differently if the qflist is open or closed
1314
call utils#exec#job#run(a:cmd, l:errFormat)
1415
else
16+
" Close quickfix list to discard custom error format
17+
silent! cclose
1518
call utils#exec#system#run(a:cmd, l:errFormat)
1619
endif
1720
endfunction

Diff for: autoload/utils/exec/job.vim

+28-26
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,26 @@ function! s:createQuickFix() abort
5151
if l:bufnr == -1
5252
return
5353
endif
54-
cexpr ''
5554
let l:old_error = &errorformat
5655
if s:err_fmt !=# ''
5756
let &errorformat = s:err_fmt
5857
endif
5958
execute 'cgetbuffer ' . l:bufnr
6059
call setqflist( [], 'a', { 'title' : s:cmake4vim_job[ 'cmd' ] } )
61-
call s:closeBuffer()
6260
if s:err_fmt !=# ''
6361
let &errorformat = l:old_error
6462
endif
6563
" Remove cmake4vim job
6664
let s:cmake4vim_job = {}
6765
endfunction
6866

69-
function! s:vimOut(channel, message) abort
70-
if empty(s:cmake4vim_job) || a:channel != s:cmake4vim_job['channel']
71-
return
72-
endif
73-
call s:appendLine(a:message)
74-
endfunction
75-
76-
function! s:vimExit(channel, message) abort
77-
if empty(s:cmake4vim_job) || a:channel != s:cmake4vim_job['job']
78-
return
79-
endif
67+
function! s:vimClose(channel) abort
8068
call s:createQuickFix()
81-
if a:message != 0
82-
copen
83-
endif
69+
call s:closeBuffer()
70+
let s:cmake4vim_job = {}
71+
72+
cwindow
73+
cbottom
8474
endfunction
8575

8676
function! s:nVimOut(job_id, data, event) abort
@@ -97,18 +87,29 @@ function! s:nVimExit(job_id, data, event) abort
9787
return
9888
endif
9989
call s:createQuickFix()
90+
call s:closeBuffer()
10091
if a:data != 0
10192
copen
10293
endif
10394
endfunction
10495

10596
function! s:createJobBuf() abort
106-
silent execute 'belowright 10split ' . s:cmake4vim_buf
97+
let l:cursor_was_in_quickfix = getwininfo(win_getid())[0]['quickfix']
98+
" qflist is open somewhere
99+
if !empty(filter(range(1, winnr('$')), 'getwinvar(v:val, "&ft") ==# "qf"'))
100+
" move the cursor there
101+
copen
102+
silent execute 'edit ' . s:cmake4vim_buf
103+
else
104+
silent execute 'belowright 10split ' . s:cmake4vim_buf
105+
endif
107106
setlocal bufhidden=hide buftype=nofile buflisted nolist
108107
setlocal noswapfile nowrap nomodifiable
109108
nmap <buffer> <C-c> :call utils#exec#job#stop()<CR>
110109
let l:bufnum = winbufnr(0)
111-
wincmd p
110+
if !l:cursor_was_in_quickfix
111+
wincmd p
112+
endif
112113
return l:bufnum
113114
endfunction
114115
" }}} Private functions "
@@ -127,15 +128,14 @@ function! utils#exec#job#stop() abort
127128
catch
128129
endtry
129130
call s:createQuickFix()
131+
call s:closeBuffer()
130132
copen
131-
echom 'Job is canceled!'
133+
call utils#common#Warning('Job is cancelled!')
132134
endfunction
133135

134-
" Use job
135136
function! utils#exec#job#run(cmd, err_fmt) abort
136-
" Create a new quickfix
137-
let l:openbufnr = bufnr(s:cmake4vim_buf)
138-
if l:openbufnr != -1
137+
" if there is a job or if the buffer is open, abort
138+
if !empty(s:cmake4vim_job) || bufnr(s:cmake4vim_buf) != -1
139139
call utils#common#Warning('Async execute is already running')
140140
return -1
141141
endif
@@ -153,9 +153,11 @@ function! utils#exec#job#run(cmd, err_fmt) abort
153153
\ }
154154
else
155155
let l:job = job_start(a:cmd, {
156-
\ 'out_cb': function('s:vimOut'),
157-
\ 'err_cb': function('s:vimOut'),
158-
\ 'exit_cb': function('s:vimExit'),
156+
\ 'close_cb': function('s:vimClose'),
157+
\ 'out_io' : 'buffer', 'out_buf' : l:outbufnr,
158+
\ 'err_io' : 'buffer', 'err_buf' : l:outbufnr,
159+
\ 'out_modifiable' : 0,
160+
\ 'err_modifiable' : 0,
159161
\ })
160162
let s:cmake4vim_job = {
161163
\ 'job': l:job,

Diff for: test/tests/build/job_run.vader

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ Before:
3636
let g:cmake_build_dir_prefix="cmake-build-"
3737
let g:cmake_build_executor = 'job'
3838
let g:cmake_variants = {}
39-
let g:cmake_ctest_args = []
39+
let g:cmake_ctest_args = ''
4040
" Use on in order to close all windows and avoid E36 error
4141
silent on
4242

4343
After:
44+
silent cclose
4445
silent call RemoveFile("compile_commands.json")
4546
silent call RemoveCMakeDirs()
4647
cd ..

0 commit comments

Comments
 (0)