From b8f73564b747cdb250fcc75fa7aa02f56d6484e0 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sat, 17 Sep 2016 15:35:19 +0200 Subject: Better formatting when using comma-first notation (issue141). --- sqlparse/filters/reindent.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py index 7421f6d..ab6c00f 100644 --- a/sqlparse/filters/reindent.py +++ b/sqlparse/filters/reindent.py @@ -43,8 +43,10 @@ class ReindentFilter(object): # Now take current offset into account and return relative offset. return len(line) - len(self.char * self.leading_ws) - def nl(self): - return sql.Token(T.Whitespace, self.n + self.char * self.leading_ws) + def nl(self, offset=0): + return sql.Token( + T.Whitespace, + self.n + self.char * max(0, self.leading_ws + offset)) def _next_token(self, tlist, idx=-1): split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR', @@ -125,13 +127,15 @@ class ReindentFilter(object): # Add 1 for the "," separator position += len(token.value) + 1 if position > (self.wrap_after - self.offset): + adjust = 0 if self.comma_first: + adjust = -2 _, comma = tlist.token_prev( tlist.token_index(token)) if comma is None: continue token = comma - tlist.insert_before(token, self.nl()) + tlist.insert_before(token, self.nl(offset=adjust)) if self.comma_first: _, ws = tlist.token_next( tlist.token_index(token), skip_ws=False) -- cgit v1.2.1