Skip to content

Commit

Permalink
Merge pull request #5 from k-takata/async-wrapper
Browse files Browse the repository at this point in the history
Async wrapper
  • Loading branch information
k-takata committed Mar 30, 2017
2 parents 02b6415 + c14b5bc commit 61e6162
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 16 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Concept
Requirements
------------

* Vim 8.0
* Vim 8.0
(Hopefully minpac will also work on NeoVim.)
* Git 1.9 or later
* OS
Windows: tested
Expand Down Expand Up @@ -208,7 +209,16 @@ A dictionary with following items will be returned:
| `'branch'` | Branch name to be cloned. |
| `'depth'` | Depth to be cloned. |


Credit
------

Prabir Shrestha (as the author of [async.vim](https://github.com/prabirshrestha/async.vim))


License
-------

VIM License

(`autoload/minpac/job.vim` is the MIT License.)
21 changes: 21 additions & 0 deletions autoload/minpac/LICENSE-async-vim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Prabir Shrestha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 20 additions & 15 deletions autoload/minpac/impl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,43 @@ function! s:decrement_job_count() abort
endif
endfunction

function! s:job_exit_cb(name, seq, job, errcode) abort
call filter(s:joblist, {-> v:val isnot a:job})
function! s:job_exit_cb(id, errcode, event) dict abort
call filter(s:joblist, {-> v:val != a:id})

let l:err = 1
if a:errcode == 0
let l:dir = g:minpac#pluglist[a:name].dir
let l:dir = g:minpac#pluglist[self.name].dir
if isdirectory(l:dir)
" Successfully updated.
if a:seq == 0 && filereadable(l:dir . '/.gitmodules')
if self.seq == 0 && filereadable(l:dir . '/.gitmodules')
" Update git submodule.
let l:cmd = [g:minpac#opt.git, '-C', l:dir, 'submodule', '--quiet',
\ 'update', '--init', '--recursive']
echom 'Updating submodules: ' . a:name
call s:start_job(l:cmd, a:name, a:seq + 1)
echom 'Updating submodules: ' . self.name
call s:start_job(l:cmd, self.name, self.seq + 1)
return
elseif isdirectory(l:dir . '/doc')
" Generate helptags.
silent! execute 'helptags' l:dir . '/doc'
endif
echom 'Updated: ' . a:name
echom 'Updated: ' . self.name
let l:err = 0
endif
endif
if l:err
echohl ErrorMsg
echom 'Error while updating "' . a:name . '": ' . a:errcode
echom 'Error while updating "' . self.name . '": ' . a:errcode
echohl None
endif

call s:decrement_job_count()
endfunction

function! s:job_err_cb(name, channel, message) abort
function! s:job_err_cb(id, message, event) dict abort
echohl WarningMsg
echom a:name . ': ' . a:message
for l:line in a:message
echom self.name . ': ' . l:line
endfor
echohl None
endfunction

Expand All @@ -89,11 +91,14 @@ function! s:start_job(cmds, name, seq) abort
else
let l:cmds = a:cmds
endif
let l:job = job_start(l:cmds, {
\ 'exit_cb': function('s:job_exit_cb', [a:name, a:seq]),
\ 'in_io': 'null', 'out_io': 'null',
\ 'err_cb': function('s:job_err_cb', [a:name])})
if job_status(l:job) ==# 'fail'
let l:job = minpac#job#start(l:cmds, {
\ 'on_stderr': function('s:job_err_cb'),
\ 'on_exit': function('s:job_exit_cb'),
\ 'name': a:name, 'seq': a:seq
\ })
if l:job > 0
" It worked!
else
echohl ErrorMsg
echom 'Fail to execute: ' . a:cmds[0]
echohl None
Expand Down
180 changes: 180 additions & 0 deletions autoload/minpac/job.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
" Author: Prabir Shrestha <mail at prabir dot me>
" License: The MIT License
" Website: https://github.com/prabirshrestha/async.vim

let s:save_cpo = &cpo
set cpo&vim

let s:jobidseq = 0
let s:jobs = {} " { job, opts, type: 'vimjob|nvimjob'}
let s:job_type_nvimjob = 'nvimjob'
let s:job_type_vimjob = 'vimjob'
let s:job_error_unsupported_job_type = -2 " unsupported job type

function! s:job_supported_types() abort
let l:supported_types = []
if has('nvim')
let l:supported_types += [s:job_type_nvimjob]
endif
if !has('nvim') && has('job') && has('channel') && has('lambda')
let l:supported_types += [s:job_type_vimjob]
endif
return l:supported_types
endfunction

function! s:job_supports_type(type) abort
return index(s:job_supported_types(), a:type) >= 0
endfunction

function! s:out_cb(job, data, jobid, opts) abort
if has_key(a:opts, 'on_stdout')
call a:opts.on_stdout(a:jobid, split(a:data, "\n", 1), 'stdout')
endif
endfunction

function! s:err_cb(job, data, jobid, opts) abort
if has_key(a:opts, 'on_stderr')
call a:opts.on_stderr(a:jobid, split(a:data, "\n", 1), 'stderr')
endif
endfunction

