Skip to content

Commit b5b8a07

Browse files
authored
Merge pull request #112 from lambdalisue/await-type2
Add <Plug>(fern-wait) to synchronously wait asynchronous action
2 parents 3f5d5c1 + d62d87a commit b5b8a07

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

autoload/fern/mapping.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ call s:Config.config(expand('<sfile>:p'), {
2929
\ 'node',
3030
\ 'open',
3131
\ 'tree',
32+
\ 'wait',
3233
\ ],
3334
\})

autoload/fern/mapping/wait.vim

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
let s:Config = vital#fern#import('Config')
2+
let s:Promise = vital#fern#import('Async.Promise')
3+
4+
function! fern#mapping#wait#init(disable_default_mappings) abort
5+
nnoremap <buffer><silent>
6+
\ <Plug>(fern-wait-viewer:ready)
7+
\ :<C-u>call <SID>call('hook', 'viewer:ready')<CR>
8+
nmap <buffer> <Plug>(fern-wait) <Plug>(fern-wait-viewer:ready)
9+
endfunction
10+
11+
function! s:call(name, ...) abort
12+
return call(
13+
\ 'fern#mapping#call',
14+
\ [funcref(printf('s:map_%s', a:name))] + a:000,
15+
\)
16+
endfunction
17+
18+
function! s:map_hook(helper, hook) abort
19+
let [_, err] = s:Promise.wait(
20+
\ fern#hook#promise(a:hook),
21+
\ {
22+
\ 'interval': g:fern#mapping#wait#interval,
23+
\ 'timeout': g:fern#mapping#wait#timeout,
24+
\ },
25+
\)
26+
if err isnot# v:null
27+
throw printf(
28+
\ '[fern] Failed to wait hook "%s": %s',
29+
\ a:hook,
30+
\ err,
31+
\)
32+
endif
33+
endfunction
34+
35+
call s:Config.config(expand('<sfile>:p'), {
36+
\ 'interval': 100,
37+
\ 'timeout': 1000,
38+
\})

doc/fern.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ hit "e", add the following code:
169169
" Use 'open:split' instead of 'open:edit' for 'open' action
170170
nmap <buffer> <Plug>(fern-action-open) <Plug>(fern-action-open:split)
171171
<
172+
*fern-custom-wait*
173+
Fern provide following mapping helper:
174+
175+
*<Plug>(fern-wait)* Wait until the fern buffer become ready which
176+
would opened just before this mapping. This is
177+
required while fern buffers are loaded
178+
asynchronously but mappings are inovked
179+
synchronously.
180+
181+
For example, following execute "tcd:root" action every after "leave" action.
182+
>
183+
nmap <buffer> <Plug>(fern-my-leave-and-tcd)
184+
\ <Plug>(fern-action-leave)
185+
\ <Plug>(fern-wait)
186+
\ <Plug>(fern-action-tcd:root)
187+
<
188+
Without <Plug>(fern-wait), the "tcd:root" action will be invoked before actual
189+
"leave" while "leave" action is asynchronous.
190+
172191
*fern-custom-smart*
173192
Fern provide following mapping helper functions:
174193

0 commit comments

Comments
 (0)