Skip to content

Commit b8f7356

Browse files
committed
Better formatting when using comma-first notation (issue141).
1 parent 4d05b44 commit b8f7356

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

sqlparse/filters/reindent.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ def _get_offset(self, token):
4343
# Now take current offset into account and return relative offset.
4444
return len(line) - len(self.char * self.leading_ws)
4545

46-
def nl(self):
47-
return sql.Token(T.Whitespace, self.n + self.char * self.leading_ws)
46+
def nl(self, offset=0):
47+
return sql.Token(
48+
T.Whitespace,
49+
self.n + self.char * max(0, self.leading_ws + offset))
4850

4951
def _next_token(self, tlist, idx=-1):
5052
split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR',
@@ -125,13 +127,15 @@ def _process_identifierlist(self, tlist):
125127
# Add 1 for the "," separator
126128
position += len(token.value) + 1
127129
if position > (self.wrap_after - self.offset):
130+
adjust = 0
128131
if self.comma_first:
132+
adjust = -2
129133
_, comma = tlist.token_prev(
130134
tlist.token_index(token))
131135
if comma is None:
132136
continue
133137
token = comma
134-
tlist.insert_before(token, self.nl())
138+
tlist.insert_before(token, self.nl(offset=adjust))
135139
if self.comma_first:
136140
_, ws = tlist.token_next(
137141
tlist.token_index(token), skip_ws=False)

tests/test_format.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ def test_identifier_list_comment_first(self):
436436
s = 'select foo, bar, baz from table where foo in (1, 2,3)'
437437
assert f(s) == '\n'.join([
438438
'select foo',
439-
' , bar',
440-
' , baz',
439+
' , bar',
440+
' , baz',
441441
'from table',
442442
'where foo in (1',
443-
' , 2',
444-
' , 3)'])
443+
' , 2',
444+
' , 3)'])
445445

446446
def test_identifier_list_with_functions(self):
447447
f = lambda sql: sqlparse.format(sql, reindent=True)

0 commit comments

Comments
 (0)