Skip to content

Commit

Permalink
Format with stylua
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 authored and github-actions[bot] committed Oct 9, 2023
1 parent ae0f461 commit e175dab
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 76 deletions.
94 changes: 69 additions & 25 deletions lua/luasnip/loaders/from_lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ local function get_loaded_file_debuginfo()
end

local function _luasnip_load_file(file)
-- vim.loader.enabled does not seem to be official api, so always reset
-- if the loader is available.
-- To be sure, even pcall it, in case there are conditions under which
-- it might error.
-- vim.loader.enabled does not seem to be official api, so always reset
-- if the loader is available.
-- To be sure, even pcall it, in case there are conditions under which
-- it might error.
if vim.loader then
-- pcall, not sure if this can fail in some way..
-- Does not seem like it though
Expand Down Expand Up @@ -146,28 +146,39 @@ end
--- some root, and registers new files.
local Collection = {}
local Collection_mt = {
__index = Collection
__index = Collection,
}

function Collection.new(root, lazy, include_ft, exclude_ft, add_opts, lazy_watcher)
function Collection.new(
root,
lazy,
include_ft,
exclude_ft,
add_opts,
lazy_watcher
)
local ft_filter = loader_util.ft_filter(include_ft, exclude_ft)
local o = setmetatable({
root = root,
file_filter = function(path)
if not path:sub(1, #root) == root then
log.warn("Tried to filter file `%s`, which is not inside the root `%s`.", path, root)
log.warn(
"Tried to filter file `%s`, which is not inside the root `%s`.",
path,
root
)
return false
end
return lua_package_file_filter(path) and ft_filter(path)
end,
add_opts = add_opts,
lazy = lazy,
-- store ft -> set of files that should be lazy-loaded.
lazy_files = autotable(2, {warn = false}),
lazy_files = autotable(2, { warn = false }),
-- store, for all files in this collection, their filetype.
-- No need to always recompute it, and we can use this to store which
-- files belong to the collection.
path_ft = {}
path_ft = {},
}, Collection_mt)

-- only register files up to a depth of 2.
Expand All @@ -177,16 +188,19 @@ function Collection.new(root, lazy, include_ft, exclude_ft, add_opts, lazy_watch
vim.schedule_wrap(function()
-- detected new file, make sure it is allowed by our filters.
if o.file_filter(path) then
o:add_file(path, loader_util.collection_file_ft(o.root, path))
o:add_file(
path,
loader_util.collection_file_ft(o.root, path)
)
end
end)()
end,
change_file = function(path)
vim.schedule_wrap(function()
o:reload(path)
end)()
end
}, {lazy = lazy_watcher})
end,
}, { lazy = lazy_watcher })

if not watcher_ok then
error(("Could not create watcher: %s"):format(err))
Expand All @@ -204,7 +218,11 @@ function Collection:add_file(path, ft)

if self.lazy then
if not session.loaded_fts[ft] then
log.info("Registering lazy-load-snippets for ft `%s` from file `%s`", ft, path)
log.info(
"Registering lazy-load-snippets for ft `%s` from file `%s`",
ft,
path
)

-- only register to load later.
self.lazy_files[ft][path] = true
Expand All @@ -220,14 +238,16 @@ function Collection:add_file(path, ft)
self:add_file_snippets(path, ft)
end
function Collection:add_file_snippets(path, ft)
log.info(
"Adding snippets for filetype `%s` from file `%s`",
ft,
path
)
log.info("Adding snippets for filetype `%s` from file `%s`", ft, path)
local snippets, autosnippets = _luasnip_load_file(path)

loader_util.add_file_snippets(ft, path, snippets, autosnippets, self.add_opts)
loader_util.add_file_snippets(
ft,
path,
snippets,
autosnippets,
self.add_opts
)

ls.refresh_notify(ft)
end
Expand Down Expand Up @@ -278,18 +298,42 @@ local function _load(lazy, opts)
local exclude = opts.exclude
local lazy_paths = opts.lazy_paths or {}

local collection_roots = loader_util.resolve_root_paths(paths, "luasnippets")
local collection_roots =
loader_util.resolve_root_paths(paths, "luasnippets")
local lazy_roots = loader_util.resolve_lazy_root_paths(lazy_paths)

