From bf35ed08fbb45c48ff753d681874990a0573630e Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Sat, 4 Jul 2009 16:08:14 +0200 Subject: [PATCH] Added support for proper startup and support for working with supertab --- PySnippets/all.snippets | 11 ++++-- PySnippets/python.snippets | 12 +++---- PySnipEmu.py => plugin/PySnipEmu.py | 13 +++++--- plugin/PySnipEmu.vim | 52 +++++++++++++++++++++++++---- test.py | 31 ++++++++++++----- 5 files changed, 88 insertions(+), 31 deletions(-) rename PySnipEmu.py => plugin/PySnipEmu.py (99%) diff --git a/PySnippets/all.snippets b/PySnippets/all.snippets index 73cfa05..5fb89a7 100644 --- a/PySnippets/all.snippets +++ b/PySnippets/all.snippets @@ -3,7 +3,12 @@ snippet hallo "This is my cool description" Hallo Welt endsnippet -snippet db -debug(${1/ba/arg, /} ${1:args}) +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 - diff --git a/PySnippets/python.snippets b/PySnippets/python.snippets index f55e4e6..c291e51 100644 --- a/PySnippets/python.snippets +++ b/PySnippets/python.snippets @@ -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 + diff --git a/PySnipEmu.py b/plugin/PySnipEmu.py similarity index 99% rename from PySnipEmu.py rename to plugin/PySnipEmu.py index 1c8f129..2563df3 100644 --- a/PySnipEmu.py +++ b/plugin/PySnipEmu.py @@ -497,7 +497,7 @@ class SnippetInstance(TextObject): cts = self._tabstops[self._cts] debug("cts: %s" % cts) - + cursor = self.start + cts.end if cts.end.line != 0: cursor.col -= self.start.col @@ -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() diff --git a/plugin/PySnipEmu.vim b/plugin/PySnipEmu.vim index 1c072ee..2e931da 100644 --- a/plugin/PySnipEmu.vim +++ b/plugin/PySnipEmu.vim @@ -1,10 +1,35 @@ + +"" FUNCTIONS + function! PyVimSnips_ExpandSnippet() + if exists('g:SuperTabMappingForward') + if g:SuperTabMappingForward == "" + let SuperTabKey = "\" + elseif g:SuperTabMappingBackward == "" + let SuperTabKey = "\" + endif + endif + + if pumvisible() " Update snippet if completion is used, or deal with supertab + if exists('SuperTabKey') + call feedkeys(SuperTabKey) | return '' + endif + call feedkeys("\a", 'n') " Close completion menu + call feedkeys("\") | 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 "" + + return "" endfunction function! PyVimSnips_JumpBackwards() @@ -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 =PyVimSnips_ExpandSnippet() snoremap :call PyVimSnips_ExpandSnippet() -inoremap + =PyVimSnips_JumpBackwards() -snoremap + :call PyVimSnips_JumpBackwards() +inoremap =PyVimSnips_JumpBackwards() +snoremap + :call PyVimSnips_JumpBackwards() + +" TODO: the next two are only needed for testing au CursorMovedI * py PySnipSnippets.cursor_moved() au InsertEnter * py PySnipSnippets.entered_insert_mode() diff --git a/test.py b/test.py index cfedc02..c3b3af0 100755 --- a/test.py +++ b/test.py @@ -7,19 +7,26 @@ import tempfile import unittest import time +def send(s,session): + os.system("screen -x %s -X stuff '%s'" % (session,s)) + +def type(str, session): + """ + Send the keystrokes to vim via screen. Pause after each char, so + vim can handle this + """ + for c in str: + send(c, session) + 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(self,s): + send(s, self.session) - def type(self, str): - """ - Send the keystrokes to vim via screen. Pause after each char, so - vim can handle this - """ - for c in str: - self.send(c) + 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 + =PyVimSnips_JumpBackwards()\n", options.session) + send(":snoremap + :call PyVimSnips_JumpBackwards()\n", + options.session) + + # Inform all test case which screen session to use suite = unittest.TestSuite() for s in all_test_suites: