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 vim
|
||||||
import string
|
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):
|
class Snippet(object):
|
||||||
|
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*)})|(\d+))')
|
||||||
|
|
||||||
def __init__(self,trigger,value):
|
def __init__(self,trigger,value):
|
||||||
self._t = trigger
|
self._t = trigger
|
||||||
self._v = value
|
self._v = value
|
||||||
|
|
||||||
@property
|
|
||||||
def trigger(self):
|
def trigger(self):
|
||||||
return self._t
|
return self._t
|
||||||
|
trigger = property(trigger)
|
||||||
def _replace_tabstops(self):
|
|
||||||
ts = None
|
def _find_text_tabstops(self, lines):
|
||||||
|
tabstops = []
|
||||||
lines = self._v.split('\n')
|
|
||||||
for idx in range(len(lines)):
|
for idx in range(len(lines)):
|
||||||
l = lines[idx]
|
line = lines[idx]
|
||||||
|
m = self._TB_EXPR.search(line)
|
||||||
fidx = l.find("$0")
|
while m is not None:
|
||||||
if fidx != -1:
|
if m.group(1):
|
||||||
ts = idx,fidx
|
no = int(m.group(1))
|
||||||
lines[idx] = l[:idx] + l[idx+2:]
|
def_text = m.group(2)
|
||||||
return ts,lines
|
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):
|
def put(self, before, after):
|
||||||
lineno,col = vim.current.window.cursor
|
lineno,col = vim.current.window.cursor
|
||||||
|
|
||||||
col -= len(self._t)
|
col -= len(self._t)
|
||||||
|
|
||||||
endtab,lines = self._replace_tabstops()
|
ts,lines = self._replace_tabstops()
|
||||||
|
|
||||||
if endtab is not None:
|
endcol = None
|
||||||
lineno = lineno + endtab[0]
|
if len(ts):
|
||||||
col = col + endtab[1]
|
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:
|
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[0] = before + lines[0]
|
||||||
lines[-1] += after
|
lines[-1] += after
|
||||||
|
|
||||||
vim.current.buffer[lineno-1:lineno-1+len(lines)] = lines
|
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):
|
class SnippetManager(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.clear_snippets()
|
self.clear_snippets()
|
||||||
|
|
||||||
def add_snippet(self,trigger,value):
|
def add_snippet(self,trigger,value):
|
||||||
self._snippets[trigger] = Snippet(trigger,value)
|
self._snippets[trigger] = Snippet(trigger,value)
|
||||||
|
|
||||||
def clear_snippets(self):
|
def clear_snippets(self):
|
||||||
self._snippets = {}
|
self._snippets = {}
|
||||||
|
|
||||||
def try_expand(self):
|
def try_expand(self):
|
||||||
line = vim.current.line
|
line = vim.current.line
|
||||||
|
|
||||||
dummy,col = vim.current.window.cursor
|
dummy,col = vim.current.window.cursor
|
||||||
|
|
||||||
if col > 0 and line[col-1] in string.whitespace:
|
if col > 0 and line[col-1] in string.whitespace:
|
||||||
|
@ -10,6 +10,11 @@ EOF
|
|||||||
return ""
|
return ""
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PyVimSnips_SelectWord(len)
|
||||||
|
return "\<esc>".'v'.a:len."l\<c-g>"
|
||||||
|
endf
|
||||||
|
|
||||||
" Run the unit test suite that comes
|
" Run the unit test suite that comes
|
||||||
" with the application
|
" with the application
|
||||||
function! PyVimSnips_RunTests()
|
function! PyVimSnips_RunTests()
|
||||||
@ -20,9 +25,11 @@ python from PySnipEmu import PySnipSnippets
|
|||||||
|
|
||||||
inoremap <Tab> <C-R>=PyVimSnips_ExpandSnippet()<cr>
|
inoremap <Tab> <C-R>=PyVimSnips_ExpandSnippet()<cr>
|
||||||
|
|
||||||
python PySnipSnippets.add_snippet("hello", "Hello World!")
|
python PySnipSnippets.add_snippet("hello", "Hallo Welt!\nUnd Wie gehts?")
|
||||||
python PySnipSnippets.add_snippet("echo", "$0 run")
|
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):
|
def runTest(self):
|
||||||
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
|
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 #
|
# TabStops #
|
||||||
############
|
############
|
||||||
class ExitTabStop_ExceptCorrectTrue(_VimTest):
|
class ExitTabStop_ExceptCorrectResult(_VimTest):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
PySnipSnippets.add_snippet("echo","$0 run")
|
PySnipSnippets.add_snippet("echo","$0 run")
|
||||||
self.insert("echo ")
|
self.insert("echo ")
|
||||||
@ -106,6 +117,15 @@ class ExitTabStop_ExceptCorrectTrue(_VimTest):
|
|||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"test run ")
|
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__':
|
if __name__ == '__main__':
|
||||||
@ -113,12 +133,25 @@ if __name__ == '__main__':
|
|||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
s = 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)
|
res = unittest.TextTestRunner(stream=s).run(suite)
|
||||||
|
|
||||||
if res.wasSuccessful():
|
# if res.wasSuccessful():
|
||||||
vim.command("qa!")
|
# vim.command("qa!")
|
||||||
|
|
||||||
vim.current.buffer[:] = s.getvalue().split('\n')
|
vim.current.buffer[:] = s.getvalue().split('\n')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user