Skip to content

Commit

Permalink
Add fern#smart#root() for smart mapping of root node and others (#498)
Browse files Browse the repository at this point in the history
* Add `fern#smart#root()` for smart mapping of root node and others

* Fix typo

* Add missing documentation for `fern#smart#root()`
  • Loading branch information
lambdalisue authored Mar 21, 2024
1 parent 00faa2c commit 928d355
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions autoload/fern/smart.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ function! fern#smart#scheme(default, schemes) abort
endif
return a:default
endfunction

function! fern#smart#root(root, others) abort
let helper = fern#helper#new()
let node = helper.sync.get_cursor_node()
if node is# v:null
return "\<Nop>"
endif
return node.__owner is# v:null
\ ? a:root
\ : a:others
endfunction
19 changes: 18 additions & 1 deletion doc/fern.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ Fern provide following mapping helper functions:
|fern#smart#scheme()| Return a mapping expression determined by a
scheme of a current fern tree

|fern#smart#root()| Return a mapping expression determined by a
kind of a current cursor node

For example, following execute "open" on leaf but "expand" on branch.
>
nmap <buffer><expr> <Plug>(fern-my-open-or-expand)
Expand Down Expand Up @@ -686,7 +689,7 @@ fern#smart#drawer({drawer}, {drawer-right}, {explorer})
>
" Perform 'expand' on drawer and 'enter' on explorer
nmap <buffer><expr>
\ <Plug>(fern-expand-or-enter)
\ <Plug>(fern-my-expand-or-enter)
\ fern#smart#drawer(
\ "<Plug>(fern-action-expand)",
\ "<Plug>(fern-action-enter)",
Expand All @@ -708,6 +711,20 @@ fern#smart#scheme({default}, {schemes})
\ {
\ 'bookmark': "\<C-^>",
\ },
\ )
*fern#smart#root()*
fern#smart#root({root}, {others})
Return one of a given mapping expression determined by a kind of a
current cursor node. If the node is root, the {root} is returned.
Otherwise the {others} is returned.
>
" Perform 'leave' on root node and 'open-or-enter' on others
nmap <buffer><expr>
\ <Plug>(fern-my-leave-or-open-or-enter)
\ fern#smart#root(
\ "<Plug>(fern-action-leave)",
\ "<Plug>(fern-action-open-or-enter)",
\ )
-----------------------------------------------------------------------------
Expand Down

0 comments on commit 928d355

Please sign in to comment.