diff options
| -rw-r--r-- | sqlparse/engine/grouping.py | 29 | ||||
| -rw-r--r-- | tests/test_format.py | 9 | ||||
| -rw-r--r-- | tests/test_grouping.py | 3 |
3 files changed, 27 insertions, 14 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 9176fbe..3a57496 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -12,9 +12,9 @@ from sqlparse.sql import * def _group_left_right(tlist, ttype, value, cls, check_right=lambda t: True, include_semicolon=False): -# [_group_left_right(sgroup, ttype, value, cls, check_right, -# include_semicolon) for sgroup in tlist.get_sublists() -# if not isinstance(sgroup, cls)] + [_group_left_right(sgroup, ttype, value, cls, check_right, + include_semicolon) for sgroup in tlist.get_sublists() + if not isinstance(sgroup, cls)] idx = 0 token = tlist.token_next_match(idx, ttype, value) while token: @@ -127,7 +127,7 @@ def group_identifier(tlist): def group_identifier_list(tlist): [group_identifier_list(sgroup) for sgroup in tlist.get_sublists() - if not isinstance(sgroup, IdentifierList)] + if not isinstance(sgroup, (Identifier, IdentifierList))] idx = 0 token = tlist.token_next_by_instance(idx, Identifier) while token: @@ -139,17 +139,20 @@ def group_identifier_list(tlist): ',') ]) if end is None: + end = tlist.tokens[-1] + exclude_end = False + else: + exclude_end = True + grp_tokens = tlist.tokens_between(token, end, + exclude_end=exclude_end) + while grp_tokens and (grp_tokens[-1].is_whitespace() + or grp_tokens[-1].match(T.Punctuation, ',')): + grp_tokens.pop() + if len(grp_tokens) <= 1: idx = tidx + 1 else: - grp_tokens = tlist.tokens_between(token, end, exclude_end=True) - while grp_tokens and (grp_tokens[-1].is_whitespace() - or grp_tokens[-1].match(T.Punctuation, ',')): - grp_tokens.pop() - if len(grp_tokens) <= 1: - idx = tidx + 1 - else: - group = tlist.group_tokens(IdentifierList, grp_tokens) - idx = tlist.token_index(group) + group = tlist.group_tokens(IdentifierList, grp_tokens) + idx = tlist.token_index(group) token = tlist.token_next_by_instance(idx, Identifier) diff --git a/tests/test_format.py b/tests/test_format.py index 5748704..3180d10 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -143,4 +143,13 @@ class TestFormatReindent(TestCaseBase): ' else 5', 'end'])) + def test_nested_identifier_list(self): + # issue4 + f = lambda sql: sqlparse.format(sql, reindent=True) + s = '(foo as bar, bar1, bar2 as bar3, b4 as b5)' + self.ndiffAssertEqual(f(s), '\n'.join(['(foo as bar,', + ' bar1,', + ' bar2 as bar3,', + ' b4 as b5)'])) + diff --git a/tests/test_grouping.py b/tests/test_grouping.py index fc3bea5..fa72275 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -36,7 +36,8 @@ class TestGrouping(TestCaseBase): s = 'select * from foo where foo.id = 1' parsed = sqlparse.parse(s)[0] self.ndiffAssertEqual(s, parsed.to_unicode()) - self.assert_(isinstance(parsed.tokens[-1].tokens[2], Identifier)) + self.assert_(isinstance(parsed.tokens[-1].tokens[-1].tokens[0], + Identifier)) s = 'select * from (select "foo"."id" from foo)' parsed = sqlparse.parse(s)[0] self.ndiffAssertEqual(s, parsed.to_unicode()) |
