summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorSoloman Weng <soloman1124@gmail.com>2018-03-26 08:54:59 +1000
committerSoloman Weng <soloman1124@gmail.com>2018-03-26 08:54:59 +1000
commitfb52a01b6467896c38872046ca06ac6a38353ebb (patch)
tree6b60be2b028a3fb6434cf950e84436ff18cfd788 /sqlparse
parent3013ef4826ce8f7bf46c450debe816c9fcae2a05 (diff)
downloadsqlparse-fb52a01b6467896c38872046ca06ac6a38353ebb.tar.gz
Wrap long function
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/filters/reindent.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py
index 49c7807..45d1270 100644
--- a/sqlparse/filters/reindent.py
+++ b/sqlparse/filters/reindent.py
@@ -24,6 +24,7 @@ class ReindentFilter(object):
self.indent_columns = indent_columns
self._curr_stmt = None
self._last_stmt = None
+ self._last_func = None
def _flatten_up_to_token(self, token):
"""Yields all tokens up to token but excluding current."""
@@ -118,6 +119,10 @@ class ReindentFilter(object):
with offset(self, self._get_offset(first) + 1):
self._process_default(tlist, not is_dml_dll)
+ def _process_function(self, tlist):
+ self._last_func = tlist[0]
+ self._process_default(tlist)
+
def _process_identifierlist(self, tlist):
identifiers = list(tlist.get_identifiers())
if self.indent_columns:
@@ -126,6 +131,7 @@ class ReindentFilter(object):
else:
first = next(identifiers.pop(0).flatten())
num_offset = 1 if self.char == '\t' else self._get_offset(first)
+
if not tlist.within(sql.Function):
with offset(self, num_offset):
position = 0
@@ -150,6 +156,31 @@ class ReindentFilter(object):
tlist.insert_after(
token, sql.Token(T.Whitespace, ' '))
position = 0
+ else:
+ # ensure whitespace
+ for token in tlist:
+ _, next_ws = tlist.token_next(
+ tlist.token_index(token), skip_ws=False)
+ if token.value == ',' and not next_ws.is_whitespace:
+ tlist.insert_after(
+ token, sql.Token(T.Whitespace, ' '))
+
+ end_at = self.offset + sum(len(i.value) + 1 for i in identifiers)
+ adjusted_offset = 0
+ if end_at > (self.wrap_after - self.offset) and self._last_func:
+ adjusted_offset = -len(self._last_func.value) - 1
+
+ with offset(self, adjusted_offset), indent(self):
+ if adjusted_offset < 0:
+ tlist.insert_before(identifiers[0], self.nl())
+ position = 0
+ for token in identifiers:
+ # Add 1 for the "," separator
+ position += len(token.value) + 1
+ if position > (self.wrap_after - self.offset):
+ adjust = 0
+ tlist.insert_before(token, self.nl(offset=adjust))
+ position = 0
self._process_default(tlist)
def _process_case(self, tlist):