From 8c5c0684ac61ab7d0c5e77881728c8106f2877f9 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sat, 17 Jan 2015 09:01:11 +0100 Subject: Group comments to parent object (fixes #160). --- sqlparse/engine/grouping.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'sqlparse/engine') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 5189f7e..d6f1360 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -370,6 +370,23 @@ def group_order(tlist): token = tlist.token_next_by_type(idx, T.Keyword.Order) +def align_comments(tlist): + [align_comments(sgroup) for sgroup in tlist.get_sublists()] + idx = 0 + token = tlist.token_next_by_instance(idx, sql.Comment) + while token: + before = tlist.token_prev(tlist.token_index(token)) + if isinstance(before, sql.TokenList): + grp = tlist.tokens_between(before, token)[1:] + before.tokens.extend(grp) + for t in grp: + tlist.tokens.remove(t) + idx = tlist.token_index(before) + 1 + else: + idx = tlist.token_index(token) + 1 + token = tlist.token_next_by_instance(idx, sql.Comment) + + def group(tlist): for func in [ group_comments, @@ -384,9 +401,11 @@ def group(tlist): group_aliased, group_assignment, group_comparison, + align_comments, group_identifier_list, group_if, group_for, group_foreach, - group_begin]: + group_begin, + ]: func(tlist) -- cgit v1.2.1