Trying to get multiline mirrors & tab values to work

This commit is contained in:
Holger Rapp 2009-07-02 11:48:35 +02:00
parent f7e0624c8b
commit 86bbd40ea9
2 changed files with 58 additions and 24 deletions

View File

@ -10,6 +10,12 @@ def debug(s):
f.write(s+'\n') f.write(s+'\n')
f.close() 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): class Position(object):
def __init__(self, line, col): def __init__(self, line, col):
self.line = line self.line = line
@ -83,20 +89,39 @@ class Mirror(TextObject):
if ts != self._tabstop: if ts != self._tabstop:
return 0 return 0
mirror_line = self._parent.start.line + self.start.line text = self._tabstop.current_text.splitlines()
line = vim.current.buffer[mirror_line]
line = line[:self.start.col] + \ first_line_idx = self._parent.start.line + self.start.line
self._tabstop.current_text + \ first_line = vim.current.buffer[first_line_idx][:self.start.col]
line[self.end.col:]
oldspan = self.end.col-self.start.col last_line_idx = self._parent.start.line + self.end.line
self._end.col = self.start.col+len(self._tabstop.current_text) last_line = vim.current.buffer[last_line_idx][self.end.col:]
newspan = self.end.col-self.start.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): class TabStop(TextObject):
@ -205,28 +230,27 @@ class SnippetInstance(TextObject):
def _update_mirrors(self,for_ts): def _update_mirrors(self,for_ts):
for m in self._mirrors: for m in self._mirrors:
moved = m.update(for_ts) moved_lines, moved_cols = m.update(for_ts)
if moved: self._move_textobjects_behind(moved_lines, moved_cols, m)
self._move_textobjects_behind(moved, m)
def _move_textobjects_behind(self, amount, obj): def _move_textobjects_behind(self, lines, cols, obj):
if self._cts is None: if lines == 0 and cols == 0:
return return
for m in self._text_objects: for m in self._text_objects:
if m.start.line != obj.start.line: if m.start.line != obj.start.line:
continue continue
if m.start.col >= obj.start.col and m != obj: if m.start.col >= obj.start.col and m != obj:
m.start.col += amount m.start.col += cols
m.end.col += amount m.end.col += cols
def backspace(self,count): def backspace(self,count):
cts = self._tabstops[self._cts] cts = self._tabstops[self._cts]
ll = len(cts.current_text) ll = len(cts.current_text)
cts.current_text = cts.current_text[:-count] 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) self._update_mirrors(cts)
@ -234,11 +258,11 @@ class SnippetInstance(TextObject):
cts = self._tabstops[self._cts] cts = self._tabstops[self._cts]
if self._selected_tab is not None: 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 = "" cts.current_text = ""
self._selected_tab = None self._selected_tab = None
else: else:
self._move_textobjects_behind(len(chars), cts) self._move_textobjects_behind(0, len(chars), cts)
cts.current_text += chars cts.current_text += chars

18
test.py
View File

@ -185,18 +185,21 @@ class TabStopTestBackwardJumping_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo ${0:End} mitte${1:Beginning}") snippets = ("hallo", "hallo ${0:End} mitte${1:Beginning}")
wanted = "hallo Blah mitteLets replace it again" wanted = "hallo Blah mitteLets replace it again"
def cmd(self): 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() def runTest(self): self.check_output()
class TabStopTestBackwardJumping2_ExceptCorrectResult(_VimTest): class TabStopTestBackwardJumping2_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo $0 $1") snippets = ("hallo", "hallo $0 $1")
wanted = "hallo Blah Lets replace it again" wanted = "hallo Blah Lets replace it again"
def cmd(self): 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() def runTest(self): self.check_output()
class TabStopTestMultilineExpand_ExceptCorrectResult(_VimTest): class TabStopTestMultilineExpand_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo $0\nnice $1 work\n$3 $2\nSeem to work") 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): def cmd(self):
self.type("test hallo World") self.type("test hallo World")
self.escape() self.escape()
@ -266,6 +269,12 @@ class SimpleMirrorMultilineMany_ExceptCorrectResult(_VimTest):
def cmd(self): def cmd(self):
self.type("test\thallo") self.type("test\thallo")
def runTest(self): self.check_output() 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): class SimpleMirrorDelete_ExceptCorrectResult(_VimTest):
@ -367,7 +376,8 @@ if __name__ == '__main__':
options,selected_tests = parse_args() options,selected_tests = parse_args()
# The next line doesn't work in python 2.3 # 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 # Inform all test case which screen session to use
suite = unittest.TestSuite() suite = unittest.TestSuite()