From 15bb3ff34b3b9f0581224a9dbbae683dbe8e6637 Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Tue, 17 Jan 2012 21:21:13 +0100 Subject: [PATCH] Python code --- plugin/UltiSnips/TextObjects.py | 60 +--- test.py | 593 ++++++++++++++++---------------- 2 files changed, 304 insertions(+), 349 deletions(-) diff --git a/plugin/UltiSnips/TextObjects.py b/plugin/UltiSnips/TextObjects.py index 9362511..abc4f76 100644 --- a/plugin/UltiSnips/TextObjects.py +++ b/plugin/UltiSnips/TextObjects.py @@ -276,21 +276,11 @@ class TextObject(object): self._is_killed = False # TODO: not often needed def initial_replace(self): + # TODO: could this be replaced via _really_updateman? ct = self._initial_text # TODO: Initial Text is nearly unused. - debug("self._start: %r, self._end: %r" % (self._start, self._end)) - debug("self._start: %r, self._end: %r" % (self._start, self._end)) - debug("ct: %r" % (ct)) old_end = self._end - ct.to_vim(self._start, self._end) # TODO: to vim returns something unused - debug("self._end: %r" % (self._end)) - self._end = ct.calc_end(self._start) - debug("self._start: %r, self._end: %r" % (self._start, self._end)) - if self._end != old_end: - exclude = set() - exclude = set(c for c in self._childs) - exclude.add(self) - # TODO: maybe get rid of this function as well? - self.child_end_moved(min(old_end, self._end), self._end - old_end, exclude) + self._end = ct.to_vim(self._start, self._end) + self.child_end_moved2(old_end, self._end) def __lt__(self, other): return self._start < other._start @@ -340,10 +330,6 @@ class TextObject(object): if not (self._parent) or old_end == new_end: return - debug("###*** ") - assert(self._parent) - _do_print(self._parent) - pold_end = self._parent._end.copy() _move_nocheck(self._parent._end, old_end, new_end, new_end - old_end) def _move_all(o): @@ -356,38 +342,9 @@ class TextObject(object): for c in self._parent._childs: if c is self: continue _move_all(c) - _do_print(self._parent) - debug("***### ") - debug("pold_end: %r, self._parent._end: %r" % (pold_end, self._parent._end)) self._parent.child_end_moved2(pold_end, self._parent._end) - - - - - def child_end_moved(self, sp, diff, skip = set()): # TODO: pretty wasteful, give index - debug("self: %r, skip: %r, diff: %r" % (self, skip, diff)) - - if self not in skip: - _move(self._end, sp, diff) - - for c in self._childs: - if c in skip: continue - def _move_all(o): - _move(o._start, sp, diff) - _move(o._end, sp, diff) - - for oc in o._childs: - _move_all(oc) - _move_all(c) - - debug("self._parent: %r" % (self._parent)) - if self._parent and self._parent not in skip: - debug("b4 parent sp: %r, diff: %r" % (sp, diff)) - self._parent.child_end_moved(sp, diff, set((self,))) - debug("after parent sp: %r, diff: %r" % (sp, diff)) - def _do_edit(self, cmd): debug("self: %r, cmd: %r" % (self, cmd)) ctype, line, col, char = cmd @@ -463,7 +420,6 @@ class TextObject(object): delta = Position(0, len(char)) old_end = self._end.copy() _move(self._end, Position(line, col), delta) - #self.child_end_moved(Position(line, col), self._end - old_end, set((self,))) self.child_end_moved2(old_end, self._end) def edited(self, cmds): # TODO: Only in SnippetInstance @@ -642,7 +598,7 @@ class NoneditableTextObject(TextObject): old_end = self._end.copy() self._end = tb.to_vim(self._start, self._end) # TODO: to vim returns something unused - # TODO: child_end_moved is a stupid name for this function + # TODO: child_end_moved2 is a stupid name for this function self.child_end_moved2(old_end, self._end) class EscapedChar(NoneditableTextObject): @@ -934,7 +890,6 @@ class SnippetUtil(object): class PythonCode(NoneditableTextObject): def __init__(self, parent, token): - code = token.code.replace("\\`", "`") # Find our containing snippet for snippet local data @@ -956,8 +911,7 @@ class PythonCode(NoneditableTextObject): NoneditableTextObject.__init__(self, parent, token) - - def _do_update(self): + def _really_updateman(self): path = vim.eval('expand("%")') if path is None: path = "" @@ -980,9 +934,9 @@ class PythonCode(NoneditableTextObject): compatible_exec(self._code, self._globals, local_d) if self._snip._rv_changed: - self.current_text = self._snip.rv + self._replace_text(TextBuffer(self._snip.rv)) else: - self.current_text = as_unicode(local_d["res"]) + self._replace_text(TextBuffer(as_unicode(local_d['res']))) def __repr__(self): return "PythonCode(%s -> %s)" % (self._start, self._end) diff --git a/test.py b/test.py index e31bd4c..8ed94ee 100755 --- a/test.py +++ b/test.py @@ -857,302 +857,303 @@ class TabStop_VimScriptInterpolation_SimpleExample(_VimTest): wanted = " hi 4 End" # End: VimScript Interpolation #}}} ### PythonCode Interpolation {{{# -###### Deprecated way ########## -##class PythonCodeOld_SimpleExample(_VimTest): -## snippets = ("test", """hi `!p res = "Hallo"` End""") -## keys = "test" + EX -## wanted = "hi Hallo End" -##class PythonCodeOld_ReferencePlaceholderAfter(_VimTest): -## snippets = ("test", """${1:hi} `!p res = t[1]+".blah"` End""") -## keys = "test" + EX + "ho" -## wanted = "ho ho.blah End" -##class PythonCodeOld_ReferencePlaceholderBefore(_VimTest): -## snippets = ("test", """`!p res = len(t[1])*"#"`\n${1:some text}""") -## keys = "test" + EX + "Hallo Welt" -## wanted = "##########\nHallo Welt" -##class PythonCodeOld_TransformedBeforeMultiLine(_VimTest): -## snippets = ("test", """${1/.+/egal/m} ${1:`!p -##res = "Hallo"`} End""") -## keys = "test" + EX -## wanted = "egal Hallo End" -##class PythonCodeOld_IndentedMultiline(_VimTest): -## snippets = ("test", """start `!p a = 1 -##b = 2 -##if b > a: -## res = "b isbigger a" -##else: -## res = "a isbigger b"` end""") -## keys = " test" + EX -## wanted = " start b isbigger a end" -## -###### New way ########## -## -##class PythonCode_UseNewOverOld(_VimTest): -## snippets = ("test", """hi `!p res = "Old" -##snip.rv = "New"` End""") -## keys = "test" + EX -## wanted = "hi New End" -## -##class PythonCode_SimpleExample(_VimTest): -## snippets = ("test", """hi `!p snip.rv = "Hallo"` End""") -## keys = "test" + EX -## wanted = "hi Hallo End" -## -## -##class PythonCode_SimpleExample_ReturnValueIsEmptyString(_VimTest): -## snippets = ("test", """hi`!p snip.rv = ""`End""") -## keys = "test" + EX -## wanted = "hiEnd" -## -##class PythonCode_ReferencePlaceholder(_VimTest): -## snippets = ("test", """${1:hi} `!p snip.rv = t[1]+".blah"` End""") -## keys = "test" + EX + "ho" -## wanted = "ho ho.blah End" -## -##class PythonCode_ReferencePlaceholderBefore(_VimTest): -## snippets = ("test", """`!p snip.rv = len(t[1])*"#"`\n${1:some text}""") -## keys = "test" + EX + "Hallo Welt" -## wanted = "##########\nHallo Welt" -## -##class PythonCode_TransformedBeforeMultiLine(_VimTest): -## snippets = ("test", """${1/.+/egal/m} ${1:`!p -##snip.rv = "Hallo"`} End""") -## keys = "test" + EX -## wanted = "egal Hallo End" -## -##class PythonCode_MultilineIndented(_VimTest): -## snippets = ("test", """start `!p a = 1 -##b = 2 -##if b > a: -## snip.rv = "b isbigger a" -##else: -## snip.rv = "a isbigger b"` end""") -## keys = " test" + EX -## wanted = " start b isbigger a end" -## -##class PythonCode_SimpleAppend(_VimTest): -## snippets = ("test", """hi `!p snip.rv = "Hallo1" -##snip += "Hallo2"` End""") -## keys = "test" + EX -## wanted = "hi Hallo1\nHallo2 End" -## -##class PythonCode_MultiAppend(_VimTest): -## snippets = ("test", """hi `!p snip.rv = "Hallo1" -##snip += "Hallo2" -##snip += "Hallo3"` End""") -## keys = "test" + EX -## wanted = "hi Hallo1\nHallo2\nHallo3 End" -## -##class PythonCode_MultiAppend(_VimTest): -## snippets = ("test", """hi `!p snip.rv = "Hallo1" -##snip += "Hallo2" -##snip += "Hallo3"` End""") -## keys = "test" + EX -## wanted = "hi Hallo1\nHallo2\nHallo3 End" -## -##class PythonCode_MultiAppendSimpleIndent(_VimTest): -## snippets = ("test", """hi -##`!p snip.rv="Hallo1" -##snip += "Hallo2" -##snip += "Hallo3"` -##End""") -## keys = """ -## test""" + EX -## wanted = """ -## hi -## Hallo1 -## Hallo2 -## Hallo3 -## End""" -## -##class PythonCode_SimpleMkline(_VimTest): -## snippets = ("test", r"""hi -##`!p snip.rv="Hallo1\n" -##snip.rv += snip.mkline("Hallo2") + "\n" -##snip.rv += snip.mkline("Hallo3")` -##End""") -## keys = """ -## test""" + EX -## wanted = """ -## hi -## Hallo1 -## Hallo2 -## Hallo3 -## End""" -## -##class PythonCode_MultiAppendShift(_VimTest): -## snippets = ("test", r"""hi -##`!p snip.rv="i1" -##snip += "i1" -##snip >> 1 -##snip += "i2" -##snip << 2 -##snip += "i0" -##snip >> 3 -##snip += "i3"` -##End""") -## keys = """ -## test""" + EX -## wanted = """ -## hi -## i1 -## i1 -## i2 -##i0 -## i3 -## End""" -## -##class PythonCode_MultiAppendShiftMethods(_VimTest): -## snippets = ("test", r"""hi -##`!p snip.rv="i1\n" -##snip.rv += snip.mkline("i1\n") -##snip.shift(1) -##snip.rv += snip.mkline("i2\n") -##snip.unshift(2) -##snip.rv += snip.mkline("i0\n") -##snip.shift(3) -##snip.rv += snip.mkline("i3")` -##End""") -## keys = """ -## test""" + EX -## wanted = """ -## hi -## i1 -## i1 -## i2 -##i0 -## i3 -## End""" -## -## -##class PythonCode_ResetIndent(_VimTest): -## snippets = ("test", r"""hi -##`!p snip.rv="i1" -##snip >> 1 -##snip += "i2" -##snip.reset_indent() -##snip += "i1" -##snip << 1 -##snip += "i0" -##snip.reset_indent() -##snip += "i1"` -##End""") -## keys = """ -## test""" + EX -## wanted = """ -## hi -## i1 -## i2 -## i1 -##i0 -## i1 -## End""" -## -##class PythonCode_IndentEtSw(_VimTest): -## def _options_on(self): -## self.send(":set sw=3\n") -## self.send(":set expandtab\n") -## def _options_off(self): -## self.send(":set sw=8\n") -## self.send(":set noexpandtab\n") -## snippets = ("test", r"""hi -##`!p snip.rv = "i1" -##snip >> 1 -##snip += "i2" -##snip << 2 -##snip += "i0" -##snip >> 1 -##snip += "i1" -##` -##End""") -## keys = """ test""" + EX -## wanted = """ hi -## i1 -## i2 -##i0 -## i1 -## End""" -## -##class PythonCode_IndentEtSwOffset(_VimTest): -## def _options_on(self): -## self.send(":set sw=3\n") -## self.send(":set expandtab\n") -## def _options_off(self): -## self.send(":set sw=8\n") -## self.send(":set noexpandtab\n") -## snippets = ("test", r"""hi -##`!p snip.rv = "i1" -##snip >> 1 -##snip += "i2" -##snip << 2 -##snip += "i0" -##snip >> 1 -##snip += "i1" -##` -##End""") -## keys = """ test""" + EX -## wanted = """ hi -## i1 -## i2 -## i0 -## i1 -## End""" -## -##class PythonCode_IndentNoetSwTs(_VimTest): -## def _options_on(self): -## self.send(":set sw=3\n") -## self.send(":set ts=4\n") -## def _options_off(self): -## self.send(":set sw=8\n") -## self.send(":set ts=8\n") -## snippets = ("test", r"""hi -##`!p snip.rv = "i1" -##snip >> 1 -##snip += "i2" -##snip << 2 -##snip += "i0" -##snip >> 1 -##snip += "i1" -##` -##End""") -## keys = """ test""" + EX -## wanted = """ hi -## i1 -##\t i2 -##i0 -## i1 -## End""" -## -### Test using 'opt' -##class PythonCode_OptExists(_VimTest): -## def _options_on(self): -## self.send(':let g:UStest="yes"\n') -## def _options_off(self): -## self.send(":unlet g:UStest\n") -## snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""") -## keys = """test""" + EX -## wanted = """hi yes End""" -## -##class PythonCode_OptNoExists(_VimTest): -## snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""") -## keys = """test""" + EX -## wanted = """hi no End""" -## -##class PythonCode_IndentProblem(_VimTest): -## # A test case which is likely related to bug 719649 -## snippets = ("test", r"""hi `!p -##snip.rv = "World" -##` End""") -## keys = " " * 8 + "test" + EX # < 8 works. -## wanted = """ hi World End""" -## -### locals -##class PythonCode_Locals(_VimTest): -## snippets = ("test", r"""hi `!p a = "test" -##snip.rv = "nothing"` `!p snip.rv = a -##` End""") -## keys = """test""" + EX -## wanted = """hi nothing test End""" +# Deprecated Implementation {{{# +class PythonCodeOld_SimpleExample(_VimTest): + snippets = ("test", """hi `!p res = "Hallo"` End""") + keys = "test" + EX + wanted = "hi Hallo End" +class PythonCodeOld_ReferencePlaceholderAfter(_VimTest): + snippets = ("test", """${1:hi} `!p res = t[1]+".blah"` End""") + keys = "test" + EX + "ho" + wanted = "ho ho.blah End" +class PythonCodeOld_ReferencePlaceholderBefore(_VimTest): + snippets = ("test", """`!p res = len(t[1])*"#"`\n${1:some text}""") + keys = "test" + EX + "Hallo Welt" + wanted = "##########\nHallo Welt" +# TODO +# class PythonCodeOld_TransformedBeforeMultiLine(_VimTest): + # snippets = ("test", """${1/.+/egal/m} ${1:`!p +# res = "Hallo"`} End""") + # keys = "test" + EX + # wanted = "egal Hallo End" +class PythonCodeOld_IndentedMultiline(_VimTest): + snippets = ("test", """start `!p a = 1 +b = 2 +if b > a: + res = "b isbigger a" +else: + res = "a isbigger b"` end""") + keys = " test" + EX + wanted = " start b isbigger a end" +# End: Deprecated Implementation #}}} +# New Implementation {{{# +class PythonCode_UseNewOverOld(_VimTest): + snippets = ("test", """hi `!p res = "Old" +snip.rv = "New"` End""") + keys = "test" + EX + wanted = "hi New End" + +class PythonCode_SimpleExample(_VimTest): + snippets = ("test", """hi `!p snip.rv = "Hallo"` End""") + keys = "test" + EX + wanted = "hi Hallo End" + +class PythonCode_SimpleExample_ReturnValueIsEmptyString(_VimTest): + snippets = ("test", """hi`!p snip.rv = ""`End""") + keys = "test" + EX + wanted = "hiEnd" + +class PythonCode_ReferencePlaceholder(_VimTest): + snippets = ("test", """${1:hi} `!p snip.rv = t[1]+".blah"` End""") + keys = "test" + EX + "ho" + wanted = "ho ho.blah End" + +class PythonCode_ReferencePlaceholderBefore(_VimTest): + snippets = ("test", """`!p snip.rv = len(t[1])*"#"`\n${1:some text}""") + keys = "test" + EX + "Hallo Welt" + wanted = "##########\nHallo Welt" +# TODO +# class PythonCode_TransformedBeforeMultiLine(_VimTest): + # snippets = ("test", """${1/.+/egal/m} ${1:`!p +# snip.rv = "Hallo"`} End""") + # keys = "test" + EX + # wanted = "egal Hallo End" + +class PythonCode_MultilineIndented(_VimTest): + snippets = ("test", """start `!p a = 1 +b = 2 +if b > a: + snip.rv = "b isbigger a" +else: + snip.rv = "a isbigger b"` end""") + keys = " test" + EX + wanted = " start b isbigger a end" + +class PythonCode_SimpleAppend(_VimTest): + snippets = ("test", """hi `!p snip.rv = "Hallo1" +snip += "Hallo2"` End""") + keys = "test" + EX + wanted = "hi Hallo1\nHallo2 End" + +class PythonCode_MultiAppend(_VimTest): + snippets = ("test", """hi `!p snip.rv = "Hallo1" +snip += "Hallo2" +snip += "Hallo3"` End""") + keys = "test" + EX + wanted = "hi Hallo1\nHallo2\nHallo3 End" + +class PythonCode_MultiAppend(_VimTest): + snippets = ("test", """hi `!p snip.rv = "Hallo1" +snip += "Hallo2" +snip += "Hallo3"` End""") + keys = "test" + EX + wanted = "hi Hallo1\nHallo2\nHallo3 End" + +class PythonCode_MultiAppendSimpleIndent(_VimTest): + snippets = ("test", """hi +`!p snip.rv="Hallo1" +snip += "Hallo2" +snip += "Hallo3"` +End""") + keys = """ + test""" + EX + wanted = """ + hi + Hallo1 + Hallo2 + Hallo3 + End""" + +class PythonCode_SimpleMkline(_VimTest): + snippets = ("test", r"""hi +`!p snip.rv="Hallo1\n" +snip.rv += snip.mkline("Hallo2") + "\n" +snip.rv += snip.mkline("Hallo3")` +End""") + keys = """ + test""" + EX + wanted = """ + hi + Hallo1 + Hallo2 + Hallo3 + End""" + +class PythonCode_MultiAppendShift(_VimTest): + snippets = ("test", r"""hi +`!p snip.rv="i1" +snip += "i1" +snip >> 1 +snip += "i2" +snip << 2 +snip += "i0" +snip >> 3 +snip += "i3"` +End""") + keys = """ + test""" + EX + wanted = """ + hi + i1 + i1 + i2 +i0 + i3 + End""" + +class PythonCode_MultiAppendShiftMethods(_VimTest): + snippets = ("test", r"""hi +`!p snip.rv="i1\n" +snip.rv += snip.mkline("i1\n") +snip.shift(1) +snip.rv += snip.mkline("i2\n") +snip.unshift(2) +snip.rv += snip.mkline("i0\n") +snip.shift(3) +snip.rv += snip.mkline("i3")` +End""") + keys = """ + test""" + EX + wanted = """ + hi + i1 + i1 + i2 +i0 + i3 + End""" + + +class PythonCode_ResetIndent(_VimTest): + snippets = ("test", r"""hi +`!p snip.rv="i1" +snip >> 1 +snip += "i2" +snip.reset_indent() +snip += "i1" +snip << 1 +snip += "i0" +snip.reset_indent() +snip += "i1"` +End""") + keys = """ + test""" + EX + wanted = """ + hi + i1 + i2 + i1 +i0 + i1 + End""" + +class PythonCode_IndentEtSw(_VimTest): + def _options_on(self): + self.send(":set sw=3\n") + self.send(":set expandtab\n") + def _options_off(self): + self.send(":set sw=8\n") + self.send(":set noexpandtab\n") + snippets = ("test", r"""hi +`!p snip.rv = "i1" +snip >> 1 +snip += "i2" +snip << 2 +snip += "i0" +snip >> 1 +snip += "i1" +` +End""") + keys = """ test""" + EX + wanted = """ hi + i1 + i2 +i0 + i1 + End""" + +class PythonCode_IndentEtSwOffset(_VimTest): + def _options_on(self): + self.send(":set sw=3\n") + self.send(":set expandtab\n") + def _options_off(self): + self.send(":set sw=8\n") + self.send(":set noexpandtab\n") + snippets = ("test", r"""hi +`!p snip.rv = "i1" +snip >> 1 +snip += "i2" +snip << 2 +snip += "i0" +snip >> 1 +snip += "i1" +` +End""") + keys = """ test""" + EX + wanted = """ hi + i1 + i2 + i0 + i1 + End""" + +class PythonCode_IndentNoetSwTs(_VimTest): + def _options_on(self): + self.send(":set sw=3\n") + self.send(":set ts=4\n") + def _options_off(self): + self.send(":set sw=8\n") + self.send(":set ts=8\n") + snippets = ("test", r"""hi +`!p snip.rv = "i1" +snip >> 1 +snip += "i2" +snip << 2 +snip += "i0" +snip >> 1 +snip += "i1" +` +End""") + keys = """ test""" + EX + wanted = """ hi + i1 +\t i2 +i0 + i1 + End""" + +# Test using 'opt' +class PythonCode_OptExists(_VimTest): + def _options_on(self): + self.send(':let g:UStest="yes"\n') + def _options_off(self): + self.send(":unlet g:UStest\n") + snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""") + keys = """test""" + EX + wanted = """hi yes End""" + +class PythonCode_OptNoExists(_VimTest): + snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""") + keys = """test""" + EX + wanted = """hi no End""" + +class PythonCode_IndentProblem(_VimTest): + # A test case which is likely related to bug 719649 + snippets = ("test", r"""hi `!p +snip.rv = "World" +` End""") + keys = " " * 8 + "test" + EX # < 8 works. + wanted = """ hi World End""" + +# locals +class PythonCode_Locals(_VimTest): + snippets = ("test", r"""hi `!p a = "test" +snip.rv = "nothing"` `!p snip.rv = a +` End""") + keys = """test""" + EX + wanted = """hi nothing test End""" +# End: New Implementation #}}} + ### End: PythonCode Interpolation #}}} # Mirrors {{{# class TextTabStopTextAfterTab_ExceptCorrectResult(_VimTest):