diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 3c0c854..f82603c 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -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 diff --git a/lua/fzf-lua/previewer/fzf.lua b/lua/fzf-lua/previewer/fzf.lua index f640954..c820b78 100644 --- a/lua/fzf-lua/previewer/fzf.lua +++ b/lua/fzf-lua/previewer/fzf.lua @@ -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() diff --git a/lua/fzf-lua/providers/nvim.lua b/lua/fzf-lua/providers/nvim.lua index 57e29ff..43575a4 100644 --- a/lua/fzf-lua/providers/nvim.lua +++ b/lua/fzf-lua/providers/nvim.lua @@ -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