Skip to content

Commit fb52a01

Browse files
committed
Wrap long function
1 parent 3013ef4 commit fb52a01

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

sqlparse/filters/reindent.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, width=2, char=' ', wrap_after=0, n='\n',
2424
self.indent_columns = indent_columns
2525
self._curr_stmt = None
2626
self._last_stmt = None
27+
self._last_func = None
2728

2829
def _flatten_up_to_token(self, token):
2930
"""Yields all tokens up to token but excluding current."""
@@ -118,6 +119,10 @@ def _process_parenthesis(self, tlist):
118119
with offset(self, self._get_offset(first) + 1):
119120
self._process_default(tlist, not is_dml_dll)
120121

122+
def _process_function(self, tlist):
123+
self._last_func = tlist[0]
124+
self._process_default(tlist)
125+
121126
def _process_identifierlist(self, tlist):
122127
identifiers = list(tlist.get_identifiers())
123128
if self.indent_columns:
@@ -126,6 +131,7 @@ def _process_identifierlist(self, tlist):
126131
else:
127132
first = next(identifiers.pop(0).flatten())
128133
num_offset = 1 if self.char == '\t' else self._get_offset(first)
134+
129135
if not tlist.within(sql.Function):
130136
with offset(self, num_offset):
131137
position = 0
@@ -150,6 +156,31 @@ def _process_identifierlist(self, tlist):
150156
tlist.insert_after(
151157
token, sql.Token(T.Whitespace, ' '))
152158
position = 0
159+
else:
160+
# ensure whitespace
161+
for token in tlist:
162+
_, next_ws = tlist.token_next(
163+
tlist.token_index(token), skip_ws=False)
164+
if token.value == ',' and not next_ws.is_whitespace:
165+
tlist.insert_after(
166+
token, sql.Token(T.Whitespace, ' '))
167+
168+
end_at = self.offset + sum(len(i.value) + 1 for i in identifiers)
169+
adjusted_offset = 0
170+
if end_at > (self.wrap_after - self.offset) and self._last_func:
171+
adjusted_offset = -len(self._last_func.value) - 1
172+
173+
with offset(self, adjusted_offset), indent(self):
174+
if adjusted_offset < 0:
175+
tlist.insert_before(identifiers[0], self.nl())
176+
position = 0
177+
for token in identifiers:
178+
# Add 1 for the "," separator
179+
position += len(token.value) + 1
180+
if position > (self.wrap_after - self.offset):
181+
adjust = 0
182+
tlist.insert_before(token, self.nl(offset=adjust))
183+
position = 0
153184
self._process_default(tlist)
154185

155186
def _process_case(self, tlist):

0 commit comments

Comments
 (0)