nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
commit 15d06b676dfcd92ac9a0bc375668d127f9822539
parent 129a241b22928257afd0ce45d00e19111f03453f
Author: Phil Runninger <PhilRunninger@users.noreply.github.com>
Date:   Mon, 10 Sep 2018 09:27:52 -0400

Merge pull request #878 from scrooloose/autochdir_interference

NERDTreeCWD: reset CWD if changed by NERDTreeFocus
Diffstat:
Mautoload/nerdtree/ui_glue.vim | 13++-----------
Mdoc/NERDTree.txt | 8++++----
Mplugin/NERD_tree.vim | 22+++++++++++++++++++++-
3 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim
@@ -142,18 +142,9 @@ function! s:chRoot(node)
 endfunction
 
 " FUNCTION: s:nerdtree#ui_glue#chRootCwd() {{{1
-" changes the current root to CWD
+" Change the NERDTree root to match the current working directory.
 function! nerdtree#ui_glue#chRootCwd()
-    try
-        let cwd = g:NERDTreePath.New(getcwd())
-    catch /^NERDTree.InvalidArgumentsError/
-        call nerdtree#echo("current directory does not exist.")
-        return
-    endtry
-    if cwd.str() == g:NERDTreeFileNode.GetRootForTab().path.str()
-       return
-    endif
-    call s:chRoot(g:NERDTreeDirNode.New(cwd, b:NERDTree))
+    NERDTreeCWD
 endfunction
 
 " FUNCTION: nnerdtree#ui_glue#clearBookmarks(bookmarks) {{{1
diff --git a/doc/NERDTree.txt b/doc/NERDTree.txt
@@ -142,8 +142,8 @@ The following features and functionality are provided by the NERD tree:
     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
-    tab, a new tree will be opened.
+    Change the NERDTree root to the current working directory.  If no
+    NERDTree exists for this tab, a new one is opened.
 
 ------------------------------------------------------------------------------
 2.2. Bookmarks                                             *NERDTreeBookmarks*
@@ -522,7 +522,7 @@ Default key: cd
 Map option: NERDTreeMapChdir
 Applies to: files and directories.
 
-Change vims current working directory to that of the selected node.
+Change Vim's current working directory to that of the selected node.
 
 ------------------------------------------------------------------------------
                                                                  *NERDTree-CD*
@@ -530,7 +530,7 @@ Default key: CD
 Map option: NERDTreeMapCWD
 Applies to: no restrictions.
 
-Change tree root to vims current working directory.
+Change the NERDTree root to Vim's current working directory.
 
 ------------------------------------------------------------------------------
                                                                   *NERDTree-I*
diff --git a/plugin/NERD_tree.vim b/plugin/NERD_tree.vim
@@ -202,8 +202,28 @@ function! NERDTreeFocus()
 endfunction
 
 function! NERDTreeCWD()
+
+    if empty(getcwd())
+        call nerdtree#echoWarning('current directory does not exist')
+        return
+    endif
+
+    try
+        let l:cwdPath = g:NERDTreePath.New(getcwd())
+    catch /^NERDTree.InvalidArgumentsError/
+        call nerdtree#echoWarning('current directory does not exist')
+        return
+    endtry
+
     call NERDTreeFocus()
-    call nerdtree#ui_glue#chRootCwd()
+
+    if b:NERDTree.root.path.equals(l:cwdPath)
+        return
+    endif
+
+    let l:newRoot = g:NERDTreeFileNode.New(l:cwdPath, b:NERDTree)
+    call b:NERDTree.changeRoot(l:newRoot)
+    normal! ^
 endfunction
 
 function! NERDTreeAddPathFilter(callback)