Skip to content

Commit

Permalink
make cascading dirs more compact
Browse files Browse the repository at this point in the history
Render cascading dirs on one line i.e.

    > foo/bar/baz
      file1

instead of

    > foo
      > bar
        > baz
          > file1

This should be useful things like java projects that have deep dir
structures.

Remove the old UI view (pre the dir arrows) as this simply isnt worth
supporting for a proof of concept. This may get added back - or not.
  • Loading branch information
scrooloose committed Nov 24, 2015
1 parent ee4d42c commit 2cef8bb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 61 deletions.
25 changes: 25 additions & 0 deletions lib/nerdtree/tree_dir_node.vim
Expand Up @@ -73,6 +73,24 @@ function! s:TreeDirNode.createChild(path, inOrder)
return newTreeNode
endfunction

"FUNCTION: TreeDirNode.displayString() {{{1
unlet s:TreeDirNode.displayString
function! s:TreeDirNode.displayString()
let vc = self.getVisibleChildren()
if len(vc) != 1
return self.path.displayString()
endif

let visChild = vc[0]

"TODO: optimize
if !visChild.path.isDirectory
return self.path.displayString()
endif

return self.path.getLastPathComponent(1) . visChild.displayString()
endfunction

"FUNCTION: TreeDirNode.findNode(path) {{{1
"Will find one of the children (recursively) that has the given path
"
Expand Down Expand Up @@ -218,6 +236,13 @@ function! s:TreeDirNode.hasVisibleChildren()
return self.getVisibleChildCount() != 0
endfunction

"FUNCTION: TreeDirNode.isCascadable() {{{1
"true if this dir has only one visible child - which is also a dir
function! s:TreeDirNode.isCascadable()
let c = self.getVisibleChildren()
return len(c) == 1 && c[0].path.isDirectory
endfunction

"FUNCTION: TreeDirNode._initChildren() {{{1
"Removes all childen from this node and re-reads them
"
Expand Down
76 changes: 15 additions & 61 deletions lib/nerdtree/tree_file_node.vim
Expand Up @@ -323,70 +323,27 @@ endfunction
"FUNCTION: TreeFileNode.renderToString {{{1
"returns a string representation for this tree to be rendered in the view
function! s:TreeFileNode.renderToString()
return self._renderToString(0, 0, [], self.getChildCount() ==# 1)
return self._renderToString(0, 0)
endfunction

"Args:
"depth: the current depth in the tree for this call
"drawText: 1 if we should actually draw the line for this node (if 0 then the
"child nodes are rendered only)
"vertMap: a binary array that indicates whether a vertical bar should be draw
"for each depth in the tree
"isLastChild:true if this curNode is the last child of its parent
function! s:TreeFileNode._renderToString(depth, drawText, vertMap, isLastChild)
function! s:TreeFileNode._renderToString(depth, drawText)
let output = ""
if a:drawText ==# 1

let treeParts = ''

"get all the leading spaces and vertical tree parts for this line
if a:depth > 1
for j in a:vertMap[0:-2]
if g:NERDTreeDirArrows
let treeParts = treeParts . ' '
else
if j ==# 1
let treeParts = treeParts . '| '
else
let treeParts = treeParts . ' '
endif
endif
endfor
endif

"get the last vertical tree part for this line which will be different
"if this node is the last child of its parent
if !g:NERDTreeDirArrows
if a:isLastChild
let treeParts = treeParts . '`'
else
let treeParts = treeParts . '|'
endif
endif
let treeParts = repeat(' ', a:depth - 1)

"smack the appropriate dir/file symbol on the line before the file/dir
"name itself
if self.path.isDirectory
if self.isOpen
if g:NERDTreeDirArrows
let treeParts = treeParts . g:NERDTreeDirArrowCollapsible . ' '
else
let treeParts = treeParts . '~'
endif
else
if g:NERDTreeDirArrows
let treeParts = treeParts . g:NERDTreeDirArrowExpandable . ' '
else
let treeParts = treeParts . '+'
endif
endif
let sym = self.isOpen ? g:NERDTreeDirArrowCollapsible : g:NERDTreeDirArrowExpandable
let treeParts = treeParts . sym . ' '
else
if g:NERDTreeDirArrows
let treeParts = treeParts . ' '
else
let treeParts = treeParts . '-'
endif
let treeParts = treeParts . ' '
endif

let line = treeParts . self.displayString()

let output = output . line . "\n"
Expand All @@ -396,18 +353,15 @@ function! s:TreeFileNode._renderToString(depth, drawText, vertMap, isLastChild)
if self.path.isDirectory ==# 1 && self.isOpen ==# 1

let childNodesToDraw = self.getVisibleChildren()
if len(childNodesToDraw) > 0

"draw all the nodes children except the last
let lastIndx = len(childNodesToDraw)-1
if lastIndx > 0
for i in childNodesToDraw[0:lastIndx-1]
let output = output . i._renderToString(a:depth + 1, 1, add(copy(a:vertMap), 1), 0)
endfor
endif

"draw the last child, indicating that it IS the last
let output = output . childNodesToDraw[lastIndx]._renderToString(a:depth + 1, 1, add(copy(a:vertMap), 0), 1)
if self.isCascadable() && a:depth > 0

let output = output . childNodesToDraw[0]._renderToString(a:depth, 0)

elseif len(childNodesToDraw) > 0
for i in childNodesToDraw
let output = output . i._renderToString(a:depth + 1, 1)
endfor
endif
endif

Expand Down

4 comments on commit 2cef8bb

@digitaltoad
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to turn this off? Its now much harder to do any type of directory modifications. Something like moving a parent directory with a single child seems impossible now. Unless I've missed something.

@vsushkov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@digitaltoad it looks like no, there's no such config

@ivalkeen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really makes directory modifications harder and also causes some weird behaviour when NERDTree is used as netrw replacement. Would be very nice to have a way to disable this behaviour.

@juanibiapina
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a pull request to disable this.

Please sign in to comment.