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 6, 2023
1 parent 8fcc8e3 commit 189aefd
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 62 deletions.
51 changes: 38 additions & 13 deletions lua/luasnip/loaders/from_lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ local function _luasnip_load_files(ft, files, add_opts)
ls.refresh_notify(ft)
end


-- map ft->set of files (table where files are keys, and they are either true
-- or nil, can use pairs to iterate over them).
local ft_paths = require("luasnip.loaders.data").lua_ft_paths
Expand All @@ -188,21 +187,23 @@ 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
error(("Path `%s` is not a child of root `%s`"):format(path, root))
error(
("Path `%s` is not a child of root `%s`"):format(path, root)
)
end
return lua_package_file_filter(path) and ft_filter(path)
end,
add_opts = add_opts,
lazy = lazy,
lazy_files = autotable(2, {warn = false}),
lazy_files = autotable(2, { warn = false }),
}, Collection_mt)

-- only register files up to a depth of 2.
Expand All @@ -217,7 +218,7 @@ function Collection:new(root, lazy, include_ft, exclude_ft, add_opts)
vim.schedule_wrap(function()
o:reload(path)
end)()
end
end,
})

return o
Expand All @@ -235,7 +236,11 @@ function Collection:new_file(path)

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

-- only register to load later.
table.insert(self.lazy_files[file_ft], path)
Expand All @@ -249,7 +254,7 @@ function Collection:new_file(path)
end
end

_luasnip_load_files(file_ft, {path}, self.add_opts)
_luasnip_load_files(file_ft, { path }, self.add_opts)
end
function Collection:do_lazy_load(ft)
if session.loaded_fts[ft] then
Expand All @@ -259,7 +264,7 @@ function Collection:do_lazy_load(ft)

log.info("Loading lazy-load-snippets for filetype `%s`", ft)
for _, file in ipairs(self.lazy_files) do
_luasnip_load_files(ft, {file}, self.add_opts)
_luasnip_load_files(ft, { file }, self.add_opts)
end
end
function Collection:reload(fname)
Expand All @@ -269,7 +274,7 @@ function Collection:reload(fname)
return
end

_luasnip_load_files(file_ft, {fname}, self.add_opts)
_luasnip_load_files(file_ft, { fname }, self.add_opts)
-- clean snippets if enough were removed.
ls.clean_invalidated({ inv_limit = 100 })
end
Expand All @@ -283,22 +288,42 @@ end
function M.load(opts)
opts = opts or {}

local collection_roots = loader_util.resolve_root_paths(opts.path, "luasnippets")
local collection_roots =
loader_util.resolve_root_paths(opts.path, "luasnippets")
local add_opts = loader_util.add_opts(opts)

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

function M.lazy_load(opts)
opts = opts or {}

local collection_roots = loader_util.resolve_root_paths(opts.path, "luasnippets")
local collection_roots =
loader_util.resolve_root_paths(opts.path, "luasnippets")
local add_opts = loader_util.add_opts(opts)

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

-- load for current buffer on startup.
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
4 changes: 3 additions & 1 deletion lua/luasnip/loaders/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,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
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 189aefd

Please sign in to comment.