From 3e8a2dddc1e76485c86a91e040d1190aef03f977 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Sat, 22 Apr 2017 14:44:47 +0800 Subject: [PATCH] Add sync support --- autoload/SpaceVim/api/job.vim | 18 ++++++++++-------- autoload/SpaceVim/api/vim/compatible.vim | 1 + test/api/job.vader | 22 ++++++++++++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/autoload/SpaceVim/api/job.vim b/autoload/SpaceVim/api/job.vim index 4bb6a44c7..269bee56c 100644 --- a/autoload/SpaceVim/api/job.vim +++ b/autoload/SpaceVim/api/job.vim @@ -5,11 +5,10 @@ endfunction " make vim and neovim use same job func. let s:self = {} let s:self.jobs = {} -let s:self.nvim_job = has('nvim') -let s:self.vim_job = !has('nvim') && has('job') && has('patch-8.0.0027') -if !s:self.nvim_job && !s:self.vim_job - let s:self.vim_co = SpaceVim#api#import('vim#compatible') -endif +let s:self.nvim_job = has('nvim') && 0 +let s:self.vim_job = !has('nvim') && has('job') && has('patch-8.0.0027') && 0 +let s:self.vim_co = SpaceVim#api#import('vim#compatible') + function! s:self.warn(...) abort if len(a:000) == 0 echohl WarningMsg | echom 'Current version do not support job feature, fallback to sync system()' | echohl None @@ -98,16 +97,19 @@ function! s:self.start(argv, ...) abort let opts = {} endif let output = self.vim_co.systemlist(a:argv) - let id = len(self.jobs) + 1 + let id = -1 if v:shell_error if has_key(opts,'on_stderr') - call call(opts.on_stderr, [id, output, 'on_stderr']) + call call(opts.on_stderr, [id, output, 'stderr']) endif else if has_key(opts,'on_stdout') - call call(opts.on_stderr, [id, output, 'on_stdout']) + call call(opts.on_stdout, [id, output, 'stdout']) endif endif + if has_key(opts,'on_exit') + call call(opts.on_exit, [id, v:shell_error, 'exit']) + endif return id endif endfunction diff --git a/autoload/SpaceVim/api/vim/compatible.vim b/autoload/SpaceVim/api/vim/compatible.vim index 4d2718420..2c1aa083d 100644 --- a/autoload/SpaceVim/api/vim/compatible.vim +++ b/autoload/SpaceVim/api/vim/compatible.vim @@ -2,6 +2,7 @@ function! SpaceVim#api#vim#compatible#get() abort return map({ \ 'execute' : '', \ 'system' : '', + \ 'systemlist' : '', \ }, \ "function('s:' . v:key)" \ ) diff --git a/test/api/job.vader b/test/api/job.vader index 5334373b0..295a4b1c7 100644 --- a/test/api/job.vader +++ b/test/api/job.vader @@ -3,7 +3,7 @@ Execute ( SpaceVim api: job ): let argv = ['cat'] let g:stdout = '' let stderr = '' - let exit = 1 + let exit_data = 1 function! s:on_stdout(id, data, event) abort if a:event ==# 'stdout' for a in a:data @@ -12,7 +12,7 @@ Execute ( SpaceVim api: job ): endif endfor elseif a:event ==# 'exit' - let g:exit = a:data + let g:exit_data = a:data else let g:stderr = a:data endif @@ -23,11 +23,17 @@ Execute ( SpaceVim api: job ): \ 'on_exit' : function('s:on_stdout'), \ } let jobid = job.start(argv,opt) - call job.send(jobid, 'foo') - sleep 10m - AssertEqual stdout, 'foo' - AssertEqual job.status(jobid), 'run' - call job.stop(jobid) - AssertEqual exit, 1 + if jobid >= 0 + call job.send(jobid, 'foo') + sleep 10m + AssertEqual stdout, 'foo' + AssertEqual job.status(jobid), 'run' + call job.stop(jobid) + AssertEqual exit_data, 1 + else + let jobid = job.start(['echo', 'foo'],opt) + AssertEqual stdout, 'foo' + AssertEqual exit_data, 0 + endif