From 698f2d49103f20c09bebad1656da26def81d36cf Mon Sep 17 00:00:00 2001 From: Stanislav Seletskiy Date: Wed, 21 Oct 2015 14:17:10 +0600 Subject: [PATCH] print snippet information in backtrace (fix #551) --- pythonx/UltiSnips/snippet/definition/_base.py | 25 ++++++++++- pythonx/UltiSnips/snippet_manager.py | 6 +++ test/test_ParseSnippets.py | 41 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/pythonx/UltiSnips/snippet/definition/_base.py b/pythonx/UltiSnips/snippet/definition/_base.py index 62af17e..a974334 100644 --- a/pythonx/UltiSnips/snippet/definition/_base.py +++ b/pythonx/UltiSnips/snippet/definition/_base.py @@ -6,6 +6,7 @@ import re import vim +import textwrap from UltiSnips import _vim from UltiSnips.compatibility import as_unicode @@ -116,7 +117,29 @@ class SnippetDefinition(object): snip = SnippetUtilForAction(locals) - exec(code, {'snip': snip}) + try: + exec(code, {'snip': snip}) + except Exception as e: + e.snippet_info = textwrap.dedent(""" + Trigger: {} + Description: {} + Context: {} + Pre-expand: {} + Post-expand: {} + """).format( + self._trigger, + self._description, + self._context_code if self._context_code else '', + self._actions['pre_expand'] if 'pre_expand' in self._actions + else '', + self._actions['post_expand'] if 'post_expand' in self._actions + else '', + code, + ) + + e.snippet_code = code + + raise return snip diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index 408b6be..b4e99c7 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -10,6 +10,7 @@ import platform import traceback import sys import vim +import re from contextlib import contextmanager from UltiSnips import _vim @@ -68,6 +69,11 @@ Following is the full stack trace: """ msg += traceback.format_exc() + if hasattr(e, 'snippet_info'): + msg += "\nSnippet, caused error:\n" + msg += re.sub( + '^(?=\S)', ' ', e.snippet_info, flags=re.MULTILINE + ) # snippet_code comes from _python_code.py, it's set manually for # providing error message with stacktrace of failed python code # inside of the snippet. diff --git a/test/test_ParseSnippets.py b/test/test_ParseSnippets.py index 2d00b5a..1b2b327 100644 --- a/test/test_ParseSnippets.py +++ b/test/test_ParseSnippets.py @@ -263,3 +263,44 @@ class ParseSnippets_PrintPythonStacktraceMultiline(_VimTest): wanted = keys expected_error = " > \s+qwe" + +class ParseSnippets_PrintErroneousSnippet(_VimTest): + files = { 'us/all.snippets': r""" + snippet test "asd()" e + endsnippet + """} + keys = 'test' + EX + wanted = keys + expected_error = "Trigger: test" + + +class ParseSnippets_PrintErroneousSnippetContext(_VimTest): + files = { 'us/all.snippets': r""" + snippet test "asd()" e + endsnippet + """} + keys = 'test' + EX + wanted = keys + expected_error = "Context: asd" + + +class ParseSnippets_PrintErroneousSnippetPreAction(_VimTest): + files = { 'us/all.snippets': r""" + pre_expand "asd()" + snippet test + endsnippet + """} + keys = 'test' + EX + wanted = keys + expected_error = "Pre-expand: asd" + + +class ParseSnippets_PrintErroneousSnippetPostAction(_VimTest): + files = { 'us/all.snippets': r""" + post_expand "asd()" + snippet test + endsnippet + """} + keys = 'test' + EX + wanted = keys + expected_error = "Post-expand: asd"