function! s:exit_cb(job, status, jobid, opts) abort
if has_key(a:opts, 'on_exit')
call a:opts.on_exit(a:jobid, a:status, 'exit')
endif
if has_key(s:jobs, a:jobid)
call remove(s:jobs, a:jobid)
endif
endfunction

function! s:on_stdout(jobid, data, event) abort
if has_key(s:jobs, a:jobid)
let l:jobinfo = s:jobs[a:jobid]
if has_key(l:jobinfo.opts, 'on_stdout')
call l:jobinfo.opts.on_stdout(a:jobid, a:data, a:event)
endif
endif
endfunction

function! s:on_stderr(jobid, data, event) abort
if has_key(s:jobs, a:jobid)
let l:jobinfo = s:jobs[a:jobid]
if has_key(l:jobinfo.opts, 'on_stderr')
call l:jobinfo.opts.on_stderr(a:jobid, a:data, a:event)
endif
endif
endfunction

function! s:on_exit(jobid, status, event) abort
if has_key(s:jobs, a:jobid)
let l:jobinfo = s:jobs[a:jobid]
if has_key(l:jobinfo.opts, 'on_exit')
call l:jobinfo.opts.on_exit(a:jobid, a:status, a:event)
endif
endif
endfunction

function! s:job_start(cmd, opts) abort
let l:jobtypes = s:job_supported_types()
let l:jobtype = ''

if has_key(a:opts, 'type')
if type(a:opts.type) == type('')
if !s:job_supports_type(a:opts.type)
return s:job_error_unsupported_job_type
endif
let l:jobtype = a:opts.type
else
let l:jobtypes = a:opts.type
endif
endif

if empty(l:jobtype)
" find the best jobtype
for l:jobtype2 in l:jobtypes
if s:job_supports_type(l:jobtype2)
let l:jobtype = l:jobtype2
endif
endfor
endif

if l:jobtype == ''
return s:job_error_unsupported_job_type
endif

if l:jobtype == s:job_type_nvimjob
let l:job = jobstart(a:cmd, {
\ 'on_stdout': function('s:on_stdout'),
\ 'on_stderr': function('s:on_stderr'),
\ 'on_exit': function('s:on_exit'),
\})
if l:job <= 0
return l:job
endif
let l:jobid = l:job " nvimjobid and internal jobid is same
let s:jobs[l:jobid] = {
\ 'type': s:job_type_nvimjob,
\ 'opts': a:opts,
\ }
let s:jobs[l:jobid].job = l:job
elseif l:jobtype == s:job_type_vimjob
let s:jobidseq = s:jobidseq + 1
let l:jobid = s:jobidseq
let l:job = job_start(a:cmd, {
\ 'out_cb': {job,data->s:out_cb(job, data, l:jobid, a:opts)},
\ 'err_cb': {job,data->s:err_cb(job, data, l:jobid, a:opts)},
\ 'exit_cb': {job,data->s:exit_cb(job, data, l:jobid, a:opts)},
\ 'mode': 'raw',
\})
if job_status(l:job) != 'run'
return -1
endif
let s:jobs[l:jobid] = {
\ 'type': s:job_type_vimjob,
\ 'opts': a:opts,
\ 'job': l:job,
\ 'channel': job_getchannel(l:job)
\ }
else
return s:job_error_unsupported_job_type
endif

return l:jobid
endfunction

function! s:job_stop(jobid) abort
if has_key(s:jobs, a:jobid)
let l:jobinfo = s:jobs[a:jobid]
if l:jobinfo.type == s:job_type_nvimjob
call jobstop(a:jobid)
elseif l:jobinfo.type == s:job_type_vimjob
call job_stop(s:jobs[a:jobid].job)
endif
if has_key(s:jobs, a:jobid)
call remove(s:jobs, a:jobid)
endif
endif
endfunction

function! s:job_send(jobid, data) abort
let l:jobinfo = s:jobs[a:jobid]
if l:jobinfo.type == s:job_type_nvimjob
call jobsend(a:jobid, a:data)
elseif l:jobinfo.type == s:job_type_vimjob
call ch_sendraw(l:jobinfo.channel, a:data)
endif
endfunction

" public apis {{{
function! minpac#job#start(cmd, opts) abort
return s:job_start(a:cmd, a:opts)
endfunction

function! minpac#job#stop(jobid) abort
call s:job_stop(a:jobid)
endfunction

function! minpac#job#send(jobid, data) abort
call s:job_send(a:jobid, a:data)
endfunction
" }}}
1 change: 1 addition & 0 deletions doc/minpac.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CONCEPT *minpac-concept*
REQUIREMENTS *minpac-requirements*

* Vim 8.0
(Hopefully minpac will also work on NeoVim.)
* Git 1.9 or later
* OS
Windows: tested
Expand Down
2 changes: 2 additions & 0 deletions tools/pull-async-vim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
git subtree pull --prefix autoload/minpac https://github.com/prabirshrestha/async.vim.git master --squash

0 comments on commit 61e6162

Please sign in to comment.