UltiSnips loads in python3 now, but does not work so far

This commit is contained in:
Holger Rapp 2012-01-10 14:16:25 +01:00
parent 089e50817f
commit 1653eb89ea
6 changed files with 164 additions and 62 deletions

View File

@ -8,10 +8,17 @@
" directory above this file. " directory above this file.
" }}} " }}}
if exists('did_UltiSnips_vim') || &cp || version < 700 || !has("python") if exists('did_UltiSnips_vim') || &cp || version < 700
finish finish
endif endif
if !has("python3")
if !has("python")
echo "UltiSnips requires py >= 2.5 or any py3"
finish
endif
endif
" Global Variables {{{ " Global Variables {{{
" The trigger used to expand a snippet. " The trigger used to expand a snippet.
@ -65,10 +72,18 @@ function! UltiSnipsEdit(...)
if a:0 == 1 && a:1 != '' if a:0 == 1 && a:1 != ''
let type = a:1 let type = a:1
else 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 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' let mode = 'e'
if exists('g:UltiSnipsEditSplit') if exists('g:UltiSnipsEditSplit')
@ -93,42 +108,67 @@ function! CompensateForPUM()
""" to explicitly check for the presence of the popup menu, and update """ to explicitly check for the presence of the popup menu, and update
""" the vim-state accordingly. """ the vim-state accordingly.
if pumvisible() if pumvisible()
py UltiSnips_Manager.cursor_moved() if has("python3")
python3 UltiSnips_Manager.cursor_moved()
else
python UltiSnips_Manager.cursor_moved()
endif
endif endif
endfunction endfunction
function! UltiSnips_ExpandSnippet() function! UltiSnips_ExpandSnippet()
py UltiSnips_Manager.expand() if has("python3")
python3 UltiSnips_Manager.expand()
else
python UltiSnips_Manager.expand()
endif
return "" return ""
endfunction endfunction
function! UltiSnips_ExpandSnippetOrJump() function! UltiSnips_ExpandSnippetOrJump()
call CompensateForPUM() 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 "" return ""
endfunction endfunction
function! UltiSnips_ListSnippets() function! UltiSnips_ListSnippets()
py UltiSnips_Manager.list_snippets() if has("python3")
python3 UltiSnips_Manager.list_snippets()
else
python UltiSnips_Manager.list_snippets()
endif
return "" return ""
endfunction endfunction
function! UltiSnips_JumpBackwards() function! UltiSnips_JumpBackwards()
call CompensateForPUM() call CompensateForPUM()
py UltiSnips_Manager.jump_backwards() if has("python3")
python3 UltiSnips_Manager.jump_backwards()
else
python UltiSnips_Manager.jump_backwards()
endif
return "" return ""
endfunction endfunction
function! UltiSnips_JumpForwards() function! UltiSnips_JumpForwards()
call CompensateForPUM() call CompensateForPUM()
py UltiSnips_Manager.jump_forwards() if has("python3")
python3 UltiSnips_Manager.jump_forwards()
else
python UltiSnips_Manager.jump_forwards()
endif
return "" return ""
endfunction endfunction
function! UltiSnips_AddSnippet(trigger, value, descr, options, ...) function! UltiSnips_AddSnippet(trigger, value, descr, options, ...)
" Takes the same arguments as SnippetManager.add_snippet: " Takes the same arguments as SnippetManager.add_snippet:
" (trigger, value, descr, options, ft = "all", globals = None) " (trigger, value, descr, options, ft = "all", globals = None)
py << EOB if has("python3")
python3 << EOB
args = vim.eval("a:000") args = vim.eval("a:000")
trigger = vim.eval("a:trigger") trigger = vim.eval("a:trigger")
value = vim.eval("a:value") value = vim.eval("a:value")
@ -137,17 +177,36 @@ options = vim.eval("a:options")
UltiSnips_Manager.add_snippet(trigger, value, descr, options, *args) UltiSnips_Manager.add_snippet(trigger, value, descr, options, *args)
EOB 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 "" return ""
endfunction endfunction
function! UltiSnips_Anon(value, ...) function! UltiSnips_Anon(value, ...)
" Takes the same arguments as SnippetManager.expand_anon: " Takes the same arguments as SnippetManager.expand_anon:
" (value, trigger="", descr="", options="", globals = None) " (value, trigger="", descr="", options="", globals = None)
py << EOB if has("python3")
python3 << EOB
args = vim.eval("a:000") args = vim.eval("a:000")
value = vim.eval("a:value") value = vim.eval("a:value")
UltiSnips_Manager.expand_anon(value, *args) UltiSnips_Manager.expand_anon(value, *args)
EOB EOB
else
python << EOB
args = vim.eval("a:000")
value = vim.eval("a:value")
UltiSnips_Manager.expand_anon(value, *args)
EOB
endif
return "" return ""
endfunction endfunction
@ -168,23 +227,52 @@ function! UltiSnips_MapKeys()
exec "snoremap <silent> " . g:UltiSnipsListSnippets . " <Esc>:call UltiSnips_ListSnippets()<cr>" exec "snoremap <silent> " . g:UltiSnipsListSnippets . " <Esc>:call UltiSnips_ListSnippets()<cr>"
" Do not remap this. " Do not remap this.
snoremap <silent> <BS> <Esc>:py UltiSnips_Manager.backspace_while_selected()<cr> if has("python3")
snoremap <silent> <BS> <Esc>:python3 UltiSnips_Manager.backspace_while_selected()<cr>
else
snoremap <silent> <BS> <Esc>:python UltiSnips_Manager.backspace_while_selected()<cr>
endif
endf endf
function! UltiSnips_CursorMoved() function! UltiSnips_CursorMoved()
py UltiSnips_Manager.cursor_moved() if has("python3")
python3 UltiSnips_Manager.cursor_moved()
else
python UltiSnips_Manager.cursor_moved()
endif
endf endf
function! UltiSnips_EnteredInsertMode() 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 endf
function! UltiSnips_LeavingWindow() function! UltiSnips_LeavingWindow()
py UltiSnips_Manager.leaving_window() if has("python3")
python3 UltiSnips_Manager.leaving_window()
else
python UltiSnips_Manager.leaving_window()
endif
endf endf
" }}} " }}}
"" STARTUP CODE {{{ "" STARTUP CODE {{{
" Expand our path " Expand our path
if has("python3")
python3 << EOF
import vim, os, sys
new_path = vim.eval('expand("<sfile>: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 python << EOF
import vim, os, sys 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.forward_trigger = vim.eval("g:UltiSnipsJumpForwardTrigger")
UltiSnips_Manager.backward_trigger = vim.eval("g:UltiSnipsJumpBackwardTrigger") UltiSnips_Manager.backward_trigger = vim.eval("g:UltiSnipsJumpBackwardTrigger")
EOF EOF
endif
au CursorMovedI * call UltiSnips_CursorMoved() au CursorMovedI * call UltiSnips_CursorMoved()
au InsertEnter * call UltiSnips_EnteredInsertMode() au InsertEnter * call UltiSnips_EnteredInsertMode()

View File

@ -9,7 +9,7 @@ into Logical Units called Tokens.
import string import string
import re import re
from Geometry import Position from .Geometry import Position
__all__ = [ __all__ = [
"tokenize", "EscapeCharToken", "TransformationToken", "TabStopToken", "tokenize", "EscapeCharToken", "TransformationToken", "TabStopToken",

View File

@ -644,7 +644,7 @@ class PythonCode(TextObject):
self._globals = {} self._globals = {}
globals = snippet.globals.get("!p", []) 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 # Add Some convenience to the code
self._code = "import re, os, vim, string, random\n" + 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") 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: if self._snip._rv_changed:
self.current_text = self._snip.rv self.current_text = self._snip.rv

View File

@ -4,11 +4,20 @@
import os import os
import types import types
import vim import vim
import sys
def as_utf8(s): if sys.version_info > (2,8):
if not isinstance(s, types.UnicodeType): def as_utf8(s):
s = s.decode("utf-8") return s.encode("utf-8")
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): def vim_string(inp):
""" Creates a vim-friendly string from a group of """ Creates a vim-friendly string from a group of
@ -16,13 +25,13 @@ def vim_string(inp):
""" """
def conv(obj): def conv(obj):
if isinstance(obj, list): 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): elif isinstance(obj, dict):
rv = u'{' + u','.join([ rv = unicode('{' + ','.join([
u"%s:%s" % (conv(key), conv(value)) "%s:%s" % (conv(key), conv(value))
for key, value in obj.iteritems()]) + u'}' for key, value in obj.iteritems()]) + '}')
else: else:
rv = u'"%s"' % str(obj).decode("utf-8").replace(u'"', u'\\"') rv = '"%s"' % str(obj).decode("utf-8").replace('"', '\\"')
return rv return rv
return conv(inp).encode("utf-8") return conv(inp).encode("utf-8")

View File

@ -14,7 +14,7 @@ import vim
from UltiSnips.Geometry import Position from UltiSnips.Geometry import Position
from UltiSnips.TextObjects import * from UltiSnips.TextObjects import *
from UltiSnips.Buffer import VimBuffer 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 from UltiSnips.Langmap import LangMapTranslator
# The following lines silence DeprecationWarnings. They are raised # The following lines silence DeprecationWarnings. They are raised
@ -266,9 +266,9 @@ class Snippet(object):
_TABS = re.compile(r"^\t*") _TABS = re.compile(r"^\t*")
def __init__(self, trigger, value, descr, options, globals): def __init__(self, trigger, value, descr, options, globals):
self._t = as_utf8(trigger) self._t = as_unicode(trigger)
self._v = as_utf8(value) self._v = as_unicode(value)
self._d = as_utf8(descr) self._d = as_unicode(descr)
self._opts = options self._opts = options
self._matched = "" self._matched = ""
self._last_re = None self._last_re = None
@ -596,7 +596,7 @@ class VimState(object):
"| redir END") "| redir END")
# Check if any mappings where found # 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"): if (len(all_maps) == 1 and all_maps[0][0] not in " sv"):
# "No maps found". String could be localized. Hopefully # "No maps found". String could be localized. Hopefully
# it doesn't start with any of these letters in any # it doesn't start with any of these letters in any
@ -989,7 +989,7 @@ class SnippetManager(object):
if rv > len(snippets): if rv > len(snippets):
rv = len(snippets) rv = len(snippets)
return snippets[rv-1] return snippets[rv-1]
except vim.error, e: except vim.error as e:
if str(e) == 'invalid expression': if str(e) == 'invalid expression':
return None return None
raise raise

62
test.py
View File

@ -31,6 +31,7 @@ import unittest
import time import time
import re import re
import platform import platform
import sys
WIN = platform.system() == "Windows" WIN = platform.system() == "Windows"
@ -39,12 +40,12 @@ from textwrap import dedent
# Some constants for better reading # Some constants for better reading
BS = u'\x7f' BS = '\x7f'
ESC = u'\x1b' ESC = '\x1b'
ARR_L = u'\x1bOD' ARR_L = '\x1bOD'
ARR_R = u'\x1bOC' ARR_R = '\x1bOC'
ARR_U = u'\x1bOA' ARR_U = '\x1bOA'
ARR_D = u'\x1bOB' ARR_D = '\x1bOB'
# multi-key sequences generating a single key press # multi-key sequences generating a single key press
SEQUENCES = [ARR_L, ARR_R, ARR_U, ARR_D] SEQUENCES = [ARR_L, ARR_R, ARR_U, ARR_D]
@ -82,26 +83,26 @@ def is_focused(title=None):
BRACES = re.compile("([}{])") BRACES = re.compile("([}{])")
WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"] WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"]
WIN_REPLACES = [ WIN_REPLACES = [
(BS, u"{BS}"), (BS, "{BS}"),
(ARR_L, u"{LEFT}"), (ARR_L, "{LEFT}"),
(ARR_R, u"{RIGHT}"), (ARR_R, "{RIGHT}"),
(ARR_U, u"{UP}"), (ARR_U, "{UP}"),
(ARR_D, u"{DOWN}"), (ARR_D, "{DOWN}"),
("\t", u"{TAB}"), ("\t", "{TAB}"),
("\n", u"~"), ("\n", "~"),
(ESC, u"{ESC}"), (ESC, "{ESC}"),
# On my system ` waits for a second keystroke, so `+SPACE = "`". On # On my system ` waits for a second keystroke, so `+SPACE = "`". On
# most systems, `+Space = "` ". I work around this, by sending the host # most systems, `+Space = "` ". I work around this, by sending the host
# ` as `+_+BS. Awkward, but the only way I found to get this working. # ` as `+_+BS. Awkward, but the only way I found to get this working.
(u"`", u"`_{BS}"), ("`", "`_{BS}"),
(u"´", u"´_{BS}"), ("´", "´_{BS}"),
(u"{^}", u"{^}_{BS}"), ("{^}", "{^}_{BS}"),
] ]
def convert_keys(keys): def convert_keys(keys):
keys = BRACES.sub(ur"{\1}", keys) keys = BRACES.sub(r"{\1}", keys)
for k in WIN_ESCAPES: for k in WIN_ESCAPES:
keys = keys.replace(k, u"{%s}" % k) keys = keys.replace(k, "{%s}" % k)
for f, r in WIN_REPLACES: for f, r in WIN_REPLACES:
keys = keys.replace(f, r) keys = keys.replace(f, r)
return keys return keys
@ -135,7 +136,7 @@ def send_win(keys, session):
def send_screen(s,session): def send_screen(s,session):
s = s.replace("'", r"'\''") 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): def send(s, session):
@ -176,7 +177,10 @@ class _VimTest(unittest.TestCase):
send(s, self.session) send(s, self.session)
def send_py(self,s): 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): def type(self,s):
type(s, self.session, self.sleeptime) type(s, self.session, self.sleeptime)
@ -220,7 +224,7 @@ class _VimTest(unittest.TestCase):
self.send(":silent! close\n") self.send(":silent! close\n")
# Reset UltiSnips # 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 # Make it unlikely that we do not parse any shipped snippets
self.send(":let g:UltiSnipsSnippetDirectories=['<un_def_ined>']\n") self.send(":let g:UltiSnipsSnippetDirectories=['<un_def_ined>']\n")
@ -1387,7 +1391,7 @@ class Transformation_SimpleCaseTransformInFrontDefVal_ECR(_VimTest):
keys = "test" + EX + "hallo foo" keys = "test" + EX + "hallo foo"
wanted = "hallo batzl hallo foo" wanted = "hallo batzl hallo foo"
class Transformation_MultipleTransformations_ECR(_VimTest): 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 " keys = "test" + EX + "SomE tExt "
wanted = "SomE tExt SOME TEXT \nsome text " wanted = "SomE tExt SOME TEXT \nsome text "
class Transformation_TabIsAtEndAndDeleted_ECR(_VimTest): class Transformation_TabIsAtEndAndDeleted_ECR(_VimTest):
@ -1426,7 +1430,7 @@ class Transformation_BackreferenceTwice_ExceptCorrectResult(_VimTest):
wanted = "dead parrot this parrot is a bit dead" wanted = "dead parrot this parrot is a bit dead"
class Transformation_CleverTransformUpercaseChar_ExceptCorrectResult(_VimTest): class Transformation_CleverTransformUpercaseChar_ExceptCorrectResult(_VimTest):
snippets = ("test", "$1 ${1/(.)/\u$1/}") snippets = ("test", "$1 ${1/(.)/\\u$1/}")
keys = "test" + EX + "hallo" keys = "test" + EX + "hallo"
wanted = "hallo Hallo" wanted = "hallo Hallo"
class Transformation_CleverTransformLowercaseChar_ExceptCorrectResult(_VimTest): class Transformation_CleverTransformLowercaseChar_ExceptCorrectResult(_VimTest):
@ -1434,7 +1438,7 @@ class Transformation_CleverTransformLowercaseChar_ExceptCorrectResult(_VimTest):
keys = "test" + EX + "Hallo" keys = "test" + EX + "Hallo"
wanted = "Hallo hallo" wanted = "Hallo hallo"
class Transformation_CleverTransformLongUpper_ExceptCorrectResult(_VimTest): class Transformation_CleverTransformLongUpper_ExceptCorrectResult(_VimTest):
snippets = ("test", "$1 ${1/(.*)/\U$1\E/}") snippets = ("test", "$1 ${1/(.*)/\\U$1\E/}")
keys = "test" + EX + "hallo" keys = "test" + EX + "hallo"
wanted = "hallo HALLO" wanted = "hallo HALLO"
class Transformation_CleverTransformLongLower_ExceptCorrectResult(_VimTest): class Transformation_CleverTransformLongLower_ExceptCorrectResult(_VimTest):
@ -1667,7 +1671,7 @@ class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand2(_VimTest):
wanted = "-Expand me!" wanted = "-Expand me!"
class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand3(_VimTest): class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand3(_VimTest):
snippets = (("test", "Expand me!", "", "i"), ) snippets = (("test", "Expand me!", "", "i"), )
keys = u"ßßtest" + EX keys = "ßßtest" + EX
wanted = "ßßExpand me!" wanted = "ßßExpand me!"
class _SnippetOptions_ExpandWordSnippets(_VimTest): class _SnippetOptions_ExpandWordSnippets(_VimTest):
@ -1913,13 +1917,13 @@ class Snippet_With_DoubleQuote(_VimTest):
wanted = "Expand me\"!" wanted = "Expand me\"!"
class Snippet_With_Umlauts_List(_VimTest): class Snippet_With_Umlauts_List(_VimTest):
snippets = _snip_quote(u'ü') snippets = _snip_quote('ü')
keys = 'te' + LS + "2\n" keys = 'te' + LS + "2\n"
wanted = "Expand meü!" wanted = "Expand meü!"
class Snippet_With_Umlauts(_VimTest): class Snippet_With_Umlauts(_VimTest):
snippets = _snip_quote(u'ü') snippets = _snip_quote('ü')
keys = u'teüst' + EX keys = 'teüst' + EX
wanted = "Expand meü!" wanted = "Expand meü!"
class Snippet_With_DoubleQuote_List(_VimTest): class Snippet_With_DoubleQuote_List(_VimTest):