From e175dabf2327cd530f586c06e4fcf7a1707eace2 Mon Sep 17 00:00:00 2001 From: L3MON4D3 Date: Mon, 9 Oct 2023 20:43:46 +0000 Subject: [PATCH] Format with stylua --- lua/luasnip/loaders/from_lua.lua | 94 ++++++++++++++----- lua/luasnip/loaders/tree_watcher.lua | 85 ++++++++++------- lua/luasnip/loaders/util.lua | 10 +- .../session/snippet_collection/init.lua | 3 +- lua/luasnip/util/auto_table.lua | 11 ++- lua/luasnip/util/path.lua | 2 +- tests/integration/snippet_basics_spec.lua | 14 ++- 7 files changed, 143 insertions(+), 76 deletions(-) diff --git a/lua/luasnip/loaders/from_lua.lua b/lua/luasnip/loaders/from_lua.lua index 3f896788d..8a7dab830 100644 --- a/lua/luasnip/loaders/from_lua.lua +++ b/lua/luasnip/loaders/from_lua.lua @@ -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 @@ -146,16 +146,27 @@ 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) @@ -163,11 +174,11 @@ function Collection.new(root, lazy, include_ft, exclude_ft, add_opts, lazy_watch 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. @@ -177,7 +188,10 @@ 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, @@ -185,8 +199,8 @@ function Collection.new(root, lazy, include_ft, exclude_ft, add_opts, lazy_watch 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)) @@ -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 @@ -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 @@ -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 diff --git a/lua/luasnip/loaders/tree_watcher.lua b/lua/luasnip/loaders/tree_watcher.lua index a4b27e0b2..ddea97b42 100644 --- a/lua/luasnip/loaders/tree_watcher.lua +++ b/lua/luasnip/loaders/tree_watcher.lua @@ -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 @@ -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) @@ -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 @@ -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) @@ -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) @@ -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 @@ -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, { diff --git a/lua/luasnip/loaders/util.lua b/lua/luasnip/loaders/util.lua index 9c44108d7..1e35093df 100644 --- a/lua/luasnip/loaders/util.lua +++ b/lua/luasnip/loaders/util.lua @@ -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. @@ -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, diff --git a/lua/luasnip/session/snippet_collection/init.lua b/lua/luasnip/session/snippet_collection/init.lua index 8210a50d3..fce1c185e 100644 --- a/lua/luasnip/session/snippet_collection/init.lua +++ b/lua/luasnip/session/snippet_collection/init.lua @@ -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 diff --git a/lua/luasnip/util/auto_table.lua b/lua/luasnip/util/auto_table.lua index da4c04153..3e86d8fd8 100644 --- a/lua/luasnip/util/auto_table.lua +++ b/lua/luasnip/util/auto_table.lua @@ -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 @@ -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 diff --git a/lua/luasnip/util/path.lua b/lua/luasnip/util/path.lua index 63185877e..e05722b40 100644 --- a/lua/luasnip/util/path.lua +++ b/lua/luasnip/util/path.lua @@ -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! diff --git a/tests/integration/snippet_basics_spec.lua b/tests/integration/snippet_basics_spec.lua index 9da6df720..e82eba697 100644 --- a/tests/integration/snippet_basics_spec.lua +++ b/tests/integration/snippet_basics_spec.lua @@ -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)