2009-06-23 08:45:04 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2009-07-06 17:09:53 -04:00
|
|
|
import glob
|
2009-07-04 06:14:13 -04:00
|
|
|
import os
|
2009-07-06 17:09:53 -04:00
|
|
|
import re
|
|
|
|
import string
|
|
|
|
import vim
|
2009-07-04 06:14:13 -04:00
|
|
|
|
2009-07-08 03:57:55 -04:00
|
|
|
from PySnipEmu.Geometry import Position
|
2009-07-09 01:50:48 -04:00
|
|
|
from PySnipEmu.TextObjects import *
|
2009-07-09 07:49:15 -04:00
|
|
|
from PySnipEmu.Buffer import VimBuffer
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-08 03:57:55 -04:00
|
|
|
from PySnipEmu.debug import debug
|
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-07-09 07:49:15 -04:00
|
|
|
def launch(self, text_before, start):
|
2009-07-06 04:40:07 -04:00
|
|
|
indent = self._INDENT.match(text_before).group(0)
|
2009-07-04 11:59:50 -04:00
|
|
|
v = self._v
|
2009-07-06 04:40:07 -04:00
|
|
|
if len(indent):
|
2009-07-04 15:58:13 -04:00
|
|
|
lines = self._v.splitlines()
|
2009-07-06 04:40:07 -04:00
|
|
|
v = lines[0]
|
|
|
|
if len(lines) > 1:
|
|
|
|
v += os.linesep + \
|
|
|
|
os.linesep.join([indent + l for l in lines[1:]])
|
2009-07-04 15:58:13 -04:00
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
return SnippetInstance(StartMarker(start), v )
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
class VimState(object):
|
2009-07-04 06:45:35 -04:00
|
|
|
def __init__(self):
|
|
|
|
self._abs_pos = None
|
|
|
|
self._moved = Position(0,0)
|
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._lines = None
|
|
|
|
self._dlines = None
|
|
|
|
self._cols = None
|
|
|
|
self._dcols = None
|
2009-07-06 04:31:12 -04:00
|
|
|
self._lline = None
|
2009-07-05 16:25:01 -04:00
|
|
|
self._text_changed = None
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
def update(self):
|
2009-07-04 06:45:35 -04:00
|
|
|
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._abs_pos = abs_pos
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
# Update buffer infos
|
2009-07-05 16:25:01 -04:00
|
|
|
cols = len(vim.current.buffer[line])
|
|
|
|
if self._cols:
|
|
|
|
self._dcols = cols - self._cols
|
|
|
|
self._cols = cols
|
|
|
|
|
|
|
|
lines = len(vim.current.buffer)
|
|
|
|
if self._lines:
|
|
|
|
self._dlines = lines - self._lines
|
|
|
|
self._lines = lines
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
# Check if the buffer has changed in any ways
|
|
|
|
self._text_changed = False
|
2009-07-06 04:31:12 -04:00
|
|
|
# does it have more lines?
|
|
|
|
if self._dlines:
|
|
|
|
self._text_changed = True
|
|
|
|
# did we stay in the same line and it has more columns now?
|
|
|
|
elif not self.moved.line and self._dcols:
|
|
|
|
self._text_changed = True
|
|
|
|
# If the length didn't change but we moved a column, check if
|
|
|
|
# the char under the cursor has changed (might be one char tab).
|
|
|
|
elif self.moved.col == 1:
|
|
|
|
self._text_changed = self._lline != vim.current.buffer[line]
|
|
|
|
self._lline = vim.current.buffer[line]
|
2009-07-06 02:22:46 -04:00
|
|
|
|
2009-07-09 01:50:48 -04:00
|
|
|
def select_range(self, r):
|
|
|
|
delta = r.end - r.start
|
|
|
|
lineno, col = r.start.line, r.start.col
|
|
|
|
|
|
|
|
vim.current.window.cursor = lineno + 1, col
|
|
|
|
|
|
|
|
if delta.col == 0:
|
|
|
|
if (col + delta.col) == 0:
|
|
|
|
vim.command(r'call feedkeys("\<Esc>i")')
|
|
|
|
else:
|
|
|
|
vim.command(r'call feedkeys("\<Esc>a")')
|
|
|
|
else:
|
|
|
|
# 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 col != 0 and vim.eval("mode()") == 'i':
|
|
|
|
move_one_right = "l"
|
|
|
|
else:
|
|
|
|
move_one_right = ""
|
|
|
|
|
|
|
|
if delta.col <= 1:
|
|
|
|
do_select = ""
|
|
|
|
else:
|
|
|
|
do_select = "%il" % (delta.col-1)
|
|
|
|
|
|
|
|
vim.command(r'call feedkeys("\<Esc>%sv%s\<c-g>")' %
|
|
|
|
(move_one_right, do_select))
|
|
|
|
|
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
def buf_changed(self):
|
|
|
|
return self._text_changed
|
|
|
|
buf_changed = property(buf_changed)
|
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
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-06 02:22:46 -04:00
|
|
|
self._vstate = VimState()
|
2009-07-09 02:23:39 -04:00
|
|
|
self._ctab = None
|
2009-07-04 06:45:35 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
self._expect_move_wo_change = False
|
|
|
|
|
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] = {}
|
2009-07-06 17:09:53 -04:00
|
|
|
for p in vim.eval("&runtimepath").split(',')[::-1]:
|
|
|
|
pattern = p + os.path.sep + "PySnippets" + os.path.sep + \
|
|
|
|
"*%s.snippets" % ft
|
|
|
|
|
|
|
|
for fn in glob.glob(pattern):
|
2009-07-04 06:14:13 -04:00
|
|
|
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-07-09 01:55:25 -04:00
|
|
|
self._csnippet = None
|
2009-07-09 04:43:45 -04:00
|
|
|
self._ctab = None
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-05 16:25:01 -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-07-09 02:23:39 -04:00
|
|
|
self._ctab = None
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = False
|
2009-07-04 06:14:13 -04:00
|
|
|
|
2009-07-09 01:55:25 -04:00
|
|
|
if self._csnippet:
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = True
|
2009-07-09 02:23:39 -04:00
|
|
|
self._ctab = self._csnippet.select_next_tab(backwards)
|
|
|
|
if self._ctab is None:
|
2009-07-09 01:55:25 -04:00
|
|
|
self._csnippet = None
|
2009-07-04 11:36:06 -04:00
|
|
|
|
|
|
|
return True
|
2009-07-09 01:50:48 -04:00
|
|
|
else:
|
2009-07-09 02:23:39 -04:00
|
|
|
self._vstate.select_range(self._ctab.abs_range)
|
2009-07-04 06:45:35 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-07-04 10:08:14 -04:00
|
|
|
return True
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
lineno,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
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = True
|
2009-07-09 07:49:15 -04:00
|
|
|
|
|
|
|
text_before = before.rstrip()[:-len(word)]
|
|
|
|
self._vb = VimBuffer(text_before, after)
|
|
|
|
|
|
|
|
start = Position(lineno-1, len(text_before))
|
|
|
|
self._csnippet = snippet.launch(text_before, start)
|
|
|
|
|
|
|
|
self._vb.replace_lines(lineno-1, lineno-1, self._csnippet._current_text)
|
2009-07-09 02:23:39 -04:00
|
|
|
|
2009-07-09 01:50:48 -04:00
|
|
|
# TODO: this code is duplicated above
|
2009-07-09 07:49:15 -04:00
|
|
|
self._ctab = self._csnippet.select_next_tab()
|
|
|
|
if self._ctab is not None:
|
|
|
|
self._vstate.select_range(self._ctab.abs_range)
|
2009-07-04 06:45:35 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
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-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-07-05 16:25:01 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
if not self._vstate.buf_changed and not self._expect_move_wo_change:
|
2009-07-05 16:25:01 -04:00
|
|
|
# Cursor moved without input.
|
2009-07-09 02:23:39 -04:00
|
|
|
self._ctab = None
|
2009-07-06 04:40:07 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
# Did we leave the snippet with this movement?
|
2009-07-09 07:49:15 -04:00
|
|
|
if self._csnippet and not \
|
|
|
|
(self._vstate.pos in self._csnippet.abs_range):
|
|
|
|
self.reset()
|
2009-07-06 04:40:07 -04:00
|
|
|
|
2009-07-09 02:23:39 -04:00
|
|
|
if not self._ctab:
|
2009-07-06 04:31:12 -04:00
|
|
|
return
|
|
|
|
|
2009-07-09 04:43:45 -04:00
|
|
|
if self._vstate.buf_changed and self._ctab:
|
2009-07-09 07:49:15 -04:00
|
|
|
sline = self._csnippet.abs_start.line
|
|
|
|
dlines = self._csnippet.end.line - self._csnippet.start.line
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
if 0 <= self._vstate.moved.line <= 1:
|
2009-07-02 05:58:46 -04:00
|
|
|
# Detect a carriage return
|
2009-07-09 04:43:45 -04:00
|
|
|
if self._vstate.moved.col <= 0 and self._vstate.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
|
2009-07-06 02:22:46 -04:00
|
|
|
del vim.current.buffer[self._vstate.pos.line-1]
|
2009-07-09 04:20:25 -04:00
|
|
|
self._csnippet.chars_entered('\n')
|
2009-07-06 02:22:46 -04:00
|
|
|
elif self._vstate.moved.col < 0: # Some deleting was going on
|
2009-07-09 04:20:25 -04:00
|
|
|
self._csnippet.backspace(-self._vstate.moved.col)
|
2009-07-01 09:20:40 -04:00
|
|
|
else:
|
|
|
|
line = vim.current.line
|
2009-07-04 09:01:19 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
chars = line[self._vstate.pos.col - self._vstate.moved.col:
|
|
|
|
self._vstate.pos.col]
|
2009-07-09 04:20:25 -04:00
|
|
|
self._csnippet.chars_entered(chars)
|
|
|
|
|
2009-07-09 07:49:15 -04:00
|
|
|
# Replace
|
|
|
|
self._vb.replace_lines(sline, sline + dlines,
|
|
|
|
self._csnippet._current_text)
|
|
|
|
|
2009-07-09 04:20:25 -04:00
|
|
|
ct_end = self._ctab.abs_end
|
|
|
|
vim.current.window.cursor = ct_end.line +1, ct_end.col
|
|
|
|
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = False
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def entered_insert_mode(self):
|
2009-07-06 10:55:48 -04:00
|
|
|
self._vstate.update()
|
2009-07-09 01:55:25 -04:00
|
|
|
if self._csnippet and self._vstate.has_moved:
|
2009-07-09 04:43:45 -04:00
|
|
|
self.reset()
|
2009-07-04 11:36:06 -04:00
|
|
|
|
2009-07-09 07:53:49 -04:00
|
|
|
# TODO
|
|
|
|
# def self._chars_entered(self, chars):
|
|
|
|
# self._ctab.current_text += chars
|
|
|
|
#
|
|
|
|
# self._csnippet.update()
|
|
|
|
|
2009-07-06 12:34:12 -04:00
|
|
|
def backspace(self):
|
2009-07-06 11:44:04 -04:00
|
|
|
# BS was called in select mode
|
2009-07-06 12:34:12 -04:00
|
|
|
|
2009-07-09 01:55:25 -04:00
|
|
|
if self._csnippet and self._csnippet.tab_selected:
|
2009-07-06 11:44:04 -04:00
|
|
|
# This only happens when a default value is delted using backspace
|
2009-07-09 07:49:15 -04:00
|
|
|
old_range = self._csnippet.abs_range
|
2009-07-06 11:44:04 -04:00
|
|
|
vim.command(r'call feedkeys("i")')
|
2009-07-09 04:20:25 -04:00
|
|
|
self._csnippet.chars_entered('')
|
2009-07-09 07:49:15 -04:00
|
|
|
|
|
|
|
# TODOCode is duplicated aboce
|
|
|
|
# Replace
|
|
|
|
self._vb.replace_lines(old_range.start.line, old_range.end.line,
|
|
|
|
self._csnippet._current_text)
|
|
|
|
ct_end = self._ctab.abs_end
|
|
|
|
vim.current.window.cursor = ct_end.line +1, ct_end.col
|
|
|
|
|
2009-07-06 11:44:04 -04:00
|
|
|
self._vstate.update()
|
2009-07-09 07:49:15 -04:00
|
|
|
|
2009-07-06 11:44:04 -04:00
|
|
|
else:
|
|
|
|
vim.command(r'call feedkeys("\<BS>")')
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
PySnipSnippets = SnippetManager()
|
2009-07-01 14:14:54 -04:00
|
|
|
|