diff options
author | Johannes Hoff <johshoff@gmail.com> | 2017-06-20 20:18:15 -0700 |
---|---|---|
committer | Johannes Hoff <johshoff@gmail.com> | 2017-06-20 20:18:15 -0700 |
commit | 7cdefe9aa751fa54831cc1e64fbf4b16acc6c90e (patch) | |
tree | fe73b9a2cc3c66f70b66c21ac4122c61958409e0 /sqlparse | |
parent | dc788ab5b1af2e2a4a988b31a09d8d6656853f76 (diff) | |
download | sqlparse-7cdefe9aa751fa54831cc1e64fbf4b16acc6c90e.tar.gz |
Option to indent after first line
This adds a command line argument `--indent_after_first` which sets the
indentation of all lines after the first one, e.g. SELECT, UPDATE, etc. For
example:
$ sqlparse/__main__.py -r sample.sql
UPDATE foo
SET a = 1
WHERE a > 2
AND a < 10;
$ sqlparse/__main__.py -r --indent_after_first sample.sql
UPDATE foo
SET a = 1
WHERE a > 2
AND a < 10;
Diffstat (limited to 'sqlparse')
-rwxr-xr-x | sqlparse/cli.py | 7 | ||||
-rw-r--r-- | sqlparse/filters/reindent.py | 4 | ||||
-rw-r--r-- | sqlparse/formatter.py | 6 |
3 files changed, 15 insertions, 2 deletions
diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 0b5c204..0f66d90 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -102,6 +102,13 @@ def create_parser(): help='indentation width (defaults to 2 spaces)') group.add_argument( + '--indent_after_first', + dest='indent_after_first', + action='store_true', + default=False, + help='indent after first line of statement (e.g. SELECT)') + + group.add_argument( '-a', '--reindent_aligned', action='store_true', default=False, diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py index f077849..c2bc349 100644 --- a/sqlparse/filters/reindent.py +++ b/sqlparse/filters/reindent.py @@ -12,11 +12,11 @@ from sqlparse.utils import offset, indent class ReindentFilter(object): def __init__(self, width=2, char=' ', wrap_after=0, n='\n', - comma_first=False): + comma_first=False, indent_after_first = False): self.n = n self.width = width self.char = char - self.indent = 0 + self.indent = 1 if indent_after_first else 0 self.offset = 0 self.wrap_after = wrap_after self.comma_first = comma_first diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py index a58d5af..b36fe56 100644 --- a/sqlparse/formatter.py +++ b/sqlparse/formatter.py @@ -70,6 +70,11 @@ def validate_options(options): elif reindent_aligned: options['strip_whitespace'] = True + indent_after_first = options.get('indent_after_first', False) + if indent_after_first not in [True, False]: + raise SQLParseError('Invalid value for indent_after_first: ' + '{0!r}'.format(indent_after_first)) + indent_tabs = options.get('indent_tabs', False) if indent_tabs not in [True, False]: raise SQLParseError('Invalid value for indent_tabs: ' @@ -153,6 +158,7 @@ def build_filter_stack(stack, options): stack.stmtprocess.append( filters.ReindentFilter(char=options['indent_char'], width=options['indent_width'], + indent_after_first=options['indent_after_first'], wrap_after=options['wrap_after'], comma_first=options['comma_first'])) |