diff --git a/ChangeLog b/ChangeLog index d5909a3..1d1e388 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ trunk: + - Conditional Inserts can now be nested - Added support for b option. This only considers a snippet at the beginning of a line ( *UltiSnips-adding-snippets* ) - Added support for ! option. This overwrites previously defined snippets diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index a5bf004..39b228f 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -240,10 +240,13 @@ can be used: > fn - The current filename path - The complete path to the current file t - The values of the placeholders, t[1] -> current text of ${1} and so on + cur - The current text of the placeholder. You can check if this is != "" + to make sure that the interpolation is only done once -Also, the re and os modules are already imported inside the snippet code. This -allows for very flexible snippets. For example, the following snippet mirrors -the first Tab Stops value on the same line in uppercase and right aligned: +Also, the vim, re, os, string and random modules are already imported inside +the snippet code. This allows for very flexible snippets. For example, the +following snippet mirrors the first Tab Stops value on the same line in +uppercase and right aligned: ------------------- SNIP ------------------- snippet wow diff --git a/plugin/UltiSnips/TextObjects.py b/plugin/UltiSnips/TextObjects.py index 78050d8..7865ffc 100644 --- a/plugin/UltiSnips/TextObjects.py +++ b/plugin/UltiSnips/TextObjects.py @@ -23,7 +23,7 @@ class _CleverReplace(object): _DOLLAR = re.compile(r"\$(\d+)", re.DOTALL) _SIMPLE_CASEFOLDINGS = re.compile(r"\\([ul].)", re.DOTALL) _LONG_CASEFOLDINGS = re.compile(r"\\([UL].*?)\\E", re.DOTALL) - _CONDITIONAL = re.compile(r"\(\?(\d+):(.*?)(? 1: + rv = self._unescape(self._replace_conditional(match,args[1])) + + v = v[:start] + rv + v[end:] + + m = self._CONDITIONAL.search(v) + return v + def _unescape(self, v): return self._UNESCAPE.subn(lambda m: m.group(0)[-1], v)[0] def replace(self, match): @@ -53,18 +103,11 @@ class _CleverReplace(object): def _conditional(m): args = m.group(2).split(':') - # TODO: the returned string should be checked for conditionals - if match.group(int(m.group(1))): - return self._unescape(args[0]) - elif len(args) > 1: - return self._unescape(args[1]) - else: - return "" # Replace CaseFoldings tv = self._SIMPLE_CASEFOLDINGS.subn(self._scase_folding, tv)[0] tv = self._LONG_CASEFOLDINGS.subn(self._lcase_folding, tv)[0] - tv = self._CONDITIONAL.subn(_conditional, tv)[0] + tv = self._replace_conditional(match, tv) rv = tv.decode("string-escape") @@ -624,7 +667,7 @@ class PythonCode(TextObject): code = code.replace("\\`", "`") # Add Some convenience to the code - self._code = "import re, os, vim\n" + code.strip() + self._code = "import re, os, vim, string, random\n" + code.strip() TextObject.__init__(self, parent, start, end, "") @@ -634,10 +677,13 @@ class PythonCode(TextObject): path = "" fn = os.path.basename(path) + ct = self.current_text d = { 't': _Tabs(self), 'fn': fn, 'path': path, + 'cur': ct, + 'res': ct, } exec self._code in d diff --git a/test.py b/test.py index 75e990c..fde3d48 100755 --- a/test.py +++ b/test.py @@ -838,6 +838,13 @@ class Transformation_ConditionalInsertRWEllipsis_ECR(_VimTest): snippets = ("test", r"$1 ${1/(\w+(?:\W+\w+){,7})\W*(.+)?/$1(?2:...)/}") keys = "test" + EX + "a b c d e f ghhh h oha" wanted = "a b c d e f ghhh h oha a b c d e f ghhh h..." +class Transformation_ConditionalInConditional_ECR(_VimTest): + # TODO: here lingers a bug + snippets = ("test", r"$1 ${1/^.*?(-)?(>)?$/(?2::(?1:>:.))/}") + keys = "test" + EX + "hallo" + ESC + "$a\n" + \ + "test" + EX + "hallo-" + ESC + "$a\n" + \ + "test" + EX + "hallo->" + wanted = "hallo .\nhallo- >\nhallo-> " class Transformation_CINewlines_ECR(_VimTest): snippets = ("test", r"$1 ${1/, */\n/}")