From fd14757c04f894919e014629fcec91de3c34892c Mon Sep 17 00:00:00 2001 From: Martin Grenfell Date: Thu, 17 Jul 2014 20:16:57 +0100 Subject: [PATCH] add proper events and make the notifier class generic Expand the event system to have explicit Event objects and potentially many Notifiers. Previously they was only one notifier and one (implied) event. A lot of this is stolen from #358. --- autoload/nerdtree.vim | 3 ++- lib/nerdtree/event.vim | 13 ++++++++++++ lib/nerdtree/notifier.vim | 35 +++++++++++++++++++++++++++++++ lib/nerdtree/path.vim | 4 ++-- lib/nerdtree/refresh_notifier.vim | 21 ------------------- lib/nerdtree/tree_dir_node.vim | 2 +- 6 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 lib/nerdtree/event.vim create mode 100644 lib/nerdtree/notifier.vim delete mode 100644 lib/nerdtree/refresh_notifier.vim diff --git a/autoload/nerdtree.vim b/autoload/nerdtree.vim index 618328e..7f588b8 100644 --- a/autoload/nerdtree.vim +++ b/autoload/nerdtree.vim @@ -74,10 +74,11 @@ function! nerdtree#loadClassFiles() runtime lib/nerdtree/tree_dir_node.vim runtime lib/nerdtree/opener.vim runtime lib/nerdtree/creator.vim - runtime lib/nerdtree/refresh_notifier.vim runtime lib/nerdtree/flag_set.vim runtime lib/nerdtree/nerdtree.vim runtime lib/nerdtree/ui.vim + runtime lib/nerdtree/event.vim + runtime lib/nerdtree/notifier.vim endfunction " FUNCTION: nerdtree#postSourceActions() {{{2 diff --git a/lib/nerdtree/event.vim b/lib/nerdtree/event.vim new file mode 100644 index 0000000..964e8ff --- /dev/null +++ b/lib/nerdtree/event.vim @@ -0,0 +1,13 @@ +"CLASS: Event +"============================================================ +let s:Event = {} +let g:NERDTreeEvent = s:Event + +function! s:Event.New(nerdtree, subject, action, params) abort + let newObj = copy(self) + let newObj.nerdtree = a:nerdtree + let newObj.subject = a:subject + let newObj.action = a:action + let newObj.params = a:params + return newObj +endfunction diff --git a/lib/nerdtree/notifier.vim b/lib/nerdtree/notifier.vim new file mode 100644 index 0000000..b445f8a --- /dev/null +++ b/lib/nerdtree/notifier.vim @@ -0,0 +1,35 @@ +"CLASS: Notifier +"============================================================ +let s:Notifier = {} + +function! s:Notifier.AddListener(event, funcname) + let listeners = s:Notifier.GetListenersForEvent(a:event) + if listeners == [] + let listenersMap = s:Notifier.GetListenersMap() + let listenersMap[a:event] = listeners + endif + call add(listeners, a:funcname) +endfunction + +function! s:Notifier.NotifyListeners(event, path, params) + let event = g:NERDTreeEvent.New(b:NERDTree, a:path, a:event, a:params) + + for listener in s:Notifier.GetListenersForEvent(a:event) + call {listener}(event) + endfor +endfunction + +function! s:Notifier.GetListenersMap() + if !exists("s:refreshListenersMap") + let s:refreshListenersMap = {} + endif + return s:refreshListenersMap +endfunction + +function! s:Notifier.GetListenersForEvent(name) + let listenersMap = s:Notifier.GetListenersMap() + return get(listenersMap, a:name, []) +endfunction + +let g:NERDTreePathNotifier = deepcopy(s:Notifier) + diff --git a/lib/nerdtree/path.vim b/lib/nerdtree/path.vim index 5ab6b49..5de1dfe 100644 --- a/lib/nerdtree/path.vim +++ b/lib/nerdtree/path.vim @@ -549,13 +549,13 @@ endfunction "FUNCTION: Path.refresh() {{{1 function! s:Path.refresh() call self.readInfoFromDisk(self.str()) - call g:NERDTreeRefreshNotifier.NotifyListeners(self) + call g:NERDTreePathNotifier.NotifyListeners('refresh', self, {}) call self.cacheDisplayString() endfunction "FUNCTION: Path.refreshFlags() {{{1 function! s:Path.refreshFlags() - call g:NERDTreeRefreshNotifier.NotifyListeners(self) + call g:NERDTreePathNotifier.NotifyListeners('refreshFlags', self, {}) call self.cacheDisplayString() endfunction diff --git a/lib/nerdtree/refresh_notifier.vim b/lib/nerdtree/refresh_notifier.vim deleted file mode 100644 index 0cc62b0..0000000 --- a/lib/nerdtree/refresh_notifier.vim +++ /dev/null @@ -1,21 +0,0 @@ -"CLASS: RefreshNotifier -"============================================================ -let s:RefreshNotifier = {} -let g:NERDTreeRefreshNotifier = s:RefreshNotifier - -function! s:RefreshNotifier.AddListener(funcname) - call add(s:RefreshNotifier.GetListeners(), a:funcname) -endfunction - -function! s:RefreshNotifier.NotifyListeners(refreshedPath) - for listener in s:RefreshNotifier.GetListeners() - call {listener}(a:refreshedPath) - endfor -endfunction - -function! s:RefreshNotifier.GetListeners() - if !exists("s:refreshListeners") - let s:refreshListeners = [] - endif - return s:refreshListeners -endfunction diff --git a/lib/nerdtree/tree_dir_node.vim b/lib/nerdtree/tree_dir_node.vim index e7cc846..3f11e93 100644 --- a/lib/nerdtree/tree_dir_node.vim +++ b/lib/nerdtree/tree_dir_node.vim @@ -252,7 +252,7 @@ function! s:TreeDirNode._initChildren(silent) try let path = g:NERDTreePath.New(i) call self.createChild(path, 0) - call g:NERDTreeRefreshNotifier.NotifyListeners(path) + call g:NERDTreePathNotifier.NotifyListeners('init', path, {}) catch /^NERDTree.\(InvalidArguments\|InvalidFiletype\)Error/ let invalidFilesFound += 1 endtry