Skip to content

Commit ce26f9f

Browse files
committed
perf(snippets): fix previous attempt at caching snippet lazy vars
Related to #2024
1 parent 54cc521 commit ce26f9f

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

lua/blink/cmp/sources/snippets/default/builtin.lua

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@ local builtin = {
77
lazy = {},
88
}
99

10-
function builtin.lazy.TM_FILENAME() return vim.fn.expand('%:t') end
11-
12-
function builtin.lazy.TM_FILENAME_BASE() return vim.fn.expand('%:t:s?\\.[^\\.]\\+$??') end
13-
14-
function builtin.lazy.TM_DIRECTORY() return vim.fn.expand('%:p:h') end
15-
16-
function builtin.lazy.TM_FILEPATH() return vim.fn.expand('%:p') end
17-
18-
function builtin.lazy.TM_SELECTED_TEXT() return vim.fn.trim(vim.fn.getreg(vim.v.register, true), '\n', 2) end
10+
--- Higher-order function to add single-value caching
11+
local function cached(fn)
12+
local cache_key = -1
13+
local cached_value = nil
14+
return function(key, ...)
15+
assert(key ~= -1, 'key cannot be -1')
16+
if cache_key == key then return cached_value end
17+
18+
cached_value = fn(...)
19+
cache_key = key
20+
return cached_value
21+
end
22+
end
1923

20-
function builtin.lazy.CLIPBOARD(opts) return vim.fn.getreg(opts.clipboard_register or vim.v.register, true) end
24+
builtin.lazy.TM_FILENAME = cached(function() return vim.fn.expand('%:t') end)
25+
builtin.lazy.TM_FILENAME_BASE = cached(function() return vim.fn.expand('%:t:s?\\.[^\\.]\\+$??') end)
26+
builtin.lazy.TM_DIRECTORY = cached(function() return vim.fn.expand('%:p:h') end)
27+
builtin.lazy.TM_FILEPATH = cached(function() return vim.fn.expand('%:p') end)
28+
builtin.lazy.TM_SELECTED_TEXT = cached(function() return vim.fn.trim(vim.fn.getreg(vim.v.register, true), '\n', 2) end)
29+
builtin.lazy.CLIPBOARD = cached(
30+
function(opts) return vim.fn.getreg(opts.clipboard_register or vim.v.register, true) end
31+
)
2132

2233
local function buf_to_ws_part()
2334
local LSP_WORSKPACE_PARTS = 'LSP_WORSKPACE_PARTS'
@@ -38,18 +49,18 @@ local function buf_to_ws_part()
3849
return ws_parts
3950
end
4051

41-
function builtin.lazy.RELATIVE_FILEPATH() -- The relative (to the opened workspace or folder) file path of the current document
42-
return buf_to_ws_part()[2]
43-
end
44-
45-
function builtin.lazy.WORKSPACE_FOLDER() -- The path of the opened workspace or folder
52+
builtin.lazy.RELATIVE_FILEPATH = cached(
53+
function() -- The relative (to the opened workspace or folder) file path of the current document
54+
return buf_to_ws_part()[2]
55+
end
56+
)
57+
builtin.lazy.WORKSPACE_FOLDER = cached(function() -- The path of the opened workspace or folder
4658
return buf_to_ws_part()[1]
47-
end
48-
49-
function builtin.lazy.WORKSPACE_NAME() -- The name of the opened workspace or folder
59+
end)
60+
builtin.lazy.WORKSPACE_NAME = cached(function() -- The name of the opened workspace or folder
5061
local parts = vim.split(buf_to_ws_part()[1] or '', '[\\/]')
5162
return parts[#parts]
52-
end
63+
end)
5364

5465
function builtin.lazy.CURRENT_YEAR() return os.date('%Y') end
5566

@@ -129,11 +140,9 @@ local function buffer_comment_chars()
129140
return comments
130141
end
131142

132-
function builtin.lazy.LINE_COMMENT() return buffer_comment_chars()[1] end
133-
134-
function builtin.lazy.BLOCK_COMMENT_START() return buffer_comment_chars()[2] end
135-
136-
function builtin.lazy.BLOCK_COMMENT_END() return buffer_comment_chars()[3] end
143+
builtin.lazy.LINE_COMMENT = cached(function() return buffer_comment_chars()[1] end)
144+
builtin.lazy.BLOCK_COMMENT_START = cached(function() return buffer_comment_chars()[2] end)
145+
builtin.lazy.BLOCK_COMMENT_END = cached(function() return buffer_comment_chars()[3] end)
137146

138147
local function get_cursor()
139148
local c = vim.api.nvim_win_get_cursor(0)

lua/blink/cmp/sources/snippets/default/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function snippets:get_completions(context, callback)
3535
end
3636

3737
local items = vim.tbl_map(
38-
function(item) return self.registry:snippet_to_completion_item(item) end,
38+
function(item) return self.registry:snippet_to_completion_item(item, context.id) end,
3939
self.cache[filetype]
4040
)
4141
callback({

lua/blink/cmp/sources/snippets/default/registry.lua

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,24 @@ function registry:get_global_snippets()
106106
end
107107

108108
--- @param snippet blink.cmp.Snippet
109+
--- @param cache_key number
109110
--- @return blink.cmp.CompletionItem
110-
function registry:snippet_to_completion_item(snippet)
111+
function registry:snippet_to_completion_item(snippet, cache_key)
111112
local body = type(snippet.body) == 'string' and snippet.body or table.concat(snippet.body, '\n')
112113
return {
113114
kind = require('blink.cmp.types').CompletionItemKind.Snippet,
114115
label = snippet.prefix,
115116
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
116-
insertText = self:expand_vars(body),
117+
insertText = self:expand_vars(body, cache_key),
117118
description = snippet.description,
118119
}
119120
end
120121

121122
--- @param snippet string
123+
--- @param cache_key number
122124
--- @return string
123-
function registry:parse_body(snippet)
124-
local parse = utils.safe_parse(self:expand_vars(snippet))
125-
return parse and tostring(parse) or snippet
126-
end
127-
128-
--- @param snippet string
129-
--- @return string
130-
function registry:expand_vars(snippet)
125+
function registry:expand_vars(snippet, cache_key)
131126
local lazy_vars = self.builtin_vars.lazy
132-
local lazy_vars_cache = {}
133127
local eager_vars = self.builtin_vars.eager or {}
134128

135129
local resolved_snippet = snippet
@@ -151,9 +145,7 @@ function registry:expand_vars(snippet)
151145
if eager_vars[data.name] then
152146
resolved_snippet = resolved_snippet:gsub('%$[{]?(' .. data.name .. ')[}]?', eager_vars[data.name])
153147
elseif lazy_vars[data.name] then
154-
local replacement = lazy_vars_cache[data.name]
155-
or lazy_vars[data.name]({ clipboard_register = self.config.clipboard_register })
156-
lazy_vars_cache[data.name] = replacement
148+
local replacement = lazy_vars[data.name](cache_key, { clipboard_register = self.config.clipboard_register })
157149
-- gsub otherwise fails with strings like `%20` in the replacement string
158150
local escaped_for_gsub = replacement:gsub('%%', '%%%%')
159151
resolved_snippet = resolved_snippet:gsub('%$[{]?(' .. data.name .. ')[}]?', escaped_for_gsub)

0 commit comments

Comments
 (0)