Skip to content

Commit

Permalink
feat(select): add support for fzf-lua (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jan 17, 2022
1 parent f03962c commit c2208c3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lua/dressing/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local default_config = {
},
select = {
-- Priority list of preferred vim.select implementations
backend = { "telescope", "fzf", "builtin", "nui" },
backend = { "telescope", "fzf_lua", "fzf", "builtin", "nui" },

-- Options for telescope selector
telescope = {
Expand All @@ -44,6 +44,14 @@ local default_config = {
},
},

-- Options for fzf_lua selector
fzf_lua = {
winopts = {
width = 0.5,
height = 0.4,
},
},

-- Options for nui Menu
nui = {
position = "50%",
Expand Down
33 changes: 33 additions & 0 deletions lua/dressing/select/fzf_lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local M = {}

M.is_supported = function()
return pcall(require, "fzf-lua")
end

M.select = function(config, items, opts, on_choice)
local fzf = require("fzf-lua")
local labels = {}
for i, item in ipairs(items) do
table.insert(labels, string.format("%d: %s", i, opts.format_item(item)))
end

local fzf_opts = vim.tbl_deep_extend("keep", config, {
fzf_opts = {
["--no-multi"] = "",
["--preview-window"] = "hidden:right:0",
},
})
fzf.fzf_wrap(fzf_opts, labels, function(selected)
if not selected then
on_choice(nil, nil)
else
local label = selected[1]
local colon = string.find(label, ":")
local lnum = tonumber(string.sub(label, 1, colon - 1))
local item = items[lnum]
on_choice(item, lnum)
end
end)()
end

return M
21 changes: 21 additions & 0 deletions tests/manual/select.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Run this test with :source %

local function run_test(backend)
local config = require("dressing.config")
local prev_backend = config.select.backend
config.select.backend = backend
vim.ui.select({ "first", "second", "third" }, {
prompt = "Make selection",
kind = "test",
}, function(item, lnum)
if item and lnum then
vim.notify(string.format("selected '%s' (idx %d)", item, lnum), vim.log.levels.INFO)
else
vim.notify("Selection canceled", vim.log.levels.INFO)
end
config.select.backend = prev_backend
end)
end

-- Replace this with the desired backend to test
run_test("fzf_lua")

0 comments on commit c2208c3

Please sign in to comment.