diff --git a/lua/esqueleto/autocmd.lua b/lua/esqueleto/autocmd.lua index 37dd293..f67695b 100644 --- a/lua/esqueleto/autocmd.lua +++ b/lua/esqueleto/autocmd.lua @@ -8,20 +8,27 @@ M.createautocmd = function(opts) -- create autocommands for skeleton insertion local group = vim.api.nvim_create_augroup("esqueleto", { clear = true }) - vim.api.nvim_create_autocmd({ "BufWinEnter", "BufReadPost", "FileType" }, { + local function getpatterns() + if type(opts.patterns) == "function" then + if type(opts.directories) == "table" then + return vim.tbl_map(opts.patterns, opts.directories) + else + return opts.patterns(opts.directories) + end + else + return opts.patterns + end + end + + vim.api.nvim_create_autocmd({ "BufNewFile", "BufReadPost", "FileType" }, { group = group, desc = "esqueleto.nvim :: Insert template", - pattern = opts.patterns, + pattern = getpatterns(), callback = function() - if vim.bo.buftype == "nofile" then - return nil - end - + if vim.bo.buftype == "nofile" then return nil end local filepath = vim.fn.expand("%") local emptyfile = vim.fn.getfsize(filepath) < 4 - if emptyfile then - utils.inserttemplate(opts) - end + if emptyfile then utils.inserttemplate(opts) end end, }) end diff --git a/lua/esqueleto/config.lua b/lua/esqueleto/config.lua index b9f4697..7c970d2 100644 --- a/lua/esqueleto/config.lua +++ b/lua/esqueleto/config.lua @@ -4,8 +4,8 @@ local M = {} M.default_config = { autouse = true, - directories = { vim.fn.stdpath("config") .. "/skeletons" }, - patterns = {}, + directories = { vim.fn.stdpath("config") .. "/skeleton" }, + patterns = function(dir) return vim.fn.readdir(dir) end, wildcards = { expand = true, lookup = { @@ -48,8 +48,8 @@ M.updateconfig = function(config) -- Validate setup vim.validate({ autouse = { config.autouse, 'boolean' }, - directories = { config.directories, 'table' }, - patterns = { config.patterns, 'table' }, + directories = { config.directories, { 'table' } }, + patterns = { config.patterns, { 'table', 'function' } }, wildcards = { config.wildcards, "table" }, advanced = { config.advanced, 'table' }, ["advanced.ignored"] = { config.advanced.ignored, { 'table', 'function' } },