fix(chezmoi): same leader-fc behavior for fzf-lua as telescope

Pick all nvim config files and edit with or without chezmoi.
This commit is contained in:
qw457812 2024-07-27 23:11:08 +08:00
parent 2c681260af
commit 710270d657

View file

@ -1,36 +1,26 @@
-- exclude directories and externals -- exclude directories and externals
local chezmoi_list_args = { "--include", "files", "--exclude", "externals" } local chezmoi_list_args = { "--include", "files", "--exclude", "externals" }
---@param targets? string|string[] local pick_chezmoi = function()
local function fzf_chezmoi(targets) if LazyVim.pick.picker.name == "telescope" then
require("telescope").extensions.chezmoi.find_files()
elseif LazyVim.pick.picker.name == "fzf" then
local fzf_lua = require("fzf-lua") local fzf_lua = require("fzf-lua")
local chezmoi = require("chezmoi.commands") local chezmoi = require("chezmoi.commands")
local results = chezmoi.list({ local results = chezmoi.list({ args = chezmoi_list_args })
targets = targets or {},
args = chezmoi_list_args,
})
local opts = { local opts = {
fzf_opts = {}, fzf_opts = {},
fzf_colors = true, fzf_colors = true,
actions = { actions = {
["default"] = function(selected) ["default"] = function(selected)
if not vim.tbl_isempty(selected) then if not vim.tbl_isempty(selected) then
chezmoi.edit({ chezmoi.edit({ targets = "~/" .. selected[1] })
targets = "~/" .. selected[1],
})
end end
end, end,
}, },
} }
fzf_lua.fzf_exec(results, opts) fzf_lua.fzf_exec(results, opts)
end end
local pick_chezmoi = function()
if LazyVim.pick.picker.name == "telescope" then
require("telescope").extensions.chezmoi.find_files()
elseif LazyVim.pick.picker.name == "fzf" then
fzf_chezmoi()
end
end end
--- pick nvim config --- pick nvim config
@ -60,9 +50,7 @@ local pick_config = function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection then if selection then
chezmoi.edit({ chezmoi.edit({ targets = config_dir .. "/" .. selection.value })
targets = config_dir .. "/" .. selection.value,
})
end end
end end
@ -71,7 +59,7 @@ local pick_config = function()
end end
-- it's possible that only part of nvim config files are managed with chezmoi -- it's possible that only part of nvim config files are managed with chezmoi
-- pick all and just edit if unmanaged -- pick them all and edit with or without chezmoi
actions.select_default:replace_if(function() actions.select_default:replace_if(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
return selection and vim.tbl_contains(managed_config_files, config_dir .. "/" .. selection.value) return selection and vim.tbl_contains(managed_config_files, config_dir .. "/" .. selection.value)
@ -80,8 +68,23 @@ local pick_config = function()
end, end,
}) })
elseif LazyVim.pick.picker.name == "fzf" then elseif LazyVim.pick.picker.name == "fzf" then
-- only pick the managed config files require("fzf-lua").files({
fzf_chezmoi(config_dir) cwd = config_dir,
actions = {
["default"] = function(selected, opts)
if vim.tbl_isempty(selected) then
return
end
local file = require("fzf-lua.path").entry_to_file(selected[1], opts).path
if vim.tbl_contains(managed_config_files, file) then
chezmoi.edit({ targets = file })
else
require("fzf-lua.actions").file_edit(selected, opts)
end
end,
},
})
end end
end end