Skip to content

Commit f45e136

Browse files
committed
Apply BoldKeywordFilter after AlignedIndentFilter
The "<strong>" tokens inserted by the BoldKeywordFilter were causing the AlignedIndentFilter to apply excessive indentation to queries which used CASE statements. Fix by rewriting BoldIndentFilter as a statement filter rather than a preprocess filter, and applying after AlignedIndentFilter.
1 parent ef9cfbb commit f45e136

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

debug_toolbar/panels/sql/utils.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@ def elide_until_from(stream):
5050
class BoldKeywordFilter:
5151
"""sqlparse filter to bold SQL keywords"""
5252

53-
def process(self, stream):
54-
for token_type, value in stream:
55-
is_keyword = token_type in T.Keyword
56-
if is_keyword:
57-
yield T.Other, "<strong>"
58-
yield token_type, value
59-
if is_keyword:
60-
yield T.Other, "</strong>"
53+
def process(self, stmt):
54+
idx = 0
55+
while idx < len(stmt.tokens):
56+
token = stmt[idx]
57+
if token.is_keyword:
58+
stmt.insert_before(idx, sqlparse.sql.Token(T.Other, "<strong>"))
59+
stmt.insert_after(
60+
idx + 1,
61+
sqlparse.sql.Token(T.Other, "</strong>"),
62+
skip_ws=False,
63+
)
64+
idx += 2
65+
elif token.is_group:
66+
self.process(token)
67+
idx += 1
6168

6269

6370
def escaped_value(token):
@@ -112,7 +119,7 @@ def get_filter_stack(*, prettify, simplify):
112119
stack.stmtprocess.append(
113120
sqlparse.filters.AlignedIndentFilter(char="&nbsp;", n="<br/>")
114121
)
115-
stack.preprocess.append(BoldKeywordFilter())
122+
stack.stmtprocess.append(BoldKeywordFilter())
116123
stack.postprocess.append(EscapedStringSerializer()) # Statement -> str
117124
return stack
118125

0 commit comments

Comments
 (0)