More linting and some code simplifications.

This commit is contained in:
Holger Rapp 2014-02-07 09:03:24 +01:00
parent 4f7cec61ae
commit 2377a4405b
4 changed files with 62 additions and 51 deletions

View File

@ -16,8 +16,7 @@ from UltiSnips.geometry import Position
from UltiSnips.snippet import Snippet from UltiSnips.snippet import Snippet
from UltiSnips.snippet_dictionary import SnippetDictionary from UltiSnips.snippet_dictionary import SnippetDictionary
from UltiSnips.snippets_file_parser import SnippetsFileParser from UltiSnips.snippets_file_parser import SnippetsFileParser
from UltiSnips.vim_state import VimState from UltiSnips.vim_state import VimState, VisualContentPreserver
from UltiSnips.visual_content_preserver import VisualContentPreserver
import UltiSnips._vim as _vim import UltiSnips._vim as _vim
def _ask_snippets(snippets): def _ask_snippets(snippets):

View File

@ -1,52 +1,106 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
"""Some classes to conserve Vim's state for comparing over time."""
from collections import deque from collections import deque
from UltiSnips.compatibility import as_unicode, byte2col
from UltiSnips.geometry import Position from UltiSnips.geometry import Position
import UltiSnips._vim as _vim import UltiSnips._vim as _vim
class VimPosition(Position): class VimPosition(Position):
"""Represents the current position in the buffer, together with some status
variables that might change our decisions down the line."""
def __init__(self): def __init__(self):
"""Represents the current position in the buffer, together with some
status variables that might change our decisions down the line. """
pos = _vim.buf.cursor pos = _vim.buf.cursor
self._mode = _vim.eval("mode()") self._mode = _vim.eval("mode()")
Position.__init__(self, pos.line, pos.col) Position.__init__(self, pos.line, pos.col)
@property @property
def mode(self): def mode(self):
"""Returns the mode() this position was created."""
return self._mode return self._mode
class VimState(object): class VimState(object):
"""Caches some state information from Vim to better guess what editing
tasks the user might have done in the last step."""
def __init__(self): def __init__(self):
"""
This class caches some state information from Vim to better guess what
editing tasks the user might have done in the last step.
"""
self._poss = deque(maxlen=5) self._poss = deque(maxlen=5)
self._lvb = None self._lvb = None
def remember_position(self): def remember_position(self):
"""Remember the current position as a previous pose."""
self._poss.append(VimPosition()) self._poss.append(VimPosition())
def remember_buffer(self, to): def remember_buffer(self, to):
"""Remember the content of the buffer and the position."""
self._lvb = _vim.buf[to.start.line:to.end.line+1] self._lvb = _vim.buf[to.start.line:to.end.line+1]
self._lvb_len = len(_vim.buf) self._lvb_len = len(_vim.buf)
self.remember_position() self.remember_position()
@property @property
def diff_in_buffer_length(self): def diff_in_buffer_length(self):
"""Returns the difference in the length of the current buffer compared
to the remembered."""
return len(_vim.buf) - self._lvb_len return len(_vim.buf) - self._lvb_len
@property @property
def pos(self): def pos(self):
"""The last remembered position."""
return self._poss[-1] return self._poss[-1]
@property @property
def ppos(self): def ppos(self):
"""The second to last remembered position."""
return self._poss[-2] return self._poss[-2]
@property @property
def remembered_buffer(self): def remembered_buffer(self):
"""The content of the remembered buffer."""
return self._lvb[:] return self._lvb[:]
class VisualContentPreserver(object):
"""Saves the current visual selection and the selection mode it was done in
(e.g. line selection, block selection or regular selection.)"""
def __init__(self):
self.reset()
def reset(self):
"""Forget the preserved state."""
self._mode = ""
self._text = as_unicode("")
def conserve(self):
"""Save the last visual selection ond the mode it was made in."""
sl, sbyte = map(int,
(_vim.eval("""line("'<")"""), _vim.eval("""col("'<")""")))
el, ebyte = map(int,
(_vim.eval("""line("'>")"""), _vim.eval("""col("'>")""")))
sc = byte2col(sl, sbyte - 1)
ec = byte2col(el, ebyte - 1)
self._mode = _vim.eval("visualmode()")
_vim_line_with_eol = lambda ln: _vim.buf[ln] + '\n'
if sl == el:
text = _vim_line_with_eol(sl-1)[sc:ec+1]
else:
text = _vim_line_with_eol(sl-1)[sc:]
for cl in range(sl, el-1):
text += _vim_line_with_eol(cl)
text += _vim_line_with_eol(el-1)[:ec+1]
self._text = text
@property
def text(self):
"""The conserved text."""
return self._text
@property
def mode(self):
"""The conserved visualmode()."""
return self._mode

View File

@ -1,42 +0,0 @@
#!/usr/bin/env python
# encoding: utf-8
from UltiSnips.compatibility import as_unicode, byte2col
import UltiSnips._vim as _vim
class VisualContentPreserver(object):
def __init__(self):
"""Saves the current visual selection and the selection mode it was
done in (e.g. line selection, block selection or regular selection.)"""
self.reset()
def reset(self):
self._mode = ""
self._text = as_unicode("")
def conserve(self):
sl, sbyte = map(int, (_vim.eval("""line("'<")"""), _vim.eval("""col("'<")""")))
el, ebyte = map(int, (_vim.eval("""line("'>")"""), _vim.eval("""col("'>")""")))
sc = byte2col(sl, sbyte - 1)
ec = byte2col(el, ebyte - 1)
self._mode = _vim.eval("visualmode()")
def _vim_line_with_eol(ln):
return _vim.buf[ln] + '\n'
if sl == el:
text = _vim_line_with_eol(sl-1)[sc:ec+1]
else:
text = _vim_line_with_eol(sl-1)[sc:]
for cl in range(sl,el-1):
text += _vim_line_with_eol(cl)
text += _vim_line_with_eol(el-1)[:ec+1]
self._text = text
@property
def text(self):
return self._text
@property
def mode(self):
return self._mode

View File

@ -61,7 +61,7 @@ reports=no
required-attributes= required-attributes=
# List of builtins function names that should not be used, separated by a comma # List of builtins function names that should not be used, separated by a comma
bad-functions=map,filter,apply,input bad-functions=apply,input
# Regular expression which should only match correct module names # Regular expression which should only match correct module names
module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$