Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Allow ":NERDTreeFind" to reveal new files #785

Merged
merged 6 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 26 additions & 32 deletions autoload/nerdtree/ui_glue.vim
Original file line number Diff line number Diff line change
Expand Up @@ -261,61 +261,55 @@ 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 p = 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

if 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 = p.getParent()
call nerdtree#echo('current directory does not exist.')
let l:cwd = l:pathObj.getParent()
endtry

if 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(p.getParent().str())
call g:NERDTreeCreator.CreateTabTree(l:pathObj.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:pathObj.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:pathObj.getParent(), b:NERDTree))
endif
endif
call g:NERDTree.CursorToTreeWin()
let node = b:NERDTree.root.reveal(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 p.isUnixHiddenFile()
let g:NERDTreeShowHidden = showhidden
if l:pathObj.isUnixHiddenFile()
let g:NERDTreeShowHidden = l:showHidden
endif
endfunction

Expand Down
29 changes: 14 additions & 15 deletions doc/NERDTree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<path>] *:NERDTreeFind*
Without the optional argument, find and reveal the file for the active
buffer in the NERDTree window. With the <path> 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
Expand Down Expand Up @@ -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({
Expand Down
7 changes: 7 additions & 0 deletions lib/nerdtree/tree_dir_node.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down