Skip to content

Commit

Permalink
Reimplement the bookmark comparison method
Browse files Browse the repository at this point in the history
Sorting the list of user bookmarks requires care to ensure that Vim's
builtin sort function is called correctly. Previously, this function was
called incorrectly. This is why the sorting of bookmarks never worked.

The offending functions have been removed here and replaced with
"s:Bookmark.CompareBookmarksByName". To understand the necessity for
this change, read ":h sort()" for the requirements of the function
reference argument (esp., note that it must return -1, 0, or 1).

In addition to fixing this problem, the new comparison function will
inspect the "g:NERDTreeBookmarksSort" setting to determine whether
case-sensitivity is preferred in the sort. The documentation has been
modified to accurately reflect this adjustment. The change is also made
in such a way as not to break any existing configurations.

Fixes #361 ("My bookmarks aren't sorted").
  • Loading branch information
Jason Franklin committed May 26, 2017
1 parent 5aec5ec commit 0b65089
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
5 changes: 0 additions & 5 deletions autoload/nerdtree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ function! nerdtree#completeBookmarks(A,L,P)
return filter(g:NERDTreeBookmark.BookmarkNames(), 'v:val =~# "^' . a:A . '"')
endfunction

"FUNCTION: nerdtree#compareBookmarks(dir) {{{2
function! nerdtree#compareBookmarks(first, second)
return a:first.compareTo(a:second)
endfunction

"FUNCTION: nerdtree#compareNodes(dir) {{{2
function! nerdtree#compareNodes(n1, n2)
return a:n1.path.compareTo(a:n2.path)
Expand Down
10 changes: 7 additions & 3 deletions doc/NERD_tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,15 @@ This is where bookmarks are saved. See |NERDTreeBookmarkCommands|.

------------------------------------------------------------------------------
*'NERDTreeBookmarksSort'*
Values: 0 or 1
Values: 0, 1, or 2
Default: 1

If set to 0 then the bookmarks list is not sorted.
If set to 1 the bookmarks list is sorted.
This option controls the method by which the list of user bookmarks is
sorted. When sorted, bookmarks will render in alphabetical order by name.

If set to 0, the bookmarks list is not sorted.
If set to 1, the bookmarks list is sorted in a case-insensitive manner.
If set to 2, the bookmarks list is sorted in a case-sensitive manner.

------------------------------------------------------------------------------
*'NERDTreeMouseMode'*
Expand Down
35 changes: 26 additions & 9 deletions lib/nerdtree/bookmark.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function! s:Bookmark.AddBookmark(name, path)
endif
endfor
call add(s:Bookmark.Bookmarks(), s:Bookmark.New(a:name, a:path))
if g:NERDTreeBookmarksSort ==# 1
if g:NERDTreeBookmarksSort == 1 || g:NERDTreeBookmarksSort == 2
call s:Bookmark.SortBookmarksList()
endif
endfunction
Expand Down Expand Up @@ -104,17 +104,34 @@ function! s:Bookmark.CacheBookmarks(silent)
call nerdtree#echo(invalidBookmarksFound . " invalid bookmarks were read. See :help NERDTreeInvalidBookmarks for info.")
endif
endif
if g:NERDTreeBookmarksSort ==# 1
if g:NERDTreeBookmarksSort == 1 || g:NERDTreeBookmarksSort == 2
call s:Bookmark.SortBookmarksList()
endif
endif
endfunction

" FUNCTION: Bookmark.compareTo(otherbookmark) {{{1
" Compare these two bookmarks for sorting purposes
function! s:Bookmark.compareTo(otherbookmark)
return a:otherbookmark.name < self.name
" FUNCTION: Bookmark.CompareBookmarksByName(firstBookmark, secondBookmark) {{{1
" Class method that indicates the relative position of two bookmarks when
" placed in alphabetical order by name. Case-sensitivity is determined by an
" option. Supports the "s:Bookmark.SortBookmarksList()" method.
function! s:Bookmark.CompareBookmarksByName(firstBookmark, secondBookmark)
let l:result = 0
if g:NERDTreeBookmarksSort == 1
if a:firstBookmark.name <? a:secondBookmark.name
let l:result = -1
elseif a:firstBookmark.name >? a:secondBookmark.name
let l:result = 1
endif
elseif g:NERDTreeBookmarksSort == 2
if a:firstBookmark.name <# a:secondBookmark.name
let l:result = -1
elseif a:firstBookmark.name ># a:secondBookmark.name
let l:result = 1
endif
endif
return l:result
endfunction

" FUNCTION: Bookmark.ClearAll() {{{1
" Class method to delete all bookmarks.
function! s:Bookmark.ClearAll()
Expand Down Expand Up @@ -240,10 +257,10 @@ function! s:Bookmark.setPath(path)
endfunction

" FUNCTION: Bookmark.SortBookmarksList() {{{1
" Class method that sorts the global list of bookmarks by name.
" Class method that sorts the global list of bookmarks alphabetically by name.
" Note that case-sensitivity is determined by a user option.
function! s:Bookmark.SortBookmarksList()
let CompareFunc = function("nerdtree#compareBookmarks")
call sort(s:Bookmark.Bookmarks(), CompareFunc)
call sort(s:Bookmark.Bookmarks(), s:Bookmark.CompareBookmarksByName)
endfunction

" FUNCTION: Bookmark.str() {{{1
Expand Down

0 comments on commit 0b65089

Please sign in to comment.