diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2012-04-19 21:21:53 +0200 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2012-04-19 21:21:53 +0200 |
| commit | 508db7329f05362a47a0ed9f93d0ae8d9e42dcc8 (patch) | |
| tree | c5f0abbc25fc62a8b285758e6ab0082d169f16e8 | |
| parent | 748749e596ebf10a9adc763f970cfde446bac978 (diff) | |
| download | sqlparse-508db7329f05362a47a0ed9f93d0ae8d9e42dcc8.tar.gz | |
Preserve whitespace after subgroups.
| -rw-r--r-- | sqlparse/filters.py | 9 | ||||
| -rw-r--r-- | tests/test_format.py | 6 |
2 files changed, 11 insertions, 4 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py index 6bb3415..3d2355a 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -218,10 +218,11 @@ class StripWhitespaceFilter(Filter): tlist.tokens.pop(-2) self._stripws_default(tlist) - def process(self, stack, stmt): - [self.process(stack, sgroup) for sgroup in stmt.get_sublists()] + def process(self, stack, stmt, depth=0): + [self.process(stack, sgroup, depth+1) + for sgroup in stmt.get_sublists()] self._stripws(stmt) - if stmt.tokens[-1].is_whitespace(): + if depth == 0 and stmt.tokens[-1].is_whitespace(): stmt.tokens.pop(-1) @@ -610,4 +611,4 @@ class Limit(Filter): if index and token_type in Keyword and value == 'LIMIT': return stream[4 - index][1] - return -1
\ No newline at end of file + return -1 diff --git a/tests/test_format.py b/tests/test_format.py index 7a2c655..c9c6f46 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -68,6 +68,12 @@ class TestFormat(TestCaseBase): self.assertRaises(sqlparse.SQLParseError, sqlparse.format, s, strip_whitespace=None) + def test_preserve_ws(self): + # preserve at least one whitespace after subgroups + f = lambda sql: sqlparse.format(sql, strip_whitespace=True) + s = 'select\n* /* foo */ from bar ' + self.ndiffAssertEqual(f(s), 'select * /* foo */ from bar') + def test_outputformat(self): sql = 'select * from foo;' self.assertRaises(sqlparse.SQLParseError, sqlparse.format, sql, |
