From 86605413339960e0f32e5197d1d38b547bf141cf Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Sun, 2 Jul 2017 11:51:32 -0400 Subject: [PATCH] Fix the "TreeDirNode.open()" method Issues #547 and #526 reported a problem with the "open()" method in the "TreeDirNode" class. Specifically, opening a cascade in the NERDTree will perform the opening operation on the tail of the cascade. This is a problem when other operations (such as the "u" mapping) close intermediate cascaded directories, which causes opening the tail to have no effect (other than toggling the arrow). Here, the "open()" method was modified to open all directories in a cascade whenever the tail is opened. This is the only reasonable fix for this type of problem. Fixes #547 and fixes #526. --- lib/nerdtree/tree_dir_node.vim | 46 ++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/nerdtree/tree_dir_node.vim b/lib/nerdtree/tree_dir_node.vim index 70fa1aa..19597f5 100644 --- a/lib/nerdtree/tree_dir_node.vim +++ b/lib/nerdtree/tree_dir_node.vim @@ -410,25 +410,39 @@ function! s:TreeDirNode.New(path, nerdtree) return newTreeNode endfunction -" FUNCTION: TreeDirNode.open([opts]) {{{1 -" Open the dir in the current tree or in a new tree elsewhere. -" -" If opening in the current tree, return the number of cached nodes. -unlet s:TreeDirNode.open +" FUNCTION: TreeDirNode.open([options]) {{{1 +" Open this directory node in the current tree or elsewhere if special options +" are provided. Return 0 if options were processed. Otherwise, return the +" number of new cached nodes. function! s:TreeDirNode.open(...) - let opts = a:0 ? a:1 : {} + let l:options = a:0 ? a:1 : {} - if has_key(opts, 'where') && !empty(opts['where']) - let opener = g:NERDTreeOpener.New(self.path, opts) - call opener.open(self) - else - let self.isOpen = 1 - if self.children ==# [] - return self._initChildren(0) - else - return 0 - endif + " If special options were specified, process them and return. + if has_key(l:options, 'where') && !empty(l:options['where']) + let l:opener = g:NERDTreeOpener.New(self.path, l:options) + call l:opener.open(self) + return 0 endif + + " Open any ancestors of this node that render within the same cascade. + let l:parent = self.parent + while l:parent != b:NERDTree.root && !empty(l:parent) + if index(l:parent.getCascade(), self) >= 0 + let l:parent.isOpen = 1 + let l:parent = l:parent.parent + else + break + endif + endwhile + + let self.isOpen = 1 + + let l:numChildrenCached = 0 + if empty(self.children) + let l:numChildrenCached = self._initChildren(0) + endif + + return l:numChildrenCached endfunction " FUNCTION: TreeDirNode.openAlong([opts]) {{{1