New neovim jobs API.

This commit is contained in:
Jeremy Pallats/starcraft.man 2015-03-22 23:55:04 -04:00
parent e1b7f1ae85
commit fa5ece81f7

View file

@ -72,9 +72,9 @@ let s:plug_tab = get(s:, 'plug_tab', -1)
let s:plug_buf = get(s:, 'plug_buf', -1) let s:plug_buf = get(s:, 'plug_buf', -1)
let s:mac_gui = has('gui_macvim') && has('gui_running') let s:mac_gui = has('gui_macvim') && has('gui_running')
let s:is_win = has('win32') || has('win64') let s:is_win = has('win32') || has('win64')
let s:py2 = has('python') && !s:is_win && !has('win32unix') let s:py2 = has('python') && !has('nvim') && !s:is_win && !has('win32unix')
let s:ruby = has('ruby') && (v:version >= 703 || v:version == 702 && has('patch374')) let s:ruby = has('ruby') && !has('nvim') && (v:version >= 703 || v:version == 702 && has('patch374'))
let s:nvim = has('nvim') && !s:is_win let s:nvim = has('nvim') && !exists('##JobActivity') && !s:is_win
let s:me = resolve(expand('<sfile>:p')) let s:me = resolve(expand('<sfile>:p'))
let s:base_spec = { 'branch': 'master', 'frozen': 0 } let s:base_spec = { 'branch': 'master', 'frozen': 0 }
let s:TYPE = { let s:TYPE = {
@ -817,9 +817,7 @@ function! s:job_abort()
if !s:nvim || !exists('s:jobs') if !s:nvim || !exists('s:jobs')
return return
endif endif
augroup PlugJobControl
autocmd!
augroup END
for [name, j] in items(s:jobs) for [name, j] in items(s:jobs)
silent! call jobstop(j.jobid) silent! call jobstop(j.jobid)
if j.new if j.new
@ -829,7 +827,9 @@ function! s:job_abort()
let s:jobs = {} let s:jobs = {}
endfunction endfunction
function! s:job_handler(name) abort " When a:event == 'stdout', data = list of strings
" When a:event == 'exit', data = returncode
function! s:job_handler(job_id, data, name, event) abort
if !s:plug_window_exists() " plug window closed if !s:plug_window_exists() " plug window closed
return s:job_abort() return s:job_abort()
endif endif
@ -839,21 +839,20 @@ function! s:job_handler(name) abort
endif endif
let job = s:jobs[a:name] let job = s:jobs[a:name]
if v:job_data[1] == 'exit' if a:event == 'stdout'
let job.running = 0 let job.result .= substitute(s:to_s(a:data), '[\r\n]', '', 'g') . "\n"
if s:lastline(job.result) ==# 'Error'
let job.error = 1
let job.result = substitute(job.result, "Error[\r\n]$", '', '')
endif
call s:reap(a:name)
call s:tick()
else
let job.result .= substitute(s:to_s(v:job_data[2]), '[\r\n]', '', 'g') . "\n"
" To reduce the number of buffer updates " To reduce the number of buffer updates
let job.tick = get(job, 'tick', -1) + 1 let job.tick = get(job, 'tick', -1) + 1
if job.tick % len(s:jobs) == 0 if job.tick % len(s:jobs) == 0
call s:log(job.new ? '+' : '*', a:name, job.result) call s:log(job.new ? '+' : '*', a:name, job.result)
endif endif
elseif a:event == 'exit'
let job.running = 0
if a:data != 0
let job.error = 1
endif
call s:reap(a:name)
call s:tick()
endif endif
endfunction endfunction
@ -863,18 +862,22 @@ function! s:spawn(name, cmd, opts)
let s:jobs[a:name] = job let s:jobs[a:name] = job
if s:nvim if s:nvim
let x = jobstart(a:name, 'sh', ['-c', let opts = {
\ (has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir) : a:cmd) \ 'on_stdout': function('s:job_handler'),
\ . ' || echo Error']) \ 'on_exit': function('s:job_handler'),
if x > 0 \ 'user': a:name,
let job.jobid = x \ }
augroup PlugJobControl let argv = [
execute 'autocmd JobActivity' a:name printf('call s:job_handler(%s)', string(a:name)) \ 'sh', '-c',
augroup END \ (has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir) : a:cmd),
\ ]
let jid = jobstart(argv, opts)
if jid > 0
let job.jobid = jid
else else
let job.running = 0 let job.running = 0
let job.error = 1 let job.error = 1
let job.result = x < 0 ? 'sh is not executable' : let job.result = jid < 0 ? 'sh is not executable' :
\ 'Invalid arguments (or job table is full)' \ 'Invalid arguments (or job table is full)'
endif endif
else else
@ -886,10 +889,6 @@ function! s:spawn(name, cmd, opts)
endfunction endfunction
function! s:reap(name) function! s:reap(name)
if s:nvim
silent! execute 'autocmd! PlugJobControl JobActivity' a:name
endif
let job = s:jobs[a:name] let job = s:jobs[a:name]
if job.error if job.error
call add(s:update.errors, a:name) call add(s:update.errors, a:name)