feat(chezmoi): replace leader-fc with chezmoi edit

For telescope, pick all nvim config files, edit with or without chezmoi.

For fzf, only pick nvim config files which managed by chezmoi for now.

Remove unnecessary `--watch` args for fzf-lua since we have autocmd,
besides telescope don't have that args anyway.
This commit is contained in:
qw457812 2024-07-23 14:57:02 +08:00
parent 9d91b813dd
commit 16db75c4d9

View file

@ -1,26 +1,84 @@
---@param targets? string|string[]
local function fzf_chezmoi(targets)
local fzf_lua = require("fzf-lua")
local chezmoi = require("chezmoi.commands")
local results = chezmoi.list({
targets = targets or {},
args = { "--include", "files" }, -- exclude directories
})
local opts = {
fzf_opts = {},
fzf_colors = true,
actions = {
["default"] = function(selected)
if not vim.tbl_isempty(selected) then
chezmoi.edit({
targets = "~/" .. selected[1],
})
end
end,
},
}
fzf_lua.fzf_exec(results, opts)
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
local fzf_lua = require("fzf-lua")
local results = require("chezmoi.commands").list({
args = { "--include", "files" }, -- exclude directories
})
local chezmoi = require("chezmoi.commands")
fzf_chezmoi()
end
end
local opts = {
fzf_opts = {},
fzf_colors = true,
actions = {
["default"] = function(selected)
chezmoi.edit({
targets = { "~/" .. selected[1] },
args = { "--watch" },
})
end,
},
}
fzf_lua.fzf_exec(results, opts)
--- pick nvim config
local pick_config = function()
local chezmoi = require("chezmoi.commands")
local config_dir = vim.fn.stdpath("config")
local managed_config_files = chezmoi.list({
targets = config_dir,
args = { "--path-style", "absolute", "--include", "files" },
})
if vim.tbl_isempty(managed_config_files) then
LazyVim.pick.config_files()()
return
end
if LazyVim.pick.picker.name == "telescope" then
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local config = require("chezmoi").config
require("telescope.builtin").find_files({
prompt_title = "Config Files",
cwd = config_dir,
attach_mappings = function(prompt_bufnr, map)
local edit_action = function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
if selection then
chezmoi.edit({
targets = config_dir .. "/" .. selection.value,
})
end
end
for _, v in ipairs(config.telescope.select) do
map("i", v, "select_default")
end
-- it's possible that only part of nvim config files are managed with chezmoi
-- pick all and just edit if unmanaged
actions.select_default:replace_if(function()
local selection = action_state.get_selected_entry()
return selection and vim.tbl_contains(managed_config_files, config_dir .. "/" .. selection.value)
end, edit_action)
return true
end,
})
elseif LazyVim.pick.picker.name == "fzf" then
-- only pick the managed config files
fzf_chezmoi(config_dir)
end
end
@ -36,11 +94,8 @@ return {
{
"xvzc/chezmoi.nvim",
keys = {
{
"<leader>sz",
pick_chezmoi,
desc = "Chezmoi",
},
{ "<leader>sz", pick_chezmoi, desc = "Chezmoi" },
{ "<leader>fc", pick_config, desc = "Find Config File" },
},
opts = {
edit = {
@ -66,6 +121,17 @@ return {
})
end,
},
{
"ibhagwan/fzf-lua",
optional = true,
keys = { { "<leader>fc", false } },
},
{
"nvim-telescope/telescope.nvim",
optional = true,
keys = { { "<leader>fc", false } },
},
{
"nvimdev/dashboard-nvim",
optional = true,