diff --git a/lua/luasnip/loaders/from_lua.lua b/lua/luasnip/loaders/from_lua.lua index 611fb8752..b7e97d9f8 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(ft, file, add_opts) - -- 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,7 +146,7 @@ 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) @@ -154,7 +154,11 @@ function Collection:new(root, lazy, include_ft, exclude_ft, add_opts) 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) @@ -162,11 +166,11 @@ function Collection:new(root, lazy, include_ft, exclude_ft, add_opts) 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. @@ -176,7 +180,10 @@ 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, @@ -184,7 +191,7 @@ function Collection:new(root, lazy, include_ft, exclude_ft, add_opts) vim.schedule_wrap(function() o:reload(path) end)() - end + end, }) log.info("Initialized snippet-collection at `%s`", root) @@ -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 @@ -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 function Collection:do_lazy_load(ft) @@ -259,22 +272,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.make_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.make_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. diff --git a/lua/luasnip/loaders/tree_watcher.lua b/lua/luasnip/loaders/tree_watcher.lua index 6c6ab8c4f..511d0e929 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,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) @@ -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. @@ -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. @@ -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) @@ -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 diff --git a/lua/luasnip/loaders/util.lua b/lua/luasnip/loaders/util.lua index c26d7d021..8470d854f 100644 --- a/lua/luasnip/loaders/util.lua +++ b/lua/luasnip/loaders/util.lua @@ -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. @@ -223,13 +225,13 @@ local function add_file_snippets(ft, filename, snippets, autosnippets, add_opts) vim.tbl_extend("keep", { type = "snippets", key = "__snippets__" .. ft .. "__" .. filename, - }, add_opts) + }, add_opts), }) snippet_collection.add_snippets({ [ft] = snippets }, { vim.tbl_extend("keep", { type = "autosnippets", key = "__autosnippets__" .. ft .. "__" .. filename, - }, add_opts) + }, add_opts), }) log.info( "Adding %s snippets and %s autosnippets from %s to ft `%s`", 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)