diff --git a/autoload/nerdtree.vim b/autoload/nerdtree.vim index 22283aa..0a7e4c7 100644 --- a/autoload/nerdtree.vim +++ b/autoload/nerdtree.vim @@ -34,6 +34,17 @@ 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) + 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 diff --git a/lib/nerdtree/tree_dir_node.vim b/lib/nerdtree/tree_dir_node.vim index a24c270..9a7f45a 100644 --- a/lib/nerdtree/tree_dir_node.vim +++ b/lib/nerdtree/tree_dir_node.vim @@ -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