diff options
author | Jesús Leganés Combarro <piranna@gmail.com> | 2012-06-10 05:37:08 -0700 |
---|---|---|
committer | Jesús Leganés Combarro <piranna@gmail.com> | 2012-06-10 05:37:08 -0700 |
commit | 89ef6078335ee9ec8cf816b0feb2668b4456c368 (patch) | |
tree | 4ecb8158f880382874875b52640a54bf6df25900 | |
parent | 1dfef65f69db96649f82db4173a8dad87dda4e0a (diff) | |
parent | 378f7a4b1f0b7a63e19075b9007aae25f272f344 (diff) | |
download | sqlparse-milestone_0.1.5.tar.gz |
Merge pull request #70 from piranna/milestone_0.1.5milestone_0.1.5
Milestone 0.1.5
-rw-r--r-- | sqlparse/filters.py | 35 | ||||
-rw-r--r-- | sqlparse/formatter.py | 10 | ||||
-rw-r--r-- | tests/issues/__init__.py | 0 | ||||
-rw-r--r-- | tests/issues/test_issue_06.py | 24 | ||||
-rw-r--r-- | tests/issues/test_issue_50.py (renamed from tests/test_issue_50.py) | 0 |
5 files changed, 63 insertions, 6 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py index 3bf46d5..762f6ef 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -289,12 +289,21 @@ class ReindentFilter: full_offset = len(line) - len(self.char * self.width * self.indent) return full_offset - self.offset + def _gentabs(self, offset): + result = '' + if self.char == '\t': + tabs, offset = divmod(offset, self.width) + result += self.char * tabs + result += ' ' * offset + + return result + def nl(self): """ Return an indented new line token """ # TODO: newline character should be configurable - ws = '\n' + self.char * (self.indent * self.width + self.offset) + ws = '\n' + self._gentabs(self.indent * self.width + self.offset) return sql.Token(T.Whitespace, ws) def _split_kwds(self, tlist): @@ -466,8 +475,28 @@ class ReindentFilter: ignore = False for token in identifiers: if not ignore and not token.ttype: - tlist.insert_before(token, sql.Token(T.Whitespace, - " " * offset)) + prev = tlist.token_prev(token, False) + if prev: + if prev.ttype == T.Whitespace: + value = prev.value + + spaces = 0 + while value and value[-1] == ' ': + value = value[:-1] + spaces += 1 + + value += self._gentabs(spaces + offset) + prev.value = value + else: + ws = sql.Token(T.Whitespace, + self._gentabs(offset)) + tlist.insert_before(token, ws) + + # Just first identifier + else: + ws = sql.Token(T.Whitespace, ' ' * offset) + tlist.insert_before(token, ws) + ignore = token.ttype # Decrease offset the size of the first token diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py index f182850..39e5d28 100644 --- a/sqlparse/formatter.py +++ b/sqlparse/formatter.py @@ -9,6 +9,9 @@ from sqlparse import SQLParseError from sqlparse import filters +INDENT_WIDTH = 2 + + def validate_options(options): """Validates options.""" @@ -57,7 +60,7 @@ def validate_options(options): options['indent_char'] = ' ' # indent_width - indent_width = options.get('indent_width', 2) + indent_width = options.get('indent_width', INDENT_WIDTH) try: indent_width = int(indent_width) except (TypeError, ValueError): @@ -70,7 +73,7 @@ def validate_options(options): # right_margin right_margin = options.get('right_margin', None) - if right_margin is not None: + if right_margin: try: right_margin = int(right_margin) except (TypeError, ValueError): @@ -112,7 +115,8 @@ def build_filter_stack(stack, options): stack.enable_grouping() stack.stmtprocess.append( filters.ReindentFilter(char=options['indent_char'], - width=options['indent_width'])) + width=options['indent_width'], + line_width=options['right_margin'])) if options.get('right_margin', False): stack.enable_grouping() diff --git a/tests/issues/__init__.py b/tests/issues/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/issues/__init__.py diff --git a/tests/issues/test_issue_06.py b/tests/issues/test_issue_06.py new file mode 100644 index 0000000..6226c90 --- /dev/null +++ b/tests/issues/test_issue_06.py @@ -0,0 +1,24 @@ +''' +Created on 18/05/2012 + +@author: piranna +''' + +from unittest import main, TestCase + +from sqlparse import format + + +class Issue_06(TestCase): + def test_issue(self): + result = format("SELECT foo, null bar, car FROM dual", reindent=True, + indent_tabs=True) + self.assertEqual(result, "SELECT foo,\n" + "\t\t\t null bar,\n" + "\t\t\t\t\t\tcar\n" + "FROM dual") + + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + main()
\ No newline at end of file diff --git a/tests/test_issue_50.py b/tests/issues/test_issue_50.py index d61b79a..d61b79a 100644 --- a/tests/test_issue_50.py +++ b/tests/issues/test_issue_50.py |