diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2015-01-17 09:01:11 +0100 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2015-01-17 09:01:11 +0100 |
| commit | 8c5c0684ac61ab7d0c5e77881728c8106f2877f9 (patch) | |
| tree | e06f09a2de797e873516cf644172bdaab626a959 /sqlparse/engine | |
| parent | a17db7a7557056728acf5506d3dea6841ad55fa9 (diff) | |
| download | sqlparse-8c5c0684ac61ab7d0c5e77881728c8106f2877f9.tar.gz | |
Group comments to parent object (fixes #160).
Diffstat (limited to 'sqlparse/engine')
| -rw-r--r-- | sqlparse/engine/grouping.py | 21 |
1 files changed, 20 insertions, 1 deletions
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) |
