From 81a628c6ea82b7c9f3d1e95e55c428de3f942900 Mon Sep 17 00:00:00 2001 From: Stanislav Seletskiy Date: Wed, 4 Feb 2015 11:55:28 +0600 Subject: [PATCH] Add option 'm' for trimming whitespaces in snippet Fix #391 --- doc/UltiSnips.txt | 5 +++++ pythonx/UltiSnips/snippet/definition/_base.py | 5 ++++- test/test_Chars.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 5a886be..75c6301 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -649,6 +649,11 @@ The options currently supported are: > before jumping to the next tabstop. This is useful if there is a tabstop with optional text at the end of a line. + m Trim all whitespaces from right side of snippet lines. Practically + useful when snippet contains empty lines which should remain empty + after expanding on some indentation level. Without this option + empty lines in snippets definition will have indentation too. + The end line is the 'endsnippet' keyword on a line by itself. > endsnippet diff --git a/pythonx/UltiSnips/snippet/definition/_base.py b/pythonx/UltiSnips/snippet/definition/_base.py index a7fcf9e..7ce8fcd 100644 --- a/pythonx/UltiSnips/snippet/definition/_base.py +++ b/pythonx/UltiSnips/snippet/definition/_base.py @@ -228,7 +228,10 @@ class SnippetDefinition(object): if line_num != 0: line_ind = indent + line_ind - initial_text.append(line_ind + line[tabs:]) + result_line = line_ind + line[tabs:] + if 'm' in self._opts: + result_line = result_line.rstrip() + initial_text.append(result_line) initial_text = '\n'.join(initial_text) snippet_instance = SnippetInstance( diff --git a/test/test_Chars.py b/test/test_Chars.py index a14a758..9a2f8e6 100644 --- a/test/test_Chars.py +++ b/test/test_Chars.py @@ -47,6 +47,16 @@ class RemoveTrailingWhitespace(_VimTest): wanted = """Hello\nGoodbye""" keys = 'test' + EX + BS + JF + 'Goodbye' +class TrimSpacesAtEndOfLines(_VimTest): + snippets = ('test', """next line\n\nshould be empty""", '', 'm') + wanted = """\tnext line\n\n\tshould be empty""" + keys = '\ttest' + EX + +class DoNotTrimSpacesAtEndOfLinesByDefault(_VimTest): + snippets = ('test', """next line\n\nshould be empty""", '', '') + wanted = """\tnext line\n\t\n\tshould be empty""" + keys = '\ttest' + EX + class LeaveTrailingWhitespace(_VimTest): snippets = ('test', """Hello \t ${1:default}\n$2""")