Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sorting files and directories by modification time. #901

Merged
merged 4 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/NERDTree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -995,17 +995,33 @@ appended to the array.

The regex '\/$' should be used to match directory nodes.

A special flag can be used to sort by the modification timestamps of files and
directories. It is either '[[timestamp]]' for ascending, or '[[-timestamp]]'
for descending. If placed at the beginning of the list, files and directories
are sorted by timestamp, and then by the remaining items in the sort order
list. If this flag is in any other position of the list, timestamp sorting is
done secondarily. See examples 4, 5, and 6 below.

After this sorting is done, the files in each group are sorted alphabetically.

Other examples: >
(1) ['*', '\/$']
(2) []
(3) ['\/$', '\.rb$', '\.php$', '*', '\.swp$', '\.bak$', '\~$']
(4) ['[[timestamp]]']
(5) ['\/$', '*', '[[-timestamp]]']
(6) ['\.md$', '\.c$', '[[-timestamp]]', '*']
<
1. Directories will appear last, everything else will appear above.
2. Everything will simply appear in alphabetical order.
3. Dirs will appear first, then ruby and php. Swap files, bak files and vim
backup files will appear last with everything else preceding them.
4. All files and directories are sorted by timestamp, oldest first. If any
files have identical timestamps, they are sorted alphabetically.
5. Directories are first, newest to oldest, then everything else, newest to
oldest.
6. Markdown files first, followed by C source files, then everything else.
Each group is shown newest to oldest.

------------------------------------------------------------------------------
*'NERDTreeStatusline'*
Expand Down
20 changes: 13 additions & 7 deletions lib/nerdtree/path.vim
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,27 @@ endfunction
" FUNCTION: Path.getSortKey() {{{1
" returns a key used in compare function for sorting
function! s:Path.getSortKey()
if !exists("self._sortKey") || g:NERDTreeSortOrder !=# g:NERDTreeOldSortOrder
let l:ascending = index(g:NERDTreeSortOrder,'[[timestamp]]')
let l:descending = index(g:NERDTreeSortOrder,'[[-timestamp]]')
if !exists("self._sortKey") || g:NERDTreeSortOrder !=# g:NERDTreeOldSortOrder || l:ascending >= 0 || l:descending >= 0
let self._sortKey = [self.getSortOrderIndex()]

if l:descending >= 0
call insert(self._sortKey, -getftime(self.str()), l:descending == 0 ? 0 : len(self._sortKey))
elseif l:ascending >= 0
call insert(self._sortKey, getftime(self.str()), l:ascending == 0 ? 0 : len(self._sortKey))
endif

let path = self.getLastPathComponent(1)
if !g:NERDTreeSortHiddenFirst
let path = substitute(path, '^[._]', '', '')
endif
if !g:NERDTreeCaseSensitiveSort
let path = tolower(path)
endif
if !g:NERDTreeNaturalSort
let self._sortKey = [self.getSortOrderIndex(), path]
else
let self._sortKey = [self.getSortOrderIndex()] + self._splitChunks(path)
endif
endif

call extend(self._sortKey, (g:NERDTreeNaturalSort ? self._splitChunks(path) : [path]))
endif
return self._sortKey
endfunction

Expand Down