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?

Bepitic
  • 61
  • 6

1 Answers1

3

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
Bepitic
  • 61
  • 6