File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,10 @@ def get_next_comment():
22
22
def _get_insert_token (token ):
23
23
"""Returns either a whitespace or the line breaks from token."""
24
24
# See issue484 why line breaks should be preserved.
25
- m = re .search (r'((\r\n|\r|\n)+) *$' , token .value )
25
+ # Note: The actual value for a line break is replaced by \n
26
+ # in SerializerUnicode which will be executed in the
27
+ # postprocessing state.
28
+ m = re .search (r'((\r|\n)+) *$' , token .value )
26
29
if m is not None :
27
30
return sql .Token (T .Whitespace .Newline , m .groups ()[0 ])
28
31
else :
Original file line number Diff line number Diff line change @@ -84,6 +84,23 @@ def test_strip_comments_multi(self):
84
84
res = sqlparse .format (sql , strip_comments = True )
85
85
assert res == 'select (select 2)'
86
86
87
+ def test_strip_comments_preserves_linebreak (self ):
88
+ sql = 'select * -- a comment\r \n from foo'
89
+ res = sqlparse .format (sql , strip_comments = True )
90
+ assert res == 'select *\n from foo'
91
+ sql = 'select * -- a comment\n from foo'
92
+ res = sqlparse .format (sql , strip_comments = True )
93
+ assert res == 'select *\n from foo'
94
+ sql = 'select * -- a comment\r from foo'
95
+ res = sqlparse .format (sql , strip_comments = True )
96
+ assert res == 'select *\n from foo'
97
+ sql = 'select * -- a comment\r \n \r \n from foo'
98
+ res = sqlparse .format (sql , strip_comments = True )
99
+ assert res == 'select *\n \n from foo'
100
+ sql = 'select * -- a comment\n \n from foo'
101
+ res = sqlparse .format (sql , strip_comments = True )
102
+ assert res == 'select *\n \n from foo'
103
+
87
104
def test_strip_ws (self ):
88
105
f = lambda sql : sqlparse .format (sql , strip_whitespace = True )
89
106
s = 'select\n * from foo\n \t where ( 1 = 2 )\n '
You can’t perform that action at this time.
0 commit comments