UltiSnips/PySnipEmu.py

460 lines
13 KiB
Python
Raw Normal View History

2009-06-23 08:45:04 -04:00
#!/usr/bin/env python
# encoding: utf-8
import vim
import string
import re
2009-07-01 11:11:33 -04:00
def debug(s):
f = open("/tmp/file.txt","a")
f.write(s+'\n')
f.close()
def _replace_text_in_buffer( start, end, textblock ):
first_line = vim.current.buffer[start.line][:start.col]
last_line = vim.current.buffer[end.line][end.col:]
2009-07-02 05:58:46 -04:00
# We do not use splitlines() here because it handles cases like 'text\n'
# differently than we want it here
text = textblock.replace('\r','').split('\n')
2009-07-02 05:58:46 -04:00
if not len(text):
new_end = Position(start.line, start.col)
2009-07-02 05:58:46 -04:00
arr = [ first_line + last_line ]
elif len(text) == 1:
arr = [ first_line + text[0] + last_line ]
new_end = Position(start.line, len(arr[0])-len(last_line))
2009-07-02 05:58:46 -04:00
else:
arr = [ first_line + text[0] ] + \
text[1:-1] + \
[ text[-1] + last_line ]
new_end = Position(start.line + len(arr)-1, len(arr[-1])-len(last_line))
2009-07-02 05:58:46 -04:00
vim.current.buffer[start.line:end.line+1] = arr
2009-07-02 05:58:46 -04:00
return new_end
2009-07-02 05:58:46 -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):
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())
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)
class TextObject(object):
"""
This base class represents any object in the text
that has a span in any ways
"""
2009-07-02 09:41:58 -04:00
def __init__(self, parent, start, end):
self._start = start
self._end = end
2009-07-02 09:41:58 -04:00
self._parent = parent
self._children = []
if parent is not None:
parent.add_child(self)
def add_child(self,c):
self._children.append(c)
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
@property
def start(self):
return self._start
@property
def end(self):
return self._end
2009-07-01 11:11:33 -04:00
class ChangeableText(TextObject):
def __init__(self, parent, start, end, initial = ""):
2009-07-02 09:41:58 -04:00
TextObject.__init__(self, parent, start, end)
self._ct = initial
2009-07-01 09:20:40 -04:00
def _set_text(self, text):
self._ct = text
2009-07-02 09:41:58 -04:00
text = text.replace('\r','').split('\n')
2009-07-01 14:14:54 -04:00
oldlinespan = self.end.line - self.start.line
oldcolspan = self.end.col - self.start.col
new_end = self._start + Position(len(text) - 1, len(text[-1]))
2009-07-02 10:02:02 -04:00
newlinespan = new_end.line - self.start.line
newcolspan = new_end.col - self.start.col
2009-07-01 14:14:54 -04:00
2009-07-02 09:41:58 -04:00
moved_lines = newlinespan - oldlinespan
moved_cols = newcolspan - oldcolspan
self._parent._move_textobjects_behind(moved_lines, moved_cols, self)
2009-07-02 10:02:02 -04:00
self._end = new_end
def current_text():
def fget(self):
return self._ct
def fset(self, text):
self._set_text(text)
return locals()
current_text = property(**current_text())
class Mirror(ChangeableText):
"""
A Mirror object mirrors a TabStop that is, text is repeated here
"""
def __init__(self, parent, ts, idx, start_col):
start = Position(idx,start_col)
end = start + (ts.end - ts.start)
ChangeableText.__init__(self, parent, start, end)
ts.add_mirror(self)
def _set_text(self,text):
_replace_text_in_buffer(
self._parent.start + self._start,
self._parent.start + self._end,
text,
)
ChangeableText._set_text(self, text)
2009-07-01 14:14:54 -04:00
class TabStop(ChangeableText):
"""
This is the most important TextObject. A TabStop is were the cursor
comes to rest when the user taps through the Snippet.
"""
2009-07-02 09:41:58 -04:00
def __init__(self, parent, idx, span, default_text = ""):
start = Position(idx,span[0])
end = Position(idx,span[1])
ChangeableText.__init__(self, parent, start, end, default_text)
2009-07-01 09:20:40 -04:00
2009-07-02 09:41:58 -04:00
self._mirrors = []
def _set_text(self,text):
ChangeableText._set_text(self,text)
2009-07-02 09:41:58 -04:00
for m in self._mirrors:
m.current_text = text
def add_mirror(self, m):
self._mirrors.append(m)
def select(self):
lineno, col = self._parent.start.line, self._parent.start.col
2009-07-01 14:27:28 -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
vim.current.window.cursor = newline + 1, newcol
2009-07-01 14:27:28 -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
if len(self.current_text) > 0:
2009-07-01 14:27:28 -04:00
if newcol != 0 and vim.eval("mode()") == 'i':
move_one_right = "l"
else:
move_one_right = ""
vim.command(r'call feedkeys("\<Esc>%sv%il\<c-g>")'
% (move_one_right, len(self.current_text)-1))
2009-07-01 14:27:28 -04:00
2009-07-01 11:11:33 -04:00
class SnippetInstance(TextObject):
"""
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-02 09:41:58 -04:00
def __init__(self, start, end):
TextObject.__init__(self, None, start, end)
self._cts = None
2009-07-02 09:45:25 -04:00
self._tab_selected = False
2009-07-02 09:41:58 -04:00
self._tabstops = {}
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 09:41:58 -04:00
def add_tabstop(self,no, ts):
self._tabstops[no] = ts
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())
else:
self._cts -= 1
if self._cts <= 0:
self._cts = cts_bf
else:
# All tabs handled?
if self._cts is None:
self._cts = 1
else:
self._cts += 1
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
ts = self._tabstops[self._cts]
ts.select()
2009-07-01 11:11:33 -04:00
2009-07-02 09:45:25 -04:00
self._tab_selected = True
return True
def _move_textobjects_behind(self, lines, cols, obj):
if lines == 0 and cols == 0:
return
2009-07-02 09:41:58 -04:00
for m in self._children:
if m == obj:
2009-07-01 09:20:40 -04:00
continue
if m.start.line > obj.end.line:
m.start.line += lines
m.end.line += lines
elif m.start.line == obj.end.line:
if m.start.col >= obj.end.col:
if lines:
m.start.line += lines
m.end.line += lines
2009-07-02 10:02:02 -04:00
m.end.col -= m.start.col
m.start.col = 0
else:
m.start.col += cols
m.end.col += cols
2009-07-02 05:58:46 -04:00
2009-07-01 09:20:40 -04:00
def backspace(self,count):
cts = self._tabstops[self._cts]
cts.current_text = cts.current_text[:-count]
2009-07-01 09:20:40 -04:00
def chars_entered(self, chars):
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:
cts.current_text = chars
2009-07-02 09:45:25 -04:00
self._tab_selected = False
else:
cts.current_text += chars
2009-07-01 09:20:40 -04:00
2009-06-23 08:45:04 -04:00
class Snippet(object):
2009-07-01 09:20:40 -04:00
_TABSTOP = re.compile(r'''(?xms)
(?:\${(\d+):(.*?)})| # A simple tabstop with default value
(?:\$(\d+)) # A mirror or a tabstop without default value.
''')
2009-06-23 08:45:04 -04:00
def __init__(self,trigger,value):
self._t = trigger
self._v = value
2009-07-01 14:14:54 -04:00
@property
2009-06-23 08:45:04 -04:00
def trigger(self):
return self._t
2009-07-02 09:41:58 -04:00
def _handle_tabstop(self, s, m, val, tabstops):
2009-07-01 09:20:40 -04:00
no = int(m.group(1))
def_text = m.group(2)
2009-07-01 09:20:40 -04:00
start, end = m.span()
val = val[:start] + def_text + val[end:]
2009-07-01 09:20:40 -04:00
line_idx = val[:start].count('\n')
line_start = val[:start].rfind('\n') + 1
start_in_line = start - line_start
2009-07-02 09:41:58 -04:00
ts = TabStop(s, line_idx,
(start_in_line,start_in_line+len(def_text)), def_text)
tabstops[no] = ts
2009-07-02 09:41:58 -04:00
s.add_tabstop(no,ts)
2009-07-01 09:20:40 -04:00
return val
2009-07-02 09:41:58 -04:00
def _handle_ts_or_mirror(self, s, m, val, tabstops):
2009-07-01 09:20:40 -04:00
no = int(m.group(3))
2009-07-01 09:20:40 -04:00
start, end = m.span()
2009-07-01 09:20:40 -04:00
line_idx = val[:start].count('\n')
line_start = val[:start].rfind('\n') + 1
start_in_line = start - line_start
2009-07-01 09:20:40 -04:00
if no in tabstops:
2009-07-02 09:41:58 -04:00
m = Mirror(s, tabstops[no], line_idx, start_in_line)
val = val[:start] + tabstops[no].current_text + val[end:]
2009-07-01 09:20:40 -04:00
else:
2009-07-02 09:41:58 -04:00
ts = TabStop(s, line_idx, (start_in_line,start_in_line))
val = val[:start] + val[end:]
tabstops[no] = ts
2009-07-02 09:41:58 -04:00
s.add_tabstop(no,ts)
2009-07-01 09:20:40 -04:00
return val
2009-07-02 09:41:58 -04:00
def _find_tabstops(self, s, val):
2009-07-01 09:20:40 -04:00
tabstops = {}
while 1:
m = self._TABSTOP.search(val)
if m is not None:
if m.group(1) is not None: # ${1:hallo}
2009-07-02 09:41:58 -04:00
val = self._handle_tabstop(s,m,val,tabstops)
2009-07-01 09:20:40 -04:00
elif m.group(3) is not None: # $1
2009-07-02 09:41:58 -04:00
val = self._handle_ts_or_mirror(s,m,val,tabstops)
2009-07-01 09:20:40 -04:00
else:
break
2009-07-02 09:41:58 -04:00
return val
2009-06-23 08:45:04 -04:00
def launch(self, before, after):
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
s = SnippetInstance(start,end)
text = self._find_tabstops(s, self._v)
new_end = _replace_text_in_buffer( start, end, text )
2009-07-02 09:41:58 -04:00
# TODO: hack
s.end.col = new_end.col
s.end.line = new_end.line
if s.has_tabs():
s.select_next_tab()
return s
else:
vim.current.window.cursor = new_end.line + 1, new_end.col
2009-06-23 08:45:04 -04:00
class SnippetManager(object):
def __init__(self):
self.reset()
2009-07-01 09:20:40 -04:00
self._last_cursor_pos = None
def reset(self):
self._snippets = {}
self._current_snippets = []
2009-06-23 08:45:04 -04:00
def add_snippet(self,trigger,value):
self._snippets[trigger] = Snippet(trigger,value)
def try_expand(self, backwards = False):
if len(self._current_snippets):
cs = self._current_snippets[-1]
if not cs.select_next_tab(backwards):
self._current_snippets.pop()
2009-07-01 11:11:33 -04:00
self._last_cursor_pos = vim.current.window.cursor
return
2009-06-23 08:45:04 -04:00
line = vim.current.line
2009-06-23 08:45:04 -04:00
dummy,col = vim.current.window.cursor
if col > 0 and line[col-1] in string.whitespace:
return
# Get the word to the left of the current edit position
before,after = line[:col], line[col:]
word = before.split()[-1]
if word in self._snippets:
s = self._snippets[word].launch(before.rstrip()[:-len(word)], after)
self._last_cursor_pos = vim.current.window.cursor
if s is not None:
self._current_snippets.append(s)
def cursor_moved(self):
2009-07-01 09:20:40 -04:00
cp = vim.current.window.cursor
if len(self._current_snippets) and self._last_cursor_pos is not None:
2009-07-01 09:20:40 -04:00
lineno,col = cp
llineo,lcol = self._last_cursor_pos
2009-07-02 05:58:46 -04:00
if lineno in \
[ self._last_cursor_pos[0], self._last_cursor_pos[0]+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
if col == 0 and lineno == self._last_cursor_pos[0] + 1:
cs.chars_entered('\n')
elif lcol > col: # Some deleting was going on
2009-07-01 09:20:40 -04:00
cs.backspace(lcol-col)
else:
line = vim.current.line
chars = line[lcol:col]
cs.chars_entered(chars)
2009-07-01 11:11:33 -04:00
2009-07-01 09:20:40 -04:00
self._last_cursor_pos = cp
def entered_insert_mode(self):
pass
2009-06-23 08:45:04 -04:00
PySnipSnippets = SnippetManager()
2009-07-01 14:14:54 -04:00