From 3a4991fb321fe7a4e1432e6595ebdd13dba4151a Mon Sep 17 00:00:00 2001 From: Aaron Schrab Date: Tue, 13 Nov 2012 11:07:43 -0500 Subject: [PATCH] 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 --- doc/UltiSnips.txt | 4 ++++ plugin/UltiSnips/__init__.py | 7 +++++++ test.py | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index dd4aff3..6d24774 100644 --- a/doc/UltiSnips.txt +++ b/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 diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index f8642a7..cb359ac 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -743,6 +743,13 @@ class SnippetManager(object): 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: diff --git a/test.py b/test.py index f60cc7c..a8e802f 100755 --- a/test.py +++ b/test.py @@ -2605,6 +2605,16 @@ ${0} 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):