Skip to content

Commit

Permalink
fix: expand vars in snippets for insertText
Browse files Browse the repository at this point in the history
closes Saghen#27
  • Loading branch information
Saghen authored and lopi-py committed Oct 10, 2024
1 parent 6444906 commit 978e9fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
17 changes: 7 additions & 10 deletions lua/blink/cmp/sources/snippets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,24 @@ function snippets:get_completions(_, callback)

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

local copied_items = vim.tbl_map(function(item) return utils.shallow_copy(item) end, self.cache[filetype])
local items = vim.tbl_map(
function(item) return self.registry:snippet_to_completion_item(item) end,
self.cache[filetype]
)
callback({
is_incomplete_forward = false,
is_incomplete_backward = false,
items = copied_items,
items = items,
})
end

function snippets:resolve(item, callback)
-- TODO: ideally context is passed with the filetype
local documentation = '```'
.. vim.bo.filetype
.. '\n'
.. self.registry:preview(item.insertText)
.. '```'
.. '\n---\n'
.. item.description
local documentation = '```' .. vim.bo.filetype .. '\n' .. item.insertText .. '```' .. '\n---\n' .. item.description

local resolved_item = vim.deepcopy(item)
resolved_item.documentation = {
Expand Down
7 changes: 3 additions & 4 deletions lua/blink/cmp/sources/snippets/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,21 @@ function registry:get_global_snippets()
end

--- @param snippet blink.cmp.Snippet
--- @param filetype string
--- @param include_documentation boolean
--- @return blink.cmp.CompletionItem
function registry:snippet_to_completion_item(snippet)
local body = type(snippet.body) == 'string' and snippet.body or table.concat(snippet.body, '\n')
return {
kind = vim.lsp.protocol.CompletionItemKind.Snippet,
label = snippet.prefix,
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
insertText = type(snippet.body) == 'string' and snippet.body or table.concat(snippet.body, '\n'),
insertText = self:parse_body(body),
description = snippet.description,
}
end

--- @param snippet string
--- @return string
function registry:preview(snippet)
function registry:parse_body(snippet)
local parse = utils.safe_parse(self:expand_vars(snippet))
return parse and tostring(parse) or snippet
end
Expand Down

0 comments on commit 978e9fd

Please sign in to comment.