Replace Tab Stops with transformation

Asked by Stephen Prater

I feel like I'm missing something obvious here, but I'm interested in transforming the text entered in a tab stop in place. Basically, if I enter something in all lower case, and then tab out of it (or not) and it replaces the text I entered with the transform text I entered.

How can I accomplish something like that?

Question information

Language:
English Edit question
Status:
Solved
For:
UltiSnips Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen Prater
Solved:
Last query:
Last reply:
Revision history for this message
SirVer (sirver) said :
#1

It Would help if you provide an example: keys typed and desired output. Or a sample snippet of what you tried. I do not understand why you want to achieve.

Revision history for this message
SirVer (sirver) said :
#2

S/why/what. Damn autocorrection.

Revision history for this message
Stephen Prater (yatc18ks0g8zofezrpk3x-me-ngeefk4xayt3t4u9watah) said :
#3

in particular, I'm trying to type a SKU in and get an "encoded" safe version of it back.

XKC-144544 <- what i type

XKChhh144544 <- what appears in the tab stop (it's best not to pry to deeply into why it needs to appear like that - and it's a little more complex than that, but that's the general idea.)

It's so that I can copy these SKU's from a list of SKUs without having to worry about doing the encoding in my head and where the original string I typed doesn't appear anywhere in the snippet expansion. (I don't care if it does this after I move to the next stop or something.)

I tried
`!p t[1] = transform_function(t[1])` and got an error saying assignment wasn't supported.
${1/-/hhh/} (and got "Tabstop 1 is not known but is used by a Transformation")

Revision history for this message
SirVer (sirver) said :
#4

It is starting to dawn on me what you want to achieve. No, you cannot change the content of a tabstop - in fact there is no way to change it in anyway. But what you want to achieve can likley be done using a regular expression trigger:

snippet "XKC-\d+" "Example" r
`!p snip.rv = match.group(0).replace("-","hhh")`
endsnippet

now type: XKC-14454<tab>
and you'll get
XKChhh144544

So instead of expand+type you now need to type first, then expand. Is this what you need?

Revision history for this message
Stephen Prater (yatc18ks0g8zofezrpk3x-me-ngeefk4xayt3t4u9watah) said :
#5

Unfortunately that won't work because the SKUs aren't that predictable.

I managed to hack something up using the VISUAL thing - where I just highlight the thing I want to transform then trigger, type, trigger and it comes back changed. Works for me.

Revision history for this message
SirVer (sirver) said :
#6

You can also use a catch-all regular expression "[a-zA-Z-]*" or so. But you will not be able to trigger any other snippet then. I'd still go with regular expression triggers as long as there is any pattern to the SKU (like the hiphen?). Maybe this:

snippet "([\w\d]+)-([\w\d]+)" "Example" r
`!p snip.rv = match.group(1) + "hhh" + match.group(2)`
endsnippet

or very similar
snippet "[\w\d]+-[\w\d]+" "Example" r
`!p snip.rv = match.group(0).replace("-", "hhh")`
endsnippet

lkjsf-98349587<tab>
becomes
lkjsfhhh98349587

a-b<tab>
becomes
ahhhb

Bottom line: I am sure, UltiSnips can accommodate you, but you will need to provide more exampels for an ideal solution methinks.