Evaluate python string as snippet

Asked by RobrechtDR

Is it possible to evaluate/interpret a python string as a snippet?

E.g.:

------------------- SNIP -------------------
snippet "my_snip (\d+)" "create a column of names" r
`!p
def fu(n):
    stri = ""
    for i in range(int(n)):
        stri += "+---- ${%d:my_name}\n" % i
    return stri
snip.rv = fu(match.group(1))
`
endsnippet
------------------- SNAP -------------------
my_snip 3<tab> ->
+---- ${0:my_name}
+---- ${1:my_name}
+---- ${2:my_name}

But I would like the following instead:

my_snip 3<tab> ->
+---- John
+---- my_name
+---- my_name

Such that the result is the same as calling the following snippet:

------------------- SNIP -------------------
snippet "my_snip 3" "create a column of names" b
+---- {0: my_name}
+---- {1: my_name}
+---- {2: my_name}
endsnippet

Question information

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

Thank you for your answer SirVer!

What I wanted to achieve with this was to make a dynamic grid table(http://docutils.sourceforge.net/docs/user/rst/quickref.html#tables) snippet for reStructuredText. This would be awesome i.m.o. .

E.g.

------------------- SNAP -------------------
table_grid 4 3<tab> ->

+--------------+---------------+-------------+
| Header 1 | Header 2 | Header 3 |
+========+=========+=======+
| cell 1.1 | cell 1.2 | cell 1.3 |
+--------------+----------------+------------+
| | | |
| cell 2.1 | cell 2.2 | cell 2.3 |
| | | |
+--------------+----------------+-------------|
| cell 3.1 | cell 3.2 | cell 3.3 |
+--------------+----------------+------------+

Technical characteristics of this dynamic table are as follows:

1. You should be able to pass the rows and columns to the snippet.
(OK, I have asked this. By using regex)

2. The length of each column should expand along with the length of the largest cell in that column.
(I've only managed to let the lines above and under a cell autoexpand, but not for cells in the rows under it. But probably possible)

3. The names of the cells should be able to be edited using Ultisnips' ${1: cell 1.1} syntax.
(Not possible afaik without the mentioned feature which is the topic of this thread. But maybe you know another way to achieve this?)

4. Have a separate row snippet that lets you add a row to an already existing table . It should autodetect how many columns the table has you are appending the row to.
(Not possible unless the snippet reads the file you are editing and parses it to detect the +-----+------+ structure one line above the line of the current position of the cursor. Afaik this is possible in Ultisnips. However, it should also be able to use the feature mentioned in 3. so you could edit the content of each cell conveniently.)

Could you tell me if you think my assumptions between brackets are correct?

Revision history for this message
RobrechtDR (de-rouck-robrecht) said :
#4

I'm sorry, for the grid's format in my example. The grid's formatting looked different in the edit screen vs the one that got posted. What I had in mind was the grid table in the following link: http://docutils.sourceforge.net/docs/user/rst/quickref.html#tables . The cells shouldn't able to span across other cells, a row should be able to expand in height along with to height of the cell containing the most lines.

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

I am familiar with rst tables. Sorry, I do not think there is much that UltiSnips can do right now to accomodate you - I agree that it would be awesome, but support for placeholders in python code is not trivial to add - it should be theoretically possible though.

> (I've only managed to let the lines above and under a cell autoexpand, but not for cells in the rows under it. But probably possible)
Python blocks are executed in order and share a common python context, you can therefore assign variables in one and read it in any after that. So yes, this should be possible but might be messy.

> 3. The names of the cells should be able to be edited using Ultisnips' ${1: cell 1.1} syntax.
(Not possible afaik without the mentioned feature which is the topic of this thread. But maybe you know another way to achieve this?)

Not possible right now, sorry. Patches are welcome :).

> 4. Have a separate row snippet that lets you add a row to an already existing table . It should autodetect how many columns the table has you are appending the row to.
(Not possible unless the snippet reads the file you are editing and parses it to detect the +-----+------+ structure one line above the line of the current position of the cursor. Afaik this is possible in Ultisnips. However, it should also be able to use the feature mentioned in 3. so you could edit the content of each cell conveniently.)

Parsing the file is a viable solution here. I did this before a few times. Basically, you start at vim.cursor.line and move backwards until you find a line that matches your regular expression and figure out what kind of table you need. It's easy to code up in a python code block.

Revision history for this message
RobrechtDR (de-rouck-robrecht) said :
#6

Thanks SirVer, that solved my question.