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
This commit is contained in:
Aaron Schrab 2012-11-13 11:07:43 -05:00
parent d4d1e86962
commit 3a4991fb32
3 changed files with 21 additions and 0 deletions

View File

@ -552,6 +552,10 @@ The options currently supported are: >
Vim settings and insert the tab characters as is. This option is useful Vim settings and insert the tab characters as is. This option is useful
for snippets involved with tab delimited formats, for example. 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. > The end line is the 'endsnippet' keyword on a line by itself. >
endsnippet endsnippet

View File

@ -743,6 +743,13 @@ class SnippetManager(object):
if self._cs: if self._cs:
self._ctab = self._cs.select_next_tab(backwards) self._ctab = self._cs.select_next_tab(backwards)
if self._ctab: 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) _vim.select(self._ctab.start, self._ctab.end)
jumped = True jumped = True
if self._ctab.no == 0: if self._ctab.no == 0:

10
test.py
View File

@ -2605,6 +2605,16 @@ ${0}
keys = "test" + EX + JF + "sub junk {}" keys = "test" + EX + JF + "sub junk {}"
wanted = "package c03;\nsub junk {}\n1;" wanted = "package c03;\nsub junk {}\n1;"
# End: Folding Interaction #}}} # 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 {{{# # Cursor Movement {{{#
class CursorMovement_Multiline_ECR(_VimTest): class CursorMovement_Multiline_ECR(_VimTest):