refactor native fzf previewer params

main
bhagwan 2 years ago
parent 76c7b6d960
commit 2a2c8502a4

@ -125,6 +125,19 @@ M.fzf = function(opts, contents)
-- calls without displaying the native fzf previewer split
opts.fzf_opts['--preview-window'] = previewer:preview_window(opts.preview_window)
end
-- provides preview offset when using native previewers
-- (bat/cat/etc) with providers that supply line numbers
-- (grep/quickfix/LSP)
if type(previewer.fzf_delimiter) == 'function' then
opts.fzf_opts["--delimiter"] = previewer:fzf_delimiter()
end
if type(previewer.preview_offset) == 'function' then
opts.preview_offset = previewer:preview_offset()
end
elseif not opts.preview and not opts.fzf_opts['--preview'] then
-- no preview available, override incase $FZF_DEFAULT_OPTS
-- contains a preview which will most likely fail
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
end
if opts.fn_pre_fzf then
@ -405,21 +418,6 @@ end
M.set_fzf_line_args = function(opts)
opts._line_placeholder = 2
-- set delimiter to ':'
-- entry format is 'file:line:col: text'
opts.fzf_opts["--delimiter"] = vim.fn.shellescape('[:]')
--[[
#
# Explanation of the fzf preview offset options:
#
# ~3 Top 3 lines as the fixed header
# +{2} Base scroll offset extracted from the second field
# +3 Extra offset to compensate for the 3-line header
# /2 Put in the middle of the preview area
#
'--preview-window '~3:+{2}+3/2''
]]
opts.preview_offset = string.format("+{%d}-/2", opts._line_placeholder)
return opts
end

@ -22,6 +22,40 @@ function Previewer.base:preview_window(_)
return nil
end
function Previewer.base:preview_offset()
--[[
#
# Explanation of the fzf preview offset options:
#
# ~3 Top 3 lines as the fixed header
# +{2} Base scroll offset extracted from the second field
# +3 Extra offset to compensate for the 3-line header
# /2 Put in the middle of the preview area
#
'--preview-window '~3:+{2}+3/2''
]]
if self.opts._line_placeholder then
return ("+{%d}-/2"):format(self.opts._line_placeholder)
end
end
function Previewer.base:fzf_delimiter()
if not self.opts._line_placeholder then return end
-- set delimiter to ':'
-- entry format is 'file:line:col: text'
local delim = self.opts.fzf_opts and self.opts.fzf_opts["--delimiter"]
if not delim then
delim = '[:]'
elseif not delim:match(":") then
if delim:match("%[.*%]")then
delim = '[:' .. delim:match("%[(.*%])")
else
delim = '[:' .. delim .. ']'
end
end
return delim
end
-- Generic shell command previewer
Previewer.cmd = Previewer.base:extend()

@ -189,7 +189,7 @@ M.marks = function(opts)
local marks = vim.fn.execute("marks")
marks = vim.split(marks, "\n")
local prev_act = shell.action(function (args, fzf_lines, _)
--[[ local prev_act = shell.action(function (args, fzf_lines, _)
local mark = args[1]:match("[^ ]+")
local bufnr, lnum, _, _ = unpack(vim.fn.getpos("'"..mark))
if vim.api.nvim_buf_is_loaded(bufnr) then
@ -201,7 +201,7 @@ M.marks = function(opts)
end
return "UNLOADED: " .. name
end
end)
end) ]]
local entries = {}
for i = #marks, 3, -1 do
@ -215,7 +215,7 @@ M.marks = function(opts)
table.sort(entries, function(a, b) return a<b end)
opts.fzf_opts['--preview'] = prev_act
-- opts.fzf_opts['--preview'] = prev_act
opts.fzf_opts['--no-multi'] = ''
core.fzf_wrap(opts, entries, function(selected)

@ -65,7 +65,7 @@ local fzf_tags = function(opts)
if not grep_cmd then grep_cmd = get_grep_cmd() end
local line = 1
local filepath = path.join({cwd, t.file})
local pattern = utils.rg_escape(t.text:match("/(.*)/"))
local pattern = utils.rg_escape(t.text:match("/^?(.*)/"))
if not pattern or not filepath then return line end
-- ctags uses '$' at the end of short patterns
-- 'rg|grep' does not match these properly when
@ -123,7 +123,9 @@ local fzf_tags = function(opts)
}, cb, co,
-- unless we're using native previewer
-- do not need to extract the line number
type(opts.previewer) == 'table')
not opts.previewer
or opts.previewer == 'builtin'
or type(opts.previewer) == 'table')
end)
-- pause here until we call coroutine.resume()
coroutine.yield()

Loading…
Cancel
Save