diff --git a/ChangeLog b/ChangeLog index 66b2da6..3af3672 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,7 +7,7 @@ version 2.0: behave more like Vim *UltiSnips-adding-snippets* - Backwards incompatible change: Zero Tabstop is no longer removed in nested snippets - - Support for ${VISUAL} placeholder. *UltiSnips-visual-placeholder* + - Support for ${VISUAL:default text} placeholder. *UltiSnips-visual-placeholder* - Improved handling of utf-8 characters in files and snippet definitions. - Full support for :py3. UltiSnips now works with python >= 2.6 or >= 3.2. - New or improved snippets: python, all diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 5420c18..55041fd 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -367,11 +367,13 @@ Snippets can contain a special placeholder called ${VISUAL}. When you select some text in visual mode and press the key to expand a trigger (see g:UltiSnipsExpandTrigger) the selected text will get deleted and you will drop to insert mode. The next snippet you expand will have the selected text -in the place of the ${VISUAL}. As a small example, let's take this snippet: +in the place of the ${VISUAL}. The place holder can also contain default text +when there is no visual text to be selected: ${VISUAL:default text}. +As a small example, let's take this snippet: ------------------- SNIP ------------------- snippet t -${VISUAL} +${VISUAL:inside text} endsnippet ------------------- SNAP ------------------- And starting with this line: > @@ -379,6 +381,9 @@ And starting with this line: > position the cursor on the should, then press viw (visual mode -> select inner word). Then press tab, type "t" and press tab again > -> this should be cool +If you expand this snippet without having been in visual mode before, e.g. by +writing in insert mode t, you will get > + inside text 4.4 Interpolation *UltiSnips-interpolation* ----------------- diff --git a/plugin/UltiSnips/text_objects/_lexer.py b/plugin/UltiSnips/text_objects/_lexer.py index 14627b5..b0e7a26 100644 --- a/plugin/UltiSnips/text_objects/_lexer.py +++ b/plugin/UltiSnips/text_objects/_lexer.py @@ -135,16 +135,20 @@ class TabStopToken(Token): ) class VisualToken(Token): - TOKEN = "${VISUAL}" + CHECK = re.compile(r"^\${VISUAL[:}]") @classmethod def starts_here(klass, stream): - return stream.peek(len(klass.TOKEN)) == klass.TOKEN + return klass.CHECK.match(stream.peek(10)) is not None def _parse(self, stream, indent): - for i in range(len(self.TOKEN)): + for i in range(8): # ${VISUAL stream.next() + if stream.peek() == ":": + stream.next() + self.alternative_text = _parse_till_closing_brace(stream) + def __repr__(self): return "VisualToken(%r,%r)" % ( self.start, self.end diff --git a/plugin/UltiSnips/text_objects/_visual.py b/plugin/UltiSnips/text_objects/_visual.py index 2b63102..ff545a5 100755 --- a/plugin/UltiSnips/text_objects/_visual.py +++ b/plugin/UltiSnips/text_objects/_visual.py @@ -25,6 +25,9 @@ class Visual(NoneditableTextObject): break except AttributeError: snippet = snippet._parent + if not self._text: + self._text = token.alternative_text + self._mode = "v" NoneditableTextObject.__init__(self, parent, token) diff --git a/test.py b/test.py index b4fde7a..afbbfec 100755 --- a/test.py +++ b/test.py @@ -1521,6 +1521,14 @@ class Visual_SelectOneWord(_VimTest): snippets = ("test", "h${VISUAL}b") keys = "blablub" + ESC + "0v6l" + EX + "test" + EX wanted = "hblablubb" +class VisualWithDefault_ExpandWithoutVisual(_VimTest): + snippets = ("test", "h${VISUAL:world}b") + keys = "test" + EX + "hi" + wanted = "hworldbhi" +class VisualWithDefault_ExpandWithVisual(_VimTest): + snippets = ("test", "h${VISUAL:world}b") + keys = "blablub" + ESC + "0v6l" + EX + "test" + EX + wanted = "hblablubb" class Visual_ExpandTwice(_VimTest): snippets = ("test", "h${VISUAL}b")