From cebf11eda02c75cb9f7a087a19ba0e9fecc1b135 Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Mon, 26 Dec 2011 08:44:13 +0100 Subject: [PATCH] Better handling of unicode in snippet definitions and names --- plugin/UltiSnips/Util.py | 17 ++++++++++++----- plugin/UltiSnips/__init__.py | 8 ++++---- plugin/UltiSnips/debug.py | 6 +++++- test.py | 10 ++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/plugin/UltiSnips/Util.py b/plugin/UltiSnips/Util.py index 741725c..c887d64 100644 --- a/plugin/UltiSnips/Util.py +++ b/plugin/UltiSnips/Util.py @@ -1,8 +1,14 @@ #!/usr/bin/env python # encoding: utf-8 -import vim import os +import types +import vim + +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 @@ -10,14 +16,15 @@ def vim_string(inp): """ def conv(obj): if isinstance(obj, list): - return u'[' + u",".join([conv(o) for o in obj]) + u']' + rv = u'[' + u','.join(conv(o) for o in obj) + u']' elif isinstance(obj, dict): - return u'{' + u','.join([ + rv = u'{' + u','.join([ u"%s:%s" % (conv(key), conv(value)) for key, value in obj.iteritems()]) + u'}' else: - return u'"%s"' % str(obj).replace(u'"', u'\\"') - return conv(inp) + rv = u'"%s"' % str(obj).decode("utf-8").replace(u'"', u'\\"') + return rv + return conv(inp).encode("utf-8") class IndentUtil(object): """ Utility class for dealing properly with indentation. """ diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index a255137..92dfee2 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 +from UltiSnips.Util import IndentUtil, vim_string, as_utf8 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 = trigger - self._v = value - self._d = descr + self._t = as_utf8(trigger) + self._v = as_utf8(value) + self._d = as_utf8(descr) self._opts = options self._matched = "" self._last_re = None diff --git a/plugin/UltiSnips/debug.py b/plugin/UltiSnips/debug.py index 52a963c..151a14f 100644 --- a/plugin/UltiSnips/debug.py +++ b/plugin/UltiSnips/debug.py @@ -3,9 +3,13 @@ __all__ = [ "debug" ] +import types + def debug(s): + if not isinstance(s, types.UnicodeType): + s = s.decode("utf-8") f = open("/tmp/file.txt","a") - f.write(s+'\n') + f.write(s.encode("utf-8")+'\n') f.close() diff --git a/test.py b/test.py index b63cb67..b4ad79f 100755 --- a/test.py +++ b/test.py @@ -1912,6 +1912,16 @@ class Snippet_With_DoubleQuote(_VimTest): keys = 'te"st' + EX wanted = "Expand me\"!" +class Snippet_With_Umlauts_List(_VimTest): + snippets = _snip_quote(u'ü') + keys = 'te' + LS + "2\n" + wanted = "Expand meü!" + +class Snippet_With_Umlauts(_VimTest): + snippets = _snip_quote(u'ü') + keys = u'teüst' + EX + wanted = "Expand meü!" + class Snippet_With_DoubleQuote_List(_VimTest): snippets = _snip_quote('"') keys = "te" + LS + "2\n"