Skip to content

Commit cabecb1

Browse files
committed
Add -sort option
1 parent 4bb36f6 commit cabecb1

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

autoload/flog.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ function! flog#split_limit(limit) abort
6666
return [a:limit[: l:start - 1], a:limit[l:start :]]
6767
endfunction
6868

69+
function! flog#get_sort_type(name) abort
70+
return filter(copy(g:flog_sort_types), 'v:val.name ==# ' . string(a:name))[0]
71+
endfunction
72+
6973
function! flog#get(dict, key, ...) abort
7074
if type(a:dict) != v:t_dict
7175
return v:null
@@ -209,6 +213,7 @@ function! flog#get_internal_default_args() abort
209213
\ 'no_graph': v:false,
210214
\ 'no_patch': v:false,
211215
\ 'skip': v:null,
216+
\ 'sort': v:null,
212217
\ 'max_count': v:null,
213218
\ 'open_cmd': 'tabedit',
214219
\ 'search': v:null,
@@ -319,6 +324,10 @@ function! flog#parse_set_args(args, current_args, defaults) abort
319324
let a:current_args.skip = flog#parse_arg_opt(l:arg)
320325
elseif l:arg ==# '-skip='
321326
let a:current_args.skip = a:defaults.skip
327+
elseif l:arg =~# '^-sort=.\+'
328+
let a:current_args.sort = flog#parse_arg_opt(l:arg)
329+
elseif l:arg ==# '-sort='
330+
let a:current_args.sort = a:defaults.sort
322331
elseif l:arg =~# '^-max-count=\d\+'
323332
let a:current_args.max_count = flog#parse_arg_opt(l:arg)
324333
elseif l:arg ==# '-max-count='
@@ -613,6 +622,12 @@ function! flog#complete_author(arg_lead) abort
613622
return flog#filter_completions(a:arg_lead, l:authors)
614623
endfunction
615624

625+
function! flog#complete_sort(arg_lead) abort
626+
let [l:lead, l:name] = flog#split_single_completable_arg(a:arg_lead)
627+
let l:sort_types = flog#escape_completions(l:lead, map(copy(g:flog_sort_types), 'v:val.name'))
628+
return flog#filter_completions(a:arg_lead, l:sort_types)
629+
endfunction
630+
616631
function! flog#complete(arg_lead, cmd_line, cursor_pos) abort
617632
if a:cmd_line[:a:cursor_pos] =~# ' -- '
618633
return []
@@ -636,6 +651,8 @@ function! flog#complete(arg_lead, cmd_line, cursor_pos) abort
636651
return flog#complete_rev(a:arg_lead)
637652
elseif a:arg_lead =~# '^-path='
638653
return flog#complete_path(a:arg_lead)
654+
elseif a:arg_lead =~# '^-sort='
655+
return flog#complete_sort(a:arg_lead)
639656
endif
640657
return flog#filter_completions(a:arg_lead, copy(g:flog_default_completion))
641658
endfunction
@@ -824,6 +841,9 @@ function! flog#build_log_args() abort
824841
if l:opts.skip != v:null
825842
let l:args .= ' --skip=' . shellescape(l:opts.skip)
826843
endif
844+
if l:opts.sort != v:null
845+
let l:sort_type = flog#get_sort_type(l:opts.sort)
846+
let l:args .= ' ' . l:sort_type.args
827847
endif
828848
if l:opts.max_count != v:null
829849
let l:args .= ' --max-count=' . shellescape(l:opts.max_count)
@@ -1123,6 +1143,9 @@ function! flog#set_graph_buffer_title() abort
11231143
if l:opts.skip != v:null
11241144
let l:title .= ' [skip=' . l:opts.skip . ']'
11251145
endif
1146+
if l:opts.sort != v:null
1147+
let l:title .= ' [sort=' . l:opts.sort . ']'
1148+
endif
11261149
if l:opts.max_count != v:null
11271150
let l:title .= ' [max_count=' . l:opts.max_count . ']'
11281151
endif
@@ -1315,6 +1338,30 @@ function! flog#change_skip_by_max_count(multiplier) abort
13151338
call flog#populate_graph_buffer()
13161339
endfunction
13171340

1341+
function! flog#set_sort_option(sort) abort
1342+
let l:state = flog#get_state()
1343+
let l:state.sort = a:sort
1344+
call flog#populate_graph_buffer()
1345+
endfunction
1346+
1347+
function! flog#cycle_sort_option() abort
1348+
let l:state = flog#get_state()
1349+
1350+
if l:state.sort == v:null
1351+
let l:state.sort = g:flog_sort_types[0].name
1352+
else
1353+
let l:sort_type = flog#get_sort_type(l:state.sort)
1354+
let l:sort_index = index(g:flog_sort_types, l:sort_type)
1355+
if l:sort_index == len(g:flog_sort_types) - 1
1356+
let l:state.sort = g:flog_sort_types[0].name
1357+
else
1358+
let l:state.sort = g:flog_sort_types[l:sort_index + 1].name
1359+
endif
1360+
endif
1361+
1362+
call flog#populate_graph_buffer()
1363+
endfunction
1364+
13181365
" }}}
13191366

13201367
" Graph buffer update hook {{{

doc/flog.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ COMMANDS *flog-commands*
5757
Instead of manually specifying this argument, you can
5858
also use a range or visually select some lines and
5959
type ":Flog<CR>".
60+
-sort=<sort> Sort by one of <type>, where type is in "date",
61+
"author", or "topo". These correspond to the options
62+
"--date-order", "--author-order", and "--topo-order"
63+
respectfully.
6064
-rev=<rev> The git revision to pass to the log command. Can be
6165
specified more than once. When "-limit=" is specified,
6266
only the first revision is used.
@@ -246,6 +250,27 @@ gp *flog-gp*
246250
Toggle the "--no-patch" argument. Useful while the "-limit" option is
247251
specified.
248252

253+
gss *<Plug>(FlogCycleSort)*
254+
*flog-gss*
255+
256+
Cycle through the different sort options; "--date-order",
257+
"--author-date-order", and "--topo-order".
258+
259+
gsd *<Plug>(FlogSortDate)*
260+
*flog-gsd*
261+
262+
Set the "--date-order" option. Conflicts with other sort options.
263+
264+
gsa *<Plug>(FlogSortAuthor)*
265+
*flog-gsa*
266+
267+
Set the "--author-date-order" option. Conflicts with other sort options.
268+
269+
gst *<plug>(FlogSortTopo)*
270+
*flog-gst*
271+
272+
Set the "--topo-order" option. Conflicts with other sort options.
273+
249274
gsr *<Plug>(FlogToggleReverse)*
250275
*flog-gsr*
251276

ftplugin/floggraph.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ if !hasmapto('<Plug>(FlogPatchSearch)')
157157
endif
158158
nnoremap <buffer> <Plug>(FlogPatchSearch) :<C-U>Flogsetargs -patch-search=
159159
160+
if !hasmapto('<Plug>(FlogCycleSort)')
161+
nmap <buffer> gss <Plug>(FlogCycleSort)
162+
endif
163+
nnoremap <buffer> <silent> <Plug>(FlogCycleSort) :call flog#cycle_sort_option()<CR>
164+
165+
if !hasmapto('<Plug>(FlogSortDate)')
166+
nmap <buffer> gsd <Plug>(FlogSortDate)
167+
endif
168+
nnoremap <buffer> <silent> <Plug>(FlogSortDate) :call flog#set_sort_option('date')<CR>
169+
170+
if !hasmapto('<Plug>(FlogSortAuthor)')
171+
nmap <buffer> gsa <Plug>(FlogSortAuthor)
172+
endif
173+
nnoremap <buffer> <silent> <Plug>(FlogSortAuthor) :call flog#set_sort_option('author')<CR>
174+
175+
if !hasmapto('<Plug>(FlogSortTopo)')
176+
nmap <buffer> gst <Plug>(FlogSortTopo)
177+
endif
178+
nnoremap <buffer> <silent> <Plug>(FlogSortTopo) :call flog#set_sort_option('topo')<CR>
179+
160180
" }}}
161181

162182
" Commit/branch mappings {{{

plugin/flog.vim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ let g:flog_log_data_hash_index = 1
6767

6868
" }}}
6969

70+
" Sorting type data {{{
71+
72+
let g:flog_sort_types = [
73+
\ { 'name': 'date', 'args': '--date-order' },
74+
\ { 'name': 'author', 'args': '--author-date-order' },
75+
\ { 'name': 'topo', 'args': '--topo-order' },
76+
\ ]
77+
78+
" }}}
79+
7080
" Completion data {{{
7181

7282
let g:flog_default_completion = [
@@ -88,7 +98,8 @@ let g:flog_default_completion = [
8898
\ '-rev=',
8999
\ '-reverse',
90100
\ '-search=',
91-
\ '-skip='
101+
\ '-skip=',
102+
\ '-sort=',
92103
\ ]
93104

94105
let g:flog_date_formats = [

0 commit comments

Comments
 (0)