From 519436ad2047b108573788cb71f77ebdcd3d56d6 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Fri, 26 May 2017 08:27:14 -0400 Subject: [PATCH 1/4] Apply consistent spacing before fold markers The trailing fold markers in "bookmark.vim" varied in how far they were from the end of the line. This created an unpleasant visual effect when folding was in use. --- lib/nerdtree/bookmark.vim | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/nerdtree/bookmark.vim b/lib/nerdtree/bookmark.vim index 81242605..9ac4d270 100644 --- a/lib/nerdtree/bookmark.vim +++ b/lib/nerdtree/bookmark.vim @@ -24,7 +24,7 @@ function! s:Bookmark.AddBookmark(name, path) endif endfunction -" FUNCTION: Bookmark.Bookmarks() {{{1 +" FUNCTION: Bookmark.Bookmarks() {{{1 " Class method to get all bookmarks. Lazily initializes the bookmarks global " variable function! s:Bookmark.Bookmarks() @@ -34,7 +34,7 @@ function! s:Bookmark.Bookmarks() return g:NERDTreeBookmarks endfunction -" FUNCTION: Bookmark.BookmarkExistsFor(name) {{{1 +" FUNCTION: Bookmark.BookmarkExistsFor(name) {{{1 " class method that returns 1 if a bookmark with the given name is found, 0 " otherwise function! s:Bookmark.BookmarkExistsFor(name) @@ -46,7 +46,7 @@ function! s:Bookmark.BookmarkExistsFor(name) endtry 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 " bookmark is found function! s:Bookmark.BookmarkFor(name) @@ -58,7 +58,7 @@ function! s:Bookmark.BookmarkFor(name) throw "NERDTree.BookmarkNotFoundError: no bookmark found for name: \"". a:name .'"' endfunction -" FUNCTION: Bookmark.BookmarkNames() {{{1 +" FUNCTION: Bookmark.BookmarkNames() {{{1 " Class method to return an array of all bookmark names function! s:Bookmark.BookmarkNames() let names = [] @@ -170,7 +170,7 @@ function! s:Bookmark.GetSelected() return {} endfunction -" FUNCTION: Bookmark.InvalidBookmarks() {{{1 +" FUNCTION: Bookmark.InvalidBookmarks() {{{1 " Class method to get all invalid bookmark strings read from the bookmarks " file function! s:Bookmark.InvalidBookmarks() @@ -233,20 +233,20 @@ function! s:Bookmark.openInNewTab(options) call self.open(a:options) endfunction -" FUNCTION: Bookmark.setPath(path) {{{1 +" FUNCTION: Bookmark.setPath(path) {{{1 " makes this bookmark point to the given path function! s:Bookmark.setPath(path) let self.path = a:path endfunction -" FUNCTION: Bookmark.Sort() {{{1 +" FUNCTION: Bookmark.Sort() {{{1 " Class method that sorts all bookmarks function! s:Bookmark.Sort() let CompareFunc = function("nerdtree#compareBookmarks") call sort(s:Bookmark.Bookmarks(), CompareFunc) endfunction -" FUNCTION: Bookmark.str() {{{1 +" FUNCTION: Bookmark.str() {{{1 " Get the string that should be rendered in the view for this bookmark function! s:Bookmark.str() let pathStrMaxLen = winwidth(g:NERDTree.GetWinNum()) - 4 - len(self.name) @@ -292,7 +292,7 @@ function! s:Bookmark.validate() endif endfunction -" FUNCTION: Bookmark.Write() {{{1 +" FUNCTION: Bookmark.Write() {{{1 " Class method to write all bookmarks to the bookmarks file function! s:Bookmark.Write() let bookmarkStrings = [] From 5aec5ecfef767de0bd1de168509a5aae3cdf8c22 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Fri, 26 May 2017 08:48:38 -0400 Subject: [PATCH 2/4] Rename the class method for sorting bookmarks A more intention-revealing name was chosen for the script-local sorting function. The function comment was also rewritten. --- lib/nerdtree/bookmark.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/nerdtree/bookmark.vim b/lib/nerdtree/bookmark.vim index 9ac4d270..9ca8f6f5 100644 --- a/lib/nerdtree/bookmark.vim +++ b/lib/nerdtree/bookmark.vim @@ -20,7 +20,7 @@ function! s:Bookmark.AddBookmark(name, path) endfor call add(s:Bookmark.Bookmarks(), s:Bookmark.New(a:name, a:path)) if g:NERDTreeBookmarksSort ==# 1 - call s:Bookmark.Sort() + call s:Bookmark.SortBookmarksList() endif endfunction @@ -105,7 +105,7 @@ function! s:Bookmark.CacheBookmarks(silent) endif endif if g:NERDTreeBookmarksSort ==# 1 - call s:Bookmark.Sort() + call s:Bookmark.SortBookmarksList() endif endif endfunction @@ -239,9 +239,9 @@ function! s:Bookmark.setPath(path) let self.path = a:path endfunction -" FUNCTION: Bookmark.Sort() {{{1 -" Class method that sorts all bookmarks -function! s:Bookmark.Sort() +" FUNCTION: Bookmark.SortBookmarksList() {{{1 +" Class method that sorts the global list of bookmarks by name. +function! s:Bookmark.SortBookmarksList() let CompareFunc = function("nerdtree#compareBookmarks") call sort(s:Bookmark.Bookmarks(), CompareFunc) endfunction From 0b650891222918011ae7d4f823943a1fa45d5854 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Fri, 26 May 2017 13:23:05 -0400 Subject: [PATCH 3/4] Reimplement the bookmark comparison method 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"). --- autoload/nerdtree.vim | 5 ----- doc/NERD_tree.txt | 10 +++++++--- lib/nerdtree/bookmark.vim | 35 ++++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/autoload/nerdtree.vim b/autoload/nerdtree.vim index 63fa5c13..7c5db89d 100644 --- a/autoload/nerdtree.vim +++ b/autoload/nerdtree.vim @@ -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) diff --git a/doc/NERD_tree.txt b/doc/NERD_tree.txt index 3b7b9504..6cbdc613 100644 --- a/doc/NERD_tree.txt +++ b/doc/NERD_tree.txt @@ -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'* diff --git a/lib/nerdtree/bookmark.vim b/lib/nerdtree/bookmark.vim index 9ca8f6f5..8bd95fe1 100644 --- a/lib/nerdtree/bookmark.vim +++ b/lib/nerdtree/bookmark.vim @@ -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 @@ -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 + 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() @@ -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 From 2a7a375729dc04bfcbb0411d00839e5349f59193 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Fri, 26 May 2017 13:46:09 -0400 Subject: [PATCH 4/4] Remove code duplication from sorting bookmarks It makes the most sense to sort the global bookmarks list just before rendering them in the NERDTree window. Since Vim's sort function is fast and stable, and since users are very unlikely to have a number of bookmarks that is too large, we can sort before rendering without concern for the negligible performance penalty. This has two benefits: 1. Users can change their sort settings and have them take effect on the next render or refresh. 2. As mentioned, code duplication is avoided. --- lib/nerdtree/bookmark.vim | 6 ------ lib/nerdtree/ui.vim | 4 ++++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/nerdtree/bookmark.vim b/lib/nerdtree/bookmark.vim index 8bd95fe1..ad8c3d1d 100644 --- a/lib/nerdtree/bookmark.vim +++ b/lib/nerdtree/bookmark.vim @@ -19,9 +19,6 @@ function! s:Bookmark.AddBookmark(name, path) endif endfor call add(s:Bookmark.Bookmarks(), s:Bookmark.New(a:name, a:path)) - if g:NERDTreeBookmarksSort == 1 || g:NERDTreeBookmarksSort == 2 - call s:Bookmark.SortBookmarksList() - endif endfunction " FUNCTION: Bookmark.Bookmarks() {{{1 @@ -104,9 +101,6 @@ function! s:Bookmark.CacheBookmarks(silent) call nerdtree#echo(invalidBookmarksFound . " invalid bookmarks were read. See :help NERDTreeInvalidBookmarks for info.") endif endif - if g:NERDTreeBookmarksSort == 1 || g:NERDTreeBookmarksSort == 2 - call s:Bookmark.SortBookmarksList() - endif endif endfunction diff --git a/lib/nerdtree/ui.vim b/lib/nerdtree/ui.vim index 65ebfd91..e169dbbf 100644 --- a/lib/nerdtree/ui.vim +++ b/lib/nerdtree/ui.vim @@ -313,6 +313,10 @@ function! s:UI._renderBookmarks() call cursor(line(".")+1, col(".")) endif + if g:NERDTreeBookmarksSort == 1 || g:NERDTreeBookmarksSort == 2 + call g:NERDTreeBookmark.SortBookmarksList() + endif + for i in g:NERDTreeBookmark.Bookmarks() call setline(line(".")+1, i.str()) call cursor(line(".")+1, col("."))