From b32f67ee0ad1571bfc854d9eaf8432f4d0f7da4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sun, 10 Jun 2012 12:12:13 +0200 Subject: Little optimization --- sqlparse/formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sqlparse') diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py index f182850..7c7eaad 100644 --- a/sqlparse/formatter.py +++ b/sqlparse/formatter.py @@ -70,7 +70,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): -- cgit v1.2.1 From 63f6fc8fc8a2ab19f56cfc8cb8e88ec9fab7c305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sun, 10 Jun 2012 13:03:49 +0200 Subject: Added indent tabs on ReindentFilter.nl() --- sqlparse/filters.py | 7 ++++++- sqlparse/formatter.py | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/filters.py b/sqlparse/filters.py index 3bf46d5..b075d39 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -294,7 +294,12 @@ class ReindentFilter: Return an indented new line token """ # TODO: newline character should be configurable - ws = '\n' + self.char * (self.indent * self.width + self.offset) + ws = '\n' + offset = self.indent * self.width + self.offset + if self.char == '\t': + tabs, offset = divmod(offset, self.width) + ws += self.char * tabs + ws += ' ' * offset return sql.Token(T.Whitespace, ws) def _split_kwds(self, tlist): diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py index 7c7eaad..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): @@ -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() -- cgit v1.2.1 From 0269aced73a295dea0a57c49dd77ff73031fcdd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sun, 10 Jun 2012 13:22:28 +0200 Subject: Added _gentabs() method --- sqlparse/filters.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/filters.py b/sqlparse/filters.py index b075d39..3bd492b 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -289,17 +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' - offset = self.indent * self.width + self.offset - if self.char == '\t': - tabs, offset = divmod(offset, self.width) - ws += self.char * tabs - ws += ' ' * offset + ws = '\n' + self._gentabs(self.indent * self.width + self.offset) return sql.Token(T.Whitespace, ws) def _split_kwds(self, tlist): @@ -471,8 +475,9 @@ class ReindentFilter: ignore = False for token in identifiers: if not ignore and not token.ttype: + ws = self._gentabs(offset) tlist.insert_before(token, sql.Token(T.Whitespace, - " " * offset)) + ws)) ignore = token.ttype # Decrease offset the size of the first token -- cgit v1.2.1 From 54cd5c95d9af7037e1b96a726506ceb5df4f43a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sun, 10 Jun 2012 13:52:10 +0200 Subject: Add tabs correctly on identifiers offset --- sqlparse/filters.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/filters.py b/sqlparse/filters.py index 3bd492b..401c436 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -475,9 +475,21 @@ class ReindentFilter: ignore = False for token in identifiers: if not ignore and not token.ttype: - ws = self._gentabs(offset) - tlist.insert_before(token, sql.Token(T.Whitespace, - ws)) + prev = tlist.token_prev(token, False) + if prev and 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) ignore = token.ttype # Decrease offset the size of the first token -- cgit v1.2.1 From 378f7a4b1f0b7a63e19075b9007aae25f272f344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sun, 10 Jun 2012 14:04:58 +0200 Subject: Fixed issue #6 It needs a "hack" at first identifier since we can't be able to access to it's previous whitespace, and also we would need more work to get the offset taking in account the previous tokens, so by the moment we leave only normal whitespaces. --- sqlparse/filters.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/filters.py b/sqlparse/filters.py index 401c436..762f6ef 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -476,20 +476,27 @@ class ReindentFilter: for token in identifiers: if not ignore and not token.ttype: prev = tlist.token_prev(token, False) - if prev and 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 + 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, - self._gentabs(offset)) + ws = sql.Token(T.Whitespace, ' ' * offset) tlist.insert_before(token, ws) + ignore = token.ttype # Decrease offset the size of the first token -- cgit v1.2.1