L3MON4D3 / LuaSnip

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

Autosnippets not expanding in macros #1243

Open mattia-marini opened 6 days ago

mattia-marini commented 6 days ago

How to Reproduce

Expected behavior: The snippet should expand when the recorded macro is played.

Actual behavior: The snippet trigger is inserted, but no expansion occurs. This issue is only present for autosnippets; regular snippets are expanded correctly.

Strangely enough, when the snippet trigger is fed through APIs, the snippet expands as expected. For example:

lua vim.api.nvim_feedkeys("i()", "n", true) lua vim.api.nvim_input("i()")

expands just fine

L3MON4D3 commented 3 days ago

Hey :) This is because we currently use TextChangedI, which is not triggered when replaying macros (see here). I think it's possible to only use InsertCharPre, which is also triggered in macro-replay, I'll open a PR sonn-ish :)

L3MON4D3 commented 11 hours ago

Okay, update:

My plan was to use vim.schedule to run ls.expand_auto in an InsertCharPre-autocommand. This works fine if only one character is pending (ie. the non-macro case), but with a macro, multiple characters are inserted before the first vim.schedule'd autoexpansion arrives, so this won't work in the general case either.

It would be possible to record the position of expansion in InsertCharPre, and only schedule the actual expansion at that position. This would work perfectly for the simple case of snippets with only text, but entering text into and jumping between tabstops still breaks the macro.

I think I'll first implement the dumb, text-only version and then consider what should be done to support (or whether to support) more advanced snippets in macros. (best case: after running a macro, one is dropped into the first expanded snippet in the macro, whose insertNodes contain the text entered in the recording, and then one can jump through all the snippets in the macro in the order they were recorded. Think that would need far too much work, but would be pretty cool :D)