Skip to content

Commit 8539836

Browse files
committed
perf(luasnip): reduce table lookups, again
1 parent e5e0c99 commit 8539836

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

lua/blink/cmp/sources/snippets/luasnip.lua

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ local kind_snippet = require('blink.cmp.types').CompletionItemKind.Snippet
1414
--- @class blink.cmp.LuasnipSource : blink.cmp.Source
1515
--- @field opts blink.cmp.LuasnipSourceOptions
1616
--- @field items_cache table<string, blink.cmp.CompletionItem[]>
17+
--- @field has_loaded boolean
1718
local source = {}
1819

1920
---@param snippet table
20-
---@param event string
21+
---@param event number
2122
---@param callback fun(table, table)
2223
local function add_luasnip_callback(snippet, event, callback)
23-
local events = require('luasnip.util.events')
2424
-- not defined for autosnippets
2525
if snippet.callbacks == nil then return end
2626
snippet.callbacks[-1] = snippet.callbacks[-1] or {}
27-
snippet.callbacks[-1][events[event]] = callback
27+
snippet.callbacks[-1][event] = callback
2828
end
2929

3030
---@param snippet LuaSnip.Snippet
@@ -42,8 +42,7 @@ local function regex_callback(snippet, docTrig)
4242
end
4343

4444
---@param snippet LuaSnip.Snippet
45-
local function choice_callback(snippet)
46-
local events = require('luasnip.util.events')
45+
local function choice_callback(snippet, events)
4746
local types = require('luasnip.util.types')
4847

4948
for _, node in ipairs(snippet.insert_nodes) do
@@ -102,28 +101,33 @@ function source.new(opts)
102101

103102
self.opts = opts
104103
self.items_cache = {}
104+
self.has_loaded = false
105105

106-
local luasnip_ag = vim.api.nvim_create_augroup('BlinkCmpLuaSnipReload', { clear = true })
107-
vim.api.nvim_create_autocmd('User', {
108-
pattern = 'LuasnipSnippetsAdded',
109-
callback = function() self:reload() end,
110-
group = luasnip_ag,
111-
desc = 'Reset internal cache of luasnip source of blink.cmp when new snippets are added',
112-
})
113-
vim.api.nvim_create_autocmd('User', {
114-
pattern = 'LuasnipCleanup',
115-
callback = function() self:reload() end,
116-
group = luasnip_ag,
117-
desc = 'Reload luasnip source of blink.cmp when snippets are cleared',
118-
})
106+
local ok, mod = pcall(require, 'luasnip')
107+
if ok then
108+
self.has_loaded = true
109+
luasnip = mod
110+
111+
local luasnip_ag = vim.api.nvim_create_augroup('BlinkCmpLuaSnipReload', { clear = true })
112+
local events = {
113+
{ pattern = 'LuasnipSnippetsAdded', desc = 'Clear the Luasnip cache in blink.cmp when new snippets are added' },
114+
{ pattern = 'LuasnipCleanup', desc = 'Clear the Luasnip cache in blink.cmp when snippets are cleared' },
115+
}
116+
for _, event in ipairs(events) do
117+
vim.api.nvim_create_autocmd('User', {
118+
pattern = event.pattern,
119+
callback = function() self:reload() end,
120+
group = luasnip_ag,
121+
desc = event.desc,
122+
})
123+
end
124+
end
119125

120126
return self
121127
end
122128

123129
function source:enabled()
124-
local ok, mod = pcall(require, 'luasnip')
125-
if ok then luasnip = mod end
126-
return ok
130+
return self.has_loaded
127131
end
128132

129133
---@param ctx blink.cmp.Context
@@ -134,7 +138,7 @@ function source:get_completions(ctx, callback)
134138

135139
if luasnip.choice_active() then
136140
---@type LuaSnip.ChoiceNode
137-
local active_choice = require('luasnip.session').active_choice_nodes[ctx.bufnr]
141+
local active_choice = luasnip.session.active_choice_nodes[ctx.bufnr]
138142
for i, choice in ipairs(active_choice.choices) do
139143
local text = choice:get_static_text()[1]
140144
table.insert(items, {
@@ -149,8 +153,10 @@ function source:get_completions(ctx, callback)
149153
return
150154
end
151155

156+
local events = require('luasnip.util.events')
157+
152158
-- Else, gather snippets from relevant filetypes, including extensions
153-
for _, ft in ipairs(require('luasnip.util.util').get_snippet_filetypes()) do
159+
for _, ft in ipairs(luasnip.get_snippet_filetypes()) do
154160
if self.items_cache[ft] and #self.items_cache[ft] > 0 then
155161
for _, item in ipairs(self.items_cache[ft]) do
156162
table.insert(items, utils.shallow_copy(item))
@@ -166,7 +172,7 @@ function source:get_completions(ctx, callback)
166172
if self.opts.show_autosnippets then
167173
local autosnippets = luasnip.get_snippets(ft, { type = 'autosnippets' })
168174
for _, s in ipairs(autosnippets) do
169-
add_luasnip_callback(s, 'enter', cmp.hide)
175+
add_luasnip_callback(s, events.enter, cmp.hide)
170176
end
171177
snippets = utils.shallow_copy(snippets)
172178
vim.list_extend(snippets, autosnippets)
@@ -252,12 +258,13 @@ function source:execute(ctx, item)
252258

253259
local snip = luasnip.get_id_snippet(item.data.snip_id)
254260

261+
local events = require('luasnip.util.events')
255262
if snip.regTrig then
256263
local docTrig = self.opts.prefer_doc_trig and snip.docTrig
257264
snip = snip:get_pattern_expand_helper()
258-
if docTrig then add_luasnip_callback(snip, 'pre_expand', function(s) regex_callback(s, docTrig) end) end
265+
if docTrig then add_luasnip_callback(snip, events.pre_expand, function(s) regex_callback(s, docTrig) end) end
259266
else
260-
add_luasnip_callback(snip, 'pre_expand', choice_callback)
267+
add_luasnip_callback(snip, events.pre_expand, function(s) choice_callback(s, events) end)
261268
end
262269

263270
local cursor = ctx.get_cursor() --[[@as LuaSnip.BytecolBufferPosition]]

0 commit comments

Comments
 (0)