Skip to content

Commit

Permalink
feat(store_meta_data): improve handling in add_snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
molleweide committed Jan 9, 2023
1 parent 7a5eb78 commit 7094c68
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
1 change: 0 additions & 1 deletion lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ local function add_snippets(ft, snippets, opts)
opts.refresh_notify = opts.refresh_notify or true
-- alternatively, "autosnippets"
opts.type = opts.type or "snippets"
opts.store_meta_data = session.config.store_meta_data

-- if ft is nil, snippets already has this format.
if ft then
Expand Down
30 changes: 24 additions & 6 deletions lua/luasnip/session/snippet_collection.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local session = require("luasnip.session")

-- store snippets by some key.
-- also ordered by filetype, eg.
-- {
Expand Down Expand Up @@ -136,6 +137,18 @@ local by_id = setmetatable({}, {
local snippets_meta_data = {}
local snip_id_to_meta_map = {}


local function initialize_snippet_meta_data(opts)
if type(session.config.store_meta_data) == "function" then
-- NOTE: use function is still a bit wip
table.insert(snippets_meta_data, session.config.store_meta_data(opts))

elseif session.config.store_meta_data then
table.insert(snippets_meta_data, { source = opts.source })

end
end

-- ft: any filetype, optional.
function M.clear_snippets(ft)
if ft then
Expand Down Expand Up @@ -247,12 +260,13 @@ end
local current_id = 0
-- snippets like {ft1={<snippets>}, ft2={<snippets>}}, opts should be properly
-- initialized with default values.
--
-- opts.source = path to snip module which is assigned in
-- `from_lua.load`
function M.add_snippets(snippets, opts)
-- TODO: store the source meta data here, and then point each snip
-- this should allow me to remove the snip id mapping and use a
-- pure meta data pointer.
if opts.source and opts.store_meta_data then
table.insert(snippets_meta_data, { source = opts.source })

if session.config.store_meta_data then
initialize_snippet_meta_data(opts)
end

for ft, ft_snippets in pairs(snippets) do
Expand Down Expand Up @@ -286,8 +300,12 @@ function M.add_snippets(snippets, opts)
table.insert(by_ft[snip.snippetType][ft], snip)
by_id[snip.id] = snip

if opts.source and opts.store_meta_data then
if session.config.store_meta_data then
snip_id_to_meta_map[snip.id] = #snippets_meta_data
-- TODO: (wip) following line would store single snippet specific data
-- we are not sure yet how this would be done but I leave it here
-- for reference
-- snippets_meta_data[#snippets_meta_data].snippet_specific[snip.id] = session.config.store_meta_data.each_snippet(ft, snip, opts)
end
end
end
Expand Down

0 comments on commit 7094c68

Please sign in to comment.