Skip to content

Commit 2bfb95a

Browse files
phanenibhagwan
authored andcommitted
refactor(config): extract normalize_tbl to expand dot keys
1 parent 5a0581c commit 2bfb95a

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

lua/fzf-lua/config.lua

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,40 @@ local eval = function(v, ...)
144144
return v
145145
end
146146

147+
148+
---expand opts that were specified with a dot
149+
---@param opts table
150+
local normalize_tbl = function(opts)
151+
-- convert keys only after full iteration or we will
152+
-- miss keys due to messing with map ordering
153+
local to_convert = {}
154+
for k, _ in pairs(opts) do
155+
if k:match("%.") then
156+
table.insert(to_convert, k)
157+
end
158+
end
159+
for _, k in ipairs(to_convert) do
160+
utils.map_set(opts, k, opts[k])
161+
opts[k] = nil
162+
end
163+
end
164+
147165
---@param opts fzf-lua.config.Base|{}|fun():table?
148166
---@param globals string|table?
149167
---@param __resume_key string?
150168
---@return fzf-lua.Config?
151169
function M.normalize_opts(opts, globals, __resume_key)
152-
if not opts then opts = {} end
153-
154170
-- opts can also be a function that returns an opts table
155-
if type(opts) == "function" then
156-
opts = opts() or {}
157-
end
171+
---@type fzf-lua.config.Base|{}
172+
opts = eval(opts) or {}
158173

159174
if opts._normalized then
160175
return opts
161176
end
162177

178+
-- e.g. `:FzfLua files winopts.border=single`
179+
normalize_tbl(opts)
180+
163181
local profile = opts.profile or (function()
164182
if type(globals) == "string" then
165183
local picker_opts = M.globals[globals]
@@ -171,23 +189,6 @@ function M.normalize_opts(opts, globals, __resume_key)
171189
M._profile_opts = utils.load_profiles(profile, 1)
172190
end
173191

174-
-- expand opts that were specified with a dot
175-
-- e.g. `:FzfLua files winopts.border=single`
176-
do
177-
-- convert keys only after full iteration or we will
178-
-- miss keys due to messing with map ordering
179-
local to_convert = {}
180-
for k, _ in pairs(opts) do
181-
if k:match("%.") then
182-
table.insert(to_convert, k)
183-
end
184-
end
185-
for _, k in ipairs(to_convert) do
186-
utils.map_set(opts, k, opts[k])
187-
opts[k] = nil
188-
end
189-
end
190-
191192
-- save the user's original call params separately
192193
opts.__call_opts = opts.__call_opts or utils.deepcopy(opts)
193194
opts.__call_fn = utils.__FNCREF2__()
@@ -209,7 +210,7 @@ function M.normalize_opts(opts, globals, __resume_key)
209210
-- merge with setup options "defaults" table
210211
globals = vim.tbl_deep_extend("keep", globals, M.setup_opts.defaults or {})
211212
end
212-
---@cast globals table
213+
---@cast globals fzf-lua.config.Base
213214

214215
-- merge current opts with revious __call_opts on resume
215216
if opts.resume then

lua/fzf-lua/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ _G.FzfLua = require("fzf-lua")
136136
---@field color_icons boolean?
137137
---@field _type "file"?
138138
---@field git_icons boolean?
139-
---@field _actions? fun():table
139+
---@field _actions? fun():fzf-lua.config.Actions
140140
---@field silent boolean?
141141
---@field _cached_hls string[]?
142142
---@field previewer fun(...)|table|string?
@@ -148,6 +148,7 @@ _G.FzfLua = require("fzf-lua")
148148
---@field __CTX fzf-lua.Ctx?
149149
---@field resume boolean?
150150
---@field no_resume boolean?
151+
---@field profile string|table?
151152
---set_headers
152153
---@field _headers string[]?
153154
---@field headers string[]?

0 commit comments

Comments
 (0)