draivin / hsnips

HyperSnips: a powerful snippet engine for VS Code, inspired by vim's UltiSnips
MIT License
148 stars 23 forks source link

Rather weird behavior when working with spaces #156

Closed Ja1Zhou closed 1 year ago

Ja1Zhou commented 1 year ago

I spent a lot of time debugging, and found that this snippet

snippet `(?<=[a-zA-Z0-9])([<+-=>])` iAm
 `` rv = m[1] ``
endsnippet

Somehow matches unintended cases such as m3 -> m 3, and 30 -> 3 0. I have made sure that the problem was triggered by this exact fragment, yet I really have difficulty figuring out the cause, since the captured group has nothing to do with numbers. Help would be much appreciated.

gruvw commented 1 year ago

Hello,

I think the issue comes from a misunderstanding of the - regex operator.

Here, when you write [<+-=>] it means from the character + to the character = (as in [b-d], from b to d).
It's a little tricky, but I think it will fix your issue if you just escape the - using \- or simply put the - at the end of the [] where it will lose its property: either use (?<=[a-zA-Z0-9])([<+\-=>]) or (?<=[a-zA-Z0-9])([<+=>-]).

From https://regexr.com/:

Matches a character in the range "+" to "=" (char code 43 to 61). Case sensitive.

Let me know if it worked :)

Ja1Zhou commented 1 year ago

Works like a charm, much appreciated~