log.info("Found roots `%s` for paths `%s`.", vim.inspect(collection_roots), vim.inspect(paths))
log.info("Determined roots `%s` for lazy_paths `%s`.", vim.inspect(lazy_roots), vim.inspect(lazy_paths))
log.info(
"Found roots `%s` for paths `%s`.",
vim.inspect(collection_roots),
vim.inspect(paths)
)
log.info(
"Determined roots `%s` for lazy_paths `%s`.",
vim.inspect(lazy_roots),
vim.inspect(lazy_paths)
)

for paths_lazy, roots in pairs({[true] = lazy_roots, [false] = collection_roots}) do
for paths_lazy, roots in pairs({
[true] = lazy_roots,
[false] = collection_roots,
}) do
for _, collection_root in ipairs(roots) do
local ok, coll_or_err = pcall(Collection.new, collection_root, lazy, include, exclude, add_opts, paths_lazy)
local ok, coll_or_err = pcall(
Collection.new,
collection_root,
lazy,
include,
exclude,
add_opts,
paths_lazy
)

if not ok then
log.error("Could not create collection at %s: %s", collection_root, coll_or_err)
log.error(
"Could not create collection at %s: %s",
collection_root,
coll_or_err
)
else
table.insert(M.collections, coll_or_err)
end
Expand Down
85 changes: 49 additions & 36 deletions lua/luasnip/loaders/tree_watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local M = {}

local TreeWatcher = {}
local TreeWatcher_mt = {
__index = TreeWatcher
__index = TreeWatcher,
}
function TreeWatcher:stop_recursive()
for _, child_watcher in ipairs(self.dir_watchers) do
Expand All @@ -34,38 +34,45 @@ function TreeWatcher:start()
return
end
vim.schedule_wrap(function()
log.debug("raw: self.root: %s; err: %s; relpath: %s; change: %s; rename: %s", self.root, err, relpath, events.change, events.rename)
local full_path = Path.join(self.root, relpath)
local path_stat = uv.fs_stat(full_path)

-- try to figure out what happened in the directory.
if events.rename then
if not uv.fs_stat(self.root) then
self:remove_root()
return
end
if not path_stat then
self:remove_child(relpath, full_path)
return
end
log.debug(
"raw: self.root: %s; err: %s; relpath: %s; change: %s; rename: %s",
self.root,
err,
relpath,
events.change,
events.rename
)
local full_path = Path.join(self.root, relpath)
local path_stat = uv.fs_stat(full_path)

-- try to figure out what happened in the directory.
if events.rename then
if not uv.fs_stat(self.root) then
self:remove_root()
return
end
if not path_stat then
self:remove_child(relpath, full_path)
return
end

local f_type
if path_stat.type == "link" then
f_type = uv.fs_stat(uv.fs_realpath(full_path))
else
f_type = path_stat.type
end
local f_type
if path_stat.type == "link" then
f_type = uv.fs_stat(uv.fs_realpath(full_path))
else
f_type = path_stat.type
end

if f_type == "file" then
self:new_file(relpath, full_path)
return
elseif f_type == "directory" then
self:new_dir(relpath, full_path)
return
if f_type == "file" then
self:new_file(relpath, full_path)
return
elseif f_type == "directory" then
self:new_dir(relpath, full_path)
return
end
elseif events.change then
self:change_child(relpath, full_path)
end
elseif events.change then
self:change_child(relpath, full_path)
end
end)()
end)

