Skip to content

Commit

Permalink
[[B]Commits] Allow tracking changes in the line range
Browse files Browse the repository at this point in the history
Thanks to @anhpt379

Close #898
Close #1288
  • Loading branch information
junegunn committed May 24, 2021
1 parent d7211d0 commit 161da95
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
50 changes: 25 additions & 25 deletions README.md
Expand Up @@ -61,31 +61,31 @@ so you can omit it if you use a plugin manager that doesn't support hooks.
Commands
--------

| Command | List |
| --- | --- |
| `:Files [PATH]` | Files (runs `$FZF_DEFAULT_COMMAND` if defined) |
| `:GFiles [OPTS]` | Git files (`git ls-files`) |
| `:GFiles?` | Git files (`git status`) |
| `:Buffers` | Open buffers |
| `:Colors` | Color schemes |
| `:Ag [PATTERN]` | [ag][ag] search result (`ALT-A` to select all, `ALT-D` to deselect all) |
| `:Rg [PATTERN]` | [rg][rg] search result (`ALT-A` to select all, `ALT-D` to deselect all) |
| `:Lines [QUERY]` | Lines in loaded buffers |
| `:BLines [QUERY]` | Lines in the current buffer |
| `:Tags [QUERY]` | Tags in the project (`ctags -R`) |
| `:BTags [QUERY]` | Tags in the current buffer |
| `:Marks` | Marks |
| `:Windows` | Windows |
| `:Locate PATTERN` | `locate` command output |
| `:History` | `v:oldfiles` and open buffers |
| `:History:` | Command history |
| `:History/` | Search history |
| `:Snippets` | Snippets ([UltiSnips][us]) |
| `:Commits` | Git commits (requires [fugitive.vim][f]) |
| `:BCommits` | Git commits for the current buffer |
| `:Commands` | Commands |
| `:Maps` | Normal mode mappings |
| `:Helptags` | Help tags <sup id="a1">[1](#helptags)</sup> |
| Command | List |
| --- | --- |
| `:Files [PATH]` | Files (runs `$FZF_DEFAULT_COMMAND` if defined) |
| `:GFiles [OPTS]` | Git files (`git ls-files`) |
| `:GFiles?` | Git files (`git status`) |
| `:Buffers` | Open buffers |
| `:Colors` | Color schemes |
| `:Ag [PATTERN]` | [ag][ag] search result (`ALT-A` to select all, `ALT-D` to deselect all) |
| `:Rg [PATTERN]` | [rg][rg] search result (`ALT-A` to select all, `ALT-D` to deselect all) |
| `:Lines [QUERY]` | Lines in loaded buffers |
| `:BLines [QUERY]` | Lines in the current buffer |
| `:Tags [QUERY]` | Tags in the project (`ctags -R`) |
| `:BTags [QUERY]` | Tags in the current buffer |
| `:Marks` | Marks |
| `:Windows` | Windows |
| `:Locate PATTERN` | `locate` command output |
| `:History` | `v:oldfiles` and open buffers |
| `:History:` | Command history |
| `:History/` | Search history |
| `:Snippets` | Snippets ([UltiSnips][us]) |
| `:Commits` | Git commits (requires [fugitive.vim][f]) |
| `: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> |
| `:Filetypes` | File types

- Most commands support `CTRL-T` / `CTRL-X` / `CTRL-V` key
Expand Down
33 changes: 25 additions & 8 deletions autoload/fzf/vim.vim
Expand Up @@ -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')
Expand All @@ -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,
Expand All @@ -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#commits(...) range
return s:commits(s:given_range(a:firstline, a:lastline), 0, a:000)
endfunction

function! fzf#vim#buffer_commits(...)
return s:commits(1, a:000)
function! fzf#vim#buffer_commits(...) range
return s:commits(s:given_range(a:firstline, a:lastline), 1, a:000)
endfunction

" ------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions doc/fzf-vim.txt
@@ -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*
==============================================================================

Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions plugin/fzf.vim
Expand Up @@ -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)'])
Expand Down

0 comments on commit 161da95

Please sign in to comment.