Skip to content

Commit 928d355

Browse files
authored
Add fern#smart#root() for smart mapping of root node and others (#498)
* Add `fern#smart#root()` for smart mapping of root node and others * Fix typo * Add missing documentation for `fern#smart#root()`
1 parent 00faa2c commit 928d355

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

autoload/fern/smart.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ function! fern#smart#scheme(default, schemes) abort
3636
endif
3737
return a:default
3838
endfunction
39+
40+
function! fern#smart#root(root, others) abort
41+
let helper = fern#helper#new()
42+
let node = helper.sync.get_cursor_node()
43+
if node is# v:null
44+
return "\<Nop>"
45+
endif
46+
return node.__owner is# v:null
47+
\ ? a:root
48+
\ : a:others
49+
endfunction

doc/fern.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ Fern provide following mapping helper functions:
222222
|fern#smart#scheme()| Return a mapping expression determined by a
223223
scheme of a current fern tree
224224

225+
|fern#smart#root()| Return a mapping expression determined by a
226+
kind of a current cursor node
227+
225228
For example, following execute "open" on leaf but "expand" on branch.
226229
>
227230
nmap <buffer><expr> <Plug>(fern-my-open-or-expand)
@@ -686,7 +689,7 @@ fern#smart#drawer({drawer}, {drawer-right}, {explorer})
686689
>
687690
" Perform 'expand' on drawer and 'enter' on explorer
688691
nmap <buffer><expr>
689-
\ <Plug>(fern-expand-or-enter)
692+
\ <Plug>(fern-my-expand-or-enter)
690693
\ fern#smart#drawer(
691694
\ "<Plug>(fern-action-expand)",
692695
\ "<Plug>(fern-action-enter)",
@@ -708,6 +711,20 @@ fern#smart#scheme({default}, {schemes})
708711
\ {
709712
\ 'bookmark': "\<C-^>",
710713
\ },
714+
\ )
715+
716+
*fern#smart#root()*
717+
fern#smart#root({root}, {others})
718+
Return one of a given mapping expression determined by a kind of a
719+
current cursor node. If the node is root, the {root} is returned.
720+
Otherwise the {others} is returned.
721+
>
722+
" Perform 'leave' on root node and 'open-or-enter' on others
723+
nmap <buffer><expr>
724+
\ <Plug>(fern-my-leave-or-open-or-enter)
725+
\ fern#smart#root(
726+
\ "<Plug>(fern-action-leave)",
727+
\ "<Plug>(fern-action-open-or-enter)",
711728
\ )
712729
713730
-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)