summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-06-10 14:13:56 +0200
committerJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-06-10 14:13:56 +0200
commit4983ec8789b3358801243fc7cd92c398674878ea (patch)
tree0da15c957dbbc1309127084fa7b7054fdaa4e931
parent05d1276cf64d875ebc7a34126f096a271d11e4da (diff)
parent378f7a4b1f0b7a63e19075b9007aae25f272f344 (diff)
downloadsqlparse-4983ec8789b3358801243fc7cd92c398674878ea.tar.gz
Merge branch 'milestone_0.1.5' into milestone_0.2.0
Conflicts: sqlparse/filters.py sqlparse/formatter.py
-rw-r--r--sqlparse/filters.py96
-rw-r--r--sqlparse/formatter.py12
-rw-r--r--tests/issues/__init__.py0
-rw-r--r--tests/issues/test_issue_06.py24
-rw-r--r--tests/issues/test_issue_50.py23
5 files changed, 140 insertions, 15 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index 37f0a0b..88fb9d1 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -255,9 +255,18 @@ 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):
# 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):
@@ -334,17 +343,80 @@ class ReindentFilter:
self.offset -= num_offset
def _process_identifierlist(self, tlist):
- identifiers = list(tlist.get_identifiers())
- if len(identifiers) > 1 and not tlist.within(sql.Function):
- first = list(identifiers[0].flatten())[0]
- num_offset = self._get_offset(first) - len(first.value)
- self.offset += num_offset
- for token in identifiers[1:]:
- tlist.insert_before(token, self.nl())
- for token in tlist.tokens:
- if isinstance(token, sql.Comment):
- tlist.insert_after(token, self.nl())
- self.offset -= num_offset
+ """
+ Process an identifier list
+
+ If there are more than an identifier, put each on a line
+ """
+ # Split the identifier list if we are not in a function
+ if not tlist.within(sql.Function):
+ # Get identifiers from the tlist
+ identifiers = list(tlist.get_identifiers())
+ # Split the identifier list if we have more than one identifier
+ if len(identifiers) > 1:
+ # Get first token
+ first = list(identifiers[0].flatten())[0]
+
+ # Increase offset the size of the first token
+ num_offset = self._get_offset(first) - len(first.value)
+
+ # Increase offset and insert new lines
+ self.offset += num_offset
+ offset = 0
+
+ # Insert a new line between the tokens
+ ignore = False
+ for token in identifiers[1:]:
+ if not ignore:
+ tlist.insert_before(token, self.nl())
+ ignore = token.ttype
+
+ # Check identifiers offset
+ if token.ttype:
+ l = len(token.value)
+ if offset < l:
+ offset = l
+
+ # Imsert another new line after comment tokens
+ for token in tlist.tokens:
+ if isinstance(token, sql.Comment):
+ tlist.insert_after(token, self.nl())
+
+ # Update identifiers offset
+ if offset:
+ offset += 1
+
+ ignore = False
+ for token in identifiers:
+ if not ignore and not token.ttype:
+ 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
+ self.offset -= num_offset
+
+ # Process the identifier list as usual
self._process_default(tlist)
def _process_case(self, tlist):
diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py
index 75f21a8..edbafa1 100644
--- a/sqlparse/formatter.py
+++ b/sqlparse/formatter.py
@@ -13,6 +13,9 @@ class SQLParseError(Exception):
"""Base class for exceptions in this module."""
+INDENT_WIDTH = 2
+
+
def validate_options(options):
"""Validates options."""
kwcase = options.get('keyword_case', None)
@@ -50,7 +53,9 @@ def validate_options(options):
options['indent_char'] = '\t'
else:
options['indent_char'] = ' '
- indent_width = options.get('indent_width', 2)
+
+ # indent_width
+ indent_width = options.get('indent_width', INDENT_WIDTH)
try:
indent_width = int(indent_width)
except (TypeError, ValueError):
@@ -60,7 +65,7 @@ def validate_options(options):
options['indent_width'] = indent_width
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):
@@ -102,7 +107,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/issues/test_issue_50.py b/tests/issues/test_issue_50.py
new file mode 100644
index 0000000..d61b79a
--- /dev/null
+++ b/tests/issues/test_issue_50.py
@@ -0,0 +1,23 @@
+'''
+Created on 18/05/2012
+
+@author: piranna
+'''
+
+from unittest import main, TestCase
+
+from sqlparse import format
+
+
+class Issue_50(TestCase):
+ def test_issue(self):
+ result = format("SELECT foo, null bar, car FROM dual", reindent=True)
+ self.assertEqual(result, "SELECT foo,\n"
+ " null bar,\n"
+ " car\n"
+ "FROM dual")
+
+
+if __name__ == "__main__":
+ #import sys;sys.argv = ['', 'Test.testName']
+ main() \ No newline at end of file