Skip to content

Commit

Permalink
Fix invalid load path breaks later good paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bootleq committed Jan 18, 2023
1 parent 4567028 commit 6faded3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/luasnip.txt
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
5 changes: 4 additions & 1 deletion lua/luasnip/loaders/from_vscode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down
5 changes: 5 additions & 0 deletions lua/luasnip/loaders/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/loaders_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6faded3

Please sign in to comment.