[[B]Commits] Allow tracking changes in the line range

Thanks to @anhpt379

Close #898
Close #1288
This commit is contained in:
Junegunn Choi
2021-05-24 19:35:04 +09:00
parent d7211d0f61
commit 161da95640
4 changed files with 54 additions and 37 deletions

View File

@@ -82,7 +82,7 @@ Commands
| `:History/` | Search history |
| `:Snippets` | Snippets ([UltiSnips][us]) |
| `:Commits` | Git commits (requires [fugitive.vim][f]) |
| `:BCommits` | Git commits for the current buffer |
| `:BCommits` | Git commits for the current buffer; visual-select lines to track changes in the range |
| `:Commands` | Commands |
| `:Maps` | Normal mode mappings |
| `:Helptags` | Help tags <sup id="a1">[1](#helptags)</sup> |

View File

@@ -1201,7 +1201,7 @@ function! s:commits_sink(lines)
endfor
endfunction
function! s:commits(buffer_local, args)
function! s:commits(range, buffer_local, args)
let s:git_root = s:get_git_root()
if empty(s:git_root)
return s:warn('Not in git repository')
@@ -1215,16 +1215,19 @@ function! s:commits(buffer_local, args)
let managed = !v:shell_error
endif
if a:buffer_local
if len(a:range) || a:buffer_local
if !managed
return s:warn('The current buffer is not in the working tree')
endif
let source .= ' --follow '.fzf#shellescape(current)
let source .= len(a:range)
\ ? printf(' -L %d,%d:%s --no-patch', a:range[0], a:range[1], fzf#shellescape(current))
\ : (' --follow '.fzf#shellescape(current))
let command = 'BCommits'
else
let source .= ' --graph'
let command = 'Commits'
endif
let command = a:buffer_local ? 'BCommits' : 'Commits'
let expect_keys = join(keys(get(g:, 'fzf_action', s:default_action)), ',')
let options = {
\ 'source': source,
@@ -1249,12 +1252,26 @@ function! s:commits(buffer_local, args)
return s:fzf(a:buffer_local ? 'bcommits' : 'commits', options, a:args)
endfunction
function! fzf#vim#commits(...)
return s:commits(0, a:000)
" Heuristically determine if the user specified a range
function! s:given_range(line1, line2)
" 1. From visual mode
" :'<,'>Commits
" 2. From command-line
" :10,20Commits
if a:line1 == line("'<") && a:line2 == line("'>") ||
\ (a:line1 != 1 || a:line2 != line('$'))
return [a:line1, a:line2]
endif
return []
endfunction
function! fzf#vim#buffer_commits(...)
return s:commits(1, a:000)
function! fzf#vim#commits(...) range
return s:commits(s:given_range(a:firstline, a:lastline), 0, a:000)
endfunction
function! fzf#vim#buffer_commits(...) range
return s:commits(s:given_range(a:firstline, a:lastline), 1, a:000)
endfunction
" ------------------------------------------------------------------

View File

@@ -1,4 +1,4 @@
fzf-vim.txt fzf-vim Last change: December 14 2020
fzf-vim.txt fzf-vim Last change: May 24 2021
FZF-VIM - TABLE OF CONTENTS *fzf-vim* *fzf-vim-toc*
==============================================================================
@@ -134,7 +134,7 @@ COMMANDS *fzf-vim-commands*
`:History/` | Search history
`:Snippets` | Snippets ({UltiSnips}{9})
`:Commits` | Git commits (requires {fugitive.vim}{10})
`:BCommits` | Git commits for the current buffer
`:BCommits` | Git commits for the current buffer; visual-select lines to track changes in the range
`:Commands` | Commands
`:Maps` | Normal mode mappings
`:Helptags` | Help tags [1]

View File

@@ -62,8 +62,8 @@ call s:defs([
\'command! -bar -bang Marks call fzf#vim#marks(<bang>0)',
\'command! -bar -bang Helptags call fzf#vim#helptags(<bang>0)',
\'command! -bar -bang Windows call fzf#vim#windows(<bang>0)',
\'command! -bar -bang Commits call fzf#vim#commits(fzf#vim#with_preview({ "placeholder": "" }), <bang>0)',
\'command! -bar -bang BCommits call fzf#vim#buffer_commits(fzf#vim#with_preview({ "placeholder": "" }), <bang>0)',
\'command! -bar -bang -range=% Commits <line1>,<line2>call fzf#vim#commits(fzf#vim#with_preview({ "placeholder": "" }), <bang>0)',
\'command! -bar -bang -range=% BCommits <line1>,<line2>call fzf#vim#buffer_commits(fzf#vim#with_preview({ "placeholder": "" }), <bang>0)',
\'command! -bar -bang Maps call fzf#vim#maps("n", <bang>0)',
\'command! -bar -bang Filetypes call fzf#vim#filetypes(<bang>0)',
\'command! -bang -nargs=* History call s:history(<q-args>, fzf#vim#with_preview(), <bang>0)'])