Merge pull request #703 from lifecrisis/issue677

BUGFIX: Repair the broken ":OpenBookmark" command
This commit is contained in:
Phil Runninger 2017-06-11 23:25:09 -04:00 committed by GitHub
commit 9bd34f9424
4 changed files with 56 additions and 37 deletions

View File

@ -441,21 +441,19 @@ function! s:jumpToSibling(currentNode, forward)
endfunction endfunction
" FUNCTION: nerdtree#ui_glue#openBookmark(name) {{{1 " FUNCTION: nerdtree#ui_glue#openBookmark(name) {{{1
" put the cursor on the given bookmark and, if its a file, open it " Open the Bookmark that has the specified name. This function provides the
" implementation for the ":OpenBookmark" command.
function! nerdtree#ui_glue#openBookmark(name) function! nerdtree#ui_glue#openBookmark(name)
try try
let targetNode = g:NERDTreeBookmark.GetNodeForName(a:name, 0, b:NERDTree) let l:bookmark = g:NERDTreeBookmark.BookmarkFor(a:name)
call targetNode.putCursorHere(0, 1) catch /^NERDTree.BookmarkNotFoundError/
redraw! call nerdtree#echoError('bookmark "' . a:name . '" not found')
catch /^NERDTree.BookmarkedNodeNotFoundError/ return
call nerdtree#echo("note - target node is not cached")
let bookmark = g:NERDTreeBookmark.BookmarkFor(a:name)
let targetNode = g:NERDTreeFileNode.New(bookmark.path, b:NERDTree)
endtry endtry
if targetNode.path.isDirectory if l:bookmark.path.isDirectory
call targetNode.openExplorer() call l:bookmark.open(b:NERDTree)
else else
call targetNode.open({'where': 'p'}) call l:bookmark.open(b:NERDTree, {'where': 'p'})
endif endif
endfunction endfunction

View File

@ -158,7 +158,7 @@ click bookmarks or use the |NERDTree-o| mapping to activate them. See also,
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
2.2.2. Bookmark commands *NERDTreeBookmarkCommands* 2.2.2. Bookmark commands *NERDTreeBookmarkCommands*
Note that the following commands are only available in the NERD tree buffer. Note: The following commands are only available within the NERDTree buffer.
:Bookmark [<name>] :Bookmark [<name>]
Bookmark the current node as <name>. If there is already a <name> Bookmark the current node as <name>. If there is already a <name>
@ -178,10 +178,11 @@ Note that the following commands are only available in the NERD tree buffer.
(i.e. directory nodes above it will be opened) and the cursor will be (i.e. directory nodes above it will be opened) and the cursor will be
placed on it. placed on it.
:OpenBookmark <bookmark> :OpenBookmark <name>
<bookmark> must point to a file. The file is opened as though |NERDTree-o| The Bookmark named <name> is opened as if |NERDTree-o| was applied to
was applied. If the node is cached under the current root then it will be its entry in the Bookmark table. If the Bookmark points to a directory,
revealed and the cursor will be placed on it. it is made the new root of the current NERDTree. If the Bookmark points
to a file, that file is opened for editing in another window.
:ClearBookmarks [<bookmarks>] :ClearBookmarks [<bookmarks>]
Remove all the given bookmarks. If no bookmarks are given then remove all Remove all the given bookmarks. If no bookmarks are given then remove all

View File

