Added support for proper startup and support for working with supertab

This commit is contained in:
Holger Rapp 2009-07-04 16:08:14 +02:00
parent e88378c10b
commit bf35ed08fb
5 changed files with 88 additions and 31 deletions

View File

@ -3,7 +3,12 @@ snippet hallo "This is my cool description"
Hallo Welt
endsnippet
snippet db
debug(${1/ba/arg, /} ${1:args})
endsnippet
snippet class
class ${1:MyClass}(${2:object}):
def __init__( self${4/([^,])?(.*)/(?1:, )/}${4:arg} ):
"""
TODO: Fill me in
"""
${2/object$|(.+)/(?1:\t\t$0.__init__\(self\)\n\n)/}${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s*|$)/(?2:\t\tself._$2 = $2\n)/g} $0
endsnippet

View File

@ -1,9 +1,5 @@
snippet cls
class ${1:MyClass}(${2:object}):
def __init__( self${4/([^,])?(.*)/(?1:, )/}${4:arg} ):
"""
TODO: Fill me in
"""
${2/object$|(.+)/(?1:\t\t$0.__init__\(self\)\n\n)/}${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s*|$)/(?2:\t\tself._$2 = $2\n)/g} $0
snippet db
debug("${1/(\w+)(, *)?/$1: %s(?2:, )/g}" % (${1:args}))
$0
endsnippet

View File

@ -685,18 +685,19 @@ class SnippetManager(object):
cs = self._current_snippets[-1]
if not cs.select_next_tab(backwards):
self._current_snippets.pop()
return False
self._cursor.update_position()
return
return True
dummy,col = vim.current.window.cursor
if col == 0:
return
return False
line = vim.current.line
if col > 0 and line[col-1] in string.whitespace:
return
return False
# Get the word to the left of the current edit position
before,after = line[:col], line[col:]
@ -710,7 +711,7 @@ class SnippetManager(object):
if snippet is None:
# No snippet found
return
return False
s = snippet.launch(before.rstrip()[:-len(word)], after)
@ -718,6 +719,8 @@ class SnippetManager(object):
if s is not None:
self._current_snippets.append(s)
return True
def cursor_moved(self):
self._cursor.update_position()

View File

@ -1,7 +1,32 @@
"" FUNCTIONS
function! PyVimSnips_ExpandSnippet()
if exists('g:SuperTabMappingForward')
if g:SuperTabMappingForward == "<s-tab>"
let SuperTabKey = "\<c-n>"
elseif g:SuperTabMappingBackward == "<s-tab>"
let SuperTabKey = "\<c-p>"
endif
endif
if pumvisible() " Update snippet if completion is used, or deal with supertab
if exists('SuperTabKey')
call feedkeys(SuperTabKey) | return ''
endif
call feedkeys("\<esc>a", 'n') " Close completion menu
call feedkeys("\<tab>") | return ''
endif
" Now, really expand something
py << EOF
from PySnipEmu import PySnipSnippets
PySnipSnippets.try_expand()
import vim
if not PySnipSnippets.try_expand():
vim.command("""if exists('SuperTabKey')
call feedkeys(SuperTabKey)
endif
""")
EOF
return ""
@ -16,13 +41,28 @@ EOF
endfunction
"" STARTUP CODE
" Expand our path
python from PySnipEmu import PySnipSnippets
python << EOF
import vim, os, sys
for p in vim.eval("&runtimepath").split(','):
dname = p + os.path.sep + "plugin"
if os.path.exists(dname + os.path.sep + "PySnipEmu.py"):
if dname not in sys.path:
sys.path.append(dname)
break
from PySnipEmu import PySnipSnippets
EOF
inoremap <Tab> <C-R>=PyVimSnips_ExpandSnippet()<cr>
snoremap <Tab> <Esc>:call PyVimSnips_ExpandSnippet()<cr>
inoremap + <C-R>=PyVimSnips_JumpBackwards()<cr>
snoremap + <Esc>:call PyVimSnips_JumpBackwards()<cr>
inoremap <S-Tab> <C-R>=PyVimSnips_JumpBackwards()<cr>
snoremap <S-Tab>+ <Esc>:call PyVimSnips_JumpBackwards()<cr>
" TODO: the next two are only needed for testing
au CursorMovedI * py PySnipSnippets.cursor_moved()
au InsertEnter * py PySnipSnippets.entered_insert_mode()

27
test.py
View File

@ -7,19 +7,26 @@ import tempfile
import unittest
import time
class _VimTest(unittest.TestCase):
text_before = " --- some text before --- "
text_after = " --- some text after --- "
def send(self, s):
os.system("screen -x %s -X stuff '%s'" % (self.session,s))
def send(s,session):
os.system("screen -x %s -X stuff '%s'" % (session,s))
def type(self, str):
def type(str, session):
"""
Send the keystrokes to vim via screen. Pause after each char, so
vim can handle this
"""
for c in str:
self.send(c)
send(c, session)
class _VimTest(unittest.TestCase):
text_before = " --- some text before --- "
text_after = " --- some text after --- "
def send(self,s):
send(s, self.session)
def type(self,s):
type(s, self.session)
def check_output(self):
wanted = self.text_before + '\n\n' + self.wanted + \
@ -595,6 +602,12 @@ if __name__ == '__main__':
test_loader = unittest.TestLoader()
all_test_suites = test_loader.loadTestsFromModule(__import__("test"))
# Send some mappings to vim
send(":inoremap + <C-R>=PyVimSnips_JumpBackwards()<cr>\n", options.session)
send(":snoremap + <Esc>:call PyVimSnips_JumpBackwards()<cr>\n",
options.session)
# Inform all test case which screen session to use
suite = unittest.TestSuite()
for s in all_test_suites: