summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2013-04-06 10:43:28 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2013-04-06 10:43:28 +0200
commit1f1c3e3eebf7a089ec04d60d4e25c33ac9dc0f87 (patch)
treea56126b2e50e5d40c209072579bd4eb3830b5b04
parent2150a0aaef8366c97fa4260d3ba376b211981668 (diff)
downloadsqlparse-1f1c3e3eebf7a089ec04d60d4e25c33ac9dc0f87.tar.gz
Detect runaway indentations and go back to first column instead of going crazy.
Even though the resulting statement is not correctly formatted, it's much more usable than before.
-rw-r--r--CHANGES3
-rw-r--r--sqlparse/filters.py8
2 files changed, 10 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index ff2753b..aa94dbd 100644
--- a/CHANGES
+++ b/CHANGES
@@ -15,6 +15,9 @@ Enhancements
statements in certain encodings more reliable (issue20).
* Improve parsing speed when SQL contains CLOBs or BLOBs (issue86).
* Improve formatting of ORDER BY clauses (issue89).
+ * Formatter now tries to detect runaway indentations caused by
+ parsing errors or invalid SQL statements. When re-indenting such
+ statements the formatter flips back to column 0 before going crazy.
Other
* Documentation updates.
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index e238f69..fdb32ac 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -261,7 +261,13 @@ class ReindentFilter:
def nl(self):
# TODO: newline character should be configurable
- ws = '\n' + (self.char * ((self.indent * self.width) + self.offset))
+ space = (self.char * ((self.indent * self.width) + self.offset))
+ # Detect runaway indenting due to parsing errors
+ if len(space) > 200:
+ # something seems to be wrong, flip back
+ self.indent = self.offset = 0
+ space = (self.char * ((self.indent * self.width) + self.offset))
+ ws = '\n' + space
return sql.Token(T.Whitespace, ws)
def _split_kwds(self, tlist):