nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
commit a45304832b5978b087afadda15ca605a81de4274
parent dd8fe4bf69a009b23df4be7c102c7e775b4d49e7
Author: Martin Grenfell <martin.grenfell@gmail.com>
Date:   Thu, 26 Jun 2014 09:35:02 +0100

Merge pull request #295 from DanielleSucher/create_parent_directories_as_needed

Create parent directories as needed when creating or copying nested nodes
Diffstat:
Mlib/nerdtree/path.vim | 17+++++++++++++++++
Mnerdtree_plugin/fs_menu.vim | 10++++++++--
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/lib/nerdtree/path.vim b/lib/nerdtree/path.vim
@@ -145,6 +145,7 @@ function! s:Path.Create(fullpath)
 
         "assume its a file and create
         else
+            call s:Path.createParentDirectories(a:fullpath)
             call writefile([], a:fullpath)
         endif
     catch
@@ -165,6 +166,8 @@ function! s:Path.copy(dest)
         throw "NERDTree.CopyingNotSupportedError: Copying is not supported on this OS"
     endif
 
+    call s:Path.createParentDirectories(a:dest)
+
     let dest = s:Path.WinToUnixPath(a:dest)
 
     let cmd = g:NERDTreeCopyCmd . " " . escape(self.str(), nerdtree#escChars()) . " " . escape(dest, nerdtree#escChars())
@@ -201,6 +204,20 @@ function! s:Path.copyingWillOverwrite(dest)
     endif
 endfunction
 
+"FUNCTION: Path.createParentDirectories(path) {{{1
+"
+"create parent directories for this path if needed
+"without throwing any errors is those directories already exist
+"
+"Args:
+"path: full path of the node whose parent directories may need to be created
+function! s:Path.createParentDirectories(path)
+    let dir_path = fnamemodify(a:path, ':h')
+    if !isdirectory(dir_path)
+        call mkdir(dir_path, 'p')
+    endif
+endfunction
+
 "FUNCTION: Path.delete() {{{1
 "
 "Deletes the file represented by this path.
diff --git a/nerdtree_plugin/fs_menu.vim b/nerdtree_plugin/fs_menu.vim
@@ -115,7 +115,10 @@ function! NERDTreeAddNode()
         let parentNode = b:NERDTreeRoot.findNode(newPath.getParent())
 
         let newTreeNode = g:NERDTreeFileNode.New(newPath)
-        if parentNode.isOpen || !empty(parentNode.children)
+        if empty(parentNode)
+            call b:NERDTreeRoot.refresh()
+            call nerdtree#renderView()
+        elseif parentNode.isOpen || !empty(parentNode.children)
             call parentNode.addChild(newTreeNode, 1)
             call NERDTreeRender()
             call newTreeNode.putCursorHere(1, 0)
@@ -225,7 +228,10 @@ function! NERDTreeCopyNode()
         if confirmed
             try
                 let newNode = currentNode.copy(newNodePath)
-                if !empty(newNode)
+                if empty(newNode)
+                    call b:NERDTreeRoot.refresh()
+                    call nerdtree#renderView()
+                else
                     call NERDTreeRender()
                     call newNode.putCursorHere(0, 0)
                 endif