3

It's possible to create a placeholder inside a python interpolation block?.

I want to do a snippet for my grammar exercises. I'm currently in this snippet.

snippet "ej(ercicio)?( (\d+))?" "Ejercicios - grammar" br
${1:unidad}.${2:ejercicio} )$0`!p
num = match.group(3) if match.group(2) is not None else 2
for var in range(2, int(num)+1):
→   snip += str(var) + " -> $"+str(var + 1)`

endsnippet

But it outputs

ej 8<tab>

unidad.ejercicio )¬
2 -> $3¬
3 -> $4¬
4 -> $5¬
5 -> $6¬
6 -> $7¬
7 -> $8¬
8 -> $9¬

(the first it's always an example, so i don't print it)

Any clue?

2

Thanks to husB for point me the dynamic tabstop generation, i don't know why it isn't in the doc.

The final snippet:

global !p
def create_line_plhldr(snip):

    num = int(snip.buffer[snip.line].strip())

    snip.buffer[snip.line] = ''

    anon_snippet_body = '${1:unidad}.${2:ejercicio} )'
    for var in range(2, int(num)+1):
        anon_snippet_body += '\n' + str(var) + " -> $"+str(var + 1)

    snip.expand_anon(anon_snippet_body)
endglobal

post_jump "create_line_plhldr(snip)"
snippet "ej(ercicio)?( (\d+))?" "Ejercicios - grammar" br
`!p snip.rv = match.group(3) if match.group(2) is not None else 2 `
endsnippet
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.