Skip to content

Commit

Permalink
Merge pull request #16 from cvigilv/develop
Browse files Browse the repository at this point in the history
Add multiple template directories support
  • Loading branch information
cvigilv authored Jan 3, 2023
2 parents 0b717b8 + 8997db4 commit 7764077
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -55,14 +55,21 @@ 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).
I intend on extending this plugin with some functionality I would like for a template
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
Expand Down
8 changes: 4 additions & 4 deletions doc/esqueleto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = { },
}
)
<
Expand Down
39 changes: 26 additions & 13 deletions lua/esqueleto/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -63,7 +76,7 @@ end
-- Defaults
M._defaults = {
patterns = {},
directory = vim.fn.stdpath("config") .. "/skeletons",
directories = {vim.fn.stdpath("config") .. "/skeletons"},
}

M._template_inserted = {}
Expand Down

0 comments on commit 7764077

Please sign in to comment.