[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 <IJ_M@gmx.de>
Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
This commit is contained in:
Ingo Meyer
2021-01-17 14:36:02 +01:00
committed by GitHub
parent d43df0ea2f
commit 1fcdee55cc
4 changed files with 69 additions and 3 deletions

View File

@@ -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
" ------------------------------------------------------------------

View File

@@ -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]}

60
bin/tagpreview.sh Executable file
View File

@@ -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); \
} \
}"

View File

@@ -68,7 +68,7 @@ call s:defs([
\'command! -bang -nargs=+ -complete=dir Locate call fzf#vim#locate(<q-args>, s:p(), <bang>0)',
\'command! -bang -nargs=* Ag call fzf#vim#ag(<q-args>, s:p(), <bang>0)',
\'command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case -- ".shellescape(<q-args>), 1, s:p(), <bang>0)',
\'command! -bang -nargs=* Tags call fzf#vim#tags(<q-args>, <bang>0)',
\'command! -bang -nargs=* Tags call fzf#vim#tags(<q-args>, s:p({ "placeholder": "--tag {2}:{-1}:{3}" }), <bang>0)',
\'command! -bang -nargs=* BTags call fzf#vim#buffer_tags(<q-args>, s:p({ "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)',