Expand All @@ -88,11 +95,11 @@ function TreeWatcher:start()
-- the watch-event, but that seems okay for our purposes)
local files, dirs = Path.scandir(self.root)
for _, file in ipairs(files) do
local relpath = file:sub(#self.root+2)
local relpath = file:sub(#self.root + 2)
self:new_file(relpath, file)
end
for _, dir in ipairs(dirs) do
local relpath = dir:sub(#self.root+2)
local relpath = dir:sub(#self.root + 2)
self:new_dir(relpath, dir)
end
end
Expand All @@ -119,7 +126,7 @@ function TreeWatcher:new_dir(rel, full)
-- first do callback for this directory, then look into (and potentially do
-- callbacks for) children.
self.callbacks.new_dir(full)
self.dir_watchers[rel] = M.new(full, self.depth-1, self.callbacks)
self.dir_watchers[rel] = M.new(full, self.depth - 1, self.callbacks)
end

function TreeWatcher:change_child(rel, full)
Expand Down Expand Up @@ -174,7 +181,9 @@ function TreeWatcher:remove_root()
end

local callback_mt = {
__index = function() return util.nop end
__index = function()
return util.nop
end,
}
-- root needs to be an absolute path.
function M.new(root, depth, callbacks, opts)
Expand All @@ -194,7 +203,7 @@ function M.new(root, depth, callbacks, opts)
dir_watchers = {},
removed = false,
callbacks = callbacks,
depth = depth
depth = depth,
}, TreeWatcher_mt)

-- if the path does not yet exist, set watcher up s.t. it will start
Expand All @@ -207,7 +216,11 @@ function M.new(root, depth, callbacks, opts)
error(("Could not find parent-path for %s"):format(root))
end

log.info("Path %s does not exist yet, watching %s for creation.", root, parent_path)
log.info(
"Path %s does not exist yet, watching %s for creation.",
root,
parent_path
)

local parent_watcher
parent_watcher = M.new(parent_path, 1, {
Expand Down
10 changes: 7 additions & 3 deletions lua/luasnip/loaders/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ local function collection_file_ft(collection_root, fname)
if #fname_components == #collection_components + 1 then
-- if the file is a direct child of the collection-root, get the text
-- before the last dot.
return fname_components[#collection_components + 1]:match("(.*)%.[^%.]*")
return fname_components[#collection_components + 1]:match(
"(.*)%.[^%.]*"
)
else
-- if the file is nested deeper, the name of the directory immediately
-- below the root is the filetype.
Expand Down Expand Up @@ -233,13 +235,15 @@ local function get_load_fts(bufnr)
end

local function add_file_snippets(ft, filename, snippets, autosnippets, add_opts)
snippet_collection.add_snippets({ [ft] = snippets },
snippet_collection.add_snippets(
{ [ft] = snippets },
vim.tbl_extend("keep", {
type = "snippets",
key = "__snippets__" .. ft .. "__" .. filename,
}, add_opts)
)
snippet_collection.add_snippets({ [ft] = autosnippets },
snippet_collection.add_snippets(
{ [ft] = autosnippets },
vim.tbl_extend("keep", {
type = "autosnippets",
key = "__autosnippets__" .. ft .. "__" .. filename,
Expand Down
3 changes: 2 additions & 1 deletion lua/luasnip/session/snippet_collection/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ function M.add_snippets(snippets, opts)
or opts.type
assert(
snip_type == "autosnippets" or snip_type == "snippets",
"snippetType must be either 'autosnippets' or 'snippets', was " .. vim.inspect(snip_type)
"snippetType must be either 'autosnippets' or 'snippets', was "
.. vim.inspect(snip_type)
)

local snip_ft = snip.filetype or ft
Expand Down
11 changes: 6 additions & 5 deletions lua/luasnip/util/auto_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ local function auto_creating_tables(self, key, depth)
if depth ~= 1 then
setmetatable(t, {
__index = function(s, k)
return auto_creating_tables(s, k, depth-1)
end
return auto_creating_tables(s, k, depth - 1)
end,
})
end
self[key] = t
Expand All @@ -39,12 +39,13 @@ function M.autotable(max_depth, opts)
opts = opts or {}
local warn = vim.F.if_nil(opts.warn, false)

local auto_table_func = warn and auto_creating_tables_warn_depth or auto_creating_tables
local auto_table_func = warn and auto_creating_tables_warn_depth
or auto_creating_tables

return setmetatable({}, {
__index = function(s, k)
return auto_table_func(s, k, max_depth-1)
end
return auto_table_func(s, k, max_depth - 1)
end,
})
end

Expand Down
2 changes: 1 addition & 1 deletion lua/luasnip/util/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function Path.extension(fname)
end

function Path.components(path)
return vim.split(path, sep, {plain=true, trimempty=true})
return vim.split(path, sep, { plain = true, trimempty = true })
end

-- returns nil if the file does not exist!
Expand Down
Loading

0 comments on commit e175dab

Please sign in to comment.