Skip to content

Commit e4bccf4

Browse files
committed
Pretty format comments in identifier lists (fixes issue59).
1 parent 508db73 commit e4bccf4

4 files changed

Lines changed: 28 additions & 14 deletions

File tree

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Bug Fixes
1010
* Fix statement splitting when parsing recursive statements (issue57,
1111
thanks to piranna).
1212
* Fix for negative numbers (issue56, thanks to kevinjqiu).
13+
* Pretty format comments in identifier lists (issue59).
1314
* Several minor bug fixes and improvements.
1415

1516

sqlparse/engine/grouping.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def group_identifier_list(tlist):
197197
lambda t: t.ttype == T.Number.Integer,
198198
lambda t: t.ttype == T.String.Single,
199199
lambda t: isinstance(t, sql.Comparison),
200+
lambda t: isinstance(t, sql.Comment),
200201
]
201202
tcomma = tlist.token_next_match(idx, T.Punctuation, ',')
202203
start = None
@@ -314,18 +315,19 @@ def group_functions(tlist):
314315

315316

316317
def group(tlist):
317-
for func in [group_parenthesis,
318-
group_functions,
319-
group_comments,
320-
group_where,
321-
group_case,
322-
group_identifier,
323-
group_typecasts,
324-
group_as,
325-
group_aliased,
326-
group_assignment,
327-
group_comparison,
328-
group_identifier_list,
329-
group_if,
330-
group_for]:
318+
for func in [
319+
group_comments,
320+
group_parenthesis,
321+
group_functions,
322+
group_where,
323+
group_case,
324+
group_identifier,
325+
group_typecasts,
326+
group_as,
327+
group_aliased,
328+
group_assignment,
329+
group_comparison,
330+
group_identifier_list,
331+
group_if,
332+
group_for]:
331333
func(tlist)

sqlparse/filters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ def _process_identifierlist(self, tlist):
332332
self.offset += num_offset
333333
for token in identifiers[1:]:
334334
tlist.insert_before(token, self.nl())
335+
for token in tlist.tokens:
336+
if isinstance(token, sql.Comment):
337+
tlist.insert_after(token, self.nl())
335338
self.offset -= num_offset
336339
self._process_default(tlist)
337340

sqlparse/sql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ def insert_before(self, where, token):
322322
"""Inserts *token* before *where*."""
323323
self.tokens.insert(self.token_index(where), token)
324324

325+
def insert_after(self, where, token):
326+
"""Inserts *token* after *where*."""
327+
next_token = self.token_next(where)
328+
if next_token is None:
329+
self.tokens.append(token)
330+
else:
331+
self.tokens.insert(self.token_index(next_token), token)
332+
325333
def has_alias(self):
326334
"""Returns ``True`` if an alias is present."""
327335
return self.get_alias() is not None

0 commit comments

Comments
 (0)