@ -1,5 +1,13 @@
"CLASS: Bookmark " ============================================================================
"============================================================ " CLASS: Bookmark
"
" The Bookmark class serves two purposes:
" (1) It is the top-level prototype for new, concrete Bookmark objects.
" (2) It provides an interface for client code to query and manipulate the
" global list of Bookmark objects within the current Vim session.
" ============================================================================
let s:Bookmark = {} let s:Bookmark = {}
let g:NERDTreeBookmark = s:Bookmark let g:NERDTreeBookmark = s:Bookmark
@ -44,15 +52,20 @@ function! s:Bookmark.BookmarkExistsFor(name)
endfunction endfunction
" FUNCTION: Bookmark.BookmarkFor(name) {{{1 " FUNCTION: Bookmark.BookmarkFor(name) {{{1
" Class method to get the bookmark that has the given name. {} is return if no " Class method that returns the Bookmark object having the specified name.
" bookmark is found " Throws "NERDTree.BookmarkNotFoundError" if no Bookmark is found.
function! s:Bookmark.BookmarkFor(name) function! s:Bookmark.BookmarkFor(name)
for i in s:Bookmark.Bookmarks() let l:result = {}
if i.name ==# a:name for l:bookmark in s:Bookmark.Bookmarks()
return i if l:bookmark.name ==# a:name
let l:result = l:bookmark
break
endif endif
endfor endfor
throw "NERDTree.BookmarkNotFoundError: no bookmark found for name: \"". a:name .'"' if empty(l:result)
throw 'NERDTree.BookmarkNotFoundError: "' . a:name . '" not found'
endif
return l:result
endfunction endfunction
" FUNCTION: Bookmark.BookmarkNames() {{{1 " FUNCTION: Bookmark.BookmarkNames() {{{1
@ -144,26 +157,33 @@ function! s:Bookmark.delete()
endfunction endfunction
" FUNCTION: Bookmark.getNode(nerdtree, searchFromAbsoluteRoot) {{{1 " FUNCTION: Bookmark.getNode(nerdtree, searchFromAbsoluteRoot) {{{1
" Gets the treenode for this bookmark " Returns the tree node object associated with this Bookmark.
" Throws "NERDTree.BookmarkedNodeNotFoundError" if the node is not found.
" "
" Args: " Args:
" searchFromAbsoluteRoot: specifies whether we should search from the current " searchFromAbsoluteRoot: boolean flag, search from the highest cached node
" tree root, or the highest cached node " if true and from the current tree root if false
function! s:Bookmark.getNode(nerdtree, searchFromAbsoluteRoot) function! s:Bookmark.getNode(nerdtree, searchFromAbsoluteRoot)
let searchRoot = a:searchFromAbsoluteRoot ? a:nerdtree.root.AbsoluteTreeRoot() : a:nerdtree.root if a:searchFromAbsoluteRoot
let targetNode = searchRoot.findNode(self.path) let l:searchRoot = a:nerdtree.root.AbsoluteTreeRoot()
if empty(targetNode) else
throw "NERDTree.BookmarkedNodeNotFoundError: no node was found for bookmark: " . self.name let l:searchRoot = a:nerdtree.root
endif endif
return targetNode let l:targetNode = l:searchRoot.findNode(self.path)
if empty(l:targetNode)
throw 'NERDTree.BookmarkedNodeNotFoundError: node for bookmark "' . self.name . '" not found'
endif
return l:targetNode
endfunction endfunction
" FUNCTION: Bookmark.GetNodeForName(name, searchFromAbsoluteRoot, nerdtree) {{{1 " FUNCTION: Bookmark.GetNodeForName(name, searchFromAbsoluteRoot, nerdtree) {{{1
" Class method that finds the bookmark with the given name and returns the " Class method that returns the tree node object for the Bookmark with the
" treenode for it. " given name. Throws "NERDTree.BookmarkNotFoundError" if a Bookmark with the
" name does not exist. Throws "NERDTree.BookmarkedNodeNotFoundError" if a
" tree node for the named Bookmark could not be found.
function! s:Bookmark.GetNodeForName(name, searchFromAbsoluteRoot, nerdtree) function! s:Bookmark.GetNodeForName(name, searchFromAbsoluteRoot, nerdtree)
let bookmark = s:Bookmark.BookmarkFor(a:name) let l:bookmark = s:Bookmark.BookmarkFor(a:name)
return bookmark.getNode(nerdtree, a:searchFromAbsoluteRoot) return l:bookmark.getNode(a:nerdtree, a:searchFromAbsoluteRoot)
endfunction endfunction
" FUNCTION: Bookmark.GetSelected() {{{1 " FUNCTION: Bookmark.GetSelected() {{{1

View File

@ -14,7 +14,7 @@ function! s:Creator._bindMappings()
command! -buffer -nargs=? Bookmark :call nerdtree#ui_glue#bookmarkNode('<args>') command! -buffer -nargs=? Bookmark :call nerdtree#ui_glue#bookmarkNode('<args>')
command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=1 RevealBookmark :call nerdtree#ui_glue#revealBookmark('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=1 RevealBookmark :call nerdtree#ui_glue#revealBookmark('<args>')
command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=1 OpenBookmark :call nerdtree#ui_glue#openBookmark('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=1 OpenBookmark call nerdtree#ui_glue#openBookmark('<args>')
command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=* ClearBookmarks call nerdtree#ui_glue#clearBookmarks('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=* ClearBookmarks call nerdtree#ui_glue#clearBookmarks('<args>')
command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=+ BookmarkToRoot call g:NERDTreeBookmark.ToRoot('<args>', b:NERDTree) command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=+ BookmarkToRoot call g:NERDTreeBookmark.ToRoot('<args>', b:NERDTree)
command! -buffer -nargs=0 ClearAllBookmarks call g:NERDTreeBookmark.ClearAll() <bar> call b:NERDTree.render() command! -buffer -nargs=0 ClearAllBookmarks call g:NERDTreeBookmark.ClearAll() <bar> call b:NERDTree.render()