Permalink
Browse files

Add option for stripping of trailing whitespace

Add "s" snippet option which, if set, will cause trailing whitespace to
be stripped from lines before jumping to the next tab stop.

When used with a snippet like the following this will allow the text
from the $1 tabstop to be removed and have the space in front of that
automatically deleted when the user jumps to $2. This will work even if
using the same key for snippet expansion and jumping to the next
tabstop, whereas if the space was manually removed the key would cause
the snippet to be expanded again.

    snippet do "do block" s
    do `!p snip.rv = "|" if t[1] else""`${1:args}`!p snip.rv = "|" if t[1] else""`
    	$2
    end
    $0
    endsnippet
  • Loading branch information...
1 parent d4d1e86 commit 3a4991fb321fe7a4e1432e6595ebdd13dba4151a @aschrab aschrab committed Nov 13, 2012
Showing with 21 additions and 0 deletions.
  1. +4 −0 doc/UltiSnips.txt
  2. +7 −0 plugin/UltiSnips/__init__.py
  3. +10 −0 test.py
View
4 doc/UltiSnips.txt
@@ -552,6 +552,10 @@ The options currently supported are: >
Vim settings and insert the tab characters as is. This option is useful
for snippets involved with tab delimited formats, for example.
+ s Remove whitespace immediately before the cursor at the end of a line
+ before jumping to the next tabstop. This is useful if there is a
+ tabstop with optional text at the end of a line.
+
The end line is the 'endsnippet' keyword on a line by itself. >
endsnippet
View
7 plugin/UltiSnips/__init__.py
@@ -743,6 +743,13 @@ def _jump(self, backwards = False):
if self._cs:
self._ctab = self._cs.select_next_tab(backwards)
if self._ctab:
+ before, after = _vim.buf.current_line_splitted
+ if self._cs.snippet.has_option("s"):
+ if after == "":
+ m = re.match( r'(.*?)\s+$', before )
+ if m:
+ lineno = _vim.buf.cursor.line
+ _vim.text_to_vim( Position(lineno,0), Position(lineno,len(before)+len(after)), m.group(1) )
_vim.select(self._ctab.start, self._ctab.end)
jumped = True
if self._ctab.no == 0:
View
10 test.py
@@ -2605,6 +2605,16 @@ def _options_off(self):
keys = "test" + EX + JF + "sub junk {}"
wanted = "package c03;\nsub junk {}\n1;"
# End: Folding Interaction #}}}
+# Trailing whitespace {{{#
+class RemoveTrailingWhitespace(_VimTest):
+ snippets = ("test", """Hello\t ${1:default}\n$2""", "", "s")
+ wanted = """Hello\nGoodbye"""
+ keys = "test" + EX + BS + JF + "Goodbye"
+class LeaveTrailingWhitespace(_VimTest):
+ snippets = ("test", """Hello \t ${1:default}\n$2""")
+ wanted = """Hello \t \nGoodbye"""
+ keys = "test" + EX + BS + JF + "Goodbye"
+# End: Trailing whitespace }}}#
# Cursor Movement {{{#
class CursorMovement_Multiline_ECR(_VimTest):

0 comments on commit 3a4991f

Please sign in to comment.