Skip to content

Commit ce337cb

Browse files
committed
fix: expand vars in snippets for insertText
closes #27
1 parent 655d2ee commit ce337cb

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,24 @@ function snippets:get_completions(_, callback)
2929

3030
self.cache[filetype] = {}
3131
for _, snippet in pairs(snips) do
32-
table.insert(self.cache[filetype], self.registry:snippet_to_completion_item(snippet))
32+
table.insert(self.cache[filetype], snippet)
3333
end
3434
end
3535

36-
local copied_items = vim.tbl_map(function(item) return utils.shallow_copy(item) end, self.cache[filetype])
36+
local items = vim.tbl_map(
37+
function(item) return self.registry:snippet_to_completion_item(item) end,
38+
self.cache[filetype]
39+
)
3740
callback({
3841
is_incomplete_forward = false,
3942
is_incomplete_backward = false,
40-
items = copied_items,
43+
items = items,
4144
})
4245
end
4346

4447
function snippets:resolve(item, callback)
4548
-- TODO: ideally context is passed with the filetype
46-
local documentation = '```'
47-
.. vim.bo.filetype
48-
.. '\n'
49-
.. self.registry:preview(item.insertText)
50-
.. '```'
51-
.. '\n---\n'
52-
.. item.description
49+
local documentation = '```' .. vim.bo.filetype .. '\n' .. item.insertText .. '```' .. '\n---\n' .. item.description
5350

5451
local resolved_item = vim.deepcopy(item)
5552
resolved_item.documentation = {

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,21 @@ function registry:get_global_snippets()
103103
end
104104

105105
--- @param snippet blink.cmp.Snippet
106-
--- @param filetype string
107-
--- @param include_documentation boolean
108106
--- @return blink.cmp.CompletionItem
109107
function registry:snippet_to_completion_item(snippet)
108+
local body = type(snippet.body) == 'string' and snippet.body or table.concat(snippet.body, '\n')
110109
return {
111110
kind = vim.lsp.protocol.CompletionItemKind.Snippet,
112111
label = snippet.prefix,
113112
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
114-
insertText = type(snippet.body) == 'string' and snippet.body or table.concat(snippet.body, '\n'),
113+
insertText = self:parse_body(body),
115114
description = snippet.description,
116115
}
117116
end
118117

119118
--- @param snippet string
120119
--- @return string
121-
function registry:preview(snippet)
120+
function registry:parse_body(snippet)
122121
local parse = utils.safe_parse(self:expand_vars(snippet))
123122
return parse and tostring(parse) or snippet
124123
end

0 commit comments

Comments
 (0)