Skip to content

Commit 3fc1815

Browse files
committed
feat(luasnip): enhance insertText with static text from textNodes
Instead of displaying only `trigger`, first try to generate the preview from the snippet's textNodes; if that's not available, fall back to `docTrig` if it exists, or use the `trigger` as a last resort.
1 parent 7d7e03b commit 3fc1815

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ local function add_luasnip_callback(snippet, event, callback)
2626
snippet.callbacks[-1][events[event]] = callback
2727
end
2828

29+
---@param snippet LuaSnip.Snippet
30+
---@return string?
31+
local function get_insert_text(snippet)
32+
local res = {}
33+
for _, node in ipairs(snippet.nodes) do
34+
---@cast node LuaSnip.Node
35+
-- TODO: How to know the node type? Would be nice to handle the others as well
36+
-- textNodes
37+
if type(node.static_text) == 'table' then res[#res + 1] = table.concat(node.static_text, '\n') end
38+
end
39+
40+
-- Fallback
41+
if #res == 1 then
42+
-- Prefer docTrig over trigger
43+
---@diagnostic disable-next-line: undefined-field
44+
if snippet.docTrig then return snippet.docTrig end
45+
return snippet.trigger
46+
end
47+
48+
return table.concat(res, '')
49+
end
50+
2951
---@param opts blink.cmp.LuasnipSourceOptions
3052
function source.new(opts)
3153
local self = setmetatable({}, { __index = source })
@@ -116,7 +138,7 @@ function source:get_completions(ctx, callback)
116138
local item = {
117139
kind = kind_snippet,
118140
label = snip.regTrig and snip.name or snip.trigger,
119-
insertText = self.opts.prefer_doc_trig and snip.docTrig or snip.trigger,
141+
insertText = get_insert_text(snip),
120142
insertTextFormat = vim.lsp.protocol.InsertTextFormat.PlainText,
121143
sortText = sort_text,
122144
data = { snip_id = snip.id, show_condition = snip.show_condition },

0 commit comments

Comments
 (0)