diff --git a/README.md b/README.md index aaefd4a..4de0b1a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ your `init.lua`: require("esqueleto").setup( { -- Directory where templates are stored - directory = "~/.config/nvim/skeletons/", + directories = {"~/.config/nvim/skeletons/"} -- Patterns to match when creating new file -- Can be either (i) file names or (ii) file types. @@ -55,6 +55,14 @@ require("esqueleto").setup( For more information, refer to docs (`:h esqueleto`). For example skeleton files, check [the `skeletons` folder](skeletons/). +The default options of `esqueleto` are +~~~lua + { + directories = { vim.fn.stdpath("config") .. "/skeletons" }, + patterns = { } + } +~~~ + ## Roadmap `esqueleto.nvim` is in its infancy (therefore, expect breaking changes from time to time). @@ -62,7 +70,6 @@ I intend on extending this plugin with some functionality I would like for a tem manager. At some point `esqueleto.nvim` should have the following (ordered by priority): - Template creation interface -- Handle multiple template folders - Project specific templates - Template preview via [Telescope](https://github.com/nvim-telescope/telescope.nvim) - User customizable prompt and insertion rules diff --git a/doc/esqueleto.txt b/doc/esqueleto.txt index 6b9100c..d3a77ad 100644 --- a/doc/esqueleto.txt +++ b/doc/esqueleto.txt @@ -42,7 +42,7 @@ To setup |esqueleto|, tow things must be done: First, |esqueleto| setup in the `init.lua` file is as follows: > require('esqueleto').setup({ -- Default templates directory - directory = '~/.config/nvim/skeleton/' + directories = { '~/.config/nvim/skeleton/' }, -- Patterns to detect for template insetion (empty by default, -- adding as an example) @@ -114,11 +114,11 @@ setup({patterns}, {directory}) Setup for specific file {patterns} using the templates inside a given {directory}. - Example: > + Defaults: > esqueleto.setup( { - patterns = { 'README.md', 'python' }, - directory = '~/.config/nvim/skeleton/' + directories = {vim.fn.stdpath("config") .. "/skeletons"}, + patterns = { }, } ) < diff --git a/lua/esqueleto/init.lua b/lua/esqueleto/init.lua index 7b3f076..6b771e7 100644 --- a/lua/esqueleto/init.lua +++ b/lua/esqueleto/init.lua @@ -34,25 +34,38 @@ end -- Template retriever -- TODO: Add description -M.get = function(pattern) - -- get all templates available for pattern - local templates = M._scandir(vim.fs.normalize(M._defaults.directory), pattern) - if vim.tbl_isempty(templates) then return nil end - - -- create table with template types for pattern - local types = {} - for _, template in pairs(templates) do - local _file = vim.fs.basename(template) - types[template] = M._defaults.directory .. pattern .. "/" .. _file +M.get = function(pattern, alldirectories) + local templates = {} + + -- Count directories that contain templates for pattern + local ndirs = 0 + for _, directory in pairs(alldirectories) do + directory = vim.fn.fnamemodify(directory, ':p') -- expand path + ndirs = ndirs + vim.fn.isdirectory(directory .. pattern .. '/') end - return types + -- Get templates for pattern + for _, directory in ipairs(alldirectories) do + directory = vim.fn.fnamemodify(directory, ':p') -- expand path + if vim.fn.isdirectory(directory .. pattern .. '/') == 1 then + for filepath in vim.fs.dir(directory .. pattern .. '/') do + filepath = directory .. filepath + local name = vim.fs.basename(filepath) + if ndirs > 1 then + name = vim.fn.simplify(directory) .. " :: " .. name + end + templates[name] = filepath + end + end + end + + return templates end -- Template inserter -- TODO(Carlos): Add description M.insert = function(pattern) - local templates = M.get(pattern) + local templates = M.get(pattern, M._defaults.directories) local file = M.select(templates) if file ~= nil then @@ -63,7 +76,7 @@ end -- Defaults M._defaults = { patterns = {}, - directory = vim.fn.stdpath("config") .. "/skeletons", + directories = {vim.fn.stdpath("config") .. "/skeletons"}, } M._template_inserted = {}