From 63021206cdd8056d9a1e2621a78811fc56933ecf Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Sat, 8 Feb 2014 12:07:17 +0100 Subject: [PATCH] More linting. --- pylintrc | 1 + pythonx/UltiSnips/debug.py | 3 - pythonx/UltiSnips/geometry.py | 3 - pythonx/UltiSnips/text_objects/__init__.py | 5 +- pythonx/UltiSnips/text_objects/_base.py | 29 ++++++---- pythonx/UltiSnips/text_objects/_lexer.py | 6 -- pythonx/UltiSnips/text_objects/_parser.py | 4 +- .../text_objects/_snippet_instance.py | 57 ++++++++++--------- pythonx/UltiSnips/text_objects/_tabstop.py | 18 +++--- 9 files changed, 62 insertions(+), 64 deletions(-) diff --git a/pylintrc b/pylintrc index 47439b3..dedc542 100644 --- a/pylintrc +++ b/pylintrc @@ -44,6 +44,7 @@ disable= too-few-public-methods, too-many-branches, too-many-statements, + too-many-locals, [REPORTS] diff --git a/pythonx/UltiSnips/debug.py b/pythonx/UltiSnips/debug.py index 9c6f278..b98a2b2 100644 --- a/pythonx/UltiSnips/debug.py +++ b/pythonx/UltiSnips/debug.py @@ -1,8 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 -__all__ = [ "debug", "echo_to_hierarchy", "print_stack" ] - import sys from UltiSnips.compatibility import as_unicode @@ -36,4 +34,3 @@ def print_stack(): import traceback with open(dump_filename, "ab") as dump_file: traceback.print_stack(file=dump_file) - diff --git a/pythonx/UltiSnips/geometry.py b/pythonx/UltiSnips/geometry.py index bfbef74..e069314 100644 --- a/pythonx/UltiSnips/geometry.py +++ b/pythonx/UltiSnips/geometry.py @@ -1,8 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 -__all__ = [ "Position" ] - class Position(object): def __init__(self, line, col): self.line = line @@ -79,4 +77,3 @@ class Position(object): def __repr__(self): return "(%i,%i)" % (self._line, self._col) - diff --git a/pythonx/UltiSnips/text_objects/__init__.py b/pythonx/UltiSnips/text_objects/__init__.py index 27e87f9..d9f89ba 100644 --- a/pythonx/UltiSnips/text_objects/__init__.py +++ b/pythonx/UltiSnips/text_objects/__init__.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 +"""Public facing classes for TextObjects.""" + from ._snippet_instance import SnippetInstance - -__all__ = [ "SnippetInstance" ] - diff --git a/pythonx/UltiSnips/text_objects/_base.py b/pythonx/UltiSnips/text_objects/_base.py index 2af825b..6cd66d7 100755 --- a/pythonx/UltiSnips/text_objects/_base.py +++ b/pythonx/UltiSnips/text_objects/_base.py @@ -6,8 +6,6 @@ import UltiSnips._vim as _vim from UltiSnips.geometry import Position -__all__ = ["TextObject", "EditableTextObject", "NoneditableTextObject"] - def _calc_end(text, start): """Calculate the end position of the 'text' starting at 'start.""" if len(text) == 1: @@ -39,7 +37,9 @@ def _text_to_vim(start, end, text): 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): """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) ) + 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): """ @@ -137,16 +144,20 @@ class EditableTextObject(TextObject): """ def __init__(self, *args, **kwargs): TextObject.__init__(self, *args, **kwargs) - self._childs = [] self._tabstops = {} ############## # Properties # ############## + @property + def childs(self): + """List of all childs.""" + return self._childs + @property 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 isinstance(child, EditableTextObject)] @@ -229,7 +240,8 @@ class EditableTextObject(TextObject): # We have to handle this ourselves delta = Position(1, 0) if text == "\n" else Position(0, len(text)) 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 delta.line *= -1 delta.col *= -1 @@ -323,11 +335,6 @@ class EditableTextObject(TextObject): return self._parent._get_tabstop(self, number) 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): assert self not in done done.add(self) diff --git a/pythonx/UltiSnips/text_objects/_lexer.py b/pythonx/UltiSnips/text_objects/_lexer.py index 9524679..79d08ec 100644 --- a/pythonx/UltiSnips/text_objects/_lexer.py +++ b/pythonx/UltiSnips/text_objects/_lexer.py @@ -12,12 +12,6 @@ import re from UltiSnips.geometry import Position from UltiSnips.compatibility import as_unicode -__all__ = [ - "tokenize", "EscapeCharToken", "VisualToken", "TransformationToken", - "TabStopToken", "MirrorToken", "PythonCodeToken", "VimLCodeToken", - "ShellCodeToken" -] - class _TextIterator(object): """Helper class to make iterating over text easier.""" diff --git a/pythonx/UltiSnips/text_objects/_parser.py b/pythonx/UltiSnips/text_objects/_parser.py index 6e29f96..1353163 100755 --- a/pythonx/UltiSnips/text_objects/_parser.py +++ b/pythonx/UltiSnips/text_objects/_parser.py @@ -14,8 +14,6 @@ from UltiSnips.text_objects._transformation import Transformation from UltiSnips.text_objects._viml_code import VimLCode from UltiSnips.text_objects._visual import Visual -__all__ = ["TOParser"] - class TOParser(object): TOKEN2TO = { EscapeCharToken: EscapedChar, @@ -47,7 +45,7 @@ class TOParser(object): m1 = Position(mark.line, mark.col) TabStop(self._parent_to, 0, mark, m1) - self._parent_to.replace_initital_text() + self._parent_to.replace_initial_text() ##################### # Private Functions # diff --git a/pythonx/UltiSnips/text_objects/_snippet_instance.py b/pythonx/UltiSnips/text_objects/_snippet_instance.py index 201e63a..f11ce55 100755 --- a/pythonx/UltiSnips/text_objects/_snippet_instance.py +++ b/pythonx/UltiSnips/text_objects/_snippet_instance.py @@ -1,25 +1,26 @@ #!/usr/bin/env python # 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 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 class SnippetInstance(EditableTextObject): - """ - 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 - """ + """See module docstring.""" - 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: - start = Position(0,0) + start = Position(0, 0) if end is None: - end = Position(0,0) + end = Position(0, 0) self.snippet = snippet self._cts = 0 @@ -33,14 +34,14 @@ class SnippetInstance(EditableTextObject): 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): + """recurses on the childs to do the work.""" obj.overwrite() - if isinstance(obj, EditableTextObject): - for c in obj._childs: - _place_initial_text(c) - + for child in obj._childs: + _place_initial_text(child) _place_initial_text(self) 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 """ vc = _VimCursor(self) - done = set() not_done = set() def _find_recursive(obj): + """Finds all text objects and puts them into 'not_done'.""" if isinstance(obj, EditableTextObject): - for c in obj._childs: - _find_recursive(c) + for child in obj._childs: + _find_recursive(child) not_done.add(obj) _find_recursive(self) @@ -71,7 +72,7 @@ class SnippetInstance(EditableTextObject): if obj._update(done): done.add(obj) counter -= 1 - if counter == 0: + if not counter: raise RuntimeError( "The snippets content did not converge: Check for Cyclic " "dependencies or random strings in your snippet. You can use " @@ -80,7 +81,8 @@ class SnippetInstance(EditableTextObject): vc.to_vim() 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: return @@ -107,22 +109,23 @@ class SnippetInstance(EditableTextObject): def _get_tabstop(self, requester, no): # SnippetInstances are completely self contained, therefore, we do not # need to ask our parent for Tabstops - p = self._parent + cached_parent = self._parent self._parent = None rv = EditableTextObject._get_tabstop(self, requester, no) - self._parent = p - + self._parent = cached_parent return rv 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): 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): - assert(self._start == self._end) + """Moves the cursor in the Vim to our position.""" + assert self._start == self._end _vim.buf.cursor = self._start diff --git a/pythonx/UltiSnips/text_objects/_tabstop.py b/pythonx/UltiSnips/text_objects/_tabstop.py index 6fe707f..4eff6af 100755 --- a/pythonx/UltiSnips/text_objects/_tabstop.py +++ b/pythonx/UltiSnips/text_objects/_tabstop.py @@ -1,28 +1,30 @@ #!/usr/bin/env python # 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 -__all__ = ['EditableTextObject'] - class TabStop(EditableTextObject): - """ - 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): + """See module docstring.""" + + def __init__(self, parent, token, start=None, end=None): if start is not None: self._number = token EditableTextObject.__init__(self, parent, start, end) else: self._number = token.number EditableTextObject.__init__(self, parent, token) - parent._tabstops[self._number] = self + parent._tabstops[self._number] = self # pylint:disable=protected-access @property def number(self): + """The tabstop number.""" return self._number @property 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