All tests now pass on python2.3. Autodetection of test cases no longer works :(
This commit is contained in:
parent
c86595aa28
commit
a60e392a66
123
PySnipEmu.py
123
PySnipEmu.py
@ -3,62 +3,129 @@
|
||||
|
||||
import vim
|
||||
import string
|
||||
import re
|
||||
|
||||
class TabStop(object):
|
||||
def __init__(self, no, idx, span, default_text = ""):
|
||||
self._no = no
|
||||
self._default_text = default_text
|
||||
self._span = span
|
||||
self._lineidx = idx
|
||||
|
||||
def line_idx(self):
|
||||
return self._lineidx
|
||||
line_idx = property(line_idx)
|
||||
|
||||
def span(self):
|
||||
return self._span
|
||||
span = property(span)
|
||||
|
||||
def default_text(self):
|
||||
return self._default_text
|
||||
default_text = property(default_text)
|
||||
|
||||
def number(self):
|
||||
return self._no
|
||||
number = property(number)
|
||||
|
||||
class Snippet(object):
|
||||
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*)})|(\d+))')
|
||||
|
||||
def __init__(self,trigger,value):
|
||||
self._t = trigger
|
||||
self._v = value
|
||||
|
||||
@property
|
||||
|
||||
def trigger(self):
|
||||
return self._t
|
||||
|
||||
def _replace_tabstops(self):
|
||||
ts = None
|
||||
|
||||
lines = self._v.split('\n')
|
||||
trigger = property(trigger)
|
||||
|
||||
def _find_text_tabstops(self, lines):
|
||||
tabstops = []
|
||||
|
||||
for idx in range(len(lines)):
|
||||
l = lines[idx]
|
||||
|
||||
fidx = l.find("$0")
|
||||
if fidx != -1:
|
||||
ts = idx,fidx
|
||||
lines[idx] = l[:idx] + l[idx+2:]
|
||||
return ts,lines
|
||||
line = lines[idx]
|
||||
m = self._TB_EXPR.search(line)
|
||||
while m is not None:
|
||||
if m.group(1):
|
||||
no = int(m.group(1))
|
||||
def_text = m.group(2)
|
||||
else:
|
||||
no = int(m.group(3))
|
||||
def_text = ""
|
||||
|
||||
|
||||
start, end = m.span()
|
||||
line = line[:start] + def_text + line[end:]
|
||||
|
||||
ts = TabStop(no, idx, (start,start+len(def_text)), def_text)
|
||||
|
||||
lines[idx] = line
|
||||
|
||||
tabstops.append( (ts.number, ts) )
|
||||
|
||||
m = self._TB_EXPR.search(line)
|
||||
|
||||
tabstops.sort()
|
||||
|
||||
return tabstops
|
||||
|
||||
def _replace_tabstops(self):
|
||||
lines = self._v.split('\n')
|
||||
|
||||
ts = self._find_text_tabstops(lines)
|
||||
|
||||
return ts, lines
|
||||
|
||||
def put(self, before, after):
|
||||
lineno,col = vim.current.window.cursor
|
||||
|
||||
|
||||
col -= len(self._t)
|
||||
|
||||
endtab,lines = self._replace_tabstops()
|
||||
|
||||
if endtab is not None:
|
||||
lineno = lineno + endtab[0]
|
||||
col = col + endtab[1]
|
||||
ts,lines = self._replace_tabstops()
|
||||
|
||||
endcol = None
|
||||
if len(ts):
|
||||
zts = ts[0][1]
|
||||
newline = lineno + zts.line_idx
|
||||
if newline == lineno:
|
||||
newcol = col + zts.span[0]
|
||||
endcol = col + zts.span[1]
|
||||
else:
|
||||
newcol = zts.span[0]
|
||||
endcol = zts.span[1]
|
||||
else:
|
||||
col = col + len(lines[-1])
|
||||
|
||||
newline = lineno + len(lines) - 1
|
||||
if len(lines) == 1:
|
||||
newcol = col + len(lines[-1])
|
||||
else:
|
||||
newcol = len(lines[-1])
|
||||
|
||||
|
||||
lines[0] = before + lines[0]
|
||||
lines[-1] += after
|
||||
|
||||
vim.current.buffer[lineno-1:lineno-1+len(lines)] = lines
|
||||
vim.current.window.cursor = lineno, col
|
||||
|
||||
|
||||
vim.current.window.cursor = newline, newcol
|
||||
|
||||
# if endcol:
|
||||
# # Select the word
|
||||
# vim.command("insert PyVimSnips_SelectWord(%i)" % (endcol-newcol))
|
||||
|
||||
|
||||
class SnippetManager(object):
|
||||
def __init__(self):
|
||||
self.clear_snippets()
|
||||
|
||||
|
||||
def add_snippet(self,trigger,value):
|
||||
self._snippets[trigger] = Snippet(trigger,value)
|
||||
|
||||
|
||||
def clear_snippets(self):
|
||||
self._snippets = {}
|
||||
|
||||
|
||||
def try_expand(self):
|
||||
line = vim.current.line
|
||||
|
||||
|
||||
dummy,col = vim.current.window.cursor
|
||||
|
||||
if col > 0 and line[col-1] in string.whitespace:
|
||||
|
@ -10,6 +10,11 @@ EOF
|
||||
return ""
|
||||
endfunction
|
||||
|
||||
|
||||
function! PyVimSnips_SelectWord(len)
|
||||
return "\<esc>".'v'.a:len."l\<c-g>"
|
||||
endf
|
||||
|
||||
" Run the unit test suite that comes
|
||||
" with the application
|
||||
function! PyVimSnips_RunTests()
|
||||
@ -20,9 +25,11 @@ python from PySnipEmu import PySnipSnippets
|
||||
|
||||
inoremap <Tab> <C-R>=PyVimSnips_ExpandSnippet()<cr>
|
||||
|
||||
python PySnipSnippets.add_snippet("hello", "Hello World!")
|
||||
python PySnipSnippets.add_snippet("echo", "$0 run")
|
||||
|
||||
python PySnipSnippets.add_snippet("hello", "Hallo Welt!\nUnd Wie gehts?")
|
||||
python PySnipSnippets.add_snippet("echo","$0 run")
|
||||
|
||||
|
||||
python PySnipSnippets.add_snippet("if", "if(${1:/* condition */})\n{\n${0:/* code */}\n}")
|
||||
|
||||
|
||||
|
||||
|
43
test.py
43
test.py
@ -93,11 +93,22 @@ class MultilineExpand_ExceptCorrectResult(_VimTest):
|
||||
|
||||
def runTest(self):
|
||||
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
|
||||
class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
|
||||
def cmd(self):
|
||||
PySnipSnippets.add_snippet("hallo","Hallo Welt!\nUnd Wie gehts?")
|
||||
self.insert("Wie hallo gehts?")
|
||||
vim.command("normal 02f ")
|
||||
self.expand()
|
||||
self.insert("Huiui!")
|
||||
|
||||
def runTest(self):
|
||||
self.assertEqual(self.output,
|
||||
"Wie Hallo Welt!\nUnd Wie gehts?Huiui! gehts?")
|
||||
|
||||
############
|
||||
# TabStops #
|
||||
############
|
||||
class ExitTabStop_ExceptCorrectTrue(_VimTest):
|
||||
class ExitTabStop_ExceptCorrectResult(_VimTest):
|
||||
def cmd(self):
|
||||
PySnipSnippets.add_snippet("echo","$0 run")
|
||||
self.insert("echo ")
|
||||
@ -106,6 +117,15 @@ class ExitTabStop_ExceptCorrectTrue(_VimTest):
|
||||
|
||||
def runTest(self):
|
||||
self.assertEqual(self.output,"test run ")
|
||||
|
||||
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
|
||||
def cmd(self):
|
||||
PySnipSnippets.add_snippet("echo","echo ${1:Hallo}")
|
||||
self.insert("echo ")
|
||||
self.expand()
|
||||
|
||||
def runTest(self):
|
||||
self.assertEqual(self.output,"echo Hallo ")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@ -113,12 +133,25 @@ if __name__ == '__main__':
|
||||
from cStringIO import StringIO
|
||||
|
||||
s = StringIO()
|
||||
|
||||
suite = unittest.TestLoader().loadTestsFromModule(__import__("test"))
|
||||
|
||||
tests = [
|
||||
SimpleExpand_ExceptCorrectResult(),
|
||||
SimpleExpandTypeAfterExpand_ExceptCorrectResult(),
|
||||
SimpleExpandTypeAfterExpand1_ExceptCorrectResult(),
|
||||
DoNotExpandAfterSpace_ExceptCorrectResult(),
|
||||
ExpandInTheMiddleOfLine_ExceptCorrectResult(),
|
||||
MultilineExpand_ExceptCorrectResult(),
|
||||
MultilineExpandTestTyping_ExceptCorrectResult(),
|
||||
ExitTabStop_ExceptCorrectResult(),
|
||||
TextTabStopNoReplace_ExceptCorrectResult(),
|
||||
]
|
||||
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTests(tests)
|
||||
res = unittest.TextTestRunner(stream=s).run(suite)
|
||||
|
||||
if res.wasSuccessful():
|
||||
vim.command("qa!")
|
||||
# if res.wasSuccessful():
|
||||
# vim.command("qa!")
|
||||
|
||||
vim.current.buffer[:] = s.getvalue().split('\n')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user