Skip to content

Commit

Permalink
Merge pull request #26 from Futarimiti/main
Browse files Browse the repository at this point in the history
Provide option to ignore certain files
  • Loading branch information
cvigilv authored Oct 11, 2023
2 parents 03b2c98 + 87b437b commit 0d19a9b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 13 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ require("esqueleto").setup(
patterns = { "README.md", "python" },

-- whether to auto-use a template if it's the only one for a pattern
autouse = true
autouse = true,

advanced = {
-- List of files glob patterns to ignore
-- Or alternatively, a function that determines if a file should be ignored
ignored = {},

-- Ignore OS files like .DS_Store
-- Exhaustive list: https://www.toptal.com/developers/gitignore/api/windows,macos,linux
ignore_os_files = true,
},
}
)
```
Expand All @@ -63,7 +73,11 @@ The default options of `esqueleto` are
{
directories = { vim.fn.stdpath("config") .. "/skeletons" },
patterns = { },
autouse = true
autouse = true,
advanced = {
ignored = {},
ignore_os_files = true,
}
}
~~~

Expand Down
16 changes: 16 additions & 0 deletions doc/esqueleto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ First, |esqueleto| setup in the `init.lua` file is as follows: >
-- File type
'python',
},
-- Advanced options
advanced = {
ignore_os_files = true,
ignore = {}
},
})
<
Second (and based in the setup showcased above), the following step involves
Expand Down Expand Up @@ -119,6 +125,7 @@ setup({patterns}, {directory})
{
directories = {vim.fn.stdpath("config") .. "/skeletons"},
patterns = { },
advanced = { ignore = {}, ignore_os_files = true },
}
)
<
Expand All @@ -127,6 +134,15 @@ setup({patterns}, {directory})
{patterns} (table) File names or patterns to match
{directory} (string) An absolute or relative path to the directory
with templates.
{advanced} (table) Advanced options, including:
{ignore} (table|function) List of glob patterns of
files to be ignored; alternatively, a
predicate that determines if a file
should be ignored, given its full filepath.
• {ignore_os_files} (boolean) Whether to ignore OS files,
such as `.DS_Store`, `Desktop.ini`.
For an exhaustive list, see
`https://www.toptal.com/developers/gitignore/api/windows,macos,linux`

==============================================================================
*ex-commands*
Expand Down
7 changes: 7 additions & 0 deletions lua/esqueleto/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ M.default_config = {
autouse = true,
directories = { vim.fn.stdpath("config") .. "/skeletons" },
patterns = {},
advanced = {
ignored = {},
ignore_os_files = true,
}
}

M.updateconfig = function(config)
Expand All @@ -15,6 +19,9 @@ M.updateconfig = function(config)
autouse = { config.autouse, 'boolean' },
directories = { config.directories, 'table' },
patterns = { config.patterns, 'table' },
advanced = { config.advanced, 'table' },
["advanced.ignored"] = { config.advanced.ignored, { 'table', 'function' } },
["advanced.ignore_os_files"] = { config.advanced.ignore_os_files, 'boolean' },
})

return config
Expand Down
45 changes: 45 additions & 0 deletions lua/esqueleto/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local M = {}

-- OS-specific ignoring files, obtained from gitignore.io
-- `https://www.toptal.com/developers/gitignore/api/windows,macos,linux`
-- Index: Thu Sep 28 20:45:56 BST 2023
M.ignored_os_patterns = {
"*~",
".fuse_hidden*",
".directory",
".Trash-*",
".nfs*",
".DS_Store",
".AppleDouble",
".LSOverride",
"Icon",
"._*",
".DocumentRevisions-V100",
".fseventsd",
".Spotlight-V100",
".TemporaryItems",
".Trashes",
".VolumeIcon.icns",
".com.apple.timemachine.donotpresent",
".AppleDB",
".AppleDesktop",
"Network Trash Folder",
"Temporary Items",
".apdisk",
"*.icloud",
"Thumbs.db",
"Thumbs.db:encryptable",
"ehthumbs.db",
"ehthumbs_vista.db",
"*.stackdump",
"[Dd]esktop.ini",
"$RECYCLE.BIN/",
"*.cab",
"*.msi",
"*.msix",
"*.msm",
"*.msp",
"*.lnk",
}

return M
59 changes: 48 additions & 11 deletions lua/esqueleto/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,70 @@ local M = {}

_G.esqueleto_inserted = {}

-- Write template contents to buffer
M.writetemplate = function(file)
if file ~= nil then
vim.cmd("0r " .. file)
end
end

M.gettemplates = function(pattern, alldirectories)
-- List ignored files under a directory, given a list of glob patterns
local listignored = function(dir, ignored_patterns)
return vim.tbl_flatten(vim.tbl_map(function(patterns)
return vim.fn.globpath(dir, patterns, true, true, true)
end, ignored_patterns))
end

-- Returns a ignore checker
local getignorechecker = function(opts)
local os_ignore_pats = opts.advanced.ignore_os_files and require('esqueleto.constants').ignored_os_patterns or {}
local extra = opts.advanced.ignored
local extra_ignore_pats, extra_ignore_func = (function()
if type(extra) == 'function' then
return {}, extra
else
assert(type(extra) == 'table')
return extra, function(_) return false end
end
end)()

return function(filepath)
local dir = vim.fn.fnamemodify(filepath, ':p:h')
return extra_ignore_func(dir)
or vim.tbl_contains(listignored(dir, os_ignore_pats), filepath)
or vim.tbl_contains(listignored(dir, extra_ignore_pats), filepath)
end
end

M.gettemplates = function(pattern, opts)
local templates = {}
local isignored = getignorechecker(opts)

local alldirectories = vim.tbl_map(function(f)
return vim.fn.fnamemodify(f, ':p')
end, opts.directories)

-- 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

-- 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 .. pattern .. "/" .. filepath
local name = vim.fs.basename(filepath)
if ndirs > 1 then
name = vim.fn.simplify(directory) .. " :: " .. name
local pattern_dir = directory .. pattern .. '/'
local exists_dir = vim.fn.isdirectory(pattern_dir) == 1
if exists_dir then
for basename in vim.fs.dir(pattern_dir) do
local filepath = vim.fs.normalize(pattern_dir .. basename)
-- Check if pattern is ignored
if not isignored(filepath) then
local name = vim.fs.basename(filepath)
if ndirs > 1 then
name = vim.fn.simplify(directory) .. " :: " .. name
end
templates[name] = filepath
end
templates[name] = filepath
end
end
end
Expand Down Expand Up @@ -83,7 +120,7 @@ M.inserttemplate = function(opts)
end

-- Get templates for selected pattern
local templates = M.gettemplates(pattern, opts.directories)
local templates = M.gettemplates(pattern, opts)

-- Pop-up selection UI
M.selecttemplate(templates, opts)
Expand Down

0 comments on commit 0d19a9b

Please sign in to comment.