Phyton interpolation with a new line \n

Asked by feliperama

I'm trying to do python interpolation to create a big block of XML code, however i'm having problems with identation when using \n in a string. For example, if I use the following snippet in a 2 space vim tab identation after the <tag> line

snippet test "XML tag test" b
`!p snip.rv = '<test>\n</test>\n'`
endsnippet

I'm getting these:

<tag>
  <test>
</test>
</tag>

BUT I want:
<tag>
  <test>
  </test>
</tag>

How can I return a string with multiple lines that respect the code identation space?

Question information

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

Hi SilVer, I tried your tip:

snippet test "Test" b
`!p
snip.rv = '<test>'
snip.rv += '\n' + snip.mkline('', indent='')
snip.rv += '</test>'
`
endsnippet

BUT still didn't work. Maybe is related with my vim options?
...
syntax on
set backspace=2
set showmode
set shiftwidth=2
set tabstop=2
set softtabstop=2
set expandtab
set scrolloff=5
...

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

try this

snippet test "Test" b
`!p
snip.rv += '\n' + snip.mkline('<test>', indent='')
snip.rv += '\n' + snip.mkline('<test>', indent='')
snip.rv += '\n' + snip.mkline('<test>', indent='')
`
endsnippet

Revision history for this message
feliperama (feliperama) said :
#4

No, didn't work too. The result of your example was:

---before snip insertion---
<arg>
  test

---snip inserted----
<arg>

<test>
<test>
<test>

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

sorry, my bad. the indent="" should of course be gone. Just like it is stated in the documentation.

snippet test "Test" b
`!p
snip.rv += '\n' + snip.mkline('<test>')
snip.rv += '\n' + snip.mkline('<test>')
snip.rv += '\n' + snip.mkline('<test>')
`
endsnippet

Revision history for this message
feliperama (feliperama) said :
#6

Nice!!! I did a little change to your snippet code and got the expected. Tks very much for your time, and congratulations for this great plugin : )

----------SNIPET------------
snippet test "Test" b
`!p
snip.rv += snip.mkline('<test>') + '\n'
snip.rv += snip.mkline('</test>')
`
endsnippet

---------RESULT--------
<arg>
  <test>
  <\test>