Skip to content

Commit

Permalink
add :sub to snippetString.
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 committed Nov 2, 2024
1 parent 83e3c54 commit 1c052ac
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lua/luasnip/nodes/util/snippet_string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,43 @@ function SnippetString:gsub(pattern, repl)
return self
end

function SnippetString:sub(from, to)
self = self:copy()

local snipstr_map = {}
local str = gen_snipstr_map(self, snipstr_map, 1)

to = to or #str

-- negative -> positive
if from < 0 then
from = #str + from + 1
end
if to < 0 then
to = #str + to + 1
end

-- empty range => return empty snippetString.
if from > #str or to < from or to < 1 then
return M.new({""})
end

from = math.max(from, 1)
to = math.min(to, #str)

local replacements = {}
-- from <= 1 => don't need to remove from beginning.
if from > 1 then
table.insert(replacements, { from=1, to=from-1, str = "" })
end
-- to >= #str => don't need to remove from end.
if to < #str then
table.insert(replacements, { from=to+1, to=#str, str = "" })
end

_replace(self, replacements, snipstr_map)
return self
end


return M

0 comments on commit 1c052ac

Please sign in to comment.