Skip to content
Draft
Prev Previous commit
Next Next commit
chore(luasnip): add more annotations
  • Loading branch information
soifou committed Nov 11, 2025
commit cb585040747ebcd8c01103c59d04283b6935fe12
17 changes: 11 additions & 6 deletions lua/blink/cmp/sources/snippets/luasnip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ function source:enabled()
return ok
end

---@param ctx blink.cmp.Context
---@param callback fun(result?: blink.cmp.CompletionResponse)
function source:get_completions(ctx, callback)
--- @type blink.cmp.CompletionItem[]
local items = {}
Expand Down Expand Up @@ -165,6 +167,8 @@ function source:resolve(item, callback)
callback(resolved_item)
end

---@param ctx blink.cmp.Context
---@param item blink.cmp.CompletionItem
function source:execute(ctx, item)
local snip = luasnip.get_id_snippet(item.data.snip_id)

Expand All @@ -190,11 +194,11 @@ function source:execute(ctx, item)
end
end

-- get (0, 0) indexed cursor position
local cursor = ctx.get_cursor()
local cursor = ctx.get_cursor() --[[@as LuaSnip.BytecolBufferPosition]]
cursor[1] = cursor[1] - 1

local range = text_edits.get_from_item(item).range
---@type LuaSnip.BufferRegion
local clear_region = {
from = { range.start.line, range.start.character },
to = cursor,
Expand All @@ -204,18 +208,19 @@ function source:execute(ctx, item)
local line_to_cursor = line:sub(1, cursor[2])
local range_text = line:sub(range.start.character + 1, cursor[2])

---@type LuaSnip.Opts.SnipExpandExpandParams?
local expand_params = snip:matches(line_to_cursor, {
fallback_match = range_text ~= line_to_cursor and range_text,
})

if expand_params ~= nil then
---@diagnostic disable-next-line: undefined-field
if expand_params.clear_region ~= nil then
---@diagnostic disable-next-line: undefined-field
clear_region = expand_params.clear_region
elseif expand_params.trigger ~= nil then
clear_region = {
from = { cursor[1], cursor[2] - #expand_params.trigger },
to = cursor,
}
clear_region.from = { cursor[1], cursor[2] - #expand_params.trigger }
clear_region.to = cursor
end
end

Expand Down