Skip to content

Commit

Permalink
Merge pull request #112 from lambdalisue/await-type2
Browse files Browse the repository at this point in the history
Add <Plug>(fern-wait) to synchronously wait asynchronous action
  • Loading branch information
lambdalisue authored Jun 13, 2020
2 parents 3f5d5c1 + d62d87a commit b5b8a07
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions autoload/fern/mapping.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ call s:Config.config(expand('<sfile>:p'), {
\ 'node',
\ 'open',
\ 'tree',
\ 'wait',
\ ],
\})
38 changes: 38 additions & 0 deletions autoload/fern/mapping/wait.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
let s:Config = vital#fern#import('Config')
let s:Promise = vital#fern#import('Async.Promise')

function! fern#mapping#wait#init(disable_default_mappings) abort
nnoremap <buffer><silent>
\ <Plug>(fern-wait-viewer:ready)
\ :<C-u>call <SID>call('hook', 'viewer:ready')<CR>
nmap <buffer> <Plug>(fern-wait) <Plug>(fern-wait-viewer:ready)
endfunction

function! s:call(name, ...) abort
return call(
\ 'fern#mapping#call',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_hook(helper, hook) abort
let [_, err] = s:Promise.wait(
\ fern#hook#promise(a:hook),
\ {
\ 'interval': g:fern#mapping#wait#interval,
\ 'timeout': g:fern#mapping#wait#timeout,
\ },
\)
if err isnot# v:null
throw printf(
\ '[fern] Failed to wait hook "%s": %s',
\ a:hook,
\ err,
\)
endif
endfunction

call s:Config.config(expand('<sfile>:p'), {
\ 'interval': 100,
\ 'timeout': 1000,
\})
19 changes: 19 additions & 0 deletions doc/fern.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ hit "e", add the following code:
" Use 'open:split' instead of 'open:edit' for 'open' action
nmap <buffer> <Plug>(fern-action-open) <Plug>(fern-action-open:split)
<
*fern-custom-wait*
Fern provide following mapping helper:

*<Plug>(fern-wait)* Wait until the fern buffer become ready which
would opened just before this mapping. This is
required while fern buffers are loaded
asynchronously but mappings are inovked
synchronously.

For example, following execute "tcd:root" action every after "leave" action.
>
nmap <buffer> <Plug>(fern-my-leave-and-tcd)
\ <Plug>(fern-action-leave)
\ <Plug>(fern-wait)
\ <Plug>(fern-action-tcd:root)
<
Without <Plug>(fern-wait), the "tcd:root" action will be invoked before actual
"leave" while "leave" action is asynchronous.

*fern-custom-smart*
Fern provide following mapping helper functions:

Expand Down

0 comments on commit b5b8a07

Please sign in to comment.