1
- " https://github.com/prabirshrestha/async.vim#d15123af3483350e235397116b554cb37e705130 (dirty)
1
+ " https://github.com/prabirshrestha/async.vim#236debf1a68d69a74f1f6647c273b0477e1ec1bf (dirty)
2
2
" :AsyncEmbed path=./autoload/lsp/utils/job.vim namespace=lsp#utils#job
3
3
4
4
" Author: Prabir Shrestha <mail at prabir dot me>
@@ -195,7 +195,11 @@ function! s:job_stop(jobid) abort
195
195
" silently for 'E900: Invalid job id' exception
196
196
endtry
197
197
elseif l: jobinfo .type == s: job_type_vimjob
198
- call job_stop (s: jobs [a: jobid ].job)
198
+ if type (s: jobs [a: jobid ].job) == v: t_job
199
+ call job_stop (s: jobs [a: jobid ].job)
200
+ elseif type (s: jobs [a: jobid ].job) == v: t_channel
201
+ call ch_close (s: jobs [a: jobid ].job)
202
+ endif
199
203
endif
200
204
endif
201
205
endfunction
@@ -306,6 +310,21 @@ function! s:job_pid(jobid) abort
306
310
return 0
307
311
endfunction
308
312
313
+ function ! s: callback_cb (jobid, opts, ch , data) abort
314
+ if has_key (a: opts , ' on_stdout' )
315
+ call a: opts .on_stdout (a: jobid , split (a: data , " \n " , 1 ), ' stdout' )
316
+ endif
317
+ endfunction
318
+
319
+ function ! s: close_cb (jobid, opts, ch ) abort
320
+ if has_key (a: opts , ' on_exit' )
321
+ call a: opts .on_exit (a: jobid , ' closed' , ' exit' )
322
+ endif
323
+ if has_key (s: jobs , a: jobid )
324
+ call remove (s: jobs , a: jobid )
325
+ endif
326
+ endfunction
327
+
309
328
" public apis {{{
310
329
function ! lsp#utils#job#start (cmd, opts) abort
311
330
return s: job_start (a: cmd , a: opts )
@@ -328,6 +347,33 @@ endfunction
328
347
function ! lsp#utils#job#pid (jobid) abort
329
348
return s: job_pid (a: jobid )
330
349
endfunction
350
+
351
+ function ! lsp#utils#job#connect (addr, opts) abort
352
+ let s: jobidseq = s: jobidseq + 1
353
+ let l: jobid = s: jobidseq
354
+ let l: retry = 0
355
+ while l: retry < 5
356
+ let l: ch = ch_open (a: addr , {' waittime' : 1000 })
357
+ call ch_setoptions (l: ch , {
358
+ \ ' callback' : function (' s:callback_cb' , [l: jobid , a: opts ]),
359
+ \ ' close_cb' : function (' s:close_cb' , [l: jobid , a: opts ]),
360
+ \ ' mode' : ' raw' ,
361
+ \} )
362
+ if ch_status (l: ch ) == # ' open'
363
+ break
364
+ endif
365
+ sleep 100 m
366
+ let l: retry += 1
367
+ endwhile
368
+ let s: jobs [l: jobid ] = {
369
+ \ ' type' : s: job_type_vimjob ,
370
+ \ ' opts' : a: opts ,
371
+ \ ' job' : l: ch ,
372
+ \ ' channel' : l: ch ,
373
+ \ ' buffer' : ' '
374
+ \}
375
+ return l: jobid
376
+ endfunction
331
377
" }}}
332
378
333
379
let &cpo = s: save_cpo
0 commit comments