Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
176420f
implement optional argnodes.
L3MON4D3 Dec 28, 2023
bd09791
implement subtree_do, invokes callbacks on tree of nodes (in a snippet).
L3MON4D3 Mar 4, 2024
5fdf9d8
make resolve_position work for static snippets.
L3MON4D3 Mar 4, 2024
aa1b233
fix: make root_path work for snippets.
L3MON4D3 Mar 4, 2024
b5d2a34
implement subtree_leave_entered, for leaving only entered nodes.
L3MON4D3 May 5, 2025
b1f3492
overhaul snippet-updates.
L3MON4D3 Nov 3, 2025
b419d9a
make sure visible is set on -1-node.
L3MON4D3 Mar 10, 2024
83c7649
propery remove child-snippets when `:exit`ing.
L3MON4D3 Apr 23, 2024
61bf611
exitNode: use same update_dependents as all other nodes.
L3MON4D3 Apr 23, 2024
c295b3f
update after snip_expand.
L3MON4D3 Oct 14, 2025
719d55c
Format with stylua
L3MON4D3 Oct 14, 2025
09217d6
Make insertNode correctly handle static_text if it's a snippetString.
L3MON4D3 Oct 28, 2024
3be6031
allow using snippet_string as dynamicNode-args.
L3MON4D3 Oct 28, 2024
a35adb8
restoreNode,insertNode: propagate store.
L3MON4D3 Oct 29, 2024
d4abde8
dynamicNode.update: store snippet before evaluating fn.
L3MON4D3 Oct 29, 2024
4ca232e
dynamicNode.update: copy extmarks after focusing.
L3MON4D3 Oct 29, 2024
3225dd1
dynamicNode.update: do update_restore instead of update.
L3MON4D3 Oct 29, 2024
45df41e
restoreNode: don't store on exit, store should have been called before.
L3MON4D3 Oct 29, 2024
888ac5e
add some tests for new restoreNode-behaviour.
L3MON4D3 Oct 29, 2024
722251d
choiceNode: correctly refocus when current_node is in another snippet.
L3MON4D3 Oct 29, 2024
7d8140c
we don't want to go into adjacent snippetNodes, but land between them.
L3MON4D3 Oct 29, 2024
5441143
snippet: correctly propagate exit to child_snippets (and clear them).
L3MON4D3 Oct 29, 2024
b63fb1f
add another test for the new restoreNode.
L3MON4D3 Oct 29, 2024
d4b8110
store content of nested snippets before capturing argnode.
L3MON4D3 Oct 29, 2024
508cc3f
make sure marks are invalidated even for nested snippets.
L3MON4D3 Oct 29, 2024
d40fc8a
get_args: `store` only when calling in static mode.
L3MON4D3 Oct 29, 2024
5192bf9
snippetstring: store strings as \n-separated string.
L3MON4D3 Oct 14, 2025
87ad057
small refactor.
L3MON4D3 Oct 30, 2024
fc7e586
implement a few simple string-operations on snippetString.
L3MON4D3 Oct 30, 2024
c5cfc00
fix flakiness in test.
L3MON4D3 Oct 30, 2024
3f7ce76
update: try to find new active node in child-snippet.
L3MON4D3 Oct 30, 2024
befdc53
allow replacing parts of a snippetString with other text.
L3MON4D3 Nov 2, 2024
6ee05ef
implement gsub on snippetString.
L3MON4D3 Nov 2, 2024
de371f5
snippetstring.replace: fix substitution in textNode.
L3MON4D3 Nov 2, 2024
7c34402
fix switchup.
L3MON4D3 Nov 2, 2024
b9313e2
make in-place modifying functions private.
L3MON4D3 Nov 2, 2024
80be0c4
add :sub to snippetString.
L3MON4D3 Nov 2, 2024
bea1bb7
add `opt` for the optional argument.
L3MON4D3 Nov 3, 2024
5f476e4
correctly store+restore visual selection during update.
L3MON4D3 Nov 3, 2024
24edd78
fNode: always store result in static_text.
L3MON4D3 Nov 4, 2024
4d25da0
update_dependents: get cursor-position after queried movements.
L3MON4D3 Nov 4, 2024
25ef218
move the jump_active-check into the autocommand.
L3MON4D3 Oct 14, 2025
79b336c
optionally update a node differnt from the current node.
L3MON4D3 Oct 14, 2025
e0ee698
update_dependents: use update_restore by default.
L3MON4D3 Nov 4, 2024
79722c5
choiceNode: call update_dependents after routine is done completely.
L3MON4D3 Nov 4, 2024
67b99dc
move no_region_wrap back into main-module.
L3MON4D3 Oct 14, 2025
fcfb058
dynamicNode/restoreNode: don't destroy snip on exit.
L3MON4D3 Nov 4, 2024
5123fde
handle selection on first line and column of buffer with `before`.
L3MON4D3 Nov 4, 2024
ad85601
document imperfect behaviour asserted by test.
L3MON4D3 Nov 4, 2024
482f0bf
export optional_arg as opt for tests.
L3MON4D3 Nov 4, 2024
d96028f
set jump_active=false ASAP.
L3MON4D3 Nov 4, 2024
28c1300
choiceNode: explicitly set parent and pos for choices.
L3MON4D3 Nov 6, 2024
a8e340c
fix(dynamicNode): don't access .snip in update_static.
L3MON4D3 Nov 6, 2024
98ed158
dynamicNode: optionally use .snip to generate docstring.
L3MON4D3 Nov 6, 2024
acec69f
enqueue cursor-movement due to update in typeahead.
L3MON4D3 Nov 6, 2024
2c5300f
get_args: do (static_)visible-check in get_args, not get_static_text.
L3MON4D3 Nov 6, 2024
d69df12
test docstring-generation with self-dependent dynamicNode.
L3MON4D3 Nov 6, 2024
24b925e
properly restore cursor-position in set_choice.
L3MON4D3 Nov 7, 2024
d3a7709
change_choice: use cursor-restore system from update_dependents.
L3MON4D3 Oct 14, 2025
37b963a
snippet_string: add metadata and marks.
L3MON4D3 Nov 13, 2024
eed721c
store cursor-position in snippetString to more accurately restore it.
L3MON4D3 Oct 14, 2025
6bef3b3
api_enter: only log an error when called recursively.
L3MON4D3 Nov 14, 2024
77c5de4
feedkeys: ignore errors on asynchronous nvim_win_set_cursor.
L3MON4D3 Nov 14, 2024
4be52be
correctly restore self-dependent dynamicNode.
L3MON4D3 Nov 14, 2024
26d21ae
change/set/select_choice: update current node before modifying choice.
L3MON4D3 Oct 14, 2025
29c1ee2
add a few tests for previous changes.
L3MON4D3 Nov 14, 2024
a4dd915
Format with stylua
L3MON4D3 Oct 14, 2025
dcdf2ae
fix: pass correct arguments to str_byteindex.
L3MON4D3 May 5, 2025
02fd641
fix: handle unicode->snippetstring->unicode conversion correctly.
L3MON4D3 May 5, 2025
38c55fe
add a few annotations.
L3MON4D3 Oct 14, 2025
9e1d258
feedkeys: only clear action after its confirm is called.
L3MON4D3 May 16, 2025
b525bd3
store: check that child-snip has valid extmarks before storing.
L3MON4D3 Jun 3, 2025
ab81681
improve logging of dynamicNode.
L3MON4D3 Oct 19, 2025
5c2225e
dynamicNode: limit number of nested updates.
L3MON4D3 Oct 19, 2025
42fdd36
rip out update_depth again.
L3MON4D3 Oct 19, 2025
f5bd74d
add brief documentation for new features.
L3MON4D3 Nov 2, 2025
31c9e2b
Format with stylua
L3MON4D3 Nov 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: handle unicode->snippetstring->unicode conversion correctly.
  • Loading branch information
