From 0808d756dfa38b00c4496d8605353286ab15bd8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Vigil=20V=C3=A1squez?= Date: Mon, 25 Mar 2024 11:42:59 -0300 Subject: [PATCH] fix: gracefully skip insertion on empty selection --- .stylua.toml | 10 ++++++++++ lua/esqueleto/utils.lua | 42 ++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 .stylua.toml diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 0000000..fa10e57 --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,10 @@ +column_width = 96 +line_endings = "Unix" +indent_type = "Spaces" +indent_width = 2 +quote_style = "ForceDouble" +call_parentheses = "Always" +collapse_simple_statement = "Always" + +[sort_requires] +enabled = true diff --git a/lua/esqueleto/utils.lua b/lua/esqueleto/utils.lua index 1524c3d..4d408c3 100644 --- a/lua/esqueleto/utils.lua +++ b/lua/esqueleto/utils.lua @@ -10,9 +10,7 @@ M.capture = function(cmd, raw) local f = assert(io.popen(cmd, "r")) local s = assert(f:read("*a")) f:close() - if raw then - return s - end + if raw then return s end s = string.gsub(s, "^%s+", "") s = string.gsub(s, "%s+$", "") s = string.gsub(s, "[\n\r]+", "") @@ -73,33 +71,34 @@ end -- 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)) + 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 {} + 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 + 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) + or vim.tbl_contains(listignored(dir, os_ignore_pats), filepath) + or vim.tbl_contains(listignored(dir, extra_ignore_pats), filepath) end end @@ -111,9 +110,10 @@ 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) + 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 @@ -131,9 +131,7 @@ M.gettemplates = function(pattern, opts) -- 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 + if ndirs > 1 then name = vim.fn.simplify(directory) .. " :: " .. name end templates[name] = filepath end end @@ -158,9 +156,7 @@ M.selecttemplate = function(templates, opts) -- Alphabetically sort template names for a more pleasing experience local templatenames = vim.tbl_keys(templates) - table.sort(templatenames, function(a, b) - return a:lower() < b:lower() - end) + table.sort(templatenames, function(a, b) return a:lower() < b:lower() end) -- If only one template, write and return early if #templatenames == 1 and opts.autouse then @@ -171,7 +167,9 @@ M.selecttemplate = function(templates, opts) -- Select template vim.ui.select(templatenames, { prompt = "Select skeleton to use:" }, function(choice) if templates[choice] then - M.writetemplate(vim.loop.fs_realpath(templates[choice]), opts) + M.writetemplate(vim.loop.fs_realpath(templates[choice]), opts) + else + vim.notify("[esqueleto] No template selected, leaving buffer empty", vim.log.levels.INFO) end end) end