More linting.

This commit is contained in:
Holger Rapp 2014-02-08 12:07:17 +01:00
parent a669654466
commit 63021206cd
9 changed files with 62 additions and 64 deletions

View File

@ -44,6 +44,7 @@ disable=
too-few-public-methods, too-few-public-methods,
too-many-branches, too-many-branches,
too-many-statements, too-many-statements,
too-many-locals,
[REPORTS] [REPORTS]

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
__all__ = [ "debug", "echo_to_hierarchy", "print_stack" ]
import sys import sys
from UltiSnips.compatibility import as_unicode from UltiSnips.compatibility import as_unicode
@ -36,4 +34,3 @@ def print_stack():
import traceback import traceback
with open(dump_filename, "ab") as dump_file: with open(dump_filename, "ab") as dump_file:
traceback.print_stack(file=dump_file) traceback.print_stack(file=dump_file)

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
__all__ = [ "Position" ]
class Position(object): class Position(object):
def __init__(self, line, col): def __init__(self, line, col):
self.line = line self.line = line
@ -79,4 +77,3 @@ class Position(object):
def __repr__(self): def __repr__(self):
return "(%i,%i)" % (self._line, self._col) return "(%i,%i)" % (self._line, self._col)

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
"""Public facing classes for TextObjects."""
from ._snippet_instance import SnippetInstance from ._snippet_instance import SnippetInstance
__all__ = [ "SnippetInstance" ]

View File

@ -6,8 +6,6 @@
import UltiSnips._vim as _vim import UltiSnips._vim as _vim
from UltiSnips.geometry import Position from UltiSnips.geometry import Position
__all__ = ["TextObject", "EditableTextObject", "NoneditableTextObject"]
def _calc_end(text, start): def _calc_end(text, start):
"""Calculate the end position of the 'text' starting at 'start.""" """Calculate the end position of the 'text' starting at 'start."""
if len(text) == 1: if len(text) == 1:
@ -39,7 +37,9 @@ def _text_to_vim(start, end, text):
return new_end return new_end
# These classes use their subclasses a lot and we really do not want to expose
# their functions more globally.
# pylint: disable=protected-access
class TextObject(object): class TextObject(object):
"""Represents any object in the text that has a span in any ways.""" """Represents any object in the text that has a span in any ways."""
@ -129,6 +129,13 @@ class TextObject(object):
self._end.diff(old_end) self._end.diff(old_end)
) )
def _update(self, done):
"""Update this object inside the Vim Buffer.
Return False if you need to be called again for this edit cycle.
Otherwise return True.
"""
raise NotImplementedError("Must be implemented by subclasses.")
class EditableTextObject(TextObject): class EditableTextObject(TextObject):
""" """
@ -137,16 +144,20 @@ class EditableTextObject(TextObject):
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
TextObject.__init__(self, *args, **kwargs) TextObject.__init__(self, *args, **kwargs)
self._childs = [] self._childs = []
self._tabstops = {} self._tabstops = {}
############## ##############
# Properties # # Properties #
############## ##############
@property
def childs(self):
"""List of all childs."""
return self._childs
@property @property
def _editable_childs(self): def _editable_childs(self):
"""All childs that are EditableTextObject s""" """List of all childs that are EditableTextObjects"""
return [child for child in self._childs if return [child for child in self._childs if
isinstance(child, EditableTextObject)] isinstance(child, EditableTextObject)]
@ -229,7 +240,8 @@ class EditableTextObject(TextObject):
# We have to handle this ourselves # We have to handle this ourselves
delta = Position(1, 0) if text == "\n" else Position(0, len(text)) delta = Position(1, 0) if text == "\n" else Position(0, len(text))
if ctype == "D": if ctype == "D":
if self._start == self._end: # Makes no sense to delete in empty textobject # Makes no sense to delete in empty textobject
if self._start == self._end:
return return
delta.line *= -1 delta.line *= -1
delta.col *= -1 delta.col *= -1
@ -323,11 +335,6 @@ class EditableTextObject(TextObject):
return self._parent._get_tabstop(self, number) return self._parent._get_tabstop(self, number)
def _update(self, done): def _update(self, done):
"""Update this object inside the Vim Buffer.
Return False if you need to be called again for this edit cycle.
Otherwise return True.
"""
if all((child in done) for child in self._childs): if all((child in done) for child in self._childs):
assert self not in done assert self not in done
done.add(self) done.add(self)

View File

@ -12,12 +12,6 @@ import re
from UltiSnips.geometry import Position from UltiSnips.geometry import Position
from UltiSnips.compatibility import as_unicode from UltiSnips.compatibility import as_unicode
__all__ = [
"tokenize", "EscapeCharToken", "VisualToken", "TransformationToken",
"TabStopToken", "MirrorToken", "PythonCodeToken", "VimLCodeToken",
"ShellCodeToken"
]
class _TextIterator(object): class _TextIterator(object):
"""Helper class to make iterating over text easier.""" """Helper class to make iterating over text easier."""

View File

@ -14,8 +14,6 @@ from UltiSnips.text_objects._transformation import Transformation
from UltiSnips.text_objects._viml_code import VimLCode from UltiSnips.text_objects._viml_code import VimLCode
from UltiSnips.text_objects._visual import Visual from UltiSnips.text_objects._visual import Visual
__all__ = ["TOParser"]
class TOParser(object): class TOParser(object):
TOKEN2TO = { TOKEN2TO = {
EscapeCharToken: EscapedChar, EscapeCharToken: EscapedChar,
@ -47,7 +45,7 @@ class TOParser(object):
m1 = Position(mark.line, mark.col) m1 = Position(mark.line, mark.col)
TabStop(self._parent_to, 0, mark, m1) TabStop(self._parent_to, 0, mark, m1)
self._parent_to.replace_initital_text() self._parent_to.replace_initial_text()
##################### #####################
# Private Functions # # Private Functions #

View File

@ -1,25 +1,26 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
"""A Snippet instance is an instance of a Snippet Definition. That is, when the
user expands a snippet, a SnippetInstance is created to keep track of the
corresponding TextObjects. The Snippet itself is also a TextObject. """
from UltiSnips.geometry import Position from UltiSnips.geometry import Position
import UltiSnips._vim as _vim import UltiSnips._vim as _vim
from UltiSnips.text_objects._base import EditableTextObject, NoneditableTextObject from UltiSnips.text_objects._base import EditableTextObject, \
NoneditableTextObject
from UltiSnips.text_objects._parser import TOParser from UltiSnips.text_objects._parser import TOParser
class SnippetInstance(EditableTextObject): class SnippetInstance(EditableTextObject):
""" """See module docstring."""
A Snippet instance is an instance of a Snippet Definition. That is,
when the user expands a snippet, a SnippetInstance is created to
keep track of the corresponding TextObjects. The Snippet itself is
also a TextObject because it has a start an end
"""
def __init__(self, snippet, parent, indent, initial_text, start, end, visual_content, last_re, globals): def __init__(self, snippet, parent, indent, initial_text,
start, end, visual_content, last_re, globals):
if start is None: if start is None:
start = Position(0,0) start = Position(0, 0)
if end is None: if end is None:
end = Position(0,0) end = Position(0, 0)
self.snippet = snippet self.snippet = snippet
self._cts = 0 self._cts = 0
@ -33,14 +34,14 @@ class SnippetInstance(EditableTextObject):
self.update_textobjects() self.update_textobjects()
def replace_initital_text(self): def replace_initial_text(self):
"""Puts the initial text of all text elements into Vim."""
def _place_initial_text(obj): def _place_initial_text(obj):
"""recurses on the childs to do the work."""
obj.overwrite() obj.overwrite()
if isinstance(obj, EditableTextObject): if isinstance(obj, EditableTextObject):
for c in obj._childs: for child in obj._childs:
_place_initial_text(c) _place_initial_text(child)
_place_initial_text(self) _place_initial_text(self)
def replay_user_edits(self, cmds): def replay_user_edits(self, cmds):
@ -54,13 +55,13 @@ class SnippetInstance(EditableTextObject):
the users edits have been replayed. This might also move the Cursor the users edits have been replayed. This might also move the Cursor
""" """
vc = _VimCursor(self) vc = _VimCursor(self)
done = set() done = set()
not_done = set() not_done = set()
def _find_recursive(obj): def _find_recursive(obj):
"""Finds all text objects and puts them into 'not_done'."""
if isinstance(obj, EditableTextObject): if isinstance(obj, EditableTextObject):
for c in obj._childs: for child in obj._childs:
_find_recursive(c) _find_recursive(child)
not_done.add(obj) not_done.add(obj)
_find_recursive(self) _find_recursive(self)
@ -71,7 +72,7 @@ class SnippetInstance(EditableTextObject):
if obj._update(done): if obj._update(done):
done.add(obj) done.add(obj)
counter -= 1 counter -= 1
if counter == 0: if not counter:
raise RuntimeError( raise RuntimeError(
"The snippets content did not converge: Check for Cyclic " "The snippets content did not converge: Check for Cyclic "
"dependencies or random strings in your snippet. You can use " "dependencies or random strings in your snippet. You can use "
@ -80,7 +81,8 @@ class SnippetInstance(EditableTextObject):
vc.to_vim() vc.to_vim()
self._del_child(vc) self._del_child(vc)
def select_next_tab(self, backwards = False): def select_next_tab(self, backwards=False):
"""Selects the next tabstop or the previous if 'backwards' is True."""
if self._cts is None: if self._cts is None:
return return
@ -107,22 +109,23 @@ class SnippetInstance(EditableTextObject):
def _get_tabstop(self, requester, no): def _get_tabstop(self, requester, no):
# SnippetInstances are completely self contained, therefore, we do not # SnippetInstances are completely self contained, therefore, we do not
# need to ask our parent for Tabstops # need to ask our parent for Tabstops
p = self._parent cached_parent = self._parent
self._parent = None self._parent = None
rv = EditableTextObject._get_tabstop(self, requester, no) rv = EditableTextObject._get_tabstop(self, requester, no)
self._parent = p self._parent = cached_parent
return rv return rv
class _VimCursor(NoneditableTextObject): class _VimCursor(NoneditableTextObject):
"""Helper class to keep track of the Vim Cursor""" """Helper class to keep track of the Vim Cursor when text objects expand
and move."""
def __init__(self, parent): def __init__(self, parent):
NoneditableTextObject.__init__( NoneditableTextObject.__init__(
self, parent, _vim.buf.cursor, _vim.buf.cursor, tiebreaker = Position(-1,-1), self, parent, _vim.buf.cursor, _vim.buf.cursor,
) tiebreaker=Position(-1, -1))
def to_vim(self): def to_vim(self):
assert(self._start == self._end) """Moves the cursor in the Vim to our position."""
assert self._start == self._end
_vim.buf.cursor = self._start _vim.buf.cursor = self._start

View File

@ -1,28 +1,30 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
"""This is the most important TextObject. A TabStop is were the cursor
comes to rest when the user taps through the Snippet."""
from UltiSnips.text_objects._base import EditableTextObject from UltiSnips.text_objects._base import EditableTextObject
__all__ = ['EditableTextObject']
class TabStop(EditableTextObject): class TabStop(EditableTextObject):
""" """See module docstring."""
This is the most important TextObject. A TabStop is were the cursor
comes to rest when the user taps through the Snippet. def __init__(self, parent, token, start=None, end=None):
"""
def __init__(self, parent, token, start = None, end = None):
if start is not None: if start is not None:
self._number = token self._number = token
EditableTextObject.__init__(self, parent, start, end) EditableTextObject.__init__(self, parent, start, end)
else: else:
self._number = token.number self._number = token.number
EditableTextObject.__init__(self, parent, token) EditableTextObject.__init__(self, parent, token)
parent._tabstops[self._number] = self parent._tabstops[self._number] = self # pylint:disable=protected-access
@property @property
def number(self): def number(self):
"""The tabstop number."""
return self._number return self._number
@property @property
def is_killed(self): def is_killed(self):
"""True if this tabstop has been typed over and the user therefore can
no longer jump to it."""
return self._parent is None return self._parent is None