Skip to content

Commit

Permalink
[Jumps] Support preview window (#1553)
Browse files Browse the repository at this point in the history
Require fzf 0.54.0 or above

Signed-off-by: zeminzhou <[email protected]>
Co-authored-by: Junegunn Choi <[email protected]>
  • Loading branch information
zeminzhou and junegunn authored Jul 19, 2024
1 parent 279e1ec commit 3e2ed51
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<!-- Check all that apply [x] -->

- [ ] I have fzf 0.30.0 or above
- [ ] I have fzf 0.54.0 or above
- `:echo system(fzf#exec() .. ' --version')`
- [ ] I have read through https://github.com/junegunn/fzf.vim/blob/master/README.md
- [ ] I have read through https://github.com/junegunn/fzf/blob/master/README-VIM.md
- [ ] I have read through the manual page of fzf (`man fzf`)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ so you can omit it if you use a plugin manager that doesn't support hooks.

### Dependencies

- [fzf][fzf-main] 0.41.1 or above
- [fzf][fzf-main] 0.54.0 or above
- For syntax-highlighted preview, install [bat](https://github.com/sharkdp/bat)
- If [delta](https://github.com/dandavison/delta) is available, `GF?`,
`Commits` and `BCommits` will use it to format `git diff` output.
Expand Down
45 changes: 30 additions & 15 deletions autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1337,37 +1337,52 @@ endfunction
" Jumps
" ------------------------------------------------------------------
function! s:jump_format(line)
return substitute(a:line, '[0-9]\+', '\=s:yellow(submatch(0), "Number")', '')
let line = substitute(a:line, '[0-9]\+', '\=s:yellow(submatch(0), "Number")', '')
let line = substitute(line, '\s.\{-}\ze:[0-9]\+:', '\=s:green(submatch(0), "Directory")', '')
let line = substitute(line, '\%(:[0-9]\+\)\+:', '\=s:black(submatch(0), "NonText")', '')
return line
endfunction

function! s:jump_sink(lines)
if len(a:lines) < 2
return
endif
call s:action_for(a:lines[0])
let idx = index(s:jumplist, a:lines[1])
if idx == -1
return
endif
let current = match(s:jumplist, '\v^\s*\>')
let delta = idx - current
keepjumps call s:action_for(a:lines[0])
let idx = str2nr(a:lines[1])
let delta = idx - s:jump_current - 1
if delta < 0
execute 'normal! ' . -delta . "\<C-O>"
else
execute 'normal! ' . delta . "\<C-I>"
endif
normal! zvzz
endfunction

function! fzf#vim#jumps(...)
redir => cout
silent jumps
redir END
let s:jumplist = split(cout, '\n')
let current = -match(s:jumplist, '\v^\s*\>')
let [jumps, pos] = getjumplist()
if empty(jumps)
return s:warn('No jumps')
endif
let s:jumplist = []
for idx in range(len(jumps))
let jump = jumps[idx]
let loc = expand('#'.jump.bufnr.':p:~:.')
if empty(loc)
let loc = '[No Name]'
endif
let loc .= ':'.jump.lnum
if jump.col
let loc .= ':'.jump.col
endif
let line = printf('%-2d %s: %s', idx+1, loc, getbufoneline(jump.bufnr, jump.lnum))
call add(s:jumplist, line)
endfor
let s:jump_current = pos
let current = -pos-1
return s:fzf('jumps', {
\ 'source' : extend(s:jumplist[0:0], map(s:jumplist[1:], 's:jump_format(v:val)')),
\ 'source' : map(s:jumplist, 's:jump_format(v:val)'),
\ 'sink*' : s:function('s:jump_sink'),
\ 'options' : '+m -x --ansi --tiebreak=index --cycle --scroll-off 999 --sync --bind start:pos:'.current.' --tac --header-lines 1 --tiebreak=begin --prompt "Jumps> "',
\ 'options' : ['+m', '-x', '--ansi', '--tiebreak=index', '--cycle', '--scroll-off=999', '--sync', '--bind', 'start:pos('.current.')+offset-middle', '--tac', '--tiebreak=begin', '--prompt', 'Jumps> ', '--preview-window', '+{3}-/2', '--tabstop=2', '--delimiter', '[:\s]+'],
\ }, a:000)
endfunction

Expand Down
4 changes: 3 additions & 1 deletion bin/preview.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ fi

FILE="${FILE/#\~\//$HOME/}"
if [ ! -r "$FILE" ]; then
echo "File not found ${FILE}"
if [[ "${INPUT[0]}" != '[No Name]' ]]; then
echo "File not found ${FILE}"
fi
exit 1
fi

Expand Down
2 changes: 1 addition & 1 deletion doc/fzf-vim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ so you can omit it if you use a plugin manager that doesn't support hooks.
< Dependencies >______________________________________________________________~
*fzf-vim-dependencies*

- {fzf}{1} 0.41.1 or above
- {fzf}{1} 0.54.0 or above
- For syntax-highlighted preview, install {bat}{4}
- If {delta}{5} is available, `GF?`, `Commits` and `BCommits` will use it to
format `git diff` output.
Expand Down
2 changes: 1 addition & 1 deletion plugin/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ call s:defs([
\'command! -bang -nargs=* BTags call fzf#vim#buffer_tags(<q-args>, fzf#vim#with_preview({ "placeholder": "{2}:{3..}" }), <bang>0)',
\'command! -bar -bang Snippets call fzf#vim#snippets(<bang>0)',
\'command! -bar -bang Commands call fzf#vim#commands(<bang>0)',
\'command! -bar -bang Jumps call fzf#vim#jumps(<bang>0)',
\'command! -bar -bang Jumps call fzf#vim#jumps(fzf#vim#with_preview({ "placeholder": "{2..4}"}), <bang>0)',
\'command! -bar -bang Marks call fzf#vim#marks(<bang>0)',
\'command! -bar -bang Changes call fzf#vim#changes(<bang>0)',
\'command! -bar -bang Helptags call fzf#vim#helptags(fzf#vim#with_preview({ "placeholder": "--tag {2}:{3}:{4}" }), <bang>0)',
Expand Down

0 comments on commit 3e2ed51

Please sign in to comment.