L3MON4D3 committed Nov 3, 2025
commit 02fd641baf4351c9e8f0525e1e2f64baf797a293
22 changes: 12 additions & 10 deletions lua/luasnip/util/str.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,11 @@ function M.multiline_append(strmod, strappend)
end

-- turn a row+col-offset for a multiline-string (string[]) (where the column is
-- given in utf-codepoints and 0-based) into an offset (in bytes!, 1-based) for
-- given in bytes and 0-based) into an offset (in bytes, 1-based) for
-- the \n-concatenated version of that string.
---
---@param str string[], a multiline string
---@param pos LuaSnip.ApiPosition, an api-position relative to the start of str.
function M.multiline_to_byte_offset(str, pos)
if pos[1] < 0 or pos[1] + 1 > #str or pos[2] < 0 then
-- pos is trivially (row negative or beyond str, or col negative)
Expand All @@ -218,34 +221,33 @@ function M.multiline_to_byte_offset(str, pos)

-- allow positions one beyond the last character for all lines (even the
-- last line).
local pos_line_str = str[pos[1] + 1] .. "\n"

if pos[2] >= #pos_line_str then
if pos[2] >= #str[pos[1]+1] + 1 then
-- in this case, pos is outside of the multiline-region.
return nil
end

-- I think we can always assume utf-8?
byte_pos = byte_pos + vim.str_byteindex(pos_line_str, "utf-8", pos[2])
byte_pos = byte_pos + pos[2]

-- 0- to 1-based columns.
return byte_pos + 1
end

-- inverse of multiline_to_byte_offset, 1-based byte to 0,0-based row,column, utf-aware.
-- inverse of multiline_to_byte_offset, 1-based byte to 0,0-based row,column.
---@param str string[], the multiline string
---@param byte_pos number, a 1-based index into the \n-concatenated `str`.
function M.byte_to_multiline_offset(str, byte_pos)
if byte_pos < 0 then
return nil
end

local byte_pos_so_far = 0
for i, line in ipairs(str) do
-- line-length + \n.
local line_i_end = byte_pos_so_far + #line + 1
if byte_pos <= line_i_end then
-- byte located in this line, find utf-index.
local utf16_index =
vim.str_utfindex(line .. "\n", byte_pos - byte_pos_so_far - 1)
return { i - 1, utf16_index }
-- byte is in this line, return it.
return { i - 1, byte_pos - byte_pos_so_far - 1 }
end
byte_pos_so_far = line_i_end
end
Expand Down
111 changes: 73 additions & 38 deletions tests/integration/choice_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ describe("ChoiceNode", function()
ls_helpers.clear()
ls_helpers.session_setup_luasnip()

screen = Screen.new(50, 7)
screen:attach()
screen = ls_helpers.new_screen(50, 7)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue },
[1] = { bold = true, foreground = Screen.colors.Brown },
Expand Down Expand Up @@ -604,7 +603,7 @@ describe("ChoiceNode", function()
screen:expect({ unchanged = true })

-- test some more wild stuff, just because.
feed(" <left> ")
feed("<space><space><left>")
exec_lua([[
ls.snip_expand(s("trig", {
t":",
Expand All @@ -622,52 +621,88 @@ describe("ChoiceNode", function()
}))
]])

screen:expect({
grid = [[
:.ccee e :^c{3:c}eeee:eee ee.: |
{0:~ }|
{2:-- SELECT --} |
]],
})
screen:expect([[
:.ccee ee :^c{3:c}eeee: ee ee.: |
{0:~ }|
{2:-- SELECT --} |
]])
exec_lua("ls.jump(1)")
feed("<esc><right><right>i aa <left><left>")
exec_lua("ls.set_choice(2)")
screen:expect({
grid = [[
:.ccee e :.ccee ee^ee ee.:eee ee.: |
{0:~ }|
{2:-- INSERT --} |
]],
})

screen:expect([[
:.ccee ee :.ccee ee^ee ee.: ee ee.: |
{0:~ }|
{2:-- INSERT --} |
]])

-- reselect outer choiceNode
exec_lua("ls.jump(-1)")
exec_lua("ls.jump(-1)")
exec_lua("ls.jump(-1)")
exec_lua("ls.jump(1)")
screen:expect({
grid = [[
:.cc^e{3:e e :.ccee eeee ee.:eee ee}.: |
{0:~ }|
{2:-- SELECT --} |
]],
})
screen:expect([[
:.cc^e{3:e ee :.ccee eeee ee.: ee ee}.: |
{0:~ }|
{2:-- SELECT --} |
]])
exec_lua("ls.change_choice(1)")
screen:expect({
grid = [[
:cc^e{3:e e :.ccee eeee ee.:eee ee}: |
{0:~ }|
{2:-- SELECT --} |
]],
})
screen:expect([[
:cc^e{3:e ee :.ccee eeee ee.: ee ee}: |
{0:~ }|
{2:-- SELECT --} |
]])
exec_lua("ls.jump(1)")
exec_lua("ls.jump(1)")
screen:expect({
grid = [[
:ccee e :.cc^e{3:e eeee ee}.:eee ee: |
{0:~ }|
{2:-- SELECT --} |
]],
})
screen:expect([[
:ccee ee :.cc^e{3:e eeee ee}.: ee ee: |
{0:~ }|
{2:-- SELECT --} |
]])
end)

it("correctly handles unicode when storing and restoring.", function()
exec_lua([=[
ls.snip_expand(
s("choice", {
c(1, {
{t"a ", r(1, "k", i(1)), t" a"},
{t"bb ", r(1, "k"), t" bb"}
}, {restore_cursor = true})
}))
]=])
screen:expect([[
a ^ a |
{0:~ }|
{2:-- INSERT --} |
]])
feed("a a<left><left>")
exec_lua([=[
ls.snip_expand(s("bad", {i(1, "i…i")}))
]=])
screen:expect([[
a a ^i{3:…i} a a |
{0:~ }|
{2:-- SELECT --} |
]])

exec_lua("ls.change_choice(1)")
screen:expect([[
bb a ^i{3:…i} a bb |
{0:~ }|
{2:-- SELECT --} |
]])
feed("<Esc>la")
screen:expect([[
bb a i…^i a bb |
{0:~ }|
{2:-- INSERT --} |
]])
exec_lua("ls.change_choice(1)")
screen:expect([[
a a i…^i a a |
{0:~ }|
{2:-- INSERT --} |
]])
end)
end)
6 changes: 6 additions & 0 deletions tests/unit/str_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ describe("str.multiline_to_byte_offset", function()
check("on linebreak of last line", { "asdf", "qwer" }, { 1, 4 }, 10)
check_is_nil("negative row", { "asdf", "qwer" }, { -1, 0 })
check_is_nil("negative col", { "asdf", "qwer" }, { 0, -2 })
check("unicode1", { "aa … aa" }, { 0, 6 }, 7)
check("unicode2", { "aa …a… aa" }, { 0, 6 }, 7)
check("unicode3", { "aa …a… aa", "aa …a… aa" }, { 1, 6 }, 21)
end)

describe("byte_to_multiline_offset", function()
Expand Down Expand Up @@ -342,4 +345,7 @@ describe("byte_to_multiline_offset", function()
check("multiple lines middle linebreak", { "asdf", "qwer" }, 10, { 1, 4 })
check_is_nil("before string", { "asdf", "qwer" }, -1)
check_is_nil("multiple lines behind string", { "asdf", "qwer" }, 11)
check("unicode1", { "aa … aa" }, 7, { 0, 6 })
check("unicode2", { "aa …a… aa" }, 7, { 0, 6 })
check("unicode3", { "aa …a… aa", "aa …a… aa" }, 21, { 1, 6 })
end)