2009-07-07 16:28:09 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
from PySnipEmu.Buffer import TextBuffer
|
2009-07-09 08:24:43 -04:00
|
|
|
from PySnipEmu.Geometry import Span, Position
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
__all__ = [ "Mirror", "Transformation", "SnippetInstance", "StartMarker" ]
|
|
|
|
|
2009-07-10 03:02:55 -04:00
|
|
|
# TODO: remove me
|
2009-07-09 07:49:15 -04:00
|
|
|
from PySnipEmu.debug import debug
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
###########################################################################
|
|
|
|
# Helper class #
|
|
|
|
###########################################################################
|
|
|
|
class _CleverReplace(object):
|
|
|
|
"""
|
|
|
|
This class mimics TextMates replace syntax
|
|
|
|
"""
|
|
|
|
_DOLLAR = re.compile(r"\$(\d+)", re.DOTALL)
|
|
|
|
_SIMPLE_CASEFOLDINGS = re.compile(r"\\([ul].)", re.DOTALL)
|
|
|
|
_LONG_CASEFOLDINGS = re.compile(r"\\([UL].*?)\\E", re.DOTALL)
|
|
|
|
_CONDITIONAL = re.compile(r"\(\?(\d+):(.*?)(?<!\\)\)", re.DOTALL)
|
|
|
|
|
|
|
|
_UNESCAPE = re.compile(r'\\[^ntrab]')
|
|
|
|
|
|
|
|
def __init__(self, s):
|
|
|
|
self._s = s
|
|
|
|
|
|
|
|
def _scase_folding(self, m):
|
|
|
|
if m.group(1)[0] == 'u':
|
|
|
|
return m.group(1)[-1].upper()
|
|
|
|
else:
|
|
|
|
return m.group(1)[-1].lower()
|
|
|
|
def _lcase_folding(self, m):
|
|
|
|
if m.group(1)[0] == 'U':
|
|
|
|
return m.group(1)[1:].upper()
|
|
|
|
else:
|
|
|
|
return m.group(1)[1:].lower()
|
|
|
|
|
|
|
|
def _unescape(self, v):
|
|
|
|
return self._UNESCAPE.subn(lambda m: m.group(0)[-1], v)[0]
|
|
|
|
def replace(self, match):
|
|
|
|
start, end = match.span()
|
|
|
|
|
|
|
|
tv = self._s
|
|
|
|
|
|
|
|
# Replace all $? with capture groups
|
|
|
|
tv = self._DOLLAR.subn(lambda m: match.group(int(m.group(1))), tv)[0]
|
|
|
|
|
|
|
|
def _conditional(m):
|
|
|
|
args = m.group(2).split(':')
|
|
|
|
# TODO: the returned string should be checked for conditionals
|
|
|
|
if match.group(int(m.group(1))):
|
|
|
|
return self._unescape(args[0])
|
|
|
|
elif len(args) > 1:
|
|
|
|
return self._unescape(args[1])
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
# Replace CaseFoldings
|
|
|
|
tv = self._SIMPLE_CASEFOLDINGS.subn(self._scase_folding, tv)[0]
|
|
|
|
tv = self._LONG_CASEFOLDINGS.subn(self._lcase_folding, tv)[0]
|
|
|
|
tv = self._CONDITIONAL.subn(_conditional, tv)[0]
|
|
|
|
|
|
|
|
rv = tv.decode("string-escape")
|
|
|
|
|
|
|
|
return rv
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
class _TOParser(object):
|
2009-07-07 16:28:09 -04:00
|
|
|
# A simple tabstop with default value
|
|
|
|
_TABSTOP = re.compile(r'''\${(\d+)[:}]''')
|
|
|
|
# A mirror or a tabstop without default value.
|
|
|
|
_MIRROR_OR_TS = re.compile(r'\$(\d+)')
|
|
|
|
# A mirror or a tabstop without default value.
|
|
|
|
_TRANSFORMATION = re.compile(r'\${(\d+)/(.*?)/(.*?)/([a-zA-z]*)}')
|
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
def __init__(self, parent, val):
|
|
|
|
self._v = val
|
|
|
|
self._p = parent
|
|
|
|
|
|
|
|
self._childs = []
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "TOParser(%s)" % self._p
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
def parse(self):
|
|
|
|
self._parse_tabs()
|
|
|
|
self._parse_transformations()
|
|
|
|
self._parse_mirrors_or_ts()
|
|
|
|
self._finish()
|
|
|
|
|
|
|
|
########
|
|
|
|
# TABS #
|
|
|
|
########
|
|
|
|
def _parse_tabs(self):
|
2009-07-10 02:46:49 -04:00
|
|
|
ts = []
|
|
|
|
m = self._TABSTOP.search(self._v)
|
|
|
|
while m:
|
|
|
|
ts.append(self._handle_tabstop(m))
|
|
|
|
m = self._TABSTOP.search(self._v)
|
|
|
|
|
|
|
|
|
|
|
|
for t, def_text in ts:
|
2009-07-10 02:58:18 -04:00
|
|
|
child_parser = _TOParser(t, def_text)
|
|
|
|
child_parser._parse_tabs()
|
2009-07-10 02:46:49 -04:00
|
|
|
self._childs.append(child_parser)
|
|
|
|
|
|
|
|
def _handle_tabstop(self, m):
|
|
|
|
def _find_closingbracket(v,start_pos):
|
|
|
|
bracks_open = 1
|
|
|
|
for idx, c in enumerate(v[start_pos:]):
|
|
|
|
if c == '{':
|
|
|
|
if v[idx+start_pos-1] != '\\':
|
|
|
|
bracks_open += 1
|
|
|
|
elif c == '}':
|
|
|
|
if v[idx+start_pos-1] != '\\':
|
|
|
|
bracks_open -= 1
|
|
|
|
if not bracks_open:
|
|
|
|
return start_pos+idx+1
|
|
|
|
|
|
|
|
start_pos = m.start()
|
|
|
|
end_pos = _find_closingbracket(self._v, start_pos+2)
|
|
|
|
|
|
|
|
def_text = self._v[m.end():end_pos-1]
|
|
|
|
|
|
|
|
start, end = self._get_start_end(self._v,start_pos,end_pos)
|
|
|
|
|
|
|
|
ts = TabStop(self._p, start, end, def_text)
|
|
|
|
|
|
|
|
self._p._add_tabstop(int(m.group(1)),ts)
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
self._overwrite_area(start_pos, end_pos)
|
2009-07-10 02:46:49 -04:00
|
|
|
|
|
|
|
return ts, def_text
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
###################
|
|
|
|
# TRANSFORMATIONS #
|
|
|
|
###################
|
|
|
|
def _parse_transformations(self):
|
|
|
|
self._trans = []
|
|
|
|
for m in self._TRANSFORMATION.finditer(self._v):
|
|
|
|
self._trans.append(self._handle_transformation(m))
|
|
|
|
|
|
|
|
for t in self._childs:
|
|
|
|
t._parse_transformations()
|
|
|
|
|
|
|
|
def _handle_transformation(self, m):
|
|
|
|
no = int(m.group(1))
|
|
|
|
search = m.group(2)
|
|
|
|
replace = m.group(3)
|
|
|
|
options = m.group(4)
|
|
|
|
|
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(self._v,start_pos,end_pos)
|
|
|
|
|
|
|
|
self._overwrite_area(*m.span())
|
|
|
|
|
|
|
|
return Transformation(self._p, no, start, end, search, replace, options)
|
|
|
|
|
|
|
|
#####################
|
|
|
|
# MIRRORS OR TS: $1 #
|
|
|
|
#####################
|
|
|
|
def _parse_mirrors_or_ts(self):
|
|
|
|
for m in self._MIRROR_OR_TS.finditer(self._v):
|
|
|
|
self._handle_ts_or_mirror(m)
|
|
|
|
|
|
|
|
for t in self._childs:
|
|
|
|
t._parse_mirrors_or_ts()
|
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
def _handle_ts_or_mirror(self, m):
|
|
|
|
no = int(m.group(1))
|
|
|
|
|
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(self._v,start_pos,end_pos)
|
|
|
|
|
|
|
|
ts = self._p._get_tabstop(self._p, no)
|
|
|
|
if ts is not None:
|
|
|
|
rv = Mirror(self._p, ts, start, end)
|
|
|
|
else:
|
|
|
|
rv = TabStop(self._p, start, end)
|
|
|
|
self._p._add_tabstop(no,rv)
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
self._overwrite_area(*m.span())
|
2009-07-10 02:46:49 -04:00
|
|
|
|
|
|
|
return rv
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
###################
|
|
|
|
# Resolve symbols #
|
|
|
|
###################
|
|
|
|
def _finish(self):
|
|
|
|
for c in self._childs:
|
|
|
|
c._finish()
|
2009-07-10 02:46:49 -04:00
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
for t in self._trans:
|
|
|
|
ts = self._p._get_tabstop(self._p,t._ts)
|
|
|
|
if ts is None:
|
|
|
|
raise RuntimeError, "Tabstop %i is not known" % t._ts
|
|
|
|
t._ts = ts
|
2009-07-10 02:46:49 -04:00
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
####################
|
|
|
|
# Helper functions #
|
|
|
|
####################
|
2009-07-10 02:46:49 -04:00
|
|
|
def _get_start_end(self, val, start_pos, end_pos):
|
|
|
|
def _get_pos(s, pos):
|
|
|
|
line_idx = s[:pos].count('\n')
|
|
|
|
line_start = s[:pos].rfind('\n') + 1
|
|
|
|
start_in_line = pos - line_start
|
|
|
|
return Position(line_idx, start_in_line)
|
|
|
|
|
|
|
|
return _get_pos(val, start_pos), _get_pos(val, end_pos)
|
2009-07-10 03:02:55 -04:00
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
def _overwrite_area(self, s, e):
|
|
|
|
"""Overwrite the given span with spaces"""
|
|
|
|
self._v = self._v[:s] + (e-s)*" " + self._v[e:]
|
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
|
|
|
|
|
2009-07-10 02:58:18 -04:00
|
|
|
###########################################################################
|
|
|
|
# Public classes #
|
|
|
|
###########################################################################
|
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
class TextObject(object):
|
|
|
|
"""
|
|
|
|
This base class represents any object in the text
|
|
|
|
that has a span in any ways
|
|
|
|
"""
|
2009-07-07 16:28:09 -04:00
|
|
|
def __init__(self, parent, start, end, initial_text):
|
|
|
|
self._start = start
|
|
|
|
self._end = end
|
|
|
|
|
|
|
|
self._parent = parent
|
|
|
|
|
|
|
|
self._children = []
|
|
|
|
self._tabstops = {}
|
|
|
|
|
|
|
|
if parent is not None:
|
2009-07-09 08:33:06 -04:00
|
|
|
parent._add_child(self)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
self._current_text = TextBuffer(initial_text)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 11:49:59 -04:00
|
|
|
self._cts = 0
|
2009-07-09 11:04:57 -04:00
|
|
|
|
2009-07-09 08:24:43 -04:00
|
|
|
def __cmp__(self, other):
|
|
|
|
return cmp(self._start, other._start)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 08:24:43 -04:00
|
|
|
|
|
|
|
##############
|
|
|
|
# PROPERTIES #
|
|
|
|
##############
|
2009-07-09 08:33:06 -04:00
|
|
|
def current_text():
|
|
|
|
def fget(self):
|
|
|
|
return str(self._current_text)
|
|
|
|
def fset(self, text):
|
|
|
|
self._current_text = TextBuffer(text)
|
|
|
|
|
2009-07-09 11:49:59 -04:00
|
|
|
# All our children are set to "" so they
|
2009-07-09 11:04:57 -04:00
|
|
|
# do no longer disturb anything that mirrors it
|
|
|
|
for c in self._children:
|
|
|
|
c.current_text = ""
|
2009-07-09 08:33:06 -04:00
|
|
|
self._children = []
|
2009-07-09 11:04:57 -04:00
|
|
|
self._tabstops = {}
|
2009-07-09 08:33:06 -04:00
|
|
|
return locals()
|
|
|
|
|
|
|
|
current_text = property(**current_text())
|
2009-07-09 01:50:48 -04:00
|
|
|
def abs_start(self):
|
|
|
|
if self._parent:
|
|
|
|
ps = self._parent.abs_start
|
|
|
|
if self._start.line == 0:
|
|
|
|
return ps + self._start
|
|
|
|
else:
|
|
|
|
return Position(ps.line + self._start.line, self._start.col)
|
|
|
|
return self._start
|
|
|
|
abs_start = property(abs_start)
|
|
|
|
|
|
|
|
def abs_end(self):
|
|
|
|
if self._parent:
|
|
|
|
ps = self._parent.abs_start
|
|
|
|
if self._end.line == 0:
|
|
|
|
return ps + self._end
|
|
|
|
else:
|
2009-07-09 04:20:25 -04:00
|
|
|
return Position(ps.line + self._end.line, self._end.col)
|
2009-07-09 01:50:48 -04:00
|
|
|
|
|
|
|
return self._end
|
|
|
|
abs_end = property(abs_end)
|
|
|
|
|
2009-07-09 08:24:43 -04:00
|
|
|
def span(self):
|
|
|
|
return Span(self._start, self._end)
|
|
|
|
span = property(span)
|
2009-07-09 01:50:48 -04:00
|
|
|
|
2009-07-09 08:24:43 -04:00
|
|
|
def start(self):
|
|
|
|
return self._start
|
|
|
|
start = property(start)
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
return self._end
|
|
|
|
end = property(end)
|
|
|
|
|
|
|
|
def abs_span(self):
|
|
|
|
return Span(self.abs_start, self.abs_end)
|
|
|
|
abs_span = property(abs_span)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
####################
|
|
|
|
# Public functions #
|
|
|
|
####################
|
2009-07-09 07:53:49 -04:00
|
|
|
def update(self):
|
2009-07-07 16:28:09 -04:00
|
|
|
for idx,c in enumerate(self._children):
|
|
|
|
oldend = Position(c.end.line, c.end.col)
|
|
|
|
|
2009-07-09 07:53:49 -04:00
|
|
|
new_end = c.update()
|
2009-07-09 07:49:15 -04:00
|
|
|
|
|
|
|
moved_lines = new_end.line - oldend.line
|
|
|
|
moved_cols = new_end.col - oldend.col
|
2009-07-09 08:17:35 -04:00
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
self._current_text.replace_text(c.start, oldend, c._current_text)
|
|
|
|
|
2009-07-07 16:28:09 -04:00
|
|
|
self._move_textobjects_behind(c.start, oldend, moved_lines,
|
|
|
|
moved_cols, idx)
|
|
|
|
|
|
|
|
self._do_update()
|
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
new_end = self._current_text.calc_end(self._start)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
|
|
|
self._end = new_end
|
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
return new_end
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 11:49:59 -04:00
|
|
|
def _get_next_tab(self, no):
|
|
|
|
if not len(self._tabstops.keys()):
|
|
|
|
return
|
|
|
|
tno_max = max(self._tabstops.keys())
|
|
|
|
|
|
|
|
posible_sol = []
|
|
|
|
i = no + 1
|
|
|
|
while i <= tno_max:
|
|
|
|
if i in self._tabstops:
|
|
|
|
posible_sol.append( (i, self._tabstops[i]) )
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
c = [ c._get_next_tab(no) for c in self._children ]
|
|
|
|
c = filter(lambda i: i, c)
|
|
|
|
|
|
|
|
posible_sol += c
|
|
|
|
|
|
|
|
if not len(posible_sol):
|
2009-07-09 11:04:57 -04:00
|
|
|
return None
|
|
|
|
|
2009-07-09 11:49:59 -04:00
|
|
|
return min(posible_sol)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_prev_tab(self, no):
|
|
|
|
if not len(self._tabstops.keys()):
|
|
|
|
return
|
|
|
|
tno_min = min(self._tabstops.keys())
|
|
|
|
|
|
|
|
posible_sol = []
|
|
|
|
i = no - 1
|
2009-07-10 02:46:49 -04:00
|
|
|
while i >= tno_min and i > 0:
|
2009-07-09 11:49:59 -04:00
|
|
|
if i in self._tabstops:
|
|
|
|
posible_sol.append( (i, self._tabstops[i]) )
|
|
|
|
break
|
|
|
|
i -= 1
|
|
|
|
|
|
|
|
c = [ c._get_prev_tab(no) for c in self._children ]
|
|
|
|
c = filter(lambda i: i, c)
|
|
|
|
|
|
|
|
posible_sol += c
|
|
|
|
|
|
|
|
if not len(posible_sol):
|
|
|
|
return None
|
|
|
|
|
|
|
|
return max(posible_sol)
|
|
|
|
|
2009-07-09 11:04:57 -04:00
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
###############################
|
|
|
|
# Private/Protected functions #
|
|
|
|
###############################
|
|
|
|
def _do_update(self):
|
|
|
|
pass
|
|
|
|
|
2009-07-07 16:28:09 -04:00
|
|
|
def _move_textobjects_behind(self, start, end, lines, cols, obj_idx):
|
|
|
|
if lines == 0 and cols == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
for idx,m in enumerate(self._children[obj_idx+1:]):
|
|
|
|
delta_lines = 0
|
|
|
|
delta_cols_begin = 0
|
|
|
|
delta_cols_end = 0
|
|
|
|
|
|
|
|
if m.start.line > end.line:
|
|
|
|
delta_lines = lines
|
|
|
|
elif m.start.line == end.line:
|
|
|
|
if m.start.col >= end.col:
|
|
|
|
if lines:
|
|
|
|
delta_lines = lines
|
|
|
|
delta_cols_begin = cols
|
|
|
|
if m.start.line == m.end.line:
|
|
|
|
delta_cols_end = cols
|
|
|
|
m.start.line += delta_lines
|
|
|
|
m.end.line += delta_lines
|
|
|
|
m.start.col += delta_cols_begin
|
|
|
|
m.end.col += delta_cols_end
|
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
def _get_tabstop(self, requester, no):
|
2009-07-09 08:33:06 -04:00
|
|
|
if no in self._tabstops:
|
|
|
|
return self._tabstops[no]
|
2009-07-10 02:46:49 -04:00
|
|
|
for c in self._children:
|
|
|
|
if c == requester:
|
|
|
|
continue
|
|
|
|
|
|
|
|
rv = c._get_tabstop(self, no)
|
|
|
|
if rv is not None:
|
|
|
|
return rv
|
|
|
|
|
|
|
|
if self._parent and requester != self._parent:
|
|
|
|
return self._parent._get_tabstop(self, no)
|
2009-07-09 08:33:06 -04:00
|
|
|
|
|
|
|
def _add_child(self,c):
|
|
|
|
self._children.append(c)
|
|
|
|
self._children.sort()
|
|
|
|
|
2009-07-09 09:30:23 -04:00
|
|
|
def _add_tabstop(self, no, ts):
|
|
|
|
self._tabstops[no] = ts
|
|
|
|
|
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
|
2009-07-09 08:35:52 -04:00
|
|
|
class StartMarker(TextObject):
|
|
|
|
"""
|
|
|
|
This class only remembers it's starting position. It is used to
|
|
|
|
transform relative values into absolute position values in the vim
|
|
|
|
buffer
|
|
|
|
"""
|
|
|
|
def __init__(self, start):
|
|
|
|
end = Position(start.line, start.col)
|
|
|
|
TextObject.__init__(self, None, start, end, "")
|
|
|
|
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
class Mirror(TextObject):
|
2009-07-07 16:28:09 -04:00
|
|
|
"""
|
|
|
|
A Mirror object mirrors a TabStop that is, text is repeated here
|
|
|
|
"""
|
|
|
|
def __init__(self, parent, ts, start, end):
|
2009-07-09 08:33:06 -04:00
|
|
|
TextObject.__init__(self, parent, start, end, "")
|
2009-07-07 16:28:09 -04:00
|
|
|
|
|
|
|
self._ts = ts
|
|
|
|
|
|
|
|
def _do_update(self):
|
|
|
|
self.current_text = self._ts.current_text
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Mirror(%s -> %s)" % (self._start, self._end)
|
|
|
|
|
|
|
|
|
|
|
|
class Transformation(Mirror):
|
|
|
|
def __init__(self, parent, ts, start, end, s, r, options):
|
|
|
|
Mirror.__init__(self, parent, ts, start, end)
|
|
|
|
|
|
|
|
flags = 0
|
|
|
|
self._match_this_many = 1
|
|
|
|
if options:
|
|
|
|
if "g" in options:
|
|
|
|
self._match_this_many = 0
|
|
|
|
if "i" in options:
|
|
|
|
flags |= re.IGNORECASE
|
|
|
|
|
|
|
|
self._find = re.compile(s, flags)
|
2009-07-09 08:33:06 -04:00
|
|
|
self._replace = _CleverReplace(r)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
|
|
|
def _do_update(self):
|
|
|
|
t = self._ts.current_text
|
|
|
|
t = self._find.subn(self._replace.replace, t, self._match_this_many)[0]
|
|
|
|
self.current_text = t
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Transformation(%s -> %s)" % (self._start, self._end)
|
|
|
|
|
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
class TabStop(TextObject):
|
2009-07-07 16:28:09 -04:00
|
|
|
"""
|
|
|
|
This is the most important TextObject. A TabStop is were the cursor
|
|
|
|
comes to rest when the user taps through the Snippet.
|
|
|
|
"""
|
|
|
|
def __init__(self, parent, start, end, default_text = ""):
|
2009-07-09 08:33:06 -04:00
|
|
|
TextObject.__init__(self, parent, start, end, default_text)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "TabStop(%s -> %s, %s)" % (self._start, self._end,
|
|
|
|
repr(self._current_text))
|
|
|
|
|
2009-07-09 08:33:06 -04:00
|
|
|
class SnippetInstance(TextObject):
|
2009-07-07 16:28:09 -04:00
|
|
|
"""
|
|
|
|
A Snippet instance is an instance of a Snippet Definition. That is,
|
|
|
|
when the user expands a snippet, a SnippetInstance is created to
|
|
|
|
keep track of the corresponding TextObjects. The Snippet itself is
|
|
|
|
also a TextObject because it has a start an end
|
|
|
|
"""
|
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
def __init__(self, parent, initial_text):
|
|
|
|
start = Position(0,0)
|
|
|
|
end = Position(0,0)
|
2009-07-09 11:04:57 -04:00
|
|
|
|
2009-07-10 03:02:55 -04:00
|
|
|
TextObject.__init__(self, parent, start, end, initial_text)
|
2009-07-09 07:49:15 -04:00
|
|
|
|
2009-07-10 03:02:55 -04:00
|
|
|
_TOParser(self, initial_text).parse()
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-09 07:53:49 -04:00
|
|
|
TextObject.update(self)
|
2009-07-09 02:10:46 -04:00
|
|
|
|
|
|
|
# Check if we have a zero Tab, if not, add one at the end
|
|
|
|
if 0 not in self._tabstops:
|
|
|
|
delta = self._end - self._start
|
|
|
|
col = self.end.col
|
|
|
|
if delta.line == 0:
|
|
|
|
col -= self.start.col
|
|
|
|
start = Position(delta.line, col)
|
|
|
|
end = Position(delta.line, col)
|
|
|
|
ts = TabStop(self, start, end, "")
|
2009-07-09 09:30:23 -04:00
|
|
|
self._add_tabstop(0,ts)
|
2009-07-09 02:10:46 -04:00
|
|
|
|
2009-07-09 07:53:49 -04:00
|
|
|
TextObject.update(self)
|
2009-07-09 08:17:35 -04:00
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return "SnippetInstance(%s -> %s)" % (self._start, self._end)
|
2009-07-07 16:28:09 -04:00
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
|
|
|
|
def _get_tabstop(self, requester, no):
|
|
|
|
# SnippetInstances are completly self contained,
|
|
|
|
# therefore, we do not need to ask our parent
|
|
|
|
# for Tabstops
|
|
|
|
# TODO: otherwise, this code is identical to
|
|
|
|
# TextObject._get_tabstop
|
|
|
|
if no in self._tabstops:
|
|
|
|
return self._tabstops[no]
|
|
|
|
for c in self._children:
|
|
|
|
if c == requester:
|
|
|
|
continue
|
|
|
|
|
|
|
|
rv = c._get_tabstop(self, no)
|
|
|
|
if rv is not None:
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def select_next_tab(self, backwards = False):
|
2009-07-10 02:49:53 -04:00
|
|
|
if self._cts is None:
|
|
|
|
return
|
|
|
|
|
2009-07-10 02:46:49 -04:00
|
|
|
if backwards:
|
|
|
|
cts_bf = self._cts
|
|
|
|
|
|
|
|
res = self._get_prev_tab(self._cts)
|
|
|
|
if res is None:
|
|
|
|
self._cts = cts_bf
|
|
|
|
return self._tabstops[self._cts]
|
|
|
|
self._cts, ts = res
|
|
|
|
return ts
|
|
|
|
else:
|
|
|
|
res = self._get_next_tab(self._cts)
|
|
|
|
if res is None:
|
2009-07-10 02:49:53 -04:00
|
|
|
self._cts = None
|
|
|
|
if 0 in self._tabstops:
|
|
|
|
return self._tabstops[0]
|
2009-07-10 02:46:49 -04:00
|
|
|
else:
|
|
|
|
self._cts, ts = res
|
|
|
|
return ts
|
|
|
|
|
|
|
|
return self._tabstops[self._cts]
|
|
|
|
|
|
|
|
|