Python code has access to text for VISUAL via snip.v

This commit is contained in:
Holger Rapp 2012-02-12 12:33:53 +01:00
parent c1e5bb6c5f
commit bed41564c0
3 changed files with 31 additions and 6 deletions

View File

@ -487,6 +487,11 @@ The snip object provides some properties as well: >
in it. You can check if snip.c is != "" to make sure that the
interpolation is only done once. This deprecates the "cur" variable.
snip.v:
information about the content for ${VISUAL}. Has two attributes:
snip.v.mode ('v', 'V', '^V', see *visualmode* )
snip.v.text the text that was selected
snip.fn:
the current filename.

View File

@ -3,7 +3,7 @@
import os
import re
from collections import namedtuple
import UltiSnips._vim as _vim
from UltiSnips.compatibility import compatible_exec, as_unicode
@ -21,16 +21,18 @@ class _Tabs(object):
return ""
return ts.current_text
_VisualContent = namedtuple('_VisualContent', ['mode', 'text'])
class SnippetUtil(object):
""" Provides easy access to indentation, etc.
"""
def __init__(self, initial_indent, cur=""):
def __init__(self, initial_indent, vmode, vtext):
self._ind = IndentUtil()
self._visual = _VisualContent(vmode, vtext)
self._initial_indent = self._ind.indent_to_spaces(initial_indent)
self._reset(cur)
self._reset("")
def _reset(self, cur):
""" Gets the snippet ready for another update.
@ -134,6 +136,11 @@ class SnippetUtil(object):
"""
return self._c
@property
def v(self):
"""Content of visual expansions"""
return self._visual
def opt(self, option, default=None):
""" Gets a Vim variable. """
if _vim.eval("exists('%s')" % option) == "1":
@ -168,10 +175,12 @@ class PythonCode(NoneditableTextObject):
while snippet:
try:
self._locals = snippet.locals
t = snippet.visual_content.text
m = snippet.visual_content.mode
break
except AttributeError:
snippet = snippet._parent
self._snip = SnippetUtil(token.indent)
self._snip = SnippetUtil(token.indent, m, t)
self._globals = {}
globals = snippet.globals.get("!p", [])

15
test.py
View File

@ -1162,6 +1162,19 @@ class PythonCode_AccessKilledTabstop_OverwriteFirst(_VimTest):
keys = "test" + EX + "aaa"
wanted = "aaa"
class PythonVisual_NoVisualSelection_Ignore(_VimTest):
snippets = ("test", "h`!p snip.rv = snip.v.mode + snip.v.text`b")
keys = "test" + EX + "abc"
wanted = "hbabc"
class PythonVisual_SelectOneWord(_VimTest):
snippets = ("test", "h`!p snip.rv = snip.v.mode + snip.v.text`b")
keys = "blablub" + ESC + "0v6l" + EX + "test" + EX
wanted = "hvblablubb"
class PythonVisual_LineSelect_Simple(_VimTest):
snippets = ("test", "h`!p snip.rv = snip.v.mode + snip.v.text`b")
keys = "hello\nnice\nworld" + ESC + "Vkk" + EX + "test" + EX
wanted = "hVhello\nnice\nworld\nb"
# End: New Implementation #}}}
# End: PythonCode Interpolation #}}}
# Mirrors {{{#
@ -1612,8 +1625,6 @@ class VisualTransformation_SelectOneWord(_VimTest):
snippets = ("test", r"h${VISUAL/./\U$0\E/g}b")
keys = "blablub" + ESC + "0v6l" + EX + "test" + EX
wanted = "hBLABLUBb"
# TODO: python code access to visual
# TODO: document the changes
class VisualTransformationWithDefault_ExpandWithoutVisual(_VimTest):
snippets = ("test", "h${VISUAL:world/./\U$0\E/g}b")
keys = "test" + EX + "hi"