L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.37k stars 240 forks source link

jump() doesn't work in select mode/placeholders #1212

Open DasGandlaf opened 2 months ago

DasGandlaf commented 2 months ago

Hey, thanks for this amazing plugin!

When I have this code:

local ls = require('luasnip')

vim.keymap.set('i', '<tab>', function()
  ls.expand_or_jump()
end)

ls.add_snippets('lua', {
  ls.parser.parse_snippet('foo1', 'foo1($1, $2)'),
  ls.parser.parse_snippet('foo2', 'foo2(${1:one}, ${2:two})'),
}, { key = 'lua' })

vim.keymap.set({"i", "s"}, "<c-n>", function() ls.jump( 1) end, {silent = true, noremap = true})
vim.keymap.set({"i", "s"}, "<c-p>", function() ls.jump(-1) end, {silent = true, noremap = true})

ls.jump() works when using snippet foo1. It doesn't work when using snippet foo2.

When expanding, it selects the placeholder one correctly. But ls.jump() does not work.

When logging session.current_nodes in jump() in luasnip/init.lua it returns {}.

When expanding foo2, session.current_nodes is set to nil (= next_current) in unlink_set_adjacent_as_current_no_log().

What could the problem be? Thanks!

pavel-suhakou commented 2 months ago

I also have this issue with vs code snippets ("rafamadriz/friendly-snippets"). Jumps to the first parameter but cannot jump further.

L3MON4D3 commented 2 months ago

Mhh, the first part of you problem-description sounds like you're just missing the SELECT-keybinding (set via smap or vim.keymap.set({"s", ...}, ...)). What you observedafter that, with the current_nodes being set to nil happens when the snippet-text is modified in such a way that luasnip can't tell what happened to it, which usually is because the buffer was formatted. Could you disable all formatters, and check what happens?

DasGandlaf commented 2 months ago

I'm not fully sure what you mean with the select mapping. In the code snippet, I map insert and select mode:

vim.keymap.set({"i", "s"}, "<c-n>", function() ls.jump( 1) end, {silent = true, noremap = true})
vim.keymap.set({"i", "s"}, "<c-p>", function() ls.jump(-1) end, {silent = true, noremap = true})

Do you mean something else?

With regards to the formatting, I do not have any formatting enabled, and when expanding a snippet, the buffer does not get formatted either. So current_nodes is being set to nil even without formatting.

Also I'm not even sure if the problem is with current_nodes, it's just something I found out while trying to find the mistake. It might not have to do anything with the problem, but from my limited knowledge, it seems that it is a problem.

I really appreciate your help!

L3MON4D3 commented 2 months ago

Ah, I thought you might have used the <Tab>-mapping since that was mapped to ls.expand_or_jump as opposed to just ls.expand Could you enable logging via ls.log.set_loglevel("info"), produce the issue, and then check your log via ls.log.open()? There should be some information on why the current snippet was unlinked