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 8, 2023
1 parent 2c4cb11 commit e1fdca5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 72 deletions.
61 changes: 41 additions & 20 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,27 +146,31 @@ 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)
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 @@ -176,15 +180,18 @@ function Collection:new(root, lazy, include_ft, exclude_ft, add_opts)
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,
changed_file = function(path)
vim.schedule_wrap(function()
o:reload(path)
end)()
end
end,
})

log.info("Initialized snippet-collection at `%s`", root)
Expand All @@ -199,7 +206,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 @@ -215,14 +226,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 @@ -265,11 +278,19 @@ local function _load(lazy, opts)
local include = opts.include
local exclude = opts.exclude

local collection_roots = loader_util.resolve_root_paths(opts.paths, "luasnippets")
log.info("Found roots `%s` for paths `%s`.", vim.inspect(collection_roots), vim.inspect(paths))
local collection_roots =
loader_util.resolve_root_paths(opts.paths, "luasnippets")
log.info(
"Found roots `%s` for paths `%s`.",
vim.inspect(collection_roots),
vim.inspect(paths)
)

for _, collection_root in ipairs(collection_roots) do
table.insert(M.collections, Collection:new(collection_root, lazy, include, exclude, add_opts))
table.insert(
M.collections,
Collection:new(collection_root, lazy, include, exclude, add_opts)
)
end
end

Expand Down
83 changes: 46 additions & 37 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,7 +34,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 @@ -84,7 +84,9 @@ function TreeWatcher:remove_root()
end

local callback_mt = {
__index = function() return util.nop end
__index = function()
return util.nop
end,
}
function M.new(root, depth, callbacks)
-- do nothing on missing callback.
Expand All @@ -97,7 +99,7 @@ function M.new(root, depth, callbacks)
dir_watchers = {},
removed = false,
callbacks = callbacks,
depth = depth
depth = depth,
}, TreeWatcher_mt)

-- don't watch children.
Expand All @@ -112,38 +114,45 @@ function M.new(root, depth, callbacks)
return
end
vim.schedule_wrap(function()
log.info("raw: root: %s; err: %s; relpath: %s; change: %s; rename: %s", o.root, err, relpath, events.change, events.rename)
local full_path = Path.join(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(root) then
o:remove_root()
return
log.info(
"raw: root: %s; err: %s; relpath: %s; change: %s; rename: %s",
o.root,
err,
relpath,
events.change,
events.rename
)
local full_path = Path.join(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(root) then
o:remove_root()
return
end
if not path_stat then
o: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

if f_type == "file" then
o:new_file(relpath, full_path)
return
elseif f_type == "directory" then
o:new_dir(relpath, full_path)
return
end
elseif events.change then
o:change_child(relpath, full_path)
end
if not path_stat then
o: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

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

Expand All @@ -166,11 +175,11 @@ function M.new(root, depth, callbacks)
-- the watch-event, but that seems okay for our purposes)
local files, dirs = Path.scandir(root)
for _, file in ipairs(files) do
local relpath = file:sub(#root+2)
local relpath = file:sub(#root + 2)
o:new_file(relpath, file)
end
for _, dir in ipairs(dirs) do
local relpath = dir:sub(#root+2)
local relpath = dir:sub(#root + 2)
o:new_dir(relpath, dir)
end

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 @@ -118,7 +118,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 @@ -221,13 +223,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
14 changes: 9 additions & 5 deletions tests/integration/snippet_basics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1343,17 +1343,21 @@ describe("snippets_basic", function()
it("unlink_current works.", function()
exec_lua([[ls.lsp_expand("$1 adsf $2")]])
exec_lua([[ls.jump( 1)]])
screen:expect{grid=[[
screen:expect({
grid = [[
adsf ^ |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec_lua([[ls.jump(-1)]])
screen:expect{grid=[[
screen:expect({
grid = [[
^ adsf |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec_lua([[ls.unlink_current()]])
exec_lua([[ls.jump( 1)]])
screen:expect{unchanged=true}
screen:expect({ unchanged = true })
end)
end)

0 comments on commit e1fdca5

Please sign in to comment.