Python code

This commit is contained in:
Holger Rapp 2012-01-17 21:21:13 +01:00
parent 71b1bb687f
commit 15bb3ff34b
2 changed files with 304 additions and 349 deletions

View File

@ -276,21 +276,11 @@ class TextObject(object):
self._is_killed = False # TODO: not often needed self._is_killed = False # TODO: not often needed
def initial_replace(self): def initial_replace(self):
# TODO: could this be replaced via _really_updateman?
ct = self._initial_text # TODO: Initial Text is nearly unused. 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 old_end = self._end
ct.to_vim(self._start, self._end) # TODO: to vim returns something unused self._end = ct.to_vim(self._start, self._end)
debug("self._end: %r" % (self._end)) self.child_end_moved2(old_end, 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)
def __lt__(self, other): def __lt__(self, other):
return self._start < other._start return self._start < other._start
@ -340,10 +330,6 @@ class TextObject(object):
if not (self._parent) or old_end == new_end: if not (self._parent) or old_end == new_end:
return return
debug("###*** ")
assert(self._parent)
_do_print(self._parent)
pold_end = self._parent._end.copy() pold_end = self._parent._end.copy()
_move_nocheck(self._parent._end, old_end, new_end, new_end - old_end) _move_nocheck(self._parent._end, old_end, new_end, new_end - old_end)
def _move_all(o): def _move_all(o):
@ -356,38 +342,9 @@ class TextObject(object):
for c in self._parent._childs: for c in self._parent._childs:
if c is self: continue if c is self: continue
_move_all(c) _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) 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): def _do_edit(self, cmd):
debug("self: %r, cmd: %r" % (self, cmd)) debug("self: %r, cmd: %r" % (self, cmd))
ctype, line, col, char = cmd ctype, line, col, char = cmd
@ -463,7 +420,6 @@ class TextObject(object):
delta = Position(0, len(char)) delta = Position(0, len(char))
old_end = self._end.copy() old_end = self._end.copy()
_move(self._end, Position(line, col), delta) _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) self.child_end_moved2(old_end, self._end)
def edited(self, cmds): # TODO: Only in SnippetInstance def edited(self, cmds): # TODO: Only in SnippetInstance
@ -642,7 +598,7 @@ class NoneditableTextObject(TextObject):
old_end = self._end.copy() old_end = self._end.copy()
self._end = tb.to_vim(self._start, self._end) # TODO: to vim returns something unused 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) self.child_end_moved2(old_end, self._end)
class EscapedChar(NoneditableTextObject): class EscapedChar(NoneditableTextObject):
@ -934,7 +890,6 @@ class SnippetUtil(object):
class PythonCode(NoneditableTextObject): class PythonCode(NoneditableTextObject):
def __init__(self, parent, token): def __init__(self, parent, token):
code = token.code.replace("\\`", "`") code = token.code.replace("\\`", "`")
# Find our containing snippet for snippet local data # Find our containing snippet for snippet local data
@ -956,8 +911,7 @@ class PythonCode(NoneditableTextObject):
NoneditableTextObject.__init__(self, parent, token) NoneditableTextObject.__init__(self, parent, token)
def _really_updateman(self):
def _do_update(self):
path = vim.eval('expand("%")') path = vim.eval('expand("%")')
if path is None: if path is None:
path = "" path = ""
@ -980,9 +934,9 @@ class PythonCode(NoneditableTextObject):
compatible_exec(self._code, self._globals, local_d) compatible_exec(self._code, self._globals, local_d)
if self._snip._rv_changed: if self._snip._rv_changed:
self.current_text = self._snip.rv self._replace_text(TextBuffer(self._snip.rv))
else: else:
self.current_text = as_unicode(local_d["res"]) self._replace_text(TextBuffer(as_unicode(local_d['res'])))
def __repr__(self): def __repr__(self):
return "PythonCode(%s -> %s)" % (self._start, self._end) return "PythonCode(%s -> %s)" % (self._start, self._end)

593
test.py
View File

@ -857,302 +857,303 @@ class TabStop_VimScriptInterpolation_SimpleExample(_VimTest):
wanted = " hi 4 End" wanted = " hi 4 End"
# End: VimScript Interpolation #}}} # End: VimScript Interpolation #}}}
### PythonCode Interpolation {{{# ### PythonCode Interpolation {{{#
###### Deprecated way ########## # Deprecated Implementation {{{#
##class PythonCodeOld_SimpleExample(_VimTest): class PythonCodeOld_SimpleExample(_VimTest):
## snippets = ("test", """hi `!p res = "Hallo"` End""") snippets = ("test", """hi `!p res = "Hallo"` End""")
## keys = "test" + EX keys = "test" + EX
## wanted = "hi Hallo End" wanted = "hi Hallo End"
##class PythonCodeOld_ReferencePlaceholderAfter(_VimTest): class PythonCodeOld_ReferencePlaceholderAfter(_VimTest):
## snippets = ("test", """${1:hi} `!p res = t[1]+".blah"` End""") snippets = ("test", """${1:hi} `!p res = t[1]+".blah"` End""")
## keys = "test" + EX + "ho" keys = "test" + EX + "ho"
## wanted = "ho ho.blah End" wanted = "ho ho.blah End"
##class PythonCodeOld_ReferencePlaceholderBefore(_VimTest): class PythonCodeOld_ReferencePlaceholderBefore(_VimTest):
## snippets = ("test", """`!p res = len(t[1])*"#"`\n${1:some text}""") snippets = ("test", """`!p res = len(t[1])*"#"`\n${1:some text}""")
## keys = "test" + EX + "Hallo Welt" keys = "test" + EX + "Hallo Welt"
## wanted = "##########\nHallo Welt" wanted = "##########\nHallo Welt"
##class PythonCodeOld_TransformedBeforeMultiLine(_VimTest): # TODO
## snippets = ("test", """${1/.+/egal/m} ${1:`!p # class PythonCodeOld_TransformedBeforeMultiLine(_VimTest):
##res = "Hallo"`} End""") # snippets = ("test", """${1/.+/egal/m} ${1:`!p
## keys = "test" + EX # res = "Hallo"`} End""")
## wanted = "egal Hallo End" # keys = "test" + EX
##class PythonCodeOld_IndentedMultiline(_VimTest): # wanted = "egal Hallo End"
## snippets = ("test", """start `!p a = 1 class PythonCodeOld_IndentedMultiline(_VimTest):
##b = 2 snippets = ("test", """start `!p a = 1
##if b > a: b = 2
## res = "b isbigger a" if b > a:
##else: res = "b isbigger a"
## res = "a isbigger b"` end""") else:
## keys = " test" + EX res = "a isbigger b"` end""")
## wanted = " start b isbigger a end" keys = " test" + EX
## wanted = " start b isbigger a end"
###### New way ########## # End: Deprecated Implementation #}}}
## # New Implementation {{{#
##class PythonCode_UseNewOverOld(_VimTest): class PythonCode_UseNewOverOld(_VimTest):
## snippets = ("test", """hi `!p res = "Old" snippets = ("test", """hi `!p res = "Old"
##snip.rv = "New"` End""") snip.rv = "New"` End""")
## keys = "test" + EX keys = "test" + EX
## wanted = "hi New End" wanted = "hi New End"
##
##class PythonCode_SimpleExample(_VimTest): class PythonCode_SimpleExample(_VimTest):
## snippets = ("test", """hi `!p snip.rv = "Hallo"` End""") snippets = ("test", """hi `!p snip.rv = "Hallo"` End""")
## keys = "test" + EX keys = "test" + EX
## wanted = "hi Hallo End" wanted = "hi Hallo End"
##
## class PythonCode_SimpleExample_ReturnValueIsEmptyString(_VimTest):
##class PythonCode_SimpleExample_ReturnValueIsEmptyString(_VimTest): snippets = ("test", """hi`!p snip.rv = ""`End""")
## snippets = ("test", """hi`!p snip.rv = ""`End""") keys = "test" + EX
## keys = "test" + EX wanted = "hiEnd"
## wanted = "hiEnd"
## class PythonCode_ReferencePlaceholder(_VimTest):
##class PythonCode_ReferencePlaceholder(_VimTest): snippets = ("test", """${1:hi} `!p snip.rv = t[1]+".blah"` End""")
## snippets = ("test", """${1:hi} `!p snip.rv = t[1]+".blah"` End""") keys = "test" + EX + "ho"
## keys = "test" + EX + "ho" wanted = "ho ho.blah End"
## wanted = "ho ho.blah End"
## class PythonCode_ReferencePlaceholderBefore(_VimTest):
##class PythonCode_ReferencePlaceholderBefore(_VimTest): snippets = ("test", """`!p snip.rv = len(t[1])*"#"`\n${1:some text}""")
## snippets = ("test", """`!p snip.rv = len(t[1])*"#"`\n${1:some text}""") keys = "test" + EX + "Hallo Welt"
## keys = "test" + EX + "Hallo Welt" wanted = "##########\nHallo Welt"
## wanted = "##########\nHallo Welt" # TODO
## # class PythonCode_TransformedBeforeMultiLine(_VimTest):
##class PythonCode_TransformedBeforeMultiLine(_VimTest): # snippets = ("test", """${1/.+/egal/m} ${1:`!p
## snippets = ("test", """${1/.+/egal/m} ${1:`!p # snip.rv = "Hallo"`} End""")
##snip.rv = "Hallo"`} End""") # keys = "test" + EX
## keys = "test" + EX # wanted = "egal Hallo End"
## wanted = "egal Hallo End"
## class PythonCode_MultilineIndented(_VimTest):
##class PythonCode_MultilineIndented(_VimTest): snippets = ("test", """start `!p a = 1
## snippets = ("test", """start `!p a = 1 b = 2
##b = 2 if b > a:
##if b > a: snip.rv = "b isbigger a"
## snip.rv = "b isbigger a" else:
##else: snip.rv = "a isbigger b"` end""")
## snip.rv = "a isbigger b"` end""") keys = " test" + EX
## keys = " test" + EX wanted = " start b isbigger a end"
## wanted = " start b isbigger a end"
## class PythonCode_SimpleAppend(_VimTest):
##class PythonCode_SimpleAppend(_VimTest): snippets = ("test", """hi `!p snip.rv = "Hallo1"
## snippets = ("test", """hi `!p snip.rv = "Hallo1" snip += "Hallo2"` End""")
##snip += "Hallo2"` End""") keys = "test" + EX
## keys = "test" + EX wanted = "hi Hallo1\nHallo2 End"
## wanted = "hi Hallo1\nHallo2 End"
## class PythonCode_MultiAppend(_VimTest):
##class PythonCode_MultiAppend(_VimTest): snippets = ("test", """hi `!p snip.rv = "Hallo1"
## snippets = ("test", """hi `!p snip.rv = "Hallo1" snip += "Hallo2"
##snip += "Hallo2" snip += "Hallo3"` End""")
##snip += "Hallo3"` End""") keys = "test" + EX
## keys = "test" + EX wanted = "hi Hallo1\nHallo2\nHallo3 End"
## wanted = "hi Hallo1\nHallo2\nHallo3 End"
## class PythonCode_MultiAppend(_VimTest):
##class PythonCode_MultiAppend(_VimTest): snippets = ("test", """hi `!p snip.rv = "Hallo1"
## snippets = ("test", """hi `!p snip.rv = "Hallo1" snip += "Hallo2"
##snip += "Hallo2" snip += "Hallo3"` End""")
##snip += "Hallo3"` End""") keys = "test" + EX
## keys = "test" + EX wanted = "hi Hallo1\nHallo2\nHallo3 End"
## wanted = "hi Hallo1\nHallo2\nHallo3 End"
## class PythonCode_MultiAppendSimpleIndent(_VimTest):
##class PythonCode_MultiAppendSimpleIndent(_VimTest): snippets = ("test", """hi
## snippets = ("test", """hi `!p snip.rv="Hallo1"
##`!p snip.rv="Hallo1" snip += "Hallo2"
##snip += "Hallo2" snip += "Hallo3"`
##snip += "Hallo3"` End""")
##End""") keys = """
## keys = """ test""" + EX
## test""" + EX wanted = """
## wanted = """ hi
## hi Hallo1
## Hallo1 Hallo2
## Hallo2 Hallo3
## Hallo3 End"""
## End"""
## class PythonCode_SimpleMkline(_VimTest):
##class PythonCode_SimpleMkline(_VimTest): snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv="Hallo1\n"
##`!p snip.rv="Hallo1\n" snip.rv += snip.mkline("Hallo2") + "\n"
##snip.rv += snip.mkline("Hallo2") + "\n" snip.rv += snip.mkline("Hallo3")`
##snip.rv += snip.mkline("Hallo3")` End""")
##End""") keys = """
## keys = """ test""" + EX
## test""" + EX wanted = """
## wanted = """ hi
## hi Hallo1
## Hallo1 Hallo2
## Hallo2 Hallo3
## Hallo3 End"""
## End"""
## class PythonCode_MultiAppendShift(_VimTest):
##class PythonCode_MultiAppendShift(_VimTest): snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv="i1"
##`!p snip.rv="i1" snip += "i1"
##snip += "i1" snip >> 1
##snip >> 1 snip += "i2"
##snip += "i2" snip << 2
##snip << 2 snip += "i0"
##snip += "i0" snip >> 3
##snip >> 3 snip += "i3"`
##snip += "i3"` End""")
##End""") keys = """
## keys = """ test""" + EX
## test""" + EX wanted = """
## wanted = """ hi
## hi i1
## i1 i1
## i1 i2
## i2 i0
##i0 i3
## i3 End"""
## End"""
## class PythonCode_MultiAppendShiftMethods(_VimTest):
##class PythonCode_MultiAppendShiftMethods(_VimTest): snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv="i1\n"
##`!p snip.rv="i1\n" snip.rv += snip.mkline("i1\n")
##snip.rv += snip.mkline("i1\n") snip.shift(1)
##snip.shift(1) snip.rv += snip.mkline("i2\n")
##snip.rv += snip.mkline("i2\n") snip.unshift(2)
##snip.unshift(2) snip.rv += snip.mkline("i0\n")
##snip.rv += snip.mkline("i0\n") snip.shift(3)
##snip.shift(3) snip.rv += snip.mkline("i3")`
##snip.rv += snip.mkline("i3")` End""")
##End""") keys = """
## keys = """ test""" + EX
## test""" + EX wanted = """
## wanted = """ hi
## hi i1
## i1 i1
## i1 i2
## i2 i0
##i0 i3
## i3 End"""
## End"""
##
## class PythonCode_ResetIndent(_VimTest):
##class PythonCode_ResetIndent(_VimTest): snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv="i1"
##`!p snip.rv="i1" snip >> 1
##snip >> 1 snip += "i2"
##snip += "i2" snip.reset_indent()
##snip.reset_indent() snip += "i1"
##snip += "i1" snip << 1
##snip << 1 snip += "i0"
##snip += "i0" snip.reset_indent()
##snip.reset_indent() snip += "i1"`
##snip += "i1"` End""")
##End""") keys = """
## keys = """ test""" + EX
## test""" + EX wanted = """
## wanted = """ hi
## hi i1
## i1 i2
## i2 i1
## i1 i0
##i0 i1
## i1 End"""
## End"""
## class PythonCode_IndentEtSw(_VimTest):
##class PythonCode_IndentEtSw(_VimTest): def _options_on(self):
## def _options_on(self): self.send(":set sw=3\n")
## self.send(":set sw=3\n") self.send(":set expandtab\n")
## self.send(":set expandtab\n") def _options_off(self):
## def _options_off(self): self.send(":set sw=8\n")
## self.send(":set sw=8\n") self.send(":set noexpandtab\n")
## self.send(":set noexpandtab\n") snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv = "i1"
##`!p snip.rv = "i1" snip >> 1
##snip >> 1 snip += "i2"
##snip += "i2" snip << 2
##snip << 2 snip += "i0"
##snip += "i0" snip >> 1
##snip >> 1 snip += "i1"
##snip += "i1" `
##` End""")
##End""") keys = """ test""" + EX
## keys = """ test""" + EX wanted = """ hi
## wanted = """ hi i1
## i1 i2
## i2 i0
##i0 i1
## i1 End"""
## End"""
## class PythonCode_IndentEtSwOffset(_VimTest):
##class PythonCode_IndentEtSwOffset(_VimTest): def _options_on(self):
## def _options_on(self): self.send(":set sw=3\n")
## self.send(":set sw=3\n") self.send(":set expandtab\n")
## self.send(":set expandtab\n") def _options_off(self):
## def _options_off(self): self.send(":set sw=8\n")
## self.send(":set sw=8\n") self.send(":set noexpandtab\n")
## self.send(":set noexpandtab\n") snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv = "i1"
##`!p snip.rv = "i1" snip >> 1
##snip >> 1 snip += "i2"
##snip += "i2" snip << 2
##snip << 2 snip += "i0"
##snip += "i0" snip >> 1
##snip >> 1 snip += "i1"
##snip += "i1" `
##` End""")
##End""") keys = """ test""" + EX
## keys = """ test""" + EX wanted = """ hi
## wanted = """ hi i1
## i1 i2
## i2 i0
## i0 i1
## i1 End"""
## End"""
## class PythonCode_IndentNoetSwTs(_VimTest):
##class PythonCode_IndentNoetSwTs(_VimTest): def _options_on(self):
## def _options_on(self): self.send(":set sw=3\n")
## self.send(":set sw=3\n") self.send(":set ts=4\n")
## self.send(":set ts=4\n") def _options_off(self):
## def _options_off(self): self.send(":set sw=8\n")
## self.send(":set sw=8\n") self.send(":set ts=8\n")
## self.send(":set ts=8\n") snippets = ("test", r"""hi
## snippets = ("test", r"""hi `!p snip.rv = "i1"
##`!p snip.rv = "i1" snip >> 1
##snip >> 1 snip += "i2"
##snip += "i2" snip << 2
##snip << 2 snip += "i0"
##snip += "i0" snip >> 1
##snip >> 1 snip += "i1"
##snip += "i1" `
##` End""")
##End""") keys = """ test""" + EX
## keys = """ test""" + EX wanted = """ hi
## wanted = """ hi i1
## i1 \t i2
##\t i2 i0
##i0 i1
## i1 End"""
## End"""
## # Test using 'opt'
### Test using 'opt' class PythonCode_OptExists(_VimTest):
##class PythonCode_OptExists(_VimTest): def _options_on(self):
## def _options_on(self): self.send(':let g:UStest="yes"\n')
## self.send(':let g:UStest="yes"\n') def _options_off(self):
## def _options_off(self): self.send(":unlet g:UStest\n")
## self.send(":unlet g:UStest\n") snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""")
## snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""") keys = """test""" + EX
## keys = """test""" + EX wanted = """hi yes End"""
## wanted = """hi yes End"""
## class PythonCode_OptNoExists(_VimTest):
##class PythonCode_OptNoExists(_VimTest): snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""")
## snippets = ("test", r"""hi `!p snip.rv = snip.opt("g:UStest") or "no"` End""") keys = """test""" + EX
## keys = """test""" + EX wanted = """hi no End"""
## wanted = """hi no End"""
## class PythonCode_IndentProblem(_VimTest):
##class PythonCode_IndentProblem(_VimTest): # A test case which is likely related to bug 719649
## # A test case which is likely related to bug 719649 snippets = ("test", r"""hi `!p
## snippets = ("test", r"""hi `!p snip.rv = "World"
##snip.rv = "World" ` End""")
##` End""") keys = " " * 8 + "test" + EX # < 8 works.
## keys = " " * 8 + "test" + EX # < 8 works. wanted = """ hi World End"""
## wanted = """ hi World End"""
## # locals
### locals class PythonCode_Locals(_VimTest):
##class PythonCode_Locals(_VimTest): snippets = ("test", r"""hi `!p a = "test"
## snippets = ("test", r"""hi `!p a = "test" snip.rv = "nothing"` `!p snip.rv = a
##snip.rv = "nothing"` `!p snip.rv = a ` End""")
##` End""") keys = """test""" + EX
## keys = """test""" + EX wanted = """hi nothing test End"""
## wanted = """hi nothing test End""" # End: New Implementation #}}}
### End: PythonCode Interpolation #}}} ### End: PythonCode Interpolation #}}}
# Mirrors {{{# # Mirrors {{{#
class TextTabStopTextAfterTab_ExceptCorrectResult(_VimTest): class TextTabStopTextAfterTab_ExceptCorrectResult(_VimTest):