From f9e83ad8bda884b3fa8a6e5f615dc6ffffa06379 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Sun, 6 May 2018 12:19:19 -0400 Subject: [PATCH] Fix a bug with the 'u' mapping This commit refactors the "nerdtree#ui_glue#UpDir()" function to fix a bug in the behavior of the 'u' mapping. To reproduce the bug: 1. Open a bookmarked directory with 'o' 2. Press 'u' twice, observing normal behavior 3. Open the same bookmark again with 'o' 4. Press 'u' again, observe the new root is closed The reason for this bug is that the parent node already existed and was closed by the second 'u' press in step two above. This commit fixes this bug by being careful to always open the new root and transplant the child node properly. Also, the internals of the function in question were refactored to make use of valuable NERDTree functions that ultimately help to reduce the amount of code needed for this operation. --- autoload/nerdtree/ui_glue.vim | 55 +++++++++++++++-------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 0cc3362..6ba9ee6 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -29,9 +29,9 @@ function! nerdtree#ui_glue#createDefaultBindings() call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapOpenRecursively, 'scope': "DirNode", 'callback': s."openNodeRecursively" }) - call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapUpdir, 'scope': "all", 'callback': s."upDirCurrentRootClosed" }) - call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapUpdirKeepOpen, 'scope': "all", 'callback': s."upDirCurrentRootOpen" }) - call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapChangeRoot, 'scope': "Node", 'callback': s."chRoot" }) + call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapUpdir, 'scope': 'all', 'callback': s . 'upDirCurrentRootClosed' }) + call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapUpdirKeepOpen, 'scope': 'all', 'callback': s . 'upDirCurrentRootOpen' }) + call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapChangeRoot, 'scope': 'Node', 'callback': s . 'chRoot' }) call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapChdir, 'scope': "Node", 'callback': s."chCwd" }) @@ -619,40 +619,33 @@ function! s:toggleZoom() call b:NERDTree.ui.toggleZoom() endfunction -"FUNCTION: nerdtree#ui_glue#upDir(keepState) {{{1 -"moves the tree up a level +" FUNCTION: nerdtree#ui_glue#upDir(preserveState) {{{1 +" Move the NERDTree up one level. " -"Args: -"keepState: 1 if the current root should be left open when the tree is -"re-rendered -function! nerdtree#ui_glue#upDir(keepState) - let cwd = b:NERDTree.root.path.str({'format': 'UI'}) - if cwd ==# "/" || cwd =~# '^[^/]..$' - call nerdtree#echo("already at top dir") - else - if !a:keepState - call b:NERDTree.root.close() - endif +" Args: +" preserveState: if 1, the current root is left open when the new tree is +" rendered; if 0, the current root node is closed +function! nerdtree#ui_glue#upDir(preserveState) - let oldRoot = b:NERDTree.root + try + call b:NERDTree.root.cacheParent() + catch /^NERDTree.CannotCacheParentError/ + call nerdtree#echo('already at root directory') + return + endtry - if empty(b:NERDTree.root.parent) - let path = b:NERDTree.root.path.getParent() - let newRoot = g:NERDTreeDirNode.New(path, b:NERDTree) - call newRoot.open() - call newRoot.transplantChild(b:NERDTree.root) - let b:NERDTree.root = newRoot - else - let b:NERDTree.root = b:NERDTree.root.parent - endif + let l:oldRoot = b:NERDTree.root + let l:newRoot = b:NERDTree.root.parent - if g:NERDTreeChDirMode ==# 2 - call b:NERDTree.root.path.changeToDir() - endif + call l:newRoot.open() + call l:newRoot.transplantChild(l:oldRoot) - call b:NERDTree.render() - call oldRoot.putCursorHere(0, 0) + if !a:preserveState + call l:oldRoot.close() endif + + call b:NERDTree.changeRoot(l:newRoot) + call l:oldRoot.putCursorHere(0, 0) endfunction " FUNCTION: s:upDirCurrentRootOpen() {{{1