Initial commit

This commit is contained in:
Holger Rapp 2009-06-23 14:45:04 +02:00
commit c86595aa28
3 changed files with 228 additions and 0 deletions

75
PySnipEmu.py Normal file
View File

@ -0,0 +1,75 @@
#!/usr/bin/env python
# encoding: utf-8
import vim
import string
class Snippet(object):
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')
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
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]
else:
col = col + 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
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:
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:
self._snippets[word].put(before.rstrip()[:-len(word)], after)
PySnipSnippets = SnippetManager()

29
PySnipEmu.vim Normal file
View File

@ -0,0 +1,29 @@
pyfile PySnipEmu.py
function! PyVimSnips_ExpandSnippet()
py << EOF
from PySnipEmu import PySnipSnippets
PySnipSnippets.try_expand()
EOF
return ""
endfunction
" Run the unit test suite that comes
" with the application
function! PyVimSnips_RunTests()
pyfile test.py
endfunction
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")

124
test.py Normal file
View File

@ -0,0 +1,124 @@
#!/usr/bin/env python
# encoding: utf-8
#
import vim
import unittest
from PySnipEmu import PySnipSnippets
class _VimTest(unittest.TestCase):
def setUp(self):
vim.command(":new")
try:
self.cmd()
self.output = '\n'.join(vim.current.buffer[:])
finally:
vim.command(":q!")
def insert(self,string):
"""A helper function to type some text"""
vim.command('normal i%s' % string)
def expand(self):
vim.command("call PyVimSnips_ExpandSnippet()")
def tearDown(self):
PySnipSnippets.clear_snippets()
def cmd(self):
"""Overwrite these in the children"""
pass
##################
# Simple Expands #
##################
class _SimpleExpands(_VimTest):
def setUp(self):
PySnipSnippets.add_snippet("hallo","Hallo Welt!")
_VimTest.setUp(self)
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"Hallo Welt! ")
class SimpleExpandTypeAfterExpand_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
self.insert("and again")
def runTest(self):
self.assertEqual(self.output,"Hallo Welt!and again ")
class SimpleExpandTypeAfterExpand1_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("na du hallo ")
self.expand()
self.insert("and again")
def runTest(self):
self.assertEqual(self.output,"na du Hallo Welt!and again ")
class DoNotExpandAfterSpace_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"hallo ")
class ExpandInTheMiddleOfLine_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("Wie hallo gehts?")
vim.command("normal 02f ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
class MultilineExpand_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()
def runTest(self):
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
############
# TabStops #
############
class ExitTabStop_ExceptCorrectTrue(_VimTest):
def cmd(self):
PySnipSnippets.add_snippet("echo","$0 run")
self.insert("echo ")
self.expand()
self.insert("test")
def runTest(self):
self.assertEqual(self.output,"test run ")
if __name__ == '__main__':
import sys
from cStringIO import StringIO
s = StringIO()
suite = unittest.TestLoader().loadTestsFromModule(__import__("test"))
res = unittest.TextTestRunner(stream=s).run(suite)
if res.wasSuccessful():
vim.command("qa!")
vim.current.buffer[:] = s.getvalue().split('\n')