Skip to content

Commit 6488ada

Browse files
committed
feat(profiles): apply per picker / call (closes #1729)
Examples: ```lua :FzfLua blines profile=ivy :lua FzfLua.blines({profile={"ivy","hide"}}) :lua require("fzf-lua").setup({blines = { "ivy" }}) :lua require("fzf-lua").setup({blines = { profile = "ivy" }}) ```
1 parent 96d0aeb commit 6488ada

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

lua/fzf-lua/config.lua

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ end
6262
M.setup_opts = {}
6363
M.globals = setmetatable({}, {
6464
__index = function(_, index)
65+
local function setup_opts()
66+
return M._profile_opts or M.setup_opts
67+
end
68+
local function setup_defaults()
69+
return M._profile_opts and (M._profile_opts.defaults or {}) or M.setup_opts.defaults or {}
70+
end
6571
-- build normalized globals, option priority below:
6672
-- (1) provider specific globals (post-setup)
6773
-- (2) generic global-defaults (post-setup), i.e. `setup({ defaults = { ... } })`
6874
-- (3) fzf-lua's true defaults (pre-setup, static)
6975
local fzflua_default = utils.map_get(M.defaults, index)
70-
local setup_default = utils.map_get(M.setup_opts.defaults, index)
71-
local setup_value = utils.map_get(M.setup_opts, index)
76+
local setup_default = utils.map_get(setup_defaults(), index)
77+
local setup_value = utils.map_get(setup_opts(), index)
7278
local function build_bind_tables(keys)
7379
-- bind tables are logical exception, do not merge with defaults unless `[1] == true`
7480
-- normalize all binds as lowercase to prevent duplicate keys (#654)
@@ -108,10 +114,10 @@ M.globals = setmetatable({}, {
108114
or (setup_value and (setup_value.actions or setup_value._actions)) then
109115
-- (2) the existence of the `actions` key implies we're dealing with a picker
110116
-- override global provider defaults supplied by the user's setup `defaults` table
111-
ret = vim.tbl_deep_extend("force", ret, M.setup_opts.defaults or {})
117+
ret = vim.tbl_deep_extend("force", ret, setup_defaults())
112118
end
113119
-- (3) override with the specific provider options from the users's `setup` option
114-
ret = vim.tbl_deep_extend("force", ret, utils.map_get(M.setup_opts, index) or {})
120+
ret = vim.tbl_deep_extend("force", ret, utils.map_get(setup_opts(), index) or {})
115121
return ret
116122
end,
117123
__newindex = function(_, index, _)
@@ -143,6 +149,17 @@ function M.normalize_opts(opts, globals, __resume_key)
143149
opts = opts()
144150
end
145151

152+
local profile = opts.profile or (function()
153+
if type(globals) == "string" then
154+
local picker_opts = M.globals[globals]
155+
return picker_opts.profile or picker_opts[1]
156+
end
157+
end)()
158+
if profile then
159+
-- TODO: we should probably cache the profiles
160+
M._profile_opts = utils.load_profiles(profile, 1)
161+
end
162+
146163
-- expand opts that were specified with a dot
147164
-- e.g. `:FzfLua files winopts.border=single`
148165
do
@@ -858,6 +875,9 @@ function M.normalize_opts(opts, globals, __resume_key)
858875
opts = opts.enrich(opts)
859876
end
860877

878+
-- nullify profile options
879+
M._profile_opts = nil
880+
861881
-- mark as normalized
862882
opts._normalized = true
863883

lua/fzf-lua/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function M.setup(opts, do_not_reset_defaults)
175175
if opts[1] then
176176
-- Did the user supply profile(s) to load?
177177
opts = vim.tbl_deep_extend("keep", opts,
178-
utils.load_profiles(opts[1], opts[2] == nil and 1 or opts[2], opts))
178+
utils.load_profiles(opts[1], opts[2] == nil and 1 or opts[2]))
179179
end
180180
if do_not_reset_defaults then
181181
-- no defaults reset requested, merge with previous setup options

lua/fzf-lua/utils.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ function M.load_profile_fname(fname, name, silent)
846846
end
847847
end
848848

849-
function M.load_profiles(profiles, silent, opts)
849+
function M.load_profiles(profiles, silent)
850850
local ret = {}
851851
local path = require("fzf-lua").path
852852
profiles = type(profiles) == "table" and profiles
@@ -866,10 +866,10 @@ function M.load_profiles(profiles, silent, opts)
866866
-- profile requires loading base profile(s)
867867
-- silent = 1, only warn if failed to load
868868
profile_opts = vim.tbl_deep_extend("keep",
869-
profile_opts, M.load_profiles(profile_opts[1], 1, opts))
869+
profile_opts, M.load_profiles(profile_opts[1], 1))
870870
end
871871
if type(profile_opts.fn_load) == "function" then
872-
profile_opts.fn_load(opts)
872+
profile_opts.fn_load()
873873
profile_opts.fn_load = nil
874874
end
875875
ret = vim.tbl_deep_extend("force", ret, profile_opts)

0 commit comments

Comments
 (0)