From 1fcdee55cc5975d67248b2f8ea5fbac9aa628b7c Mon Sep 17 00:00:00 2001 From: Ingo Meyer <1449087+IngoMeyer441@users.noreply.github.com> Date: Sun, 17 Jan 2021 14:36:02 +0100 Subject: [PATCH] [Tags] Add a tag preview with a custom `tagpreview.sh` script (#1223) * Add a tag preview with a custom `tagpreview.sh` script * [Tags] Respect g:fzf_preview_window Co-authored-by: Ingo Meyer Co-authored-by: Junegunn Choi --- autoload/fzf/vim.vim | 2 +- bin/preview.sh | 8 +++++- bin/tagpreview.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++ plugin/fzf.vim | 2 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100755 bin/tagpreview.sh diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index c6b7a0c..3eccb52 100644 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -941,7 +941,7 @@ function! fzf#vim#tags(query, ...) return s:fzf('tags', { \ 'source': 'perl '.fzf#shellescape(s:bin.tags).' '.join(map(tagfiles, 'fzf#shellescape(fnamemodify(v:val, ":p"))')), \ 'sink*': s:function('s:tags_sink'), - \ 'options': extend(opts, ['--nth', '1..2', '-m', '--tiebreak=begin', '--prompt', 'Tags> ', '--query', a:query])}, a:000) + \ 'options': extend(opts, ['--nth', '1..2', '-m', '-d', '\t', '--tiebreak=begin', '--prompt', 'Tags> ', '--query', a:query])}, a:000) endfunction " ------------------------------------------------------------------ diff --git a/bin/preview.sh b/bin/preview.sh index 08a8c07..3ce7c87 100755 --- a/bin/preview.sh +++ b/bin/preview.sh @@ -4,10 +4,16 @@ REVERSE="\x1b[7m" RESET="\x1b[m" if [ -z "$1" ]; then - echo "usage: $0 FILENAME[:LINENO][:IGNORED]" + echo "usage: $0 [--tag] FILENAME[:LINENO][:IGNORED]" exit 1 fi +if [ "$1" = --tag ]; then + shift + "$(dirname "${BASH_SOURCE[0]}")/tagpreview.sh" "$@" + exit $? +fi + IFS=':' read -r -a INPUT <<< "$1" FILE=${INPUT[0]} CENTER=${INPUT[1]} diff --git a/bin/tagpreview.sh b/bin/tagpreview.sh new file mode 100755 index 0000000..0e0b574 --- /dev/null +++ b/bin/tagpreview.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +REVERSE="\x1b[7m" +RESET="\x1b[m" + +if [ -z "$1" ]; then + echo "usage: $0 FILENAME:TAGFILE:EXCMD" + exit 1 +fi + +IFS=':' read -r FILE TAGFILE EXCMD <<< "$*" + +FILE="$(dirname "${TAGFILE}")/${FILE}" + +if [ ! -r "$FILE" ]; then + echo "File not found ${FILE}" + exit 1 +fi + +CENTER="$(vim -i NONE -u NONE -e -m -s "${FILE}" \ + -c "set nomagic" \ + -c "${EXCMD}" \ + -c 'let l=line(".") | new | put =l | print | qa!')" || return + +START_LINE="$(( CENTER - FZF_PREVIEW_LINES / 2 ))" +if (( START_LINE <= 0 )); then + START_LINE=1 +fi +END_LINE="$(( START_LINE + FZF_PREVIEW_LINES - 1 ))" + +# Sometimes bat is installed as batcat. +if command -v batcat > /dev/null; then + BATNAME="batcat" +elif command -v bat > /dev/null; then + BATNAME="bat" +fi + +if [ -z "$FZF_PREVIEW_COMMAND" ] && [ "${BATNAME:+x}" ]; then + ${BATNAME} --style="${BAT_STYLE:-numbers}" \ + --color=always \ + --pager=never \ + --wrap=never \ + --terminal-width="${FZF_PREVIEW_COLUMNS}" \ + --line-range="${START_LINE}:${END_LINE}" \ + --highlight-line="${CENTER}" \ + "$FILE" + exit $? +fi + +DEFAULT_COMMAND="highlight -O ansi -l {} || coderay {} || rougify {} || cat {}" +CMD=${FZF_PREVIEW_COMMAND:-$DEFAULT_COMMAND} +CMD=${CMD//{\}/$(printf %q "$FILE")} + +eval "$CMD" 2> /dev/null | awk "{ \ + if (NR >= $START_LINE && NR <= $END_LINE) { \ + if (NR == $CENTER) \ + { gsub(/\x1b[[0-9;]*m/, \"&$REVERSE\"); printf(\"$REVERSE%s\n$RESET\", \$0); } \ + else printf(\"$RESET%s\n\", \$0); \ + } \ + }" diff --git a/plugin/fzf.vim b/plugin/fzf.vim index dda26f5..1e78354 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -68,7 +68,7 @@ call s:defs([ \'command! -bang -nargs=+ -complete=dir Locate call fzf#vim#locate(, s:p(), 0)', \'command! -bang -nargs=* Ag call fzf#vim#ag(, s:p(), 0)', \'command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case -- ".shellescape(), 1, s:p(), 0)', -\'command! -bang -nargs=* Tags call fzf#vim#tags(, 0)', +\'command! -bang -nargs=* Tags call fzf#vim#tags(, s:p({ "placeholder": "--tag {2}:{-1}:{3}" }), 0)', \'command! -bang -nargs=* BTags call fzf#vim#buffer_tags(, s:p({ "placeholder": "{2}:{3}" }), 0)', \'command! -bar -bang Snippets call fzf#vim#snippets(0)', \'command! -bar -bang Commands call fzf#vim#commands(0)',