Skip to content

Commit

Permalink
feat(auto-completion): recursively inherit opts
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Apr 4, 2024
1 parent f60769d commit 7a965c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
26 changes: 26 additions & 0 deletions lua/fzf-lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,32 @@ function M.map_tolower(m)
return ret
end

-- Flatten map's keys recursively
-- { a = { a1 = ..., a2 = ... } }
-- will be transformed to:
-- {
-- ["a.a1"] = ...,
-- ["a.a2"] = ...,
-- }
---@param m table<string, unknown>?
---@return table<string, unknown>?
function M.map_flatten(m, prefix)
if vim.tbl_isempty(m) then return {} end
local ret = {}
prefix = prefix and string.format("%s.", prefix) or ""
for k, v in pairs(m) do
if type(v) == "table" and not v[1] then
local inner = M.map_flatten(v)
for ki, vi in pairs(inner) do
ret[prefix .. k .. "." .. ki] = vi
end
else
ret[prefix .. k] = v
end
end
return ret
end

local function hex2rgb(hexcol)
local r, g, b = hexcol:match("#(%x%x)(%x%x)(%x%x)")
if not r or not g or not b then return end
Expand Down
64 changes: 36 additions & 28 deletions plugin/fzf-lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,48 @@ end, {
end, commands)
end

local defaults = require("fzf-lua.defaults").defaults
local cmd_opts = defaults[l[2]] or {}
local opts = vim.tbl_filter(function(k)
return not k:match("^_")
end, vim.tbl_keys(cmd_opts))

-- Flatten map's keys recursively
-- { a = { a1 = ..., a2 = ... } }
-- will be transformed to:
-- {
-- ["a.a1"] = ...,
-- ["a.a2"] = ...,
-- }
local function map_flatten(t, prefix)
if vim.tbl_isempty(t) then return {} end
local ret = {}
prefix = prefix and string.format("%s.", prefix) or ""
for k, v in pairs(t) do
if type(v) == "table" then
local inner = map_flatten(v)
for ki, vi in pairs(inner) do
ret[prefix .. k .. "." .. ki] = vi
end
else
ret[prefix .. k] = v
-- Not all commands have their opts under the same key
local function cmd2key(cmd)
local cmd2cfg = {
{
patterns = { "^git_", "^dap", "^tmux_" },
transform = function(c) return c:gsub("_", ".") end
},
{
patterns = { "^lsp_code_actions$" },
transform = function(_) return "lsp.code_actions" end
},
{ patterns = { "^lsp_.*_symbols$" }, transform = function(_) return "lsp.symbols" end },
{ patterns = { "^lsp_" }, transform = function(_) return "lsp" end },
{ patterns = { "^diagnostics_" }, transform = function(_) return "dianostics" end },
{ patterns = { "^tags" }, transform = function(_) return "tags" end },
{ patterns = { "grep" }, transform = function(_) return "grep" end },
{ patterns = { "^complete_bline$" }, transform = function(_) return "complete_line" end },
}
for _, v in pairs(cmd2cfg) do
for _, p in ipairs(v.patterns) do
if cmd:match(p) then return v.transform(cmd) end
end
end
return ret
end

local utils = require("fzf-lua.utils")
local defaults = require("fzf-lua.defaults").defaults
local cmd_opts = utils.map_get(defaults, cmd2key(l[2]) or l[2]) or {}
local opts = vim.tbl_filter(function(k)
return not k:match("^_")
end, vim.tbl_keys(utils.map_flatten(cmd_opts)))

-- Add globals recursively, e.g. `winopts.fullscreen`
-- will be later retrieved using `utils.map_get(...)`
for _, k in ipairs({ "winopts", "keymap", "fzf_opts", "fzf_tmux_opts", "hls" }) do
opts = vim.tbl_flatten({ opts, vim.tbl_keys(map_flatten(defaults[k] or {}, k)) })
for k, v in pairs({
winopts = false,
keymap = false,
fzf_opts = false,
fzf_tmux_opts = false,
__HLS = "hls", -- rename prefix
}) do
opts = vim.tbl_flatten({ opts, vim.tbl_keys(utils.map_flatten(defaults[k] or {}, v or k)) })
end

-- Add generic options that apply to all pickers
Expand Down

0 comments on commit 7a965c8

Please sign in to comment.