diff --git a/lua/lazyvim/plugins/extras/util/chezmoi.lua b/lua/lazyvim/plugins/extras/util/chezmoi.lua index 69dcc1d3..2ccf0bc0 100644 --- a/lua/lazyvim/plugins/extras/util/chezmoi.lua +++ b/lua/lazyvim/plugins/extras/util/chezmoi.lua @@ -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 = { - { - "sz", - pick_chezmoi, - desc = "Chezmoi", - }, + { "sz", pick_chezmoi, desc = "Chezmoi" }, + { "fc", pick_config, desc = "Find Config File" }, }, opts = { edit = { @@ -66,6 +121,17 @@ return { }) end, }, + { + "ibhagwan/fzf-lua", + optional = true, + keys = { { "fc", false } }, + }, + { + "nvim-telescope/telescope.nvim", + optional = true, + keys = { { "fc", false } }, + }, + { "nvimdev/dashboard-nvim", optional = true,