Refactored some more, got rid of delta_row and delta_col. Introduced Position as zero based coordinate frame

This commit is contained in:
Holger Rapp 2009-07-02 08:25:58 +02:00
parent bf017ac7ea
commit ad7241bc82
2 changed files with 53 additions and 39 deletions

View File

@ -10,6 +10,32 @@ def debug(s):
f.write(s+'\n') f.write(s+'\n')
f.close() f.close()
class Position(object):
def __init__(self, line, col):
self.line = line
self.col = col
def col():
def fget(self):
return self._col
def fset(self, value):
if value < 0:
raise RuntimeError, "Invalid Column: %i" % col
self._col = value
return locals()
col = property(**col())
def line():
doc = "Zero base line numbers"
def fget(self):
return self._line
def fset(self, value):
if value < 0:
raise RuntimeError, "Invalid Line: %i" % line
self._line = value
return locals()
line = property(**line())
class TextObject(object): class TextObject(object):
""" """
This base class represents any object in the text This base class represents any object in the text
@ -19,9 +45,6 @@ class TextObject(object):
self._start = start self._start = start
self._end = end self._end = end
self._delta_rows = 0
self._delta_cols = 0
@property @property
def start(self): def start(self):
return self._start return self._start
@ -30,29 +53,15 @@ class TextObject(object):
def end(self): def end(self):
return self._end return self._end
def delta_rows():
def fget(self):
return self._delta_rows
def fset(self, value):
self._delta_rows = value
return locals()
delta_rows = property(**delta_rows())
def delta_cols():
def fget(self):
return self._delta_cols
def fset(self, value):
self._delta_cols = value
return locals()
delta_cols = property(**delta_cols())
class Mirror(TextObject): class Mirror(TextObject):
""" """
A Mirror object mirrors a TabStop that is, text is repeated here A Mirror object mirrors a TabStop that is, text is repeated here
""" """
def __init__(self, ts, idx, start): def __init__(self, ts, idx, start_col):
TextObject.__init__(self, (idx,start), (idx,start)) start = Position(idx,start_col)
end = Position(idx,start_col)
TextObject.__init__(self, start, end)
self._tabstop = ts self._tabstop = ts
@ -64,16 +73,16 @@ class Mirror(TextObject):
if ts != self._tabstop: if ts != self._tabstop:
return 0 return 0
mirror_line = self.start[0] mirror_line = self.start.line
line = vim.current.buffer[mirror_line] line = vim.current.buffer[mirror_line]
line = line[:self.start[1]+self.delta_cols] + \ line = line[:self.start.col] + \
self._tabstop.current_text + \ self._tabstop.current_text + \
line[self.end[1]+self.delta_cols:] line[self.end.col:]
oldspan = self.end[1]-self.start[1] oldspan = self.end.col-self.start.col
self._end = (self.start[0],self.start[1]+len(self._tabstop.current_text)) self._end.col = self.start.col+len(self._tabstop.current_text)
newspan = self.end[1]-self.start[1] newspan = self.end.col-self.start.col
vim.current.buffer[mirror_line] = line vim.current.buffer[mirror_line] = line
@ -86,7 +95,9 @@ class TabStop(TextObject):
comes to rest when the user taps through the Snippet. comes to rest when the user taps through the Snippet.
""" """
def __init__(self, idx, span, default_text = ""): def __init__(self, idx, span, default_text = ""):
TextObject.__init__(self, (idx,span[0]), (idx,span[1])) start = Position(idx,span[0])
end = Position(idx,span[1])
TextObject.__init__(self, start, end)
self._ct = default_text self._ct = default_text
@ -99,15 +110,15 @@ class TabStop(TextObject):
current_text = property(**current_text()) current_text = property(**current_text())
def select(self,start): def select(self,start):
lineno, col = start lineno, col = start.line, start.col
newline = lineno + self._start[0] newline = lineno + self._start.line
newcol = self._start[1] + self._delta_cols newcol = self._start.col
if newline == lineno: if newline == lineno:
newcol += col newcol += col
vim.current.window.cursor = newline, newcol vim.current.window.cursor = newline + 1, newcol
# Select the word # Select the word
# Depending on the current mode and position, we # Depending on the current mode and position, we
@ -131,7 +142,7 @@ class SnippetInstance(TextObject):
also a TextObject because it has a start an end also a TextObject because it has a start an end
""" """
def __init__(self,start,end, ts, mirrors): def __init__(self, start, end, ts, mirrors):
TextObject.__init__(self, start, end) TextObject.__init__(self, start, end)
self._tabstops = ts self._tabstops = ts
@ -182,7 +193,7 @@ class SnippetInstance(TextObject):
for m in self._mirrors: for m in self._mirrors:
moved = m.update(for_ts) moved = m.update(for_ts)
if moved: if moved:
self._move_to_on_line(moved, m.start[0], m.start[1]+m.delta_cols,cobj=m) self._move_to_on_line(moved, m.start.line, m.start.col,cobj=m)
def _move_to_on_line(self,amount, lineno = None, col = None, cobj = None): def _move_to_on_line(self,amount, lineno = None, col = None, cobj = None):
@ -195,10 +206,11 @@ class SnippetInstance(TextObject):
cobj = self._tabstops[self._cts] cobj = self._tabstops[self._cts]
for m in self._text_objects: for m in self._text_objects:
if m.start[0] != lineno: if m.start.line != lineno:
continue continue
if m.start[1]+m.delta_cols >= col and m != cobj: if m.start.col >= col and m != cobj:
m.delta_cols += amount m.start.col += amount
m.end.col += amount
def backspace(self,count): def backspace(self,count):
@ -317,7 +329,9 @@ class Snippet(object):
vim.current.window.cursor = newline, newcol vim.current.window.cursor = newline, newcol
if len(ts) or len(mirrors): if len(ts) or len(mirrors):
s = SnippetInstance( (lineno,col), (newline,newcol), ts, mirrors) s = SnippetInstance(
Position(lineno-1,col), Position(newline-1,newcol),
ts, mirrors)
s.select_next_tab() s.select_next_tab()

View File

@ -53,7 +53,7 @@ EOF
self.send(":w! %s\n" % fn) self.send(":w! %s\n" % fn)
# Give screen a chance to send the cmd and vim to write the file # Give screen a chance to send the cmd and vim to write the file
time.sleep(.01) time.sleep(.05)
# Read the output, chop the trailing newline # Read the output, chop the trailing newline
self.output = open(fn,"r").read()[:-1] self.output = open(fn,"r").read()[:-1]