From 1653eb89ea29ab0aaf3674dc086b587080d2dc4a Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Tue, 10 Jan 2012 14:16:25 +0100 Subject: [PATCH] UltiSnips loads in python3 now, but does not work so far --- plugin/UltiSnips.vim | 119 ++++++++++++++++++++++++++++---- plugin/UltiSnips/Lexer.py | 2 +- plugin/UltiSnips/TextObjects.py | 4 +- plugin/UltiSnips/Util.py | 27 +++++--- plugin/UltiSnips/__init__.py | 12 ++-- test.py | 62 +++++++++-------- 6 files changed, 164 insertions(+), 62 deletions(-) diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 562052a..4da889d 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -8,10 +8,17 @@ " directory above this file. " }}} -if exists('did_UltiSnips_vim') || &cp || version < 700 || !has("python") +if exists('did_UltiSnips_vim') || &cp || version < 700 finish endif +if !has("python3") + if !has("python") + echo "UltiSnips requires py >= 2.5 or any py3" + finish + endif +endif + " Global Variables {{{ " The trigger used to expand a snippet. @@ -65,10 +72,18 @@ function! UltiSnipsEdit(...) if a:0 == 1 && a:1 != '' let type = a:1 else - python vim.command("let type = '%s'" % UltiSnips_Manager.filetype) + if has("python3") + python3 vim.command("let type = '%s'" % UltiSnips_Manager.filetype) + else + python vim.command("let type = '%s'" % UltiSnips_Manager.filetype) + endif endif - python vim.command("let file = '%s'" % UltiSnips_Manager.file_to_edit(vim.eval("type"))) + if has("python3") + python3 vim.command("let file = '%s'" % UltiSnips_Manager.file_to_edit(vim.eval("type"))) + else + python vim.command("let file = '%s'" % UltiSnips_Manager.file_to_edit(vim.eval("type"))) + endif let mode = 'e' if exists('g:UltiSnipsEditSplit') @@ -93,42 +108,67 @@ function! CompensateForPUM() """ to explicitly check for the presence of the popup menu, and update """ the vim-state accordingly. if pumvisible() - py UltiSnips_Manager.cursor_moved() + if has("python3") + python3 UltiSnips_Manager.cursor_moved() + else + python UltiSnips_Manager.cursor_moved() + endif endif endfunction function! UltiSnips_ExpandSnippet() - py UltiSnips_Manager.expand() + if has("python3") + python3 UltiSnips_Manager.expand() + else + python UltiSnips_Manager.expand() + endif return "" endfunction function! UltiSnips_ExpandSnippetOrJump() call CompensateForPUM() - py UltiSnips_Manager.expand_or_jump() + if has("python3") + python3 UltiSnips_Manager.expand_or_jump() + else + python UltiSnips_Manager.expand_or_jump() + endif return "" endfunction function! UltiSnips_ListSnippets() - py UltiSnips_Manager.list_snippets() + if has("python3") + python3 UltiSnips_Manager.list_snippets() + else + python UltiSnips_Manager.list_snippets() + endif return "" endfunction function! UltiSnips_JumpBackwards() call CompensateForPUM() - py UltiSnips_Manager.jump_backwards() + if has("python3") + python3 UltiSnips_Manager.jump_backwards() + else + python UltiSnips_Manager.jump_backwards() + endif return "" endfunction function! UltiSnips_JumpForwards() call CompensateForPUM() - py UltiSnips_Manager.jump_forwards() + if has("python3") + python3 UltiSnips_Manager.jump_forwards() + else + python UltiSnips_Manager.jump_forwards() + endif return "" endfunction function! UltiSnips_AddSnippet(trigger, value, descr, options, ...) " Takes the same arguments as SnippetManager.add_snippet: " (trigger, value, descr, options, ft = "all", globals = None) -py << EOB +if has("python3") +python3 << EOB args = vim.eval("a:000") trigger = vim.eval("a:trigger") value = vim.eval("a:value") @@ -137,17 +177,36 @@ options = vim.eval("a:options") UltiSnips_Manager.add_snippet(trigger, value, descr, options, *args) EOB +else +python << EOB +args = vim.eval("a:000") +trigger = vim.eval("a:trigger") +value = vim.eval("a:value") +descr = vim.eval("a:descr") +options = vim.eval("a:options") + +UltiSnips_Manager.add_snippet(trigger, value, descr, options, *args) +EOB +endif return "" endfunction function! UltiSnips_Anon(value, ...) " Takes the same arguments as SnippetManager.expand_anon: " (value, trigger="", descr="", options="", globals = None) -py << EOB +if has("python3") +python3 << EOB args = vim.eval("a:000") value = vim.eval("a:value") UltiSnips_Manager.expand_anon(value, *args) EOB +else +python << EOB +args = vim.eval("a:000") +value = vim.eval("a:value") +UltiSnips_Manager.expand_anon(value, *args) +EOB +endif return "" endfunction @@ -168,23 +227,52 @@ function! UltiSnips_MapKeys() exec "snoremap " . g:UltiSnipsListSnippets . " :call UltiSnips_ListSnippets()" " Do not remap this. - snoremap :py UltiSnips_Manager.backspace_while_selected() + if has("python3") + snoremap :python3 UltiSnips_Manager.backspace_while_selected() + else + snoremap :python UltiSnips_Manager.backspace_while_selected() + endif endf function! UltiSnips_CursorMoved() - py UltiSnips_Manager.cursor_moved() + if has("python3") + python3 UltiSnips_Manager.cursor_moved() + else + python UltiSnips_Manager.cursor_moved() + endif endf function! UltiSnips_EnteredInsertMode() - py UltiSnips_Manager.entered_insert_mode() + if has("python3") + python3 UltiSnips_Manager.entered_insert_mode() + else + python UltiSnips_Manager.entered_insert_mode() + endif endf function! UltiSnips_LeavingWindow() - py UltiSnips_Manager.leaving_window() + if has("python3") + python3 UltiSnips_Manager.leaving_window() + else + python UltiSnips_Manager.leaving_window() + endif endf " }}} "" STARTUP CODE {{{ " Expand our path +if has("python3") +python3 << EOF +import vim, os, sys + +new_path = vim.eval('expand(":h")') +sys.path.append(new_path) + +from UltiSnips import UltiSnips_Manager +UltiSnips_Manager.expand_trigger = vim.eval("g:UltiSnipsExpandTrigger") +UltiSnips_Manager.forward_trigger = vim.eval("g:UltiSnipsJumpForwardTrigger") +UltiSnips_Manager.backward_trigger = vim.eval("g:UltiSnipsJumpBackwardTrigger") +EOF +else python << EOF import vim, os, sys @@ -196,6 +284,7 @@ UltiSnips_Manager.expand_trigger = vim.eval("g:UltiSnipsExpandTrigger") UltiSnips_Manager.forward_trigger = vim.eval("g:UltiSnipsJumpForwardTrigger") UltiSnips_Manager.backward_trigger = vim.eval("g:UltiSnipsJumpBackwardTrigger") EOF +endif au CursorMovedI * call UltiSnips_CursorMoved() au InsertEnter * call UltiSnips_EnteredInsertMode() diff --git a/plugin/UltiSnips/Lexer.py b/plugin/UltiSnips/Lexer.py index ccd2755..99d1236 100644 --- a/plugin/UltiSnips/Lexer.py +++ b/plugin/UltiSnips/Lexer.py @@ -9,7 +9,7 @@ into Logical Units called Tokens. import string import re -from Geometry import Position +from .Geometry import Position __all__ = [ "tokenize", "EscapeCharToken", "TransformationToken", "TabStopToken", diff --git a/plugin/UltiSnips/TextObjects.py b/plugin/UltiSnips/TextObjects.py index 2bf807d..d0600c9 100644 --- a/plugin/UltiSnips/TextObjects.py +++ b/plugin/UltiSnips/TextObjects.py @@ -644,7 +644,7 @@ class PythonCode(TextObject): self._globals = {} globals = snippet.globals.get("!p", []) - exec "\n".join(globals).replace("\r\n", "\n") in self._globals + exec("\n".join(globals).replace("\r\n", "\n") in self._globals) # Add Some convenience to the code self._code = "import re, os, vim, string, random\n" + code @@ -672,7 +672,7 @@ class PythonCode(TextObject): }) self._code = self._code.replace("\r\n", "\n") - exec self._code in self._globals, local_d + exec(self._code in self._globals, local_d) if self._snip._rv_changed: self.current_text = self._snip.rv diff --git a/plugin/UltiSnips/Util.py b/plugin/UltiSnips/Util.py index c887d64..3bf3562 100644 --- a/plugin/UltiSnips/Util.py +++ b/plugin/UltiSnips/Util.py @@ -4,11 +4,20 @@ import os import types import vim +import sys -def as_utf8(s): - if not isinstance(s, types.UnicodeType): - s = s.decode("utf-8") - return s.encode("utf-8") +if sys.version_info > (2,8): + def as_utf8(s): + return s.encode("utf-8") + def as_unicode(s): + if isinstance(s, bytes): + return s.decode("utf-8") + return s +else: + def as_utf8(s): + if not isinstance(s, types.UnicodeType): + s = s.decode("utf-8") + return s.encode("utf-8") def vim_string(inp): """ Creates a vim-friendly string from a group of @@ -16,13 +25,13 @@ def vim_string(inp): """ def conv(obj): if isinstance(obj, list): - rv = u'[' + u','.join(conv(o) for o in obj) + u']' + rv = unicode('[' + ','.join(conv(o) for o in obj) + ']') elif isinstance(obj, dict): - rv = u'{' + u','.join([ - u"%s:%s" % (conv(key), conv(value)) - for key, value in obj.iteritems()]) + u'}' + rv = unicode('{' + ','.join([ + "%s:%s" % (conv(key), conv(value)) + for key, value in obj.iteritems()]) + '}') else: - rv = u'"%s"' % str(obj).decode("utf-8").replace(u'"', u'\\"') + rv = '"%s"' % str(obj).decode("utf-8").replace('"', '\\"') return rv return conv(inp).encode("utf-8") diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 7c19cb9..84d0425 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -14,7 +14,7 @@ import vim from UltiSnips.Geometry import Position from UltiSnips.TextObjects import * from UltiSnips.Buffer import VimBuffer -from UltiSnips.Util import IndentUtil, vim_string, as_utf8 +from UltiSnips.Util import IndentUtil, vim_string, as_unicode from UltiSnips.Langmap import LangMapTranslator # The following lines silence DeprecationWarnings. They are raised @@ -266,9 +266,9 @@ class Snippet(object): _TABS = re.compile(r"^\t*") def __init__(self, trigger, value, descr, options, globals): - self._t = as_utf8(trigger) - self._v = as_utf8(value) - self._d = as_utf8(descr) + self._t = as_unicode(trigger) + self._v = as_unicode(value) + self._d = as_unicode(descr) self._opts = options self._matched = "" self._last_re = None @@ -596,7 +596,7 @@ class VimState(object): "| redir END") # Check if any mappings where found - all_maps = filter(len, vim.eval(r"_tmp_smaps").splitlines()) + all_maps = list(filter(len, vim.eval(r"_tmp_smaps").splitlines())) if (len(all_maps) == 1 and all_maps[0][0] not in " sv"): # "No maps found". String could be localized. Hopefully # it doesn't start with any of these letters in any @@ -989,7 +989,7 @@ class SnippetManager(object): if rv > len(snippets): rv = len(snippets) return snippets[rv-1] - except vim.error, e: + except vim.error as e: if str(e) == 'invalid expression': return None raise diff --git a/test.py b/test.py index b4ad79f..e71a444 100755 --- a/test.py +++ b/test.py @@ -31,6 +31,7 @@ import unittest import time import re import platform +import sys WIN = platform.system() == "Windows" @@ -39,12 +40,12 @@ from textwrap import dedent # Some constants for better reading -BS = u'\x7f' -ESC = u'\x1b' -ARR_L = u'\x1bOD' -ARR_R = u'\x1bOC' -ARR_U = u'\x1bOA' -ARR_D = u'\x1bOB' +BS = '\x7f' +ESC = '\x1b' +ARR_L = '\x1bOD' +ARR_R = '\x1bOC' +ARR_U = '\x1bOA' +ARR_D = '\x1bOB' # multi-key sequences generating a single key press SEQUENCES = [ARR_L, ARR_R, ARR_U, ARR_D] @@ -82,26 +83,26 @@ def is_focused(title=None): BRACES = re.compile("([}{])") WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"] WIN_REPLACES = [ - (BS, u"{BS}"), - (ARR_L, u"{LEFT}"), - (ARR_R, u"{RIGHT}"), - (ARR_U, u"{UP}"), - (ARR_D, u"{DOWN}"), - ("\t", u"{TAB}"), - ("\n", u"~"), - (ESC, u"{ESC}"), + (BS, "{BS}"), + (ARR_L, "{LEFT}"), + (ARR_R, "{RIGHT}"), + (ARR_U, "{UP}"), + (ARR_D, "{DOWN}"), + ("\t", "{TAB}"), + ("\n", "~"), + (ESC, "{ESC}"), # On my system ` waits for a second keystroke, so `+SPACE = "`". On # most systems, `+Space = "` ". I work around this, by sending the host # ` as `+_+BS. Awkward, but the only way I found to get this working. - (u"`", u"`_{BS}"), - (u"´", u"´_{BS}"), - (u"{^}", u"{^}_{BS}"), + ("`", "`_{BS}"), + ("´", "´_{BS}"), + ("{^}", "{^}_{BS}"), ] def convert_keys(keys): - keys = BRACES.sub(ur"{\1}", keys) + keys = BRACES.sub(r"{\1}", keys) for k in WIN_ESCAPES: - keys = keys.replace(k, u"{%s}" % k) + keys = keys.replace(k, "{%s}" % k) for f, r in WIN_REPLACES: keys = keys.replace(f, r) return keys @@ -135,7 +136,7 @@ def send_win(keys, session): def send_screen(s,session): s = s.replace("'", r"'\''") - os.system((u"screen -x %s -X stuff '%s'" % (session, s)).encode("utf-8")) + os.system(("screen -x %s -X stuff '%s'" % (session, s)).encode("utf-8")) def send(s, session): @@ -176,7 +177,10 @@ class _VimTest(unittest.TestCase): send(s, self.session) def send_py(self,s): - self.send(":py << EOF\n%s\nEOF\n" % s) + if sys.version_info < (3,0): + self.send(":py << EOF\n%s\nEOF\n" % s) + else: + self.send(":py3 << EOF\n%s\nEOF\n" % s) def type(self,s): type(s, self.session, self.sleeptime) @@ -220,7 +224,7 @@ class _VimTest(unittest.TestCase): self.send(":silent! close\n") # Reset UltiSnips - self.send(":py UltiSnips_Manager.reset(test_error=True)\n") + self.send_py("UltiSnips_Manager.reset(test_error=True)") # Make it unlikely that we do not parse any shipped snippets self.send(":let g:UltiSnipsSnippetDirectories=['']\n") @@ -1387,7 +1391,7 @@ class Transformation_SimpleCaseTransformInFrontDefVal_ECR(_VimTest): keys = "test" + EX + "hallo foo" wanted = "hallo batzl hallo foo" class Transformation_MultipleTransformations_ECR(_VimTest): - snippets = ("test", "${1:Some Text}${1/.+/\U$0\E/}\n${1/.+/\L$0\E/}") + snippets = ("test", "${1:Some Text}${1/.+/\\U$0\E/}\n${1/.+/\L$0\E/}") keys = "test" + EX + "SomE tExt " wanted = "SomE tExt SOME TEXT \nsome text " class Transformation_TabIsAtEndAndDeleted_ECR(_VimTest): @@ -1426,7 +1430,7 @@ class Transformation_BackreferenceTwice_ExceptCorrectResult(_VimTest): wanted = "dead parrot this parrot is a bit dead" class Transformation_CleverTransformUpercaseChar_ExceptCorrectResult(_VimTest): - snippets = ("test", "$1 ${1/(.)/\u$1/}") + snippets = ("test", "$1 ${1/(.)/\\u$1/}") keys = "test" + EX + "hallo" wanted = "hallo Hallo" class Transformation_CleverTransformLowercaseChar_ExceptCorrectResult(_VimTest): @@ -1434,7 +1438,7 @@ class Transformation_CleverTransformLowercaseChar_ExceptCorrectResult(_VimTest): keys = "test" + EX + "Hallo" wanted = "Hallo hallo" class Transformation_CleverTransformLongUpper_ExceptCorrectResult(_VimTest): - snippets = ("test", "$1 ${1/(.*)/\U$1\E/}") + snippets = ("test", "$1 ${1/(.*)/\\U$1\E/}") keys = "test" + EX + "hallo" wanted = "hallo HALLO" class Transformation_CleverTransformLongLower_ExceptCorrectResult(_VimTest): @@ -1667,7 +1671,7 @@ class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand2(_VimTest): wanted = "-Expand me!" class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand3(_VimTest): snippets = (("test", "Expand me!", "", "i"), ) - keys = u"ßßtest" + EX + keys = "ßßtest" + EX wanted = "ßßExpand me!" class _SnippetOptions_ExpandWordSnippets(_VimTest): @@ -1913,13 +1917,13 @@ class Snippet_With_DoubleQuote(_VimTest): wanted = "Expand me\"!" class Snippet_With_Umlauts_List(_VimTest): - snippets = _snip_quote(u'ü') + snippets = _snip_quote('ü') keys = 'te' + LS + "2\n" wanted = "Expand meü!" class Snippet_With_Umlauts(_VimTest): - snippets = _snip_quote(u'ü') - keys = u'teüst' + EX + snippets = _snip_quote('ü') + keys = 'teüst' + EX wanted = "Expand meü!" class Snippet_With_DoubleQuote_List(_VimTest):