Creating an inserter for snippets.nvim: $0 doesn't seem to work

Does anyone have experience writing an inserter for norcalli/snippets.nvim?
There’s an open issue for an inserter which expands the snippet within the current buffer using extmarks, so I gave it a shot. I used highlighting for the placeholder terms to try to make them easily distinguishable without being distracting.

https://asciinema.org/a/raBKf6dmsISqtIDYpP8PmK42Q

This demo was using a really simple snippet. The second $1 is to test the expansion, and the $0 is the bit that doesn’t work.

for ${1:i} = ${2:first, last, step} do
    $1
    $0
end

This is my code, so far. It’s still littered with print statements for debugging, and it’s pretty rough.
https://gist.github.com/trv6/353031dfb9b86f83e5318b93a2a2efb8

The basic functionality seems to be working (I think), but snippets with a $0 symbol don’t have the cursor moved to the symbol’s location after the expansion ends.
I thought this was handled outside of the inserter. Is it something that I have to handle? How do I distinguish the $0 structure from the others?

Pretty sure it’s working now. Looking through the existing inserters, it does look like it’s the inserter’s job to place the cursor on a $0 symbol after expansion. Fortunately, the readme says the $0 symbol is the only structure with order 0, so it’s pretty easy to find.
It seems to work for simple snippets, which is probably all I’ll ever use. I haven’t tried any of the more complex stuff in the readme.
It is a little fragile to use. You can set text at the position of an extmark, but there’s no function to get the text at the position of an extmark, so I had to match a pattern. If you change something outside of the current placeholder, the pattern will probably fail.
I’m pretty happy with how select mode works on the placeholders, although that’s implemented in a really gross way. If anyone knows a better way to do that, I’d be very happy to make a change.

On the other side of things, it looks like snippets.nvim might be abandoned. I’m a little surprised… I picked it because it was at the top of the list on the awesome-neovim repository and the first result from google. Is there a different plug-in that I should take a look at?

On the other side of things, it looks like snippets.nvim might be abandoned. I’m a little surprised… I picked it because it was at the top of the list on the awesome-neovim repository and the first result from google. Is there a different plug-in that I should take a look at?

I don’t use snippets but heard good things about luasnip.

@trv Thank you for this.
This has been a long time on my waiting list

I tried it out, and every time I select a snippet (self or lsp) from autocompletion menu it gives me this error and does not expand further. puts my cursor at the beginning of the line

Error detected while processing CompleteDone Autocommands for "<buffer=2>":
E5108: Error executing lua ...art/snippets.nvim/lua/snippets/inserters/highlighter.
lua:89: col value outside range

I ran into that quite a few times while testing. Thought it was fixed. There’s probably a mistake in the section that counts the columns occupied by the default snippet text and it’s trying to place an extmark in an empty column.

I’m not sure of a good way to count the columns. Lua’s length operator won’t work for unicode. I used strdisplaywidth, but had to make a quick check for tabs since they only occupy one column.

Edit: Whoops. I probably should have been using strwidth all along. It might work now?

String width makes sense. But somehow it does the same result, with the same error. Will go over the code this weekend. Take a stab at it

Strange. I was able to produce the same error, but strwidth resolved it. The case I found was lines indented with 3 tabs vs 2. I’m actually not sure how that broke down, but I was happy to see it go away.

I won’t be able to look at it again until Sunday. The snippet above with util.match_indentation is what I’ve been using to test. It may help to see one of your snippets, if you don’t mind.

I’ll probably be taking a look at LuaSnip this weekend as well, as recommended.