Add support for natural sorting order.

This commit is contained in:
Maxim Bublis 2016-01-19 00:21:13 -08:00
parent 4ebbb533c3
commit 6251ab1e63
4 changed files with 78 additions and 10 deletions

View File

@ -36,9 +36,33 @@ endfunction
"FUNCTION: nerdtree#compareNodesBySortKey(n1, n2) {{{2 "FUNCTION: nerdtree#compareNodesBySortKey(n1, n2) {{{2
function! nerdtree#compareNodesBySortKey(n1, n2) 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 return -1
elseif a:n1.path.getSortKey() ># a:n2.path.getSortKey() elseif len(sortKey1) > len(sortKey2)
return 1 return 1
else else
return 0 return 0

View File

@ -618,6 +618,9 @@ NERD tree. These options should be set in your vimrc.
|'NERDTreeCaseSensitiveSort'| Tells the NERD tree whether to be case |'NERDTreeCaseSensitiveSort'| Tells the NERD tree whether to be case
sensitive or not when sorting nodes. 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 |'NERDTreeSortHiddenFirst'| Tells the NERD tree whether to take the dot
at the beginning of the hidden file names at the beginning of the hidden file names
into account when sorting nodes. into account when sorting nodes.
@ -734,6 +737,33 @@ account. The above nodes would then be sorted like this: >
blarg.c blarg.c
boner.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'* *'NERDTreeChDirMode'*

View File

@ -1,12 +1,6 @@
"we need to use this number many times for sorting... so we calculate it only "we need to use this number many times for sorting... so we calculate it only
"once here "once here
let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*') 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 "CLASS: Path
"============================================================ "============================================================
@ -365,8 +359,23 @@ function! s:Path.getSortOrderIndex()
return s:NERDTreeSortStarIndex return s:NERDTreeSortStarIndex
endfunction 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 "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() function! s:Path.getSortKey()
if !exists("self._sortKey") if !exists("self._sortKey")
let path = self.getLastPathComponent(1) let path = self.getLastPathComponent(1)
@ -376,7 +385,11 @@ function! s:Path.getSortKey()
if !g:NERDTreeCaseSensitiveSort if !g:NERDTreeCaseSensitiveSort
let path = tolower(path) let path = tolower(path)
endif 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 endif
return self._sortKey return self._sortKey

View File

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