2

I am using UltiSnipswith vimtex as explained here and trying to use the snippet for Inline Math for LaTeX as shown in the link above. Everytime I try to execute the snippet it executes but immediately an error pops up in my vim.

Here is the snippet definition I am using :

snippet im "create an inline math environment" wAm
${$1}$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
    snip.rv = ' '
    else:
    snip.rv = ''
    `$2
endsnippet

And here is the error I am getting from vim :

An error occured. This is either a bug in UltiSnips or a bug in a
snippet definition. If you think this is a bug, please report it to
https://github.com/SirVer/ultisnips/issues/new
Please read and follow:
https://github.com/SirVer/ultisnips/blob/master/CONTRIBUTING.md#reproducing-bugs

Following is the full stack trace:
Traceback (most recent call last):
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/err_to_scratch_buffer.py", line 18, in wrapper
    return func(self, *args, **kwds)
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/snippet_manager.py", line 896, in _track_change
    self._try_expand(autotrigger_only=True)
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/snippet_manager.py", line 804, in _try_expand
    self._do_snippet(snippet, before)
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/snippet_manager.py", line 757, in _do_snippet
    text_before, self._visual_content, parent, start, end
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 488, in launch
    snippet_instance.update_textobjects(vim_helper.buf)
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/text_objects/snippet_instance.py", line 93, in update_textobjects
    if obj._update(done, buf):
  File "/home/nitin/.vim/plugged/ultisnips/pythonx/UltiSnips/text_objects/python_code.py", line 268, in _update
    exec(code, self._locals)  # pylint:disable=exec-used
  File "<string>", line 4
    else:
       ^
SyntaxError: invalid syntax

Executed snippet code:
  1
  2   if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
  3       snip.rv = ' '
  4       else:
  5       snip.rv = ''
  6 

Can anyone please help me figure out what is wrong and how to fix it?

2

It seems that when you copied the snippet you forgot to add the indentation spaces in the python snippet conditional code as shown in the error message:

Executed snippet code:
  1
  2   if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
  3       snip.rv = ' '
  4       else:
  5       snip.rv = ''
  6 

Try to change it to:

snippet im "create an inline math environment" wAm
${$1}$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
    snip.rv = ' '
else:
    snip.rv = ''
    `$2
endsnippet
| improve this answer | |
  • Yeah, now it works. Thanks. Could you also please say why it did work even while giving error? – Nitin Apr 27 at 17:37
  • 1
    I edited the answer to suggest the change in the snippet, your original answer was doing the correct suggestion but in the error message instead. Please revert my edit if you disagree or find that it's incorrect. – filbranden Apr 27 at 18:15
  • 2
    @fibranden, it looks good! @Nitin, that remains a mistery, a modest guess would be that since the problem is in the else, it never gets executed, thus snip.rv is always ' ' which may not be the right behavior but gave you the appearance of working – Vicente Bolea Apr 27 at 23:39

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.