Skip to content

Commit

Permalink
feat(API): simplify preview as lua function
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed May 8, 2024
1 parent 64f6eff commit a8e1659
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
24 changes: 24 additions & 0 deletions lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,30 @@ M.build_fzf_cli = function(opts)
opts.fzf_opts["--" .. flag] = opts[flag]
end
end
-- convert preview action functions to strings using our shell wrapper
do
local preview_cmd
local preview_spec = opts.fzf_opts["--preview"]
if type(preview_spec) == "function" then
preview_cmd = shell.raw_action(preview_spec, "{}", opts.debug)
elseif type(preview_spec) == "table" then
preview_spec = vim.tbl_extend("keep", preview_spec, {
fn = preview_spec.fn or preview_spec[1],
-- by default we use current item only "{}"
-- using "{+}" will send multiple selected items
field_index = "{}",
})
if preview_spec.type == "cmd" then
preview_cmd = shell.raw_preview_action_cmd(
preview_spec.fn, preview_spec.field_index, opts.debug)
else
preview_cmd = shell.raw_action(preview_spec.fn, preview_spec.field_index, opts.debug)
end
end
if preview_cmd then
opts.fzf_opts["--preview"] = preview_cmd
end
end
opts.fzf_opts["--bind"] = M.create_fzf_binds(opts.keymap.fzf)
opts.fzf_opts["--color"] = M.create_fzf_colors(opts)
opts.fzf_opts["--expect"] = actions.expect(opts.actions)
Expand Down
10 changes: 4 additions & 6 deletions lua/fzf-lua/providers/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local uv = vim.uv or vim.loop
local core = require "fzf-lua.core"
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
local shell = require "fzf-lua.shell"
local config = require "fzf-lua.config"

local M = {}
Expand All @@ -21,11 +20,10 @@ M.metatable = function(opts)

table.sort(methods, function(a, b) return a < b end)

opts.preview = shell.raw_action(function(args)
-- TODO: retrieve method help
local help = ""
return string.format("%s:%s", args[1], help)
end, nil, opts.debug)
opts.preview = function(args)
local options_md = require("fzf-lua.cmd").options_md()
return type(options_md) == "table" and options_md[args[1]:lower()] or ""
end

opts.fzf_opts["--preview-window"] = "hidden:down:10"

Expand Down
13 changes: 6 additions & 7 deletions lua/fzf-lua/providers/nvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local uv = vim.uv or vim.loop
local core = require "fzf-lua.core"
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
local shell = require "fzf-lua.shell"
local config = require "fzf-lua.config"
local devicons = require "fzf-lua.devicons"

Expand Down Expand Up @@ -51,13 +50,13 @@ M.commands = function(opts)
table.sort(entries, function(a, b) return a < b end)
end

opts.preview = shell.raw_action(function(args)
opts.preview = function(args)
local cmd = args[1]
if commands[cmd] then
cmd = vim.inspect(commands[cmd])
end
return cmd
end, nil, opts.debug)
end

core.fzf_exec(entries, opts)
end
Expand Down Expand Up @@ -216,7 +215,7 @@ M.marks = function(opts)
string.format("%-5s %s %s %s", "mark", "line", "col", "file/text"))

opts.fzf_opts["--header-lines"] = 1
--[[ opts.preview = shell.raw_action(function (args, fzf_lines, _)
--[[ opts.preview = 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
Expand All @@ -228,7 +227,7 @@ M.marks = function(opts)
end
return "UNLOADED: " .. name
end
end) ]]
end ]]

core.fzf_exec(entries, opts)
end
Expand Down Expand Up @@ -274,11 +273,11 @@ M.registers = function(opts)
end
end

opts.preview = shell.raw_action(function(args)
opts.preview = function(args)
local r = args[1]:match("%[(.*)%] ")
local _, contents = pcall(vim.fn.getreg, r)
return contents and register_escape_special(contents) or args[1]
end, nil, opts.debug)
end

core.fzf_exec(entries, opts)
end
Expand Down
8 changes: 1 addition & 7 deletions lua/fzf-lua/shell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ function M.raw_async_action(fn, fzf_field_expression, debug)
end)
uv.pipe_connect(pipe, pipe_path, function(err)
if err then
error(string.format("pipe_connect(%s) failed with error: %s",
pipe_path, err))
error(string.format("pipe_connect(%s) failed with error: %s", pipe_path, err))
else
vim.schedule(function()
fn(pipe, unpack(args))
Expand Down Expand Up @@ -110,11 +109,6 @@ function M.raw_async_action(fn, fzf_field_expression, debug)
return action_cmd, id
end

function M.async_action(fn, fzf_field_expression, debug)
local action_string, id = M.raw_async_action(fn, fzf_field_expression, debug)
return libuv.shellescape(action_string), id
end

function M.raw_action(fn, fzf_field_expression, debug)
local receiving_function = function(pipe, ...)
local ret = fn(...)
Expand Down

0 comments on commit a8e1659

Please sign in to comment.