From d72eb587f65a157e8c251f3ce74fa9e851f0190d Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Tue, 10 Jan 2012 18:11:02 +0100 Subject: [PATCH] 22/339 tests are failing --- plugin/UltiSnips/Geometry.py | 9 ++++++--- plugin/UltiSnips/Lexer.py | 4 ++-- plugin/UltiSnips/TextObjects.py | 19 ++++++++++--------- plugin/UltiSnips/Util.py | 16 ++++++++++++++++ plugin/UltiSnips/__init__.py | 2 +- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/plugin/UltiSnips/Geometry.py b/plugin/UltiSnips/Geometry.py index 857beda..bc3fe59 100644 --- a/plugin/UltiSnips/Geometry.py +++ b/plugin/UltiSnips/Geometry.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # encoding: utf-8 +from UltiSnips.Util import CheapTotalOrdering + __all__ = [ "Position", "Span" ] - -class Position(object): +class Position(CheapTotalOrdering): def __init__(self, line, col): self.line = line self.col = col @@ -43,7 +44,9 @@ class Position(object): def __cmp__(self, other): s = self._line, self._col o = other._line, other._col - return cmp(s,o) + if s < o: return -1 + if s > o: return 1 + return 0 def __repr__(self): return "(%i,%i)" % (self._line, self._col) diff --git a/plugin/UltiSnips/Lexer.py b/plugin/UltiSnips/Lexer.py index 99d1236..b18e2a4 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 UltiSnips.Geometry import Position __all__ = [ "tokenize", "EscapeCharToken", "TransformationToken", "TabStopToken", @@ -122,7 +122,7 @@ class TabStopToken(Token): self.no = _parse_number(stream) - if stream.peek() is ":": + if stream.peek() == ":": stream.next() self.initial_text = _parse_till_closing_brace(stream) diff --git a/plugin/UltiSnips/TextObjects.py b/plugin/UltiSnips/TextObjects.py index d0600c9..d99c6b2 100644 --- a/plugin/UltiSnips/TextObjects.py +++ b/plugin/UltiSnips/TextObjects.py @@ -7,7 +7,7 @@ import stat import tempfile import vim -from UltiSnips.Util import IndentUtil +from UltiSnips.Util import IndentUtil, CheapTotalOrdering from UltiSnips.Buffer import TextBuffer from UltiSnips.Geometry import Span, Position from UltiSnips.Lexer import tokenize, EscapeCharToken, TransformationToken, \ @@ -95,6 +95,7 @@ class _CleverReplace(object): def _unescape(self, v): return self._UNESCAPE.subn(lambda m: m.group(0)[-1], v)[0] + def replace(self, match): start, end = match.span() @@ -108,7 +109,9 @@ class _CleverReplace(object): tv = self._LONG_CASEFOLDINGS.subn(self._lcase_folding, tv)[0] tv = self._replace_conditional(match, tv) - return self._unescape(tv.decode("string-escape")) + tv = tv.replace(r'\"', '"') + tv = tv.replace(r"\'", "'") + return self._unescape(tv) class _TOParser(object): def __init__(self, parent_to, text, indent): @@ -167,12 +170,10 @@ class _TOParser(object): elif isinstance(token, VimLCodeToken): VimLCode(self._parent_to, token) - - ########################################################################### # Public classes # ########################################################################### -class TextObject(object): +class TextObject(CheapTotalOrdering): """ This base class represents any object in the text that has a span in any ways @@ -198,7 +199,7 @@ class TextObject(object): self._cts = 0 def __cmp__(self, other): - return cmp(self._start, other._start) + return self._start.__cmp__(other._start) ############## # PROPERTIES # @@ -446,7 +447,7 @@ class ShellCode(TextObject): # Write the code to a temporary file handle, path = tempfile.mkstemp(text=True) - os.write(handle, code) + os.write(handle, code.encode("utf-8")) os.close(handle) os.chmod(path, stat.S_IRWXU) @@ -644,7 +645,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"), self._globals) # Add Some convenience to the code self._code = "import re, os, vim, string, random\n" + code @@ -672,7 +673,7 @@ class PythonCode(TextObject): }) self._code = self._code.replace("\r\n", "\n") - exec(self._code in self._globals, local_d) + exec(self._code, 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 3bf3562..012a024 100644 --- a/plugin/UltiSnips/Util.py +++ b/plugin/UltiSnips/Util.py @@ -6,6 +6,22 @@ import types import vim import sys +class CheapTotalOrdering: + """Total ordering only appears in python 2.7. We try to stay compatible with + python 2.5 for now, so we define our own""" + + def __lt__(self, other): + return self.__cmp__(other) < 0 + + def __le__(self, other): + return self.__cmp__(other) <= 0 + + def __gt__(self, other): + return self.__cmp__(other) > 0 + + def __ge__(self, other): + return self.__cmp__(other) >= 0 + if sys.version_info > (2,8): def as_utf8(s): return s.encode("utf-8") diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 84d0425..6353f94 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -295,7 +295,7 @@ class Snippet(object): return before.strip() else: before_words = before - for i in xrange(-1, -(num_words + 1), -1): + for i in range(-1, -(num_words + 1), -1): left = before_words.rfind(word_list[i]) before_words = before_words[:left] return before[len(before_words):].strip()