-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(select): add support for fzf-lua (#14)
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |