Skip to content

Commit

Permalink
feat: remove snippet deduplication
Browse files Browse the repository at this point in the history
closes #146
  • Loading branch information
Saghen committed Oct 21, 2024
1 parent c5146a5 commit e296d8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
9 changes: 4 additions & 5 deletions lua/blink/cmp/sources/snippets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ function snippets:get_completions(_, callback)
local global_snippets = self.registry:get_global_snippets()
local extended_snippets = self.registry:get_extended_snippets(filetype)
local ft_snippets = self.registry:get_snippets_for_ft(filetype)
local snips = vim.tbl_deep_extend('force', {}, global_snippets, extended_snippets, ft_snippets)
local snips = vim.list_extend({}, global_snippets)
vim.list_extend(snips, extended_snippets)
vim.list_extend(snips, ft_snippets)

self.cache[filetype] = {}
for _, snippet in pairs(snips) do
table.insert(self.cache[filetype], snippet)
end
self.cache[filetype] = snips
end

local items = vim.tbl_map(
Expand Down
22 changes: 11 additions & 11 deletions lua/blink/cmp/sources/snippets/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function registry.new(config)
end

--- @param filetype string
--- @return table<string, blink.cmp.Snippet>
--- @return blink.cmp.Snippet[]
function registry:get_snippets_for_ft(filetype)
local loaded_snippets = {}
local files = self.registry[filetype]
Expand All @@ -49,8 +49,8 @@ function registry:get_snippets_for_ft(filetype)
local snippets = utils.parse_json_with_error_msg(f, contents)
for _, key in ipairs(vim.tbl_keys(snippets)) do
local snippet = utils.read_snippet(snippets[key], key)
for snippet_name, snippet_def in pairs(snippet) do
loaded_snippets[snippet_name] = snippet_def
for _, snippet_def in pairs(snippet) do
table.insert(loaded_snippets, snippet_def)
end
end
end
Expand All @@ -61,8 +61,8 @@ function registry:get_snippets_for_ft(filetype)
local snippets = utils.parse_json_with_error_msg(files, contents)
for _, key in ipairs(vim.tbl_keys(snippets)) do
local snippet = utils.read_snippet(snippets[key], key)
for key, snippet in pairs(snippet) do
loaded_snippets[key] = snippet
for _, snippet in pairs(snippet) do
table.insert(loaded_snippets, snippet)
end
end
end
Expand All @@ -72,31 +72,31 @@ function registry:get_snippets_for_ft(filetype)
end

--- @param filetype string
--- @return table<string, blink.cmp.Snippet>
--- @return blink.cmp.Snippet[]
function registry:get_extended_snippets(filetype)
local loaded_snippets = {}
if not filetype then return loaded_snippets end

local extended_snippets = self.config.extended_filetypes[filetype] or {}
for _, ft in ipairs(extended_snippets) do
if vim.tbl_contains(self.config.extended_filetypes, filetype) then
loaded_snippets = vim.tbl_deep_extend('force', {}, loaded_snippets, self:get_extended_snippets(ft))
vim.list_extend(loaded_snippets, self:get_extended_snippets(ft))
else
loaded_snippets = vim.tbl_deep_extend('force', {}, loaded_snippets, self:get_snippets_for_ft(ft))
vim.list_extend(loaded_snippets, self:get_snippets_for_ft(ft))
end
end
return loaded_snippets
end

--- @return table<string, blink.cmp.Snippet>
--- @return blink.cmp.Snippet[]
function registry:get_global_snippets()
local loaded_snippets = {}
local global_snippets = self.config.global_snippets
for _, ft in ipairs(global_snippets) do
if vim.tbl_contains(self.config.extended_filetypes, ft) then
loaded_snippets = vim.tbl_deep_extend('force', {}, loaded_snippets, self:get_extended_snippets(ft))
vim.list_extend(loaded_snippets, self:get_extended_snippets(ft))
else
loaded_snippets = vim.tbl_deep_extend('force', {}, loaded_snippets, self:get_snippets_for_ft(ft))
vim.list_extend(loaded_snippets, self:get_snippets_for_ft(ft))
end
end
return loaded_snippets
Expand Down

0 comments on commit e296d8f

Please sign in to comment.