2009-06-23 08:45:04 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
import vim
|
|
|
|
import string
|
2009-06-28 08:51:27 -04:00
|
|
|
import re
|
|
|
|
|
2009-07-04 06:14:13 -04:00
|
|
|
import os
|
|
|
|
|
2009-07-01 11:11:33 -04:00
|
|
|
def debug(s):
|
|
|
|
f = open("/tmp/file.txt","a")
|
|
|
|
f.write(s+'\n')
|
|
|
|
f.close()
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
class Buffer(object):
|
|
|
|
def _replace(self, start, end, content, first_line, last_line):
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
text = content[:]
|
|
|
|
if len(text) == 1:
|
|
|
|
arr = [ first_line + text[0] + last_line ]
|
|
|
|
new_end = start + Position(0,len(text[0]))
|
|
|
|
else:
|
|
|
|
arr = [ first_line + text[0] ] + \
|
|
|
|
text[1:-1] + \
|
|
|
|
[ text[-1] + last_line ]
|
|
|
|
new_end = Position(start.line + len(text)-1, len(text[-1]))
|
|
|
|
|
|
|
|
self[start.line:end.line+1] = arr
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
return new_end
|
|
|
|
|
|
|
|
class TextBuffer(Buffer):
|
|
|
|
def __init__(self, textblock):
|
|
|
|
# We do not use splitlines() here because it handles cases like 'text\n'
|
|
|
|
# differently than we want it here
|
|
|
|
self._lines = textblock.replace('\r','').split('\n')
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def calc_end(self, start):
|
|
|
|
text = self._lines[:]
|
|
|
|
if len(text) == 1:
|
|
|
|
new_end = start + Position(0,len(text[0]))
|
|
|
|
else:
|
|
|
|
new_end = Position(start.line + len(text)-1, len(text[-1]))
|
|
|
|
return new_end
|
|
|
|
|
|
|
|
def replace_text( self, start, end, content ):
|
|
|
|
first_line = self[start.line][:start.col]
|
|
|
|
last_line = self[end.line][end.col:]
|
|
|
|
return self._replace( start, end, content, first_line, last_line)
|
|
|
|
|
|
|
|
def __getitem__(self, a):
|
|
|
|
return self._lines.__getitem__(a)
|
|
|
|
def __setitem__(self, a, b):
|
|
|
|
return self._lines.__setitem__(a,b)
|
|
|
|
def __repr__(self):
|
|
|
|
return repr('\n'.join(self._lines))
|
|
|
|
def __str__(self):
|
|
|
|
return '\n'.join(self._lines)
|
|
|
|
|
|
|
|
class VimBuffer(Buffer):
|
|
|
|
def __init__(self, before, after):
|
|
|
|
self._bf = before
|
|
|
|
self._af = after
|
|
|
|
def __getitem__(self, a):
|
|
|
|
return vim.current.buffer[a]
|
|
|
|
def __setitem__(self, a, b):
|
|
|
|
if isinstance(a,slice):
|
|
|
|
vim.current.buffer[a.start:a.stop] = b
|
|
|
|
else:
|
|
|
|
vim.current.buffer[a] = b
|
|
|
|
def __repr__(self):
|
|
|
|
return "VimBuffer()"
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def replace_text( self, start, end, content ):
|
|
|
|
return self._replace( start, end, content, self._bf, self._af)
|
2009-07-02 05:48:35 -04:00
|
|
|
|
2009-07-02 02:25:58 -04:00
|
|
|
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):
|
|
|
|
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):
|
|
|
|
self._line = value
|
|
|
|
return locals()
|
|
|
|
line = property(**line())
|
|
|
|
|
2009-07-02 08:22:13 -04:00
|
|
|
def __add__(self,pos):
|
|
|
|
if not isinstance(pos,Position):
|
|
|
|
raise TypeError("unsupported operand type(s) for +: " \
|
|
|
|
"'Position' and %s" % type(pos))
|
|
|
|
|
|
|
|
return Position(self.line + pos.line, self.col + pos.col)
|
|
|
|
|
|
|
|
def __sub__(self,pos):
|
|
|
|
if not isinstance(pos,Position):
|
|
|
|
raise TypeError("unsupported operand type(s) for +: " \
|
|
|
|
"'Position' and %s" % type(pos))
|
|
|
|
|
|
|
|
return Position(self.line - pos.line, self.col - pos.col)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "(%i,%i)" % (self._line, self._col)
|
|
|
|
|
2009-07-01 09:29:14 -04:00
|
|
|
class TextObject(object):
|
2009-07-01 14:03:29 -04:00
|
|
|
"""
|
|
|
|
This base class represents any object in the text
|
|
|
|
that has a span in any ways
|
|
|
|
"""
|
2009-07-03 07:54:35 -04:00
|
|
|
# A simple tabstop with default value
|
2009-07-04 09:01:19 -04:00
|
|
|
_TABSTOP = re.compile(r'''\${(\d+)(?::(.*?))?}''')
|
2009-07-03 07:54:35 -04:00
|
|
|
# A mirror or a tabstop without default value.
|
|
|
|
_MIRROR_OR_TS = re.compile(r'\$(\d+)')
|
2009-07-03 10:13:39 -04:00
|
|
|
# A mirror or a tabstop without default value.
|
2009-07-03 11:50:52 -04:00
|
|
|
_TRANSFORMATION = re.compile(r'\${(\d+)/(.*?)/(.*?)/([a-zA-z]*)}')
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
def __init__(self, parent, start, end, initial_text):
|
2009-07-01 09:29:14 -04:00
|
|
|
self._start = start
|
|
|
|
self._end = end
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-02 09:41:58 -04:00
|
|
|
self._parent = parent
|
|
|
|
|
|
|
|
self._children = []
|
2009-07-03 04:59:55 -04:00
|
|
|
self._tabstops = {}
|
2009-07-02 09:41:58 -04:00
|
|
|
|
|
|
|
if parent is not None:
|
|
|
|
parent.add_child(self)
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
self._has_parsed = False
|
|
|
|
|
|
|
|
self._current_text = initial_text
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-04 17:01:23 -04:00
|
|
|
def __cmp__(self, other):
|
|
|
|
s = self._start.line, self._start.col
|
|
|
|
o = other._start.line, other._start.col
|
|
|
|
|
|
|
|
return cmp(s, o)
|
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
def _do_update(self):
|
|
|
|
pass
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
def update(self, change_buffer):
|
2009-07-03 07:54:35 -04:00
|
|
|
if not self._has_parsed:
|
|
|
|
self._current_text = TextBuffer(self._parse(self._current_text))
|
|
|
|
|
2009-07-04 17:01:23 -04:00
|
|
|
for idx,c in enumerate(self._children):
|
2009-07-03 05:39:46 -04:00
|
|
|
oldend = Position(c.end.line, c.end.col)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
moved_lines, moved_cols = c.update(self._current_text)
|
2009-07-04 18:26:19 -04:00
|
|
|
self._move_textobjects_behind(c.start, oldend, moved_lines,
|
|
|
|
moved_cols, idx)
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
self._do_update()
|
|
|
|
|
2009-07-04 06:14:13 -04:00
|
|
|
new_end = change_buffer.replace_text(self.start, self.end,
|
|
|
|
self._current_text)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
moved_lines = new_end.line - self._end.line
|
|
|
|
moved_cols = new_end.col - self._end.col
|
|
|
|
|
|
|
|
self._end = new_end
|
|
|
|
|
|
|
|
return moved_lines, moved_cols
|
|
|
|
|
2009-07-04 17:01:23 -04:00
|
|
|
def _move_textobjects_behind(self, start, end, lines, cols, obj_idx):
|
2009-07-03 04:59:55 -04:00
|
|
|
if lines == 0 and cols == 0:
|
|
|
|
return
|
|
|
|
|
2009-07-04 17:15:37 -04:00
|
|
|
for idx,m in enumerate(self._children[obj_idx+1:]):
|
2009-07-03 04:59:55 -04:00
|
|
|
delta_lines = 0
|
|
|
|
delta_cols_begin = 0
|
|
|
|
delta_cols_end = 0
|
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
if m.start.line > end.line:
|
2009-07-03 04:59:55 -04:00
|
|
|
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-03 06:23:30 -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
|
2009-07-03 07:54:35 -04:00
|
|
|
start_in_line = pos - line_start
|
2009-07-03 06:23:30 -04:00
|
|
|
return Position(line_idx, start_in_line)
|
|
|
|
|
|
|
|
return _get_pos(val, start_pos), _get_pos(val, end_pos)
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
def _handle_tabstop(self, m, val):
|
|
|
|
no = int(m.group(1))
|
|
|
|
def_text = m.group(2)
|
2009-07-04 09:01:19 -04:00
|
|
|
if def_text is None:
|
|
|
|
def_text = ""
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 06:23:30 -04:00
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(val,start_pos,end_pos)
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
ts = TabStop(self, start, end, def_text)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
self.add_tabstop(no,ts)
|
|
|
|
|
2009-07-03 06:05:34 -04:00
|
|
|
def _get_tabstop(self,no):
|
|
|
|
if no in self._tabstops:
|
|
|
|
return self._tabstops[no]
|
|
|
|
if self._parent:
|
|
|
|
return self._parent._get_tabstop(no)
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def _handle_ts_or_mirror(self, m, val):
|
2009-07-03 07:54:35 -04:00
|
|
|
no = int(m.group(1))
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 06:23:30 -04:00
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(val,start_pos,end_pos)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 06:05:34 -04:00
|
|
|
ts = self._get_tabstop(no)
|
|
|
|
if ts is not None:
|
2009-07-03 10:13:39 -04:00
|
|
|
Mirror(self, ts, start, end)
|
2009-07-03 05:39:46 -04:00
|
|
|
else:
|
2009-07-03 07:54:35 -04:00
|
|
|
ts = TabStop(self, start, end)
|
2009-07-03 05:39:46 -04:00
|
|
|
self.add_tabstop(no,ts)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
def _handle_transformation(self, m, val):
|
|
|
|
no = int(m.group(1))
|
2009-07-03 11:50:52 -04:00
|
|
|
search = m.group(2)
|
|
|
|
replace = m.group(3)
|
|
|
|
options = m.group(4)
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(val,start_pos,end_pos)
|
|
|
|
|
|
|
|
ts = self._get_tabstop(no)
|
|
|
|
# TODO: This is so ugly
|
|
|
|
if ts is None:
|
2009-07-03 11:50:52 -04:00
|
|
|
Transformation(self, no, start, end, search, replace, options)
|
2009-07-03 10:13:39 -04:00
|
|
|
else:
|
2009-07-03 11:50:52 -04:00
|
|
|
Transformation(self, no, start, end, search, replace, options)
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def add_tabstop(self,no, ts):
|
|
|
|
self._tabstops[no] = ts
|
|
|
|
|
|
|
|
def _parse(self, val):
|
2009-07-03 07:54:35 -04:00
|
|
|
self._has_parsed = True
|
|
|
|
|
|
|
|
if not len(val):
|
|
|
|
return val
|
|
|
|
|
|
|
|
for m in self._TABSTOP.finditer(val):
|
|
|
|
self._handle_tabstop(m,val)
|
|
|
|
# Replace the whole definition with spaces
|
|
|
|
s, e = m.span()
|
|
|
|
val = val[:s] + (e-s)*" " + val[e:]
|
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
for m in self._TRANSFORMATION.finditer(val):
|
|
|
|
self._handle_transformation(m,val)
|
|
|
|
# Replace the whole definition with spaces
|
|
|
|
s, e = m.span()
|
|
|
|
val = val[:s] + (e-s)*" " + val[e:]
|
|
|
|
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
for m in self._MIRROR_OR_TS.finditer(val):
|
|
|
|
self._handle_ts_or_mirror(m,val)
|
|
|
|
# Replace the whole definition with spaces
|
|
|
|
s, e = m.span()
|
|
|
|
val = val[:s] + (e-s)*" " + val[e:]
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
return val
|
|
|
|
|
2009-07-02 09:41:58 -04:00
|
|
|
def add_child(self,c):
|
|
|
|
self._children.append(c)
|
2009-07-04 17:01:23 -04:00
|
|
|
self._children.sort()
|
2009-07-01 09:29:14 -04:00
|
|
|
|
2009-07-02 03:49:42 -04:00
|
|
|
def parent():
|
|
|
|
doc = "The parent TextObject this TextObject resides in"
|
|
|
|
def fget(self):
|
|
|
|
return self._parent
|
|
|
|
def fset(self, value):
|
|
|
|
self._parent = value
|
|
|
|
return locals()
|
|
|
|
parent = property(**parent())
|
2009-07-02 10:02:02 -04:00
|
|
|
|
2009-07-01 09:29:14 -04:00
|
|
|
def start(self):
|
|
|
|
return self._start
|
2009-07-02 13:19:47 -04:00
|
|
|
start = property(start)
|
2009-07-01 09:29:14 -04:00
|
|
|
|
|
|
|
def end(self):
|
|
|
|
return self._end
|
2009-07-02 13:19:47 -04:00
|
|
|
end = property(end)
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
class ChangeableText(TextObject):
|
|
|
|
def __init__(self, parent, start, end, initial = ""):
|
2009-07-03 04:59:55 -04:00
|
|
|
TextObject.__init__(self, parent, start, end, initial)
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
def _set_text(self, text):
|
2009-07-03 04:59:55 -04:00
|
|
|
self._current_text = TextBuffer(text)
|
2009-07-02 15:24:56 -04:00
|
|
|
|
2009-07-03 06:05:34 -04:00
|
|
|
# Now, we can have no more childen
|
|
|
|
self._children = []
|
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
def current_text():
|
|
|
|
def fget(self):
|
2009-07-03 04:59:55 -04:00
|
|
|
return str(self._current_text)
|
2009-07-02 10:20:13 -04:00
|
|
|
def fset(self, text):
|
|
|
|
self._set_text(text)
|
|
|
|
return locals()
|
|
|
|
current_text = property(**current_text())
|
|
|
|
|
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
class Mirror(ChangeableText):
|
|
|
|
"""
|
|
|
|
A Mirror object mirrors a TabStop that is, text is repeated here
|
|
|
|
"""
|
2009-07-03 07:54:35 -04:00
|
|
|
def __init__(self, parent, ts, start, end):
|
2009-07-03 05:39:46 -04:00
|
|
|
ChangeableText.__init__(self, parent, start, end)
|
2009-07-03 06:05:34 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
self._ts = ts
|
2009-07-03 06:05:34 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
def _do_update(self):
|
|
|
|
self.current_text = self._ts.current_text
|
2009-07-01 14:14:54 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return "Mirror(%s -> %s)" % (self._start, self._end)
|
2009-07-02 10:20:13 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
class CleverReplace(object):
|
|
|
|
"""
|
|
|
|
This class mimics TextMates replace syntax
|
|
|
|
"""
|
2009-07-03 11:50:52 -04:00
|
|
|
_DOLLAR = re.compile(r"\$(\d+)", re.DOTALL)
|
|
|
|
_SIMPLE_CASEFOLDINGS = re.compile(r"\\([ul].)", re.DOTALL)
|
|
|
|
_LONG_CASEFOLDINGS = re.compile(r"\\([UL].*?)\\E", re.DOTALL)
|
2009-07-04 18:26:19 -04:00
|
|
|
_CONDITIONAL = re.compile(r"\(\?(\d+):(.*?)(?<!\\)\)", re.DOTALL)
|
|
|
|
|
|
|
|
_UNESCAPE = re.compile(r'\\[^ntrab]')
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2009-07-04 18:26:19 -04:00
|
|
|
def _unescape(self, v):
|
|
|
|
return self._UNESCAPE.subn(lambda m: m.group(0)[-1], v)[0]
|
2009-07-03 11:50:52 -04:00
|
|
|
def replace(self, match):
|
2009-07-03 10:13:39 -04:00
|
|
|
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]
|
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
def _conditional(m):
|
|
|
|
args = m.group(2).split(':')
|
|
|
|
# TODO: the returned string should be checked for conditionals
|
|
|
|
if match.group(int(m.group(1))):
|
2009-07-04 18:26:19 -04:00
|
|
|
return self._unescape(args[0])
|
2009-07-03 11:50:52 -04:00
|
|
|
elif len(args) > 1:
|
2009-07-04 18:26:19 -04:00
|
|
|
return self._unescape(args[1])
|
2009-07-03 11:50:52 -04:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
# Replace CaseFoldings
|
|
|
|
tv = self._SIMPLE_CASEFOLDINGS.subn(self._scase_folding, tv)[0]
|
|
|
|
tv = self._LONG_CASEFOLDINGS.subn(self._lcase_folding, tv)[0]
|
2009-07-03 11:50:52 -04:00
|
|
|
tv = self._CONDITIONAL.subn(_conditional, tv)[0]
|
2009-07-03 10:13:39 -04:00
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
rv = tv.decode("string-escape")
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
return rv
|
|
|
|
|
|
|
|
class Transformation(Mirror):
|
2009-07-03 11:50:52 -04:00
|
|
|
def __init__(self, parent, ts, start, end, s, r, options):
|
2009-07-03 10:13:39 -04:00
|
|
|
Mirror.__init__(self, parent, ts, start, end)
|
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
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
|
2009-07-03 10:13:39 -04:00
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
self._find = re.compile(s, flags)
|
|
|
|
self._replace = CleverReplace(r)
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
def _do_update(self):
|
|
|
|
if isinstance(self._ts,int):
|
|
|
|
self._ts = self._parent._get_tabstop(self._ts)
|
|
|
|
|
|
|
|
t = self._ts.current_text
|
2009-07-03 11:50:52 -04:00
|
|
|
t = self._find.subn(self._replace.replace, t, self._match_this_many)[0]
|
2009-07-03 10:13:39 -04:00
|
|
|
self.current_text = t
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Transformation(%s -> %s)" % (self._start, self._end)
|
|
|
|
|
|
|
|
|
2009-07-02 15:24:56 -04:00
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
class TabStop(ChangeableText):
|
2009-07-01 14:03:29 -04:00
|
|
|
"""
|
|
|
|
This is the most important TextObject. A TabStop is were the cursor
|
|
|
|
comes to rest when the user taps through the Snippet.
|
|
|
|
"""
|
2009-07-03 07:54:35 -04:00
|
|
|
def __init__(self, parent, start, end, default_text = ""):
|
2009-07-02 10:20:13 -04:00
|
|
|
ChangeableText.__init__(self, parent, start, end, default_text)
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-02 15:24:56 -04:00
|
|
|
def __repr__(self):
|
2009-07-03 04:59:55 -04:00
|
|
|
return "TabStop(%s -> %s, %s)" % (self._start, self._end,
|
|
|
|
repr(self._current_text))
|
2009-07-02 15:24:56 -04:00
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
def select(self, start):
|
|
|
|
lineno, col = start.line, start.col
|
2009-07-01 14:27:28 -04:00
|
|
|
|
2009-07-02 02:25:58 -04:00
|
|
|
newline = lineno + self._start.line
|
|
|
|
newcol = self._start.col
|
2009-07-01 17:09:20 -04:00
|
|
|
|
2009-07-01 14:27:28 -04:00
|
|
|
if newline == lineno:
|
2009-07-01 17:09:20 -04:00
|
|
|
newcol += col
|
2009-07-01 14:27:28 -04:00
|
|
|
|
2009-07-02 02:25:58 -04:00
|
|
|
vim.current.window.cursor = newline + 1, newcol
|
2009-07-04 15:58:13 -04:00
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
if len(self.current_text) == 0:
|
|
|
|
if newcol == 0:
|
|
|
|
vim.command(r'call feedkeys("\<Esc>i")')
|
|
|
|
else:
|
|
|
|
vim.command(r'call feedkeys("\<Esc>a")')
|
|
|
|
else:
|
2009-07-03 07:54:35 -04:00
|
|
|
# Select the word
|
|
|
|
# Depending on the current mode and position, we
|
|
|
|
# might need to move escape out of the mode and this
|
|
|
|
# will move our cursor one left
|
2009-07-01 14:27:28 -04:00
|
|
|
if newcol != 0 and vim.eval("mode()") == 'i':
|
|
|
|
move_one_right = "l"
|
|
|
|
else:
|
|
|
|
move_one_right = ""
|
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
if len(self.current_text) <= 1:
|
2009-07-03 07:54:35 -04:00
|
|
|
do_select = ""
|
|
|
|
else:
|
|
|
|
do_select = "%il" % (len(self.current_text)-1)
|
|
|
|
|
|
|
|
vim.command(r'call feedkeys("\<Esc>%sv%s\<c-g>")' %
|
|
|
|
(move_one_right, do_select))
|
2009-07-01 14:27:28 -04:00
|
|
|
|
|
|
|
|
2009-07-01 11:11:33 -04:00
|
|
|
class SnippetInstance(TextObject):
|
2009-07-01 14:03:29 -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-03 04:59:55 -04:00
|
|
|
def __init__(self, start, end, initial_text, text_before, text_after):
|
|
|
|
TextObject.__init__(self, None, start, end, initial_text)
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
self._cts = None
|
2009-07-02 09:45:25 -04:00
|
|
|
self._tab_selected = False
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
self._vb = VimBuffer(text_before, text_after)
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
self._current_text = TextBuffer(self._parse(initial_text))
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
TextObject.update(self, self._vb)
|
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
def tab_selected(self):
|
|
|
|
return self._tab_selected
|
|
|
|
tab_selected = property(tab_selected)
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def update(self, buf, cur):
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
TextObject.update(self, buf)
|
|
|
|
|
|
|
|
cts = self._tabstops[self._cts]
|
2009-07-04 10:08:14 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
cursor = self.start + cts.end
|
|
|
|
if cts.end.line != 0:
|
|
|
|
cursor.col -= self.start.col
|
|
|
|
lineno, col = cursor.line, cursor.col
|
|
|
|
|
|
|
|
vim.current.window.cursor = lineno +1, col
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-02 09:41:58 -04:00
|
|
|
def has_tabs(self):
|
|
|
|
return len(self._children) > 0
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 03:49:42 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
def select_next_tab(self, backwards = False):
|
|
|
|
if self._cts == 0:
|
|
|
|
if not backwards:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if backwards:
|
|
|
|
cts_bf = self._cts
|
|
|
|
|
|
|
|
if self._cts == 0:
|
|
|
|
self._cts = max(self._tabstops.keys())
|
2009-06-28 16:22:19 -04:00
|
|
|
else:
|
2009-07-01 14:03:29 -04:00
|
|
|
self._cts -= 1
|
|
|
|
if self._cts <= 0:
|
|
|
|
self._cts = cts_bf
|
|
|
|
else:
|
|
|
|
# All tabs handled?
|
|
|
|
if self._cts is None:
|
2009-06-28 16:22:19 -04:00
|
|
|
self._cts = 1
|
2009-07-01 14:03:29 -04:00
|
|
|
else:
|
|
|
|
self._cts += 1
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
if self._cts not in self._tabstops:
|
|
|
|
self._cts = 0
|
|
|
|
if 0 not in self._tabstops:
|
|
|
|
return False
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
ts = self._tabstops[self._cts]
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
ts.select(self._start)
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 09:45:25 -04:00
|
|
|
self._tab_selected = True
|
2009-07-01 14:03:29 -04:00
|
|
|
return True
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def backspace(self,count, previous_cp):
|
2009-07-02 08:22:13 -04:00
|
|
|
cts = self._tabstops[self._cts]
|
|
|
|
cts.current_text = cts.current_text[:-count]
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
self.update(self._vb, previous_cp)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def chars_entered(self, chars, cur):
|
2009-07-01 14:03:29 -04:00
|
|
|
cts = self._tabstops[self._cts]
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 09:45:25 -04:00
|
|
|
if self._tab_selected:
|
2009-07-02 08:22:13 -04:00
|
|
|
cts.current_text = chars
|
2009-07-02 09:45:25 -04:00
|
|
|
self._tab_selected = False
|
2009-07-01 14:03:29 -04:00
|
|
|
else:
|
2009-07-02 08:22:13 -04:00
|
|
|
cts.current_text += chars
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
self.update(self._vb, cur)
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
class Snippet(object):
|
2009-07-04 11:59:50 -04:00
|
|
|
_INDENT = re.compile(r"^[ \t]*")
|
|
|
|
|
2009-07-04 15:58:13 -04:00
|
|
|
def __init__(self,trigger,value, descr):
|
2009-06-23 08:45:04 -04:00
|
|
|
self._t = trigger
|
|
|
|
self._v = value
|
2009-07-04 15:58:13 -04:00
|
|
|
self._d = descr
|
|
|
|
|
|
|
|
def description(self):
|
|
|
|
return self._d
|
|
|
|
description = property(description)
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
def trigger(self):
|
|
|
|
return self._t
|
2009-07-02 13:19:47 -04:00
|
|
|
trigger = property(trigger)
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def launch(self, before, after):
|
2009-07-02 08:22:13 -04:00
|
|
|
lineno, col = vim.current.window.cursor
|
|
|
|
start = Position(lineno-1,col - len(self._t))
|
|
|
|
end = Position(lineno-1,col)
|
2009-07-02 09:41:58 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
line = vim.current.line
|
2009-07-02 09:41:58 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
text_before = line[:start.col]
|
|
|
|
text_after = line[end.col:]
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-04 11:59:50 -04:00
|
|
|
indend = self._INDENT.match(text_before).group(0)
|
|
|
|
v = self._v
|
|
|
|
if len(indend):
|
2009-07-04 15:58:13 -04:00
|
|
|
lines = self._v.splitlines()
|
2009-07-04 11:59:50 -04:00
|
|
|
v = lines[0] + os.linesep + \
|
|
|
|
os.linesep.join([ indend + l for l in lines[1:]])
|
2009-07-04 15:58:13 -04:00
|
|
|
|
2009-07-04 11:59:50 -04:00
|
|
|
s = SnippetInstance(start, end, v, text_before, text_after)
|
2009-07-02 09:41:58 -04:00
|
|
|
|
|
|
|
if s.has_tabs():
|
2009-06-28 16:22:19 -04:00
|
|
|
s.select_next_tab()
|
|
|
|
return s
|
2009-07-02 08:22:13 -04:00
|
|
|
else:
|
2009-07-03 04:59:55 -04:00
|
|
|
vim.current.window.cursor = s.end.line + 1, s.end.col
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
class Cursor(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._abs_pos = None
|
|
|
|
self._moved = Position(0,0)
|
|
|
|
|
|
|
|
def update_position(self):
|
|
|
|
line, col = vim.current.window.cursor
|
|
|
|
line -= 1
|
|
|
|
abs_pos = Position(line,col)
|
|
|
|
if self._abs_pos:
|
|
|
|
self._moved = abs_pos - self._abs_pos
|
|
|
|
# self._start = abs_pos - self._parent.start
|
|
|
|
self._abs_pos = abs_pos
|
|
|
|
|
|
|
|
def pos(self):
|
|
|
|
return self._abs_pos
|
|
|
|
pos = property(pos)
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def ppos(self):
|
|
|
|
if not self.has_moved:
|
|
|
|
return self.pos
|
|
|
|
return self.pos - self.moved
|
|
|
|
ppos = property(ppos)
|
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
def moved(self):
|
|
|
|
return self._moved
|
|
|
|
moved = property(moved)
|
|
|
|
|
|
|
|
def has_moved(self):
|
|
|
|
return bool(self._moved.line or self._moved.col)
|
|
|
|
has_moved = property(has_moved)
|
|
|
|
|
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
class SnippetManager(object):
|
|
|
|
def __init__(self):
|
2009-06-28 16:22:19 -04:00
|
|
|
self.reset()
|
2009-07-04 09:01:19 -04:00
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
self._cursor = Cursor()
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-04 06:14:13 -04:00
|
|
|
def _load_snippets_from(self, ft, fn):
|
|
|
|
cs = None
|
|
|
|
cv = ""
|
2009-07-04 15:58:13 -04:00
|
|
|
cdescr = ""
|
2009-07-04 06:14:13 -04:00
|
|
|
for line in open(fn):
|
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
if line.startswith("snippet"):
|
|
|
|
cs = line.split()[1]
|
2009-07-04 15:58:13 -04:00
|
|
|
left = line.find('"')
|
|
|
|
if left != -1:
|
|
|
|
right = line.rfind('"')
|
|
|
|
cdescr = line[left+1:right]
|
2009-07-04 06:14:13 -04:00
|
|
|
continue
|
|
|
|
if cs != None:
|
|
|
|
if line.startswith("endsnippet"):
|
|
|
|
cv = cv[:-1] # Chop the last newline
|
2009-07-04 15:58:13 -04:00
|
|
|
l = self._snippets[ft].get(cs,[])
|
|
|
|
l.append(Snippet(cs,cv,cdescr))
|
|
|
|
self._snippets[ft][cs] = l
|
2009-07-04 06:14:13 -04:00
|
|
|
cv = ""
|
2009-07-04 15:58:13 -04:00
|
|
|
cdescr = ""
|
2009-07-04 06:14:13 -04:00
|
|
|
cs = None
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
cv += line
|
|
|
|
|
|
|
|
|
|
|
|
def _load_snippets_for(self, ft):
|
|
|
|
self._snippets[ft] = {}
|
|
|
|
for p in vim.eval("&runtimepath").split(','):
|
|
|
|
fn = p + os.path.sep + "PySnippets" + os.path.sep + \
|
|
|
|
"%s.snippets" % ft
|
|
|
|
if os.path.exists(fn):
|
|
|
|
self._load_snippets_from(ft, fn)
|
|
|
|
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def reset(self):
|
2009-07-01 04:39:46 -04:00
|
|
|
self._snippets = {}
|
2009-06-28 16:22:19 -04:00
|
|
|
self._current_snippets = []
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-04 15:58:13 -04:00
|
|
|
def add_snippet(self,trigger,value, descr):
|
2009-07-04 06:14:13 -04:00
|
|
|
if "all" not in self._snippets:
|
|
|
|
self._snippets["all"] = {}
|
2009-07-04 15:58:13 -04:00
|
|
|
l = self._snippets["all"].get(trigger,[])
|
|
|
|
l.append(Snippet(trigger,value, descr))
|
|
|
|
self._snippets["all"][trigger] = l
|
|
|
|
|
|
|
|
def _find_snippets(self, ft, trigger):
|
|
|
|
snips = self._snippets.get(ft,None)
|
|
|
|
if not snips:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return snips.get(trigger, [])
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
def try_expand(self, backwards = False):
|
2009-07-04 06:14:13 -04:00
|
|
|
ft = vim.eval("&filetype")
|
|
|
|
if len(ft) and ft not in self._snippets:
|
|
|
|
self._load_snippets_for(ft)
|
|
|
|
if "all" not in self._snippets:
|
|
|
|
self._load_snippets_for("all")
|
|
|
|
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
if len(self._current_snippets):
|
2009-07-01 14:03:29 -04:00
|
|
|
cs = self._current_snippets[-1]
|
|
|
|
if not cs.select_next_tab(backwards):
|
2009-07-04 11:36:06 -04:00
|
|
|
# Jump to the end of the snippet and enter insert mode
|
|
|
|
cs = self._current_snippets[-1]
|
|
|
|
vim.current.window.cursor = cs.end.line +1, cs.end.col
|
|
|
|
vim.command(r'call feedkeys("\<Esc>a")')
|
2009-07-01 14:03:29 -04:00
|
|
|
self._current_snippets.pop()
|
2009-07-04 11:36:06 -04:00
|
|
|
|
|
|
|
return True
|
2009-07-04 06:45:35 -04:00
|
|
|
|
|
|
|
self._cursor.update_position()
|
2009-07-04 10:08:14 -04:00
|
|
|
return True
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
dummy,col = vim.current.window.cursor
|
2009-07-04 06:14:13 -04:00
|
|
|
if col == 0:
|
2009-07-04 10:08:14 -04:00
|
|
|
return False
|
2009-07-04 06:14:13 -04:00
|
|
|
|
|
|
|
line = vim.current.line
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
if col > 0 and line[col-1] in string.whitespace:
|
2009-07-04 10:08:14 -04:00
|
|
|
return False
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
# Get the word to the left of the current edit position
|
|
|
|
before,after = line[:col], line[col:]
|
|
|
|
|
|
|
|
word = before.split()[-1]
|
2009-07-04 15:58:13 -04:00
|
|
|
snippets = []
|
2009-07-04 06:14:13 -04:00
|
|
|
if len(ft):
|
2009-07-04 15:58:13 -04:00
|
|
|
snippets += self._find_snippets(ft, word)
|
|
|
|
snippets += self._find_snippets("all", word)
|
2009-07-04 06:14:13 -04:00
|
|
|
|
2009-07-04 15:58:13 -04:00
|
|
|
if not len(snippets):
|
2009-07-04 06:14:13 -04:00
|
|
|
# No snippet found
|
2009-07-04 10:08:14 -04:00
|
|
|
return False
|
2009-07-04 15:58:13 -04:00
|
|
|
elif len(snippets) == 1:
|
|
|
|
snippet, = snippets
|
|
|
|
else:
|
|
|
|
display = repr(
|
|
|
|
[ "%i: %s" % (i+1,s.description) for i,s in
|
|
|
|
enumerate(snippets)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
rv = vim.eval("inputlist(%s)" % display)
|
|
|
|
if rv is None:
|
|
|
|
return True
|
|
|
|
rv = int(rv)
|
|
|
|
snippet = snippets[rv-1]
|
2009-07-04 06:14:13 -04:00
|
|
|
|
|
|
|
s = snippet.launch(before.rstrip()[:-len(word)], after)
|
2009-07-04 06:45:35 -04:00
|
|
|
|
|
|
|
self._cursor.update_position()
|
2009-07-04 06:14:13 -04:00
|
|
|
if s is not None:
|
|
|
|
self._current_snippets.append(s)
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-04 10:08:14 -04:00
|
|
|
return True
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def cursor_moved(self):
|
2009-07-05 07:20:58 -04:00
|
|
|
debug("Cursor moved")
|
2009-07-05 12:46:08 -04:00
|
|
|
|
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
self._cursor.update_position()
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
if len(self._current_snippets) and (self._cursor.has_moved):
|
|
|
|
if 0 <= self._cursor.moved.line <= 1:
|
2009-07-01 09:20:40 -04:00
|
|
|
cs = self._current_snippets[-1]
|
|
|
|
|
2009-07-02 05:58:46 -04:00
|
|
|
# Detect a carriage return
|
2009-07-04 11:36:06 -04:00
|
|
|
if self._cursor.moved.col < 0 and self._cursor.moved.line == 1:
|
2009-07-03 05:46:42 -04:00
|
|
|
# Hack, remove a line in vim, because we are going to
|
|
|
|
# overwrite the old line range with the new snippet value.
|
|
|
|
# After the expansion, we put the cursor were the user left
|
|
|
|
# it. This action should be completely transparent for the
|
|
|
|
# user
|
|
|
|
cache_pos = vim.current.window.cursor
|
2009-07-04 06:45:35 -04:00
|
|
|
del vim.current.buffer[self._cursor.pos.line-1]
|
2009-07-04 09:01:19 -04:00
|
|
|
cs.chars_entered('\n', self._cursor)
|
2009-07-03 05:46:42 -04:00
|
|
|
vim.current.window.cursor = cache_pos
|
2009-07-04 06:45:35 -04:00
|
|
|
elif self._cursor.moved.col < 0: # Some deleting was going on
|
2009-07-04 09:01:19 -04:00
|
|
|
cs.backspace(-self._cursor.moved.col, self._cursor)
|
2009-07-01 09:20:40 -04:00
|
|
|
else:
|
|
|
|
line = vim.current.line
|
2009-07-04 09:01:19 -04:00
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
chars = line[self._cursor.pos.col - self._cursor.moved.col:
|
|
|
|
self._cursor.pos.col]
|
2009-07-04 09:01:19 -04:00
|
|
|
cs.chars_entered(chars, self._cursor)
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-04 15:58:13 -04:00
|
|
|
self._cursor.update_position()
|
2009-06-28 16:22:19 -04:00
|
|
|
|
|
|
|
def entered_insert_mode(self):
|
2009-07-04 11:36:06 -04:00
|
|
|
if len(self._current_snippets) and \
|
|
|
|
not self._current_snippets[-1].tab_selected:
|
|
|
|
self._current_snippets = []
|
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
PySnipSnippets = SnippetManager()
|
2009-07-01 14:14:54 -04:00
|
|
|
|