Refactor sort comparison functions, removing redundancy (#1166)

* Add a function to compare path objects.

* Remove redundant node comparison function, and rename the ones left.

* Remove the compareTo function in the Path object.

Use nerdtree#compareNodePaths(p1,p2) instead. There was no need for two
comparison functions that do the same thing. They were a little
different in their details, but that shouldn't be the case. Having only
one such function makes better sense and is easier to maintain.

* Update version number in change log.
This commit is contained in:
Phil Runninger 2020-08-14 18:06:03 -04:00 committed by GitHub
parent 6b5d70e5bf
commit 23000acd7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 52 deletions

View File

@ -5,6 +5,7 @@
- **.PATCH**: Pull Request Title (PR Author) [PR Number](Link to PR) - **.PATCH**: Pull Request Title (PR Author) [PR Number](Link to PR)
--> -->
#### 6.9 #### 6.9
- **.8**: Refactor sort comparison functions, removing redundancy (PhilRunninger) [#1166](https://github.com/preservim/nerdtree/pull/1166)
- **.7**: Fix argument of `exists()` function calls checking for autocommands. (PhilRunninger) [#1165](https://github.com/preservim/nerdtree/pull/1165) - **.7**: Fix argument of `exists()` function calls checking for autocommands. (PhilRunninger) [#1165](https://github.com/preservim/nerdtree/pull/1165)
- **.6**: Don't use silent when raising User events (PhilRunninger) [#1164](https://github.com/preservim/nerdtree/pull/1164) - **.6**: Don't use silent when raising User events (PhilRunninger) [#1164](https://github.com/preservim/nerdtree/pull/1164)
- **.5**: Fix highlight for file node. (pirey) [#1157](https://github.com/preservim/nerdtree/pull/1157) - **.5**: Fix highlight for file node. (pirey) [#1157](https://github.com/preservim/nerdtree/pull/1157)

View File

@ -110,15 +110,15 @@ function! nerdtree#completeBookmarks(A,L,P) abort
return filter(g:NERDTreeBookmark.BookmarkNames(), 'v:val =~# "^' . a:A . '"') return filter(g:NERDTreeBookmark.BookmarkNames(), 'v:val =~# "^' . a:A . '"')
endfunction endfunction
"FUNCTION: nerdtree#compareNodes(dir) {{{2 "FUNCTION: nerdtree#compareNodes(n1, n2) {{{2
function! nerdtree#compareNodes(n1, n2) abort function! nerdtree#compareNodes(n1, n2) abort
return a:n1.path.compareTo(a:n2.path) return nerdtree#compareNodePaths(a:n1.path, a:n2.path)
endfunction endfunction
"FUNCTION: nerdtree#compareNodesBySortKey(n1, n2) {{{2 "FUNCTION: nerdtree#compareNodePaths(p1, p2) {{{2
function! nerdtree#compareNodesBySortKey(n1, n2) abort function! nerdtree#compareNodePaths(p1, p2) abort
let sortKey1 = a:n1.path.getSortKey() let sortKey1 = a:p1.getSortKey()
let sortKey2 = a:n2.path.getSortKey() let sortKey2 = a:p2.getSortKey()
let i = 0 let i = 0
while i < min([len(sortKey1), len(sortKey2)]) while i < min([len(sortKey1), len(sortKey2)])
" Compare chunks upto common length. " Compare chunks upto common length.

View File

@ -99,50 +99,6 @@ function! s:Path.changeToDir()
endtry endtry
endfunction endfunction
" FUNCTION: Path.compareTo() {{{1
"
" Compares this Path to the given path and returns 0 if they are equal, -1 if
" this Path is 'less than' the given path, or 1 if it is 'greater'.
"
" Args:
" path: the path object to compare this to
"
" Return:
" 1, -1 or 0
function! s:Path.compareTo(path)
let thisPath = self.getLastPathComponent(1)
let thatPath = a:path.getLastPathComponent(1)
"if the paths are the same then clearly we return 0
if thisPath ==# thatPath
return 0
endif
let thisSS = self.getSortOrderIndex()
let thatSS = a:path.getSortOrderIndex()
"compare the sort sequences, if they are different then the return
"value is easy
if thisSS < thatSS
return -1
elseif thisSS > thatSS
return 1
else
if !g:NERDTreeSortHiddenFirst
let thisPath = substitute(thisPath, '^[._]', '', '')
let thatPath = substitute(thatPath, '^[._]', '', '')
endif
"if the sort sequences are the same then compare the paths
"alphabetically
let pathCompare = g:NERDTreeCaseSensitiveSort ? thisPath <# thatPath : thisPath <? thatPath
if pathCompare
return -1
else
return 1
endif
endif
endfunction
" FUNCTION: Path.Create(fullpath) {{{1 " FUNCTION: Path.Create(fullpath) {{{1
" "
" Factory method. " Factory method.

View File

@ -236,7 +236,7 @@ function! s:TreeDirNode.getChildIndex(path)
let z = self.getChildCount() let z = self.getChildCount()
while a < z while a < z
let mid = (a+z)/2 let mid = (a+z)/2
let diff = a:path.compareTo(self.children[mid].path) let diff = nerdtree#compareNodePaths(a:path, self.children[mid].path)
if diff ==# -1 if diff ==# -1
let z = mid let z = mid
@ -666,7 +666,7 @@ function! s:TreeDirNode.sortChildren()
if count(g:NERDTreeSortOrder, '*') < 1 if count(g:NERDTreeSortOrder, '*') < 1
call add(g:NERDTreeSortOrder, '*') call add(g:NERDTreeSortOrder, '*')
endif endif
let CompareFunc = function('nerdtree#compareNodesBySortKey') let CompareFunc = function('nerdtree#compareNodes')
call sort(self.children, CompareFunc) call sort(self.children, CompareFunc)
let g:NERDTreeOldSortOrder = g:NERDTreeSortOrder let g:NERDTreeOldSortOrder = g:NERDTreeSortOrder
endfunction endfunction