Open prabirshrestha opened 8 years ago
Possible but not stable
@mattn wanted to know if there has been any update to this.
I'm trying to implement a proof of concept async FindAllReferences for https://github.com/prabirshrestha/vim-ts so would like to have something like function! ctrlp#ts#findAllReferences()#asyncinit
or some api so i can call ctrlp#add(....)
I prototyped this with vim-lsp and quickpick and created a plugin called quickpick-lsp.vim. I used an example of LspWorkspaceSymbols
since a project could contain hundreds and thousands of results and need to cancel previous requests and request a new one.
Here is an api I came up for ctrlp. The core of this is to support a new property called events
that would allow us to hookup to different events such as user changing the search text and then give us apis to set the items/append items/clearitems/set busy indicators. Debounce is also pretty common so would be good to have a first class debounce support. The api is completly backwards compatible which means existing sources don't need to be migrated.
call add(g:ctrlp_ext_vars, {
\ 'init': 'ctrlp#lsp#init()',
\ 'events': 'ctrlp#lsp#events',
\ 'debounce': 250,
\ 'accept': 'ctrlp#async#accept',
\ 'lname': 'LspWorkspaceSymbols',
\ 'sname': 'LSPWorkSym',
\ 'type': 'tabs',
\ })
function! ctrlp#lsp#events(type, ctx) abort
if a:type == 'change'
call s:cancel(a:ctx)
call s:search(a:ctx)
elseif a:type == 'init'
call s:search(a:ctx)
elseif a:type == 'close'
call s:cancel(a:ctx)
endif
endfunction
function! s:cancel(ctx) abort
if has_key(a:ctx.data, 'timer')
call timer_stop(a:ctx.data.timer)
call remove(a:ctx.data, 'timer')
endif
endfunction
function! s:search(ctx) abort
call a:ctx.set_busy(1)
let a:ctx.data = { 'counter': 0, 'items': [], 'total': 5 }
let a:ctx.data.timer = timer_start(500, function('s:add_items', [a:ctx]), { 'repeat': a:ctx.data.total })
endfunction
function s:callback(ctx) abort
let a:ctx.data.counter += 1
call add(a:ctx.items, a:ctx.data.counter)
call a:ctx.set_items(a:items)
if a:ctx.data.total == a:ctx.data.counter
call a:ctx.data.set_busy(0)
endif
endfunction
Here is another example where it could be really useful such as searching and installing NPM packages.
What are your thoughts on introducing this new api?
Actually even got a simpler api working which is very similar to existing ctrlp apis. https://github.com/prabirshrestha/ctrlp.vim/tree/events
It introduces several new apis.
ctrlp#input
to get the current search termchange
event to listen to the change in search termctrlp#set(lines)
to set the lines. to clear use ctrl#set([])
ctrlp#get()
to get the linesctrlp#update()
to force re-render once you set.user_data
for set
so in accepts can get the value and do something with it.ctrlp#busy(1)
cal add(g:ctrlp_ext_vars, {
\ 'init': 'ctrlp#month#init()',
\ 'change': 'ctrlp#month#change()',
\ 'accept': 'ctrlp#month#accept',
\ 'lname': 'month',
\ 'sname': 'month',
\ 'type': 'tabe',
\ })
let s:months= ["January","February","March","April","May","June","July","August","September","October","November","December"]
fu! ctrlp#month#change()
let l:input = ctrlp#input()
call ctrlp#set(filter(copy(s:months), 'v:val =~ l:input'))
call ctrlp#update()
endf
Will try implementing vim-lsp SearchWorkspaceSymbols on this now.
Sent a PR at https://github.com/ctrlpvim/ctrlp.vim/pull/543
Given that both neovim and vim now supports jobs and channels are there any plans to add async sources?
Shougo is also rewriting unite https://github.com/Shougo/denite.nvim