From 86bbd40ea92e44936e70220307c449f636fbcf2e Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Thu, 2 Jul 2009 11:48:35 +0200 Subject: [PATCH] Trying to get multiline mirrors & tab values to work --- PySnipEmu.py | 64 ++++++++++++++++++++++++++++++++++++---------------- test.py | 18 +++++++++++---- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/PySnipEmu.py b/PySnipEmu.py index ac554cc..a02af6b 100644 --- a/PySnipEmu.py +++ b/PySnipEmu.py @@ -10,6 +10,12 @@ def debug(s): f.write(s+'\n') f.close() +def _line_count_and_len_of_last_line(txt): + lines = txt.splitlines() + if not len(lines): + return 1, 0 # empty line has zero columns but one line + return len(lines), len(lines[-1]) + class Position(object): def __init__(self, line, col): self.line = line @@ -83,20 +89,39 @@ class Mirror(TextObject): if ts != self._tabstop: return 0 - mirror_line = self._parent.start.line + self.start.line - line = vim.current.buffer[mirror_line] + text = self._tabstop.current_text.splitlines() - line = line[:self.start.col] + \ - self._tabstop.current_text + \ - line[self.end.col:] + first_line_idx = self._parent.start.line + self.start.line + first_line = vim.current.buffer[first_line_idx][:self.start.col] - oldspan = self.end.col-self.start.col - self._end.col = self.start.col+len(self._tabstop.current_text) - newspan = self.end.col-self.start.col + last_line_idx = self._parent.start.line + self.end.line + last_line = vim.current.buffer[last_line_idx][self.end.col:] + # last_line = text[-1] + last_line - vim.current.buffer[mirror_line] = line + debug("Trying to write:") - return newspan-oldspan + if not len(text): + arr = [ first_line + last_line ] + elif len(text) == 1: + arr = [ first_line + text[0] + last_line ] + else: + arr = [ first_line ] + text + [ last_line ] + + debug("%s" % (arr,)) + vim.current.buffer[first_line_idx:last_line_idx+1] = arr + + oldcolspan = self.end.col-self.start.col + lcol = len(text[-1]) if len(text) else 0 + self._end.col = self.start.col + lcol + newcolspan = self.end.col-self.start.col + + oldlinespan = self.end.line - self.start.line + ll = len(text)-1 if len(text) else 0 + self._end.line = self.start.line + ll + newlinespan = self.end.line - self.start.line + + + return newlinespan-oldlinespan, newcolspan-oldcolspan class TabStop(TextObject): @@ -205,28 +230,27 @@ class SnippetInstance(TextObject): def _update_mirrors(self,for_ts): for m in self._mirrors: - moved = m.update(for_ts) - if moved: - self._move_textobjects_behind(moved, m) + moved_lines, moved_cols = m.update(for_ts) + self._move_textobjects_behind(moved_lines, moved_cols, m) - def _move_textobjects_behind(self, amount, obj): - if self._cts is None: + def _move_textobjects_behind(self, lines, cols, obj): + if lines == 0 and cols == 0: return for m in self._text_objects: if m.start.line != obj.start.line: continue if m.start.col >= obj.start.col and m != obj: - m.start.col += amount - m.end.col += amount + m.start.col += cols + m.end.col += cols def backspace(self,count): cts = self._tabstops[self._cts] ll = len(cts.current_text) cts.current_text = cts.current_text[:-count] - self._move_textobjects_behind(len(cts.current_text)-ll, cts) + self._move_textobjects_behind(0, len(cts.current_text)-ll, cts) self._update_mirrors(cts) @@ -234,11 +258,11 @@ class SnippetInstance(TextObject): cts = self._tabstops[self._cts] if self._selected_tab is not None: - self._move_textobjects_behind(len(chars)-len(cts.current_text), cts) + self._move_textobjects_behind(0, len(chars)-len(cts.current_text), cts) cts.current_text = "" self._selected_tab = None else: - self._move_textobjects_behind(len(chars), cts) + self._move_textobjects_behind(0, len(chars), cts) cts.current_text += chars diff --git a/test.py b/test.py index ca9980e..554a57b 100755 --- a/test.py +++ b/test.py @@ -185,18 +185,21 @@ class TabStopTestBackwardJumping_ExceptCorrectResult(_VimTest): snippets = ("hallo", "hallo ${0:End} mitte${1:Beginning}") wanted = "hallo Blah mitteLets replace it again" def cmd(self): - self.type("hallo\tSomelengthy Text\tHi+Lets replace it again\tBlah\t++\t") + self.type( + "hallo\tSomelengthy Text\tHi+Lets replace it again\tBlah\t++\t") def runTest(self): self.check_output() class TabStopTestBackwardJumping2_ExceptCorrectResult(_VimTest): snippets = ("hallo", "hallo $0 $1") wanted = "hallo Blah Lets replace it again" def cmd(self): - self.type("hallo\tSomelengthy Text\tHi+Lets replace it again\tBlah\t++\t") + self.type( + "hallo\tSomelengthy Text\tHi+Lets replace it again\tBlah\t++\t") def runTest(self): self.check_output() class TabStopTestMultilineExpand_ExceptCorrectResult(_VimTest): snippets = ("hallo", "hallo $0\nnice $1 work\n$3 $2\nSeem to work") - wanted = "test hallo one more\nnice world work\ntest try\nSeem to work World" + wanted = "test hallo one more\nnice world work\n" \ + "test try\nSeem to work World" def cmd(self): self.type("test hallo World") self.escape() @@ -266,6 +269,12 @@ class SimpleMirrorMultilineMany_ExceptCorrectResult(_VimTest): def cmd(self): self.type("test\thallo") def runTest(self): self.check_output() +class MultilineTabStopSimpleMirrorMultiline_ExceptCorrectResult(_VimTest): + snippets = ("test", "$1\n\n\n$1\n\n\n$1") + wanted = "hallo Du\nHi\nhallo Du\nHi" + def cmd(self): + self.type("test\thallo Du\nHu") + def runTest(self): self.check_output() class SimpleMirrorDelete_ExceptCorrectResult(_VimTest): @@ -367,7 +376,8 @@ if __name__ == '__main__': options,selected_tests = parse_args() # The next line doesn't work in python 2.3 - all_test_suites = unittest.TestLoader().loadTestsFromModule(__import__("test")) + test_loader = unittest.TestLoader() + all_test_suites = test_loader.loadTestsFromModule(__import__("test")) # Inform all test case which screen session to use suite = unittest.TestSuite()