From 6faded3baee5efc264b12d07b0fe764062574a9b Mon Sep 17 00:00:00 2001 From: bootleq Date: Tue, 17 Jan 2023 17:14:34 +0800 Subject: [PATCH] Fix invalid load path breaks later good paths Loader `paths` can be a list, invalid path within (e.g., the directory doesn't exist) will be omitted, and other valid items should be kept. There is a problem that valid paths AFTER a bad one are omitted, too. The cause is after normalize paths, invalid item become nil hole, then `ipairs` in deduplicate function unexpectedly stopped there. Solution: remove nil before dedup. Small note: lua and snipmate loaders share `loader.util.get_load_paths_snipmate_like` helper thus fixed together. However vscode loader has its own `package.json` parsing logic so can't be treated the same. --- doc/luasnip.txt | 2 +- lua/luasnip/loaders/from_vscode.lua | 5 ++++- lua/luasnip/loaders/util.lua | 5 +++++ tests/integration/loaders_spec.lua | 21 +++++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/doc/luasnip.txt b/doc/luasnip.txt index bf7092a8e..0072c5c80 100644 --- a/doc/luasnip.txt +++ b/doc/luasnip.txt @@ -1,4 +1,4 @@ -*luasnip.txt* For NVIM v0.8.0 Last change: 2023 January 16 +*luasnip.txt* For NVIM v0.8.0 Last change: 2023 January 18 ============================================================================== Table of Contents *luasnip-table-of-contents* diff --git a/lua/luasnip/loaders/from_vscode.lua b/lua/luasnip/loaders/from_vscode.lua index 349ff81eb..03f73a9a1 100644 --- a/lua/luasnip/loaders/from_vscode.lua +++ b/lua/luasnip/loaders/from_vscode.lua @@ -205,7 +205,10 @@ local function get_snippet_files(opts) paths = opts.paths end paths = vim.tbl_map(Path.expand, paths) -- Expand before deduping, fake paths will become nil - paths = util.deduplicate(paths) -- Remove doppelgänger paths and ditch nil ones + paths = vim.tbl_filter(function(v) + return v + end, paths) -- ditch nil + paths = util.deduplicate(paths) -- Remove doppelgänger paths local ft_paths = {} diff --git a/lua/luasnip/loaders/util.lua b/lua/luasnip/loaders/util.lua index 0ddf1e979..8f723d354 100644 --- a/lua/luasnip/loaders/util.lua +++ b/lua/luasnip/loaders/util.lua @@ -37,6 +37,10 @@ local function split_lines(filestring) ) end +local function _is_present(v) + return v ~= nil +end + local function normalize_paths(paths, rtp_dirname) if not paths then paths = vim.api.nvim_get_runtime_file(rtp_dirname, true) @@ -45,6 +49,7 @@ local function normalize_paths(paths, rtp_dirname) end paths = vim.tbl_map(Path.expand, paths) + paths = vim.tbl_filter(_is_present, paths) paths = util.deduplicate(paths) return paths diff --git a/tests/integration/loaders_spec.lua b/tests/integration/loaders_spec.lua index 33226a87c..f2230c9e9 100644 --- a/tests/integration/loaders_spec.lua +++ b/tests/integration/loaders_spec.lua @@ -236,6 +236,27 @@ describe("loaders:", function() assert.are.same(3, exec_lua('return #ls.get_snippets("vim")')) end) + it("loads paths with invalid paths ditched (vscode)", function() + exec_lua(string.format( + [[require("luasnip.loaders.from_vscode").load({paths={"%s", "%s"}})]], + os.getenv("LUASNIP_SOURCE") .. "/tests/data/invalid-not-exists", + os.getenv("LUASNIP_SOURCE") .. "/tests/data/vscode-snippets" -- has 5 prio snippets + )) + + assert.are.same(5, exec_lua('return #ls.get_snippets("prio")')) + end) + + it("loads paths with invalid paths ditched (lua)", function() + exec_lua(string.format( + [[require("luasnip.loaders.from_lua").load({paths={"%s", "%s"}})]], + os.getenv("LUASNIP_SOURCE") .. "/tests/data/invalid-not-exists", + os.getenv("LUASNIP_SOURCE") + .. "/tests/data/lua-snippets/luasnippets" -- has 1 prio snippet + )) + + assert.are.same(1, exec_lua('return #ls.get_snippets("prio")')) + end) + it("respects {override,default}_priority", function() -- just the filetype the test-snippets are added for. exec("set ft=prio")