From 161da95640ff09449f7010d906c68d9994d3523d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 24 May 2021 19:35:04 +0900 Subject: [PATCH] [[B]Commits] Allow tracking changes in the line range Thanks to @anhpt379 Close #898 Close #1288 --- README.md | 50 ++++++++++++++++++++++---------------------- autoload/fzf/vim.vim | 33 ++++++++++++++++++++++------- doc/fzf-vim.txt | 4 ++-- plugin/fzf.vim | 4 ++-- 4 files changed, 54 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index eef293e..37edd21 100644 --- a/README.md +++ b/README.md @@ -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 [1](#helptags) | +| 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 [1](#helptags) | | `:Filetypes` | File types - Most commands support `CTRL-T` / `CTRL-X` / `CTRL-V` key diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index 5e1b690..983ef1f 100644 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -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 " ------------------------------------------------------------------ diff --git a/doc/fzf-vim.txt b/doc/fzf-vim.txt index 5485f4f..a04a17b 100644 --- a/doc/fzf-vim.txt +++ b/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* ============================================================================== @@ -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] diff --git a/plugin/fzf.vim b/plugin/fzf.vim index 4b16b2a..f74c6f6 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -62,8 +62,8 @@ call s:defs([ \'command! -bar -bang Marks call fzf#vim#marks(0)', \'command! -bar -bang Helptags call fzf#vim#helptags(0)', \'command! -bar -bang Windows call fzf#vim#windows(0)', -\'command! -bar -bang Commits call fzf#vim#commits(fzf#vim#with_preview({ "placeholder": "" }), 0)', -\'command! -bar -bang BCommits call fzf#vim#buffer_commits(fzf#vim#with_preview({ "placeholder": "" }), 0)', +\'command! -bar -bang -range=% Commits ,call fzf#vim#commits(fzf#vim#with_preview({ "placeholder": "" }), 0)', +\'command! -bar -bang -range=% BCommits ,call fzf#vim#buffer_commits(fzf#vim#with_preview({ "placeholder": "" }), 0)', \'command! -bar -bang Maps call fzf#vim#maps("n", 0)', \'command! -bar -bang Filetypes call fzf#vim#filetypes(0)', \'command! -bang -nargs=* History call s:history(, fzf#vim#with_preview(), 0)'])