mirror of
https://github.com/preservim/nerdtree.git
synced 2024-11-29 04:33:40 +08:00
Speed up sortChildren() by using sorting token
This improves the sorting functions from 12 seconds to 0.66 seconds for ~4000 objects
This commit is contained in:
parent
a87b1bf3c5
commit
1f2531ec3a
|
@ -34,6 +34,18 @@ function! nerdtree#compareNodes(n1, n2)
|
|||
return a:n1.path.compareTo(a:n2.path)
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#compareNodesBySortingToken(n1, n2) {{{2
|
||||
function! nerdtree#compareNodesBySortingToken(n1, n2)
|
||||
return a:n1.path.compareTo(a:n2.path)
|
||||
if a:n1.sorting_token < a:n2.sorting_token
|
||||
return -1
|
||||
elseif a:n1.sorting_token > a:n2.sorting_token
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" FUNCTION: nerdtree#deprecated(func, [msg]) {{{2
|
||||
" Issue a deprecation warning for a:func. If a second arg is given, use this
|
||||
" as the deprecation message
|
||||
|
|
|
@ -504,7 +504,24 @@ endfunction
|
|||
"directory priority.
|
||||
"
|
||||
function! s:TreeDirNode.sortChildren()
|
||||
let CompareFunc = function("nerdtree#compareNodes")
|
||||
let CompareFunc = function("nerdtree#compareNodesBySortingToken")
|
||||
" To optimize sorting, let's generate the sorting token for comparison
|
||||
|
||||
" calculate how large number is needed to represent " order index
|
||||
let digit = ceil(log10(len(g:NERDTreeSortOrder)))
|
||||
let format = "%0" . float2nr(digit) . "d" " e.g. '%04d'
|
||||
|
||||
for child in self.children
|
||||
let path = child.path.getLastPathComponent(1)
|
||||
if !g:NERDTreeSortHiddenFirst
|
||||
let path = substitute(path, '^[._]', '', '')
|
||||
endif
|
||||
if !g:NERDTreeCaseSensitiveSort
|
||||
let path = tolower(path)
|
||||
endif
|
||||
let child.sorting_token = printf(format, child.path.getSortOrderIndex()) . path
|
||||
endfor
|
||||
|
||||
call sort(self.children, CompareFunc)
|
||||
endfunction
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user