Skip to content

Commit 2ed5d8e

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

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

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

Lines changed: 33 additions & 28 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,29 +101,32 @@ 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

123-
function source:enabled()
124-
local ok, mod = pcall(require, 'luasnip')
125-
if ok then luasnip = mod end
126-
return ok
127-
end
129+
function source:enabled() return self.has_loaded end
128130

129131
---@param ctx blink.cmp.Context
130132
---@param callback fun(result?: blink.cmp.CompletionResponse)
@@ -134,7 +136,7 @@ function source:get_completions(ctx, callback)
134136

135137
if luasnip.choice_active() then
136138
---@type LuaSnip.ChoiceNode
137-
local active_choice = require('luasnip.session').active_choice_nodes[ctx.bufnr]
139+
local active_choice = luasnip.session.active_choice_nodes[ctx.bufnr]
138140
for i, choice in ipairs(active_choice.choices) do
139141
local text = choice:get_static_text()[1]
140142
table.insert(items, {
@@ -149,8 +151,10 @@ function source:get_completions(ctx, callback)
149151
return
150152
end
151153

154+
local events = require('luasnip.util.events')
155+
152156
-- Else, gather snippets from relevant filetypes, including extensions
153-
for _, ft in ipairs(require('luasnip.util.util').get_snippet_filetypes()) do
157+
for _, ft in ipairs(luasnip.get_snippet_filetypes()) do
154158
if self.items_cache[ft] and #self.items_cache[ft] > 0 then
155159
for _, item in ipairs(self.items_cache[ft]) do
156160
table.insert(items, utils.shallow_copy(item))
@@ -166,7 +170,7 @@ function source:get_completions(ctx, callback)
166170
if self.opts.show_autosnippets then
167171
local autosnippets = luasnip.get_snippets(ft, { type = 'autosnippets' })
168172
for _, s in ipairs(autosnippets) do
169-
add_luasnip_callback(s, 'enter', cmp.hide)
173+
add_luasnip_callback(s, events.enter, cmp.hide)
170174
end
171175
snippets = utils.shallow_copy(snippets)
172176
vim.list_extend(snippets, autosnippets)
@@ -252,12 +256,13 @@ function source:execute(ctx, item)
252256

253257
local snip = luasnip.get_id_snippet(item.data.snip_id)
254258

259+
local events = require('luasnip.util.events')
255260
if snip.regTrig then
256261
local docTrig = self.opts.prefer_doc_trig and snip.docTrig
257262
snip = snip:get_pattern_expand_helper()
258-
if docTrig then add_luasnip_callback(snip, 'pre_expand', function(s) regex_callback(s, docTrig) end) end
263+
if docTrig then add_luasnip_callback(snip, events.pre_expand, function(s) regex_callback(s, docTrig) end) end
259264
else
260-
add_luasnip_callback(snip, 'pre_expand', choice_callback)
265+
add_luasnip_callback(snip, events.pre_expand, function(s) choice_callback(s, events) end)
261266
end
262267

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

0 commit comments

Comments
 (0)