Skip to content

Commit

Permalink
Refactor plugin init (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-kotake authored Nov 17, 2024
2 parents f66272b + f0b82bb commit 1d48539
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .cz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ commitizen:
name: cz_conventional_commits
tag_format: $version
update_changelog_on_bump: true
version: 2.0.0
version: 2.0.1
version_scheme: semver
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.1 (2024-11-16)

### Refactor

- simplify initialization code

## 2.0.0 (2024-11-16)

### BREAKING CHANGE
Expand Down
123 changes: 47 additions & 76 deletions lua/nvim-chezmoi/core/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,104 +6,75 @@ local path = require("plenary.path")
local M = {}

M.init = function(source_path)
M.source_path = source_path
end

M.source_files = function(opts)
local files = {}

if type(M.source_path) ~= "string" then
local result = require("nvim-chezmoi.chezmoi.commands.source_path"):exec()
if result.success then
M.source_path = result.data[1]
end
local telescope_ok, telescope = pcall(require, "telescope")
if not telescope_ok then
return
end

local source_path = M.source_path
local chezmoi_files = scan.scan_dir(source_path, {
hidden = opts.hidden or false,
search_pattern = opts.pattern,
})
M.source_path = source_path

for _, chezmoi_file in ipairs(chezmoi_files) do
local file_path = path:new(chezmoi_file):normalize(source_path)
if type(opts.pathResolveFn) == "function" then
file_path = opts.pathResolveFn(file_path)
end
local chezmoi_managed = require("nvim-chezmoi.chezmoi.commands.managed")
chezmoi_managed:create_user_commands()

files[#files + 1] = {
chezmoi_file,
file_path,
}
local user_commands = {
{
name = "ChezmoiFiles",
callback = function()
vim.cmd("Telescope nvim-chezmoi special_files")
end,
opts = {
desc = "Chezmoi special files under source path",
nargs = 0,
},
},
}

for _, cmd in ipairs(user_commands) do
vim.api.nvim_create_user_command(cmd.name, cmd.callback, cmd.opts)
end

return files
telescope.load_extension("nvim-chezmoi")
end

M.source_managed = function()
local chezmoi_managed = require("nvim-chezmoi.chezmoi.commands.managed")
local files = chezmoi_managed:exec()
local files = require("nvim-chezmoi.chezmoi.commands.managed"):exec()
local managed_files = {}
if files.success then
for _, v in pairs(files.data) do
managed_files[#managed_files + 1] = {
v.sourceAbsolute,
v.relative,
file = v.sourceAbsolute,
display = v.relative,
target_file = v.absolute,
isEncrypted = v:isEncrypted(),
}
end
end
return managed_files
end

M.chezmoi_files = function()
return M.source_files({
local files = {}

if type(M.source_path) ~= "string" then
local result = require("nvim-chezmoi.chezmoi.commands.source_path"):exec()
M.source_path = result.data[1]
end

local source_path = M.source_path
local chezmoi_files = scan.scan_dir(source_path, {
hidden = true,
pattern = "%.chezmoi*",
search_pattern = "%.chezmoi*",
})

for _, chezmoi_file in ipairs(chezmoi_files) do
local file_path = path:new(chezmoi_file):normalize(source_path)
files[#files + 1] = {
file = chezmoi_file,
display = file_path,
}
end

return files
end

return M

-- M.managed = function()
-- local result = chezmoi.managed({
-- "--path-style",
-- "absolute",
-- "--exclude",
-- "externals,dirs,scripts",
-- })
-- if not result.success then
-- return {}
-- end
--
-- local chezmoi_source_path = chezmoi.target_path()
-- local source_files = {}
--
-- for _, value in ipairs(result.data) do
-- local source_file
-- local target_file
-- local cached = chezmoi_cache.find("managed_full_" .. value, { value })
-- if cached ~= nil then
-- source_file = cached.result[2]
-- target_file = cached.result[3]
-- else
-- local source_path = chezmoi.source_path({ value })
-- if source_path.success then
-- source_file = source_path.data[1]
-- target_file = value
-- end
-- end
--
-- if source_file ~= nil and source_file ~= "" then
-- local data = {
-- chezmoi_source_path.data[1],
-- source_file,
-- target_file:gsub("^" .. chezmoi_source_path.data[1] .. "/?", ""),
-- }
-- source_files[#source_files + 1] = data
-- chezmoi_cache.new("managed_full_" .. value, { value }, data)
-- end
-- end
--
-- return source_files
-- end
--
87 changes: 27 additions & 60 deletions lua/nvim-chezmoi/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,13 @@ local setup_plugin = function()
group = utils.augroup("SourcePath"),
pattern = M.opts.source_path .. "/*",
callback = function(ev)
chezmoi_exec_tmpl:create_buf_user_commands(ev.buf)
chezmoi_edit:create_buf_user_commands(ev.buf)
chezmoi_edit:detect_filetype(ev.buf)
chezmoi_exec_tmpl:create_buf_user_commands(ev.buf)
end,
})

M.load_telescope()
end

local setup_plugin_no_source_path = function()
local chezmoi = require("nvim-chezmoi.chezmoi.commands.source_path")
chezmoi:async(nil, function(result)
if not result.success then
return
end

local source_path = result.data[1]
M.opts.source_path = source_path
setup_plugin()

-- Create buf user commands for already opened source file buffer.
local utils = require("nvim-chezmoi.core.utils")
for _, buf in ipairs(vim.fn.getbufinfo({ buf = "buflisted" })) do
-- Get the file name of the buffer (bufname is under the `bufname` field)
local file_path = vim.fn.bufname(buf.bufnr)
-- Only check buffers with a valid file path
if file_path ~= "" and utils.is_child_of(source_path, file_path) then
vim.api.nvim_exec_autocmds({ "BufRead" }, {
buffer = buf.bufnr,
})
end
end
end)
require("nvim-chezmoi.core.telescope").init(M.opts.source_path)
end

--- @param opts? NvimChezmoiConfig | nil
Expand All @@ -83,39 +57,32 @@ function M.setup(opts)
if M.opts.source_path ~= nil then
setup_plugin()
else
setup_plugin_no_source_path()
end
end

function M.load_telescope()
local telescope_ok, telescope = pcall(require, "telescope")
if not telescope_ok then
return
end

require("nvim-chezmoi.core.telescope").init(M.opts.source_path)

local chezmoi_managed = require("nvim-chezmoi.chezmoi.commands.managed")
chezmoi_managed:create_user_commands()

local user_commands = {
{
name = "ChezmoiFiles",
callback = function()
vim.cmd("Telescope nvim-chezmoi special_files")
end,
opts = {
desc = "Chezmoi special files under source path",
nargs = 0,
},
},
}

for _, cmd in ipairs(user_commands) do
vim.api.nvim_create_user_command(cmd.name, cmd.callback, cmd.opts)
require("nvim-chezmoi.chezmoi.commands.source_path"):async(
{},
function(result)
if not result.success then
return
end

M.opts.source_path = result.data[1]
setup_plugin()

-- Create buf user commands for already opened source file buffer.
local utils = require("nvim-chezmoi.core.utils")
for _, buf in ipairs(vim.fn.getbufinfo({ buf = "buflisted" })) do
local file_path = vim.fn.bufname(buf.bufnr)
-- Only check buffers with a valid file path
if
file_path ~= "" and utils.is_child_of(M.opts.source_path, file_path)
then
vim.api.nvim_exec_autocmds({ "BufRead" }, {
buffer = buf.bufnr,
})
end
end
end
)
end

telescope.load_extension("nvim-chezmoi")
end

return M
24 changes: 20 additions & 4 deletions lua/telescope/_extensions/nvim-chezmoi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ local picker_config_default = function(opts, title, filesFn)
end)(),
entry_maker = function(entry)
return {
value = entry[1],
path = entry[1],
display = entry[2],
ordinal = entry[2],
value = entry,
path = entry.file,
display = entry.display,
ordinal = entry.display,
}
end,
}),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
local value = selection.value
if value.isEncrypted then
require("nvim-chezmoi.chezmoi.commands.edit"):exec(
selection.value.target_file
)
return
end

vim.cmd.edit(selection.path)
end)
return true
end,
}
end

Expand Down

0 comments on commit 1d48539

Please sign in to comment.