From 496d9a17d86185b6c41ea34877c7024d10837531 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Thu, 21 Dec 2017 08:28:28 -0500 Subject: [PATCH 1/6] Remove code duplication in "findAndRevealPath" --- autoload/nerdtree/ui_glue.vim | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 92795ca..411c312 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -270,13 +270,13 @@ function! s:findAndRevealPath(path) endif try - let p = g:NERDTreePath.New(l:path) + let l:p = g:NERDTreePath.New(l:path) catch /^NERDTree.InvalidArgumentsError/ - call nerdtree#echo("no file for the current buffer") + call nerdtree#echo('no file for the current buffer') return endtry - if p.isUnixHiddenPath() + if l:p.isUnixHiddenPath() let showhidden=g:NERDTreeShowHidden let g:NERDTreeShowHidden = 1 endif @@ -285,36 +285,29 @@ function! s:findAndRevealPath(path) try let cwd = g:NERDTreePath.New(getcwd()) catch /^NERDTree.InvalidArgumentsError/ - call nerdtree#echo("current directory does not exist.") - let cwd = p.getParent() + call nerdtree#echo('current directory does not exist.') + let cwd = l:p.getParent() endtry - if p.isUnder(cwd) + if l:p.isUnder(cwd) call g:NERDTreeCreator.CreateTabTree(cwd.str()) else - call g:NERDTreeCreator.CreateTabTree(p.getParent().str()) + call g:NERDTreeCreator.CreateTabTree(l:p.getParent().str()) endif else - if !p.isUnder(g:NERDTreeFileNode.GetRootForTab().path) - if !g:NERDTree.IsOpen() - call g:NERDTreeCreator.ToggleTabTree('') - else - call g:NERDTree.CursorToTreeWin() - endif + NERDTreeFocus + + if !l:p.isUnder(g:NERDTreeFileNode.GetRootForTab().path) call b:NERDTree.ui.setShowHidden(g:NERDTreeShowHidden) - call s:chRoot(g:NERDTreeDirNode.New(p.getParent(), b:NERDTree)) - else - if !g:NERDTree.IsOpen() - call g:NERDTreeCreator.ToggleTabTree("") - endif + call s:chRoot(g:NERDTreeDirNode.New(l:p.getParent(), b:NERDTree)) endif endif - call g:NERDTree.CursorToTreeWin() - let node = b:NERDTree.root.reveal(p) + + let node = b:NERDTree.root.reveal(l:p) call b:NERDTree.render() call node.putCursorHere(1,0) - if p.isUnixHiddenFile() + if l:p.isUnixHiddenFile() let g:NERDTreeShowHidden = showhidden endif endfunction From 5301dd24fb1478ebbdb5a026c8a84c1b8b03b2c5 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Thu, 21 Dec 2017 08:43:15 -0500 Subject: [PATCH 2/6] Improve variable naming in "findAndRevealPath" --- autoload/nerdtree/ui_glue.vim | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 411c312..910ead3 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -270,45 +270,45 @@ function! s:findAndRevealPath(path) endif try - let l:p = g:NERDTreePath.New(l:path) + let l:pathObj = g:NERDTreePath.New(l:path) catch /^NERDTree.InvalidArgumentsError/ call nerdtree#echo('no file for the current buffer') return endtry - if l:p.isUnixHiddenPath() - let showhidden=g:NERDTreeShowHidden + if l:pathObj.isUnixHiddenPath() + let l:showHidden = g:NERDTreeShowHidden let g:NERDTreeShowHidden = 1 endif if !g:NERDTree.ExistsForTab() try - let cwd = g:NERDTreePath.New(getcwd()) + let l:cwd = g:NERDTreePath.New(getcwd()) catch /^NERDTree.InvalidArgumentsError/ call nerdtree#echo('current directory does not exist.') - let cwd = l:p.getParent() + let l:cwd = l:pathObj.getParent() endtry - if l:p.isUnder(cwd) - call g:NERDTreeCreator.CreateTabTree(cwd.str()) + if l:pathObj.isUnder(l:cwd) + call g:NERDTreeCreator.CreateTabTree(l:cwd.str()) else - call g:NERDTreeCreator.CreateTabTree(l:p.getParent().str()) + call g:NERDTreeCreator.CreateTabTree(l:pathObj.getParent().str()) endif else NERDTreeFocus - if !l:p.isUnder(g:NERDTreeFileNode.GetRootForTab().path) + if !l:pathObj.isUnder(g:NERDTreeFileNode.GetRootForTab().path) call b:NERDTree.ui.setShowHidden(g:NERDTreeShowHidden) - call s:chRoot(g:NERDTreeDirNode.New(l:p.getParent(), b:NERDTree)) + call s:chRoot(g:NERDTreeDirNode.New(l:pathObj.getParent(), b:NERDTree)) endif endif - let node = b:NERDTree.root.reveal(l:p) + let l:node = b:NERDTree.root.reveal(l:pathObj) call b:NERDTree.render() - call node.putCursorHere(1,0) + call l:node.putCursorHere(1, 0) - if l:p.isUnixHiddenFile() - let g:NERDTreeShowHidden = showhidden + if l:pathObj.isUnixHiddenFile() + let g:NERDTreeShowHidden = l:showHidden endif endfunction From 90d08dc626f0638cd079895a68ee0cf7f7e03d9a Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Thu, 21 Dec 2017 09:26:26 -0500 Subject: [PATCH 3/6] Add a debugging message --- autoload/nerdtree/ui_glue.vim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 910ead3..894a88a 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -304,6 +304,11 @@ function! s:findAndRevealPath(path) endif let l:node = b:NERDTree.root.reveal(l:pathObj) + + if empty(l:node) + echomsg 'l:node is totally empty...' + endif + call b:NERDTree.render() call l:node.putCursorHere(1, 0) From 344119439e417839d217f31c6195f996e91ce8a1 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Thu, 21 Dec 2017 10:26:07 -0500 Subject: [PATCH 4/6] Refresh children of directory nodes on "reveal()" The ":NERDTreeFind" command calls the "reveal()" method on the NERDTree root node. The "reveal()" method would, in turn, call the node's "open()" method. Since the "open()" method would only initialize the child nodes of the root (i.e., read them from disk) when the list of child nodes was empty, new paths would not be included in the list. This commit will result in the refreshing of the child node list whenever "reveal()" is called on a directory node (unless it is the first time the node is being opened... the most efficient option). The result is that ":NERDTreeFind" will discover newly created paths that exist on disk but are not cached in the NERDTree. A stray debugging message is also removed. Fixes issue #779. --- autoload/nerdtree/ui_glue.vim | 5 ----- lib/nerdtree/tree_dir_node.vim | 7 +++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 894a88a..910ead3 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -304,11 +304,6 @@ function! s:findAndRevealPath(path) endif let l:node = b:NERDTree.root.reveal(l:pathObj) - - if empty(l:node) - echomsg 'l:node is totally empty...' - endif - call b:NERDTree.render() call l:node.putCursorHere(1, 0) diff --git a/lib/nerdtree/tree_dir_node.vim b/lib/nerdtree/tree_dir_node.vim index 03c3545..43285db 100644 --- a/lib/nerdtree/tree_dir_node.vim +++ b/lib/nerdtree/tree_dir_node.vim @@ -568,6 +568,13 @@ function! s:TreeDirNode.reveal(path, ...) throw "NERDTree.InvalidArgumentsError: " . a:path.str() . " should be under " . self.path.str() endif + " Refresh "self.children" to avoid missing paths created after this node + " was last opened. If "self.children" is empty, the call to "open()" will + " initialize the children. + if !empty(self.children) + " Silence messages/errors. They were seen on the first open. + silent! call self._initChildren(1) + endif call self.open() if self.path.equals(a:path.getParent()) From 01b011d38e2af0e11f03d34d80d4cfae497732da Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Fri, 22 Dec 2017 08:45:57 -0500 Subject: [PATCH 5/6] Have "finAndRevealPath()" fail on no file If a file does not exist for the current buffer, this function should fail with a clear warning message. Here, I improved the messages that this function prints so that it fails gracefully when no path can be determined in the calling context. --- autoload/nerdtree/ui_glue.vim | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 910ead3..e2dc96a 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -261,18 +261,19 @@ function! s:displayHelp() call b:NERDTree.ui.centerView() endfunction -" FUNCTION: s:findAndRevealPath(path) {{{1 -function! s:findAndRevealPath(path) - let l:path = a:path +" FUNCTION: s:findAndRevealPath(pathStr) {{{1 +function! s:findAndRevealPath(pathStr) + let l:pathStr = !empty(a:pathStr) ? a:pathStr : expand('%:p') - if empty(l:path) - let l:path = expand('%:p') + if empty(l:pathStr) + call nerdtree#echoWarning('no file for the current buffer') + return endif - + try - let l:pathObj = g:NERDTreePath.New(l:path) + let l:pathObj = g:NERDTreePath.New(l:pathStr) catch /^NERDTree.InvalidArgumentsError/ - call nerdtree#echo('no file for the current buffer') + call nerdtree#echoWarning('invalid path') return endtry From f6dad4796e30cd1960dc2bed230e69c20f02313c Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Fri, 22 Dec 2017 09:15:07 -0500 Subject: [PATCH 6/6] Update the documentation The docs for ":NERDTreeFind" are updated. Some additional formatting changes are made to other sections. --- doc/NERDTree.txt | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/doc/NERDTree.txt b/doc/NERDTree.txt index f68880c..38fbad9 100644 --- a/doc/NERDTree.txt +++ b/doc/NERDTree.txt @@ -126,20 +126,20 @@ The following features and functionality are provided by the NERD tree: Changes made to one tree are reflected in both as they are actually the same buffer. - If only one other NERD tree exists, that tree is automatically mirrored. If - more than one exists, the script will ask which tree to mirror. + If only one other NERD tree exists, that tree is automatically mirrored. + If more than one exists, the script will ask which tree to mirror. :NERDTreeClose *:NERDTreeClose* Close the NERD tree in this tab. -:NERDTreeFind *:NERDTreeFind* - Find the current file in the tree. +:NERDTreeFind [] *:NERDTreeFind* + Without the optional argument, find and reveal the file for the active + buffer in the NERDTree window. With the argument, find and + reveal the specified path. - If no tree exists and the current file is under vim's CWD, then init a - tree at the CWD and reveal the file. Otherwise init a tree in the current - file's directory. - - In any case, the current file is revealed and the cursor is placed on it. + Focus will be shifted to the NERDTree window, and the cursor will be + placed on the tree node for the determined path. If a NERDTree for the + current tab does not exist, a new one will be initialized. :NERDTreeCWD *:NERDTreeCWD* Change tree root to current directory. If no NERD tree exists for this @@ -1128,13 +1128,12 @@ NERDTreeAddKeyMap({options}) *NERDTreeAddKeyMap()* Additionally, a "scope" argument may be supplied. This constrains the mapping so that it is only activated if the cursor is on a certain object. That object is then passed into the handling method. Possible values are: - "FileNode" - a file node - "DirNode" - a directory node - "Node" - a file or directory node - "Bookmark" - A bookmark - "all" - the keymap is not constrained to any scope (default). When - thei is used, the handling function is not passed any arguments. + "FileNode" .... a file node + "DirNode" ..... a directory node + "Node" ........ a file node OR a directory node + "Bookmark" .... a bookmark + "all" ......... global scope; handler receives no arguments (default) Example: > call NERDTreeAddKeyMap({