Skip to content

Commit

Permalink
Allow use of function references as callbacks (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
HiPhish authored and PhilRunninger committed Dec 15, 2019
1 parent 82b1649 commit a7886fb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
in an unordered list. The format is:
- **.PATCH**: Pull Request Title (PR Author) [PR Number](Link to PR)
-->
#### 6.4
- **.0**: Allow use of function references as callbacks (HiPhish) [#1067](https://github.com/scrooloose/nerdtree/pull/1067)
#### 6.3
- **.0**: Add new command that behaves like NERDTreeToggle but defaults to the root of a VCS repository. (willfindlay) [#1060](https://github.com/scrooloose/nerdtree/pull/1060)
#### 6.2
Expand Down
4 changes: 4 additions & 0 deletions doc/NERDTree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,10 @@ following code conventions are used:
See this blog post for more details:
http://got-ravings.blogspot.com/2008/09/vim-pr0n-prototype-based-objects.html

A number of API functions take a callback argument to call. The callback can
be either a string with the name of a function to call, or a |Funcref| object
which will be called directly.

------------------------------------------------------------------------------
4.1. Key map API *NERDTreeKeymapAPI*

Expand Down
2 changes: 1 addition & 1 deletion lib/nerdtree/key_map.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ endfunction
"FUNCTION: KeyMap.invoke() {{{1
"Call the KeyMaps callback function
function! s:KeyMap.invoke(...)
let Callback = function(self.callback)
let Callback = type(self.callback) == v:t_func ? self.callback : function(self.callback)
if a:0
call Callback(a:1)
else
Expand Down
8 changes: 6 additions & 2 deletions lib/nerdtree/menu_item.vim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ endfunction
"specified
function! s:MenuItem.enabled()
if self.isActiveCallback != -1
return {self.isActiveCallback}()
return type(self.isActiveCallback) == v:t_func ? self.isActiveCallback() : {self.isActiveCallback}()
endif
return 1
endfunction
Expand All @@ -94,7 +94,11 @@ function! s:MenuItem.execute()
call mc.showMenu()
else
if self.callback != -1
call {self.callback}()
if type(self.callback) == v:t_func
call self.callback()
else
call {self.callback}()
endif
endif
endif
endfunction
Expand Down
5 changes: 3 additions & 2 deletions lib/nerdtree/notifier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ endfunction
function! s:Notifier.NotifyListeners(event, path, nerdtree, params)
let event = g:NERDTreeEvent.New(a:nerdtree, a:path, a:event, a:params)

for listener in s:Notifier.GetListenersForEvent(a:event)
call {listener}(event)
for Listener in s:Notifier.GetListenersForEvent(a:event)
let Callback = type(Listener) == v:t_func ? Listener : function(Listener)
call Callback(event)
endfor
endfunction

Expand Down
5 changes: 3 additions & 2 deletions lib/nerdtree/path.vim
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ function! s:Path.ignore(nerdtree)
endif
endfor

for callback in g:NERDTree.PathFilters()
if {callback}({'path': self, 'nerdtree': a:nerdtree})
for Callback in g:NERDTree.PathFilters()
let Callback = type(Callback) == v:t_func ? Callback : function(Callback)
if Callback({'path': self, 'nerdtree': a:nerdtree})
return 1
endif
endfor
Expand Down

0 comments on commit a7886fb

Please sign in to comment.