Merge pull request #536 from satori/master

Add support for natural sorting order.
This commit is contained in:
Phil Runninger 2017-06-21 11:33:47 -04:00 committed by GitHub
commit 626e80f6e4
4 changed files with 78 additions and 10 deletions

View File

@ -59,9 +59,33 @@ endfunction
"FUNCTION: nerdtree#compareNodesBySortKey(n1, n2) {{{2
function! nerdtree#compareNodesBySortKey(n1, n2)
if a:n1.path.getSortKey() <# a:n2.path.getSortKey()
let sortKey1 = a:n1.path.getSortKey()
let sortKey2 = a:n2.path.getSortKey()
let i = 0
while i < min([len(sortKey1), len(sortKey2)])
" Compare chunks upto common length.
" If chunks have different type, the one which has
" integer type is the lesser.
if type(sortKey1[i]) == type(sortKey2[i])
if sortKey1[i] <# sortKey2[i]
return - 1
elseif sortKey1[i] ># sortKey2[i]
return 1
endif
elseif sortKey1[i] == type(0)
return -1
elseif sortKey2[i] == type(0)
return 1
endif
let i = i + 1
endwhile
" Keys are identical upto common length.
" The key which has smaller chunks is the lesser one.
if len(sortKey1) < len(sortKey2)
return -1
elseif a:n1.path.getSortKey() ># a:n2.path.getSortKey()
elseif len(sortKey1) > len(sortKey2)
return 1
else
return 0

View File

@ -623,6 +623,9 @@ NERD tree. These options should be set in your vimrc.
|'NERDTreeCaseSensitiveSort'| Tells the NERD tree whether to be case
sensitive or not when sorting nodes.
|'NERDTreeNaturalSort'| Tells the NERD tree whether to use
natural sort order or not when sorting nodes.
|'NERDTreeSortHiddenFirst'| Tells the NERD tree whether to take the dot
at the beginning of the hidden file names
into account when sorting nodes.
@ -746,6 +749,33 @@ account. The above nodes would then be sorted like this: >
blarg.c
boner.c
<
------------------------------------------------------------------------------
*'NERDTreeNaturalSort'*
Values: 0 or 1.
Default: 0.
By default the NERD tree does not sort nodes in natural sort order, i.e. nodes
could appear like this: >
z1.txt
z10.txt
z100.txt
z11.txt
z110.txt
z2.txt
z20.txt
z3.txt
<
But if you set this option to 1 then the natural sort order will be used. The
above nodes would then be sorted like this: >
z1.txt
z2.txt
z3.txt
z10.txt
z11.txt
z20.txt
z100.txt
z110.txt
<
------------------------------------------------------------------------------
*'NERDTreeChDirMode'*

View File

@ -1,12 +1,6 @@
"we need to use this number many times for sorting... so we calculate it only
"once here
let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*')
" used in formating sortKey, e.g. '%04d'
if exists("log10")
let s:sortKeyFormat = "%0" . float2nr(ceil(log10(len(g:NERDTreeSortOrder)))) . "d"
else
let s:sortKeyFormat = "%04d"
endif
"CLASS: Path
"============================================================
@ -368,8 +362,23 @@ function! s:Path.getSortOrderIndex()
return s:NERDTreeSortStarIndex
endfunction
"FUNCTION: Path._splitChunks(path) {{{1
"returns a list of path chunks
function! s:Path._splitChunks(path)
let chunks = split(a:path, '\(\D\+\|\d\+\)\zs')
let i = 0
while i < len(chunks)
"convert number literals to numbers
if match(chunks[i], '^\d\+$') == 0
let chunks[i] = str2nr(chunks[i])
endif
let i = i + 1
endwhile
return chunks
endfunction
"FUNCTION: Path.getSortKey() {{{1
"returns a string used in compare function for sorting
"returns a key used in compare function for sorting
function! s:Path.getSortKey()
if !exists("self._sortKey")
let path = self.getLastPathComponent(1)
@ -379,7 +388,11 @@ function! s:Path.getSortKey()
if !g:NERDTreeCaseSensitiveSort
let path = tolower(path)
endif
let self._sortKey = printf(s:sortKeyFormat, self.getSortOrderIndex()) . path
if !g:NERDTreeNaturalSort
let self._sortKey = [self.getSortOrderIndex(), path]
else
let self._sortKey = [self.getSortOrderIndex()] + self._splitChunks(path)
endif
endif
return self._sortKey

View File

@ -46,6 +46,7 @@ endfunction
call s:initVariable("g:NERDTreeAutoCenter", 1)
call s:initVariable("g:NERDTreeAutoCenterThreshold", 3)
call s:initVariable("g:NERDTreeCaseSensitiveSort", 0)
call s:initVariable("g:NERDTreeNaturalSort", 0)
call s:initVariable("g:NERDTreeSortHiddenFirst", 1)
call s:initVariable("g:NERDTreeChDirMode", 0)
call s:initVariable("g:NERDTreeCreatePrefix", "silent")