summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
Diffstat (limited to 'sqlparse')
-rwxr-xr-xsqlparse/cli.py7
-rw-r--r--sqlparse/filters/reindent.py17
-rw-r--r--sqlparse/formatter.py8
3 files changed, 30 insertions, 2 deletions
diff --git a/sqlparse/cli.py b/sqlparse/cli.py
index 9479dd1..bd2e1b8 100755
--- a/sqlparse/cli.py
+++ b/sqlparse/cli.py
@@ -118,6 +118,13 @@ def create_parser():
type=int,
help='Column after which lists should be wrapped')
+ group.add_argument(
+ '--comma_first',
+ dest='comma_first',
+ default=False,
+ type=bool,
+ help='Insert linebreak before comma (default False)')
+
return parser
diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py
index dab4d03..7421f6d 100644
--- a/sqlparse/filters/reindent.py
+++ b/sqlparse/filters/reindent.py
@@ -11,13 +11,15 @@ from sqlparse.utils import offset, indent
class ReindentFilter(object):
- def __init__(self, width=2, char=' ', wrap_after=0, n='\n'):
+ def __init__(self, width=2, char=' ', wrap_after=0, n='\n',
+ comma_first=False):
self.n = n
self.width = width
self.char = char
self.indent = 0
self.offset = 0
self.wrap_after = wrap_after
+ self.comma_first = comma_first
self._curr_stmt = None
self._last_stmt = None
@@ -123,7 +125,20 @@ class ReindentFilter(object):
# Add 1 for the "," separator
position += len(token.value) + 1
if position > (self.wrap_after - self.offset):
+ if self.comma_first:
+ _, comma = tlist.token_prev(
+ tlist.token_index(token))
+ if comma is None:
+ continue
+ token = comma
tlist.insert_before(token, self.nl())
+ if self.comma_first:
+ _, ws = tlist.token_next(
+ tlist.token_index(token), skip_ws=False)
+ if (ws is not None
+ and not ws.ttype is T.Text.Whitespace):
+ tlist.insert_after(
+ token, sql.Token(T.Whitespace, ' '))
position = 0
self._process_default(tlist)
diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py
index 72b50eb..a58d5af 100644
--- a/sqlparse/formatter.py
+++ b/sqlparse/formatter.py
@@ -97,6 +97,11 @@ def validate_options(options):
raise SQLParseError('wrap_after requires a positive integer')
options['wrap_after'] = wrap_after
+ comma_first = options.get('comma_first', False)
+ if comma_first not in [True, False]:
+ raise SQLParseError('comma_first requires a boolean value')
+ options['comma_first'] = comma_first
+
right_margin = options.get('right_margin')
if right_margin is not None:
try:
@@ -148,7 +153,8 @@ def build_filter_stack(stack, options):
stack.stmtprocess.append(
filters.ReindentFilter(char=options['indent_char'],
width=options['indent_width'],
- wrap_after=options['wrap_after']))
+ wrap_after=options['wrap_after'],
+ comma_first=options['comma_first']))
if options.get('reindent_aligned', False):
stack.enable_grouping()