feat(preview): toggle cursorline hl when scrolling (#462)

Other changes:
- preview: added new highlight setting `CursorLineNr`
- internal changes to preview command shellescape
main
bhagwan 2 years ago
parent d7de3b51ae
commit 519dbf067b

@ -296,6 +296,7 @@ require'fzf-lua'.setup {
-- Only valid with the builtin previewer:
cursor = 'Cursor', -- cursor highlight (grep/LSP matches)
cursorline = 'CursorLine', -- cursor line
cursorlinenr = 'CursorLineNr', -- cursor line number
search = 'IncSearch', -- search matches (ctags|help)
-- title = 'Normal', -- preview border title (file/buffer)
-- scrollbar_f = 'PmenuThumb', -- scrollbar "full" section highlight

@ -335,6 +335,7 @@ Consult the list below for available settings:
-- Only valid with the builtin previewer:
cursor = 'Cursor', -- cursor highlight (grep/LSP matches)
cursorline = 'CursorLine', -- cursor line
cursorlinenr = 'CursorLineNr', -- cursor line number
search = 'IncSearch', -- search matches (ctags)
-- title = 'Normal', -- preview border title (file/buffer)
-- scrollbar_f = 'PmenuThumb', -- scrollbar "full" section highlight

@ -41,6 +41,7 @@ M.globals = {
-- builtin preview only
cursor = 'Cursor',
cursorline = 'CursorLine',
cursorlinenr = 'CursorLineNr',
search = 'IncSearch',
-- title = 'Normal',
-- scrollbar_f = 'PmenuThumb',

@ -181,10 +181,8 @@ M.fzf = function(opts, contents)
previewer = preview_opts._ctor()(preview_opts, opts, fzf_win)
end
if previewer then
-- we use fzf_opts because previewer:cmdline is already
-- shellescaped, clear opts.preview so it doesn't override
opts.preview = nil
opts.fzf_opts['--preview'] = previewer:cmdline()
-- Set the preview command line
opts.preview = previewer:cmdline()
if type(previewer.preview_window) == 'function' then
-- do we need to override the preview_window args?
-- this can happen with the builtin previewer

@ -208,7 +208,7 @@ function Previewer.base:display_entry(entry_str)
end
function Previewer.base:action(_)
function Previewer.base:cmdline(_)
local act = shell.raw_action(function (items, _, _)
self:display_entry(items[1])
return ""
@ -216,11 +216,6 @@ function Previewer.base:action(_)
return act
end
function Previewer.base:cmdline(_)
return vim.fn.shellescape(self:action())
-- return 'true'
end
function Previewer.base:preview_window(_)
if self.win and not self.win.winopts.split then
return 'nohidden:right:0'
@ -267,6 +262,22 @@ function Previewer.base:scroll(direction)
'%d, function() vim.cmd("norm! <C-v>%s") vim.cmd("startinsert") end)<CR>'):
format(tonumber(preview_winid), input))
end
-- 'cursorline' is effectively our match highlight, once the
-- user scrolls, the highlight is no longer relevenat (#462)
-- conditionally toggle 'cursorline' based on cursor position
if self.orig_pos and self.winopts.cursorline then
local wininfo = vim.fn.getwininfo(preview_winid)
if wininfo and wininfo[1] and
self.orig_pos[1] >= wininfo[1].topline and
self.orig_pos[1] <= wininfo[1].botline then
-- reset cursor pos even when it's already there, no bigggie
-- local curpos = vim.api.nvim_win_get_cursor(preview_winid)
vim.api.nvim_win_set_cursor(preview_winid, self.orig_pos)
vim.api.nvim_win_set_option(preview_winid, 'cursorline', true)
else
vim.api.nvim_win_set_option(preview_winid, 'cursorline', false)
end
end
self.win:update_scrollbar()
end

@ -103,7 +103,7 @@ function Previewer.bat:cmdline(o)
if self.opts.line_field_index then
highlight_line = string.format("--highlight-line={%d}", self.opts.line_field_index)
end
return vim.fn.shellescape(self:sh_wrap(self.cmd, self.args, o.action, highlight_line))
return self:sh_wrap(self.cmd, self.args, o.action, highlight_line)
end
-- Specialized head previewer
@ -122,7 +122,7 @@ function Previewer.head:cmdline(o)
-- if self.opts.line_field_index then
-- lines = string.format("--lines={%d}", self.opts.line_field_index)
-- end
return vim.fn.shellescape(self:sh_wrap(self.cmd, self.args, o.action, lines))
return self:sh_wrap(self.cmd, self.args, o.action, lines)
end
-- new async_action from nvim-fzf
@ -192,7 +192,7 @@ end
function Previewer.cmd_async:cmdline(o)
o = o or {}
local act = shell.preview_action_cmd(function(items)
local act = shell.raw_preview_action_cmd(function(items)
local filepath, _, errcmd = self:parse_entry_and_verify(items[1])
local cmd = errcmd or ('%s %s %s'):format(
self.cmd, self.args, vim.fn.shellescape(filepath))
@ -213,7 +213,7 @@ end
function Previewer.bat_async:cmdline(o)
o = o or {}
local act = shell.preview_action_cmd(function(items, fzf_lines)
local act = shell.raw_preview_action_cmd(function(items, fzf_lines)
local filepath, entry, errcmd = self:parse_entry_and_verify(items[1])
local line_range = ''
if entry.ctag then
@ -260,7 +260,7 @@ end
function Previewer.git_diff:cmdline(o)
o = o or {}
local act = shell.preview_action_cmd(function(items, fzf_lines, fzf_columns)
local act = shell.raw_preview_action_cmd(function(items, fzf_lines, fzf_columns)
if not items or vim.tbl_isempty(items) then
utils.warn("shell error while running preview action.")
return
@ -321,7 +321,7 @@ end
function Previewer.man_pages:cmdline(o)
o = o or {}
local act = shell.preview_action_cmd(function(items)
local act = shell.raw_preview_action_cmd(function(items)
-- local manpage = require'fzf-lua.providers.manpages'.getmanpage(items[1])
local manpage = items[1]:match("[^[,( ]+")
local cmd = ("%s %s %s"):format(

@ -253,8 +253,6 @@ M.buffer_lines = function(opts)
end
end
opts.fzf_opts["--preview-window"] = 'hidden:right:0'
if opts.search and #opts.search>0 then
opts.fzf_opts['--query'] = vim.fn.shellescape(opts.search)
end
@ -324,9 +322,6 @@ M.tabs = function(opts)
cb(nil)
end
-- opts.fzf_opts["--no-multi"] = ''
opts.fzf_opts["--preview-window"] = 'hidden:right:0'
opts = core.set_fzf_field_index(opts, 3, "{}")
core.fzf_exec(contents, opts)

@ -19,25 +19,25 @@ M.colorschemes = function(opts)
opts = config.normalize_opts(opts, config.globals.colorschemes)
if not opts then return end
local prev_act = shell.action(function (args)
if opts.live_preview and args then
local colorscheme = args[1]
vim.cmd("colorscheme " .. colorscheme)
end
end, nil, opts.debug)
local current_colorscheme = get_current_colorscheme()
local current_background = vim.o.background
local colors = vim.list_extend(opts.colors or {}, vim.fn.getcompletion('', 'color'))
-- must add ':nohidden' or fzf ignore the preview action
-- disabling our live preview of colorschemes
opts.fzf_opts['--preview'] = prev_act
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'nohidden:right:0'
core.fzf_wrap(opts, colors, function(selected)
if opts.live_preview then
-- must add ':nohidden' or fzf ignores the preview action
opts.fzf_opts['--preview-window'] = 'nohidden:right:0'
opts.preview = shell.raw_action(function (args)
if opts.live_preview and args then
local colorscheme = args[1]
vim.cmd("colorscheme " .. colorscheme)
end
end, nil, opts.debug)
end
opts.fn_selected = function(selected)
-- reset color scheme if live_preview is enabled
-- and nothing or non-default action was selected
if opts.live_preview and (not selected or #selected[1]>0) then
@ -53,8 +53,9 @@ M.colorschemes = function(opts)
if opts.post_reset_cb then
opts.post_reset_cb()
end
end
end)()
core.fzf_exec(colors, opts)
end

@ -124,9 +124,7 @@ M.branches = function(opts)
if not opts then return end
opts.fzf_opts["--no-multi"] = ''
opts.__preview = path.git_cwd(opts.preview, opts)
-- nullify 'opts.preview' doesn't take priority over '--preview'
opts.preview = nil
opts.fzf_opts["--preview"] = shell.preview_action_cmd(function(items)
opts.preview = shell.raw_preview_action_cmd(function(items)
local branch = items[1]:gsub("%*", "") -- remove the * from current branch
if branch:find("%)") ~= nil then
-- (HEAD detached at origin/master)

@ -92,7 +92,6 @@ M.helptags = function(opts)
if not opts then return end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(fzf_fn, opts)

@ -17,7 +17,6 @@ M.manpages = function(opts)
end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(opts.cmd, opts)
end

@ -57,7 +57,6 @@ local history = function(opts, str)
end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(entries, opts)
end
@ -329,7 +328,6 @@ M.spell_suggest = function(opts)
if vim.tbl_isempty(entries) then return end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(entries, opts)
@ -344,7 +342,6 @@ M.filetypes = function(opts)
if vim.tbl_isempty(entries) then return end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(entries, opts)
@ -360,7 +357,6 @@ M.packadd = function(opts)
if vim.tbl_isempty(entries) then return end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(entries, opts)
@ -397,7 +393,6 @@ M.menus = function(opts)
end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
core.fzf_exec(entries, opts)

@ -105,8 +105,7 @@ M.ui_select = function(items, opts, on_choice)
config.set_action_helpstr(_opts.actions['default'], "accept-item")
core.fzf_wrap(_opts, entries, function(selected)
_opts.fn_selected = function(selected)
config.set_action_helpstr(_opts.actions['default'], nil)
if not selected then
@ -118,8 +117,9 @@ M.ui_select = function(items, opts, on_choice)
if _opts.post_action_cb then
_opts.post_action_cb()
end
end
end)()
core.fzf_exec(entries, _opts)
end

@ -107,8 +107,14 @@ function M.action(fn, fzf_field_expression ,debug)
end
M.preview_action_cmd = function(fn, fzf_field_expression, debug)
local action_string, id =
M.raw_preview_action_cmd(fn, fzf_field_expression, debug)
return vim.fn.shellescape(action_string), id
end
M.raw_preview_action_cmd = function(fn, fzf_field_expression, debug)
return M.async_action(function(pipe, ...)
return M.raw_async_action(function(pipe, ...)
local function on_finish(_, _)
if pipe and not uv.is_closing(pipe) then

@ -295,8 +295,12 @@ end
function FzfWin:reset_win_highlights(win, is_border)
local hl = ("Normal:%s,FloatBorder:%s"):format(
self.winopts.hl.normal, self.winopts.hl.border)
if self._previewer and self.winopts.hl.cursorline then
hl = hl .. (",CursorLine:%s"):format(self.winopts.hl.cursorline)
if self._previewer then
for _, h in ipairs({ 'CursorLine', 'CursorLineNr' }) do
if self.winopts.hl[h:lower()] then
hl = hl .. (",%s:%s"):format(h, self.winopts.hl[h:lower()])
end
end
end
if is_border then
-- our border is manuually drawn so we need

Loading…
Cancel
Save