Skip to content

Commit 0980a30

Browse files
authored
Add comment fix (#102)
1 parent bc667d6 commit 0980a30

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

rewrite/rewrite/python/_parser_visitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
NameTree, OmitParentheses, Expression, TypeTree, TypedTree, Comment
1616
from rewrite.java import tree as j
1717
from . import tree as py
18-
from .support_types import PyComment
1918
from .markers import KeywordArguments, KeywordOnlyArguments, Quoted
19+
from .support_types import PyComment
2020

2121
T = TypeVar('T')
2222
J2 = TypeVar('J2', bound=J)
@@ -2167,7 +2167,7 @@ def __format(self, source: str, offset: int, stop: Optional[str] = None) -> Tupl
21672167
whitespace.append(char)
21682168
elif char == '#':
21692169
if comments:
2170-
comments[-1] = comments[-1].with_suffix('\n' + ''.join(whitespace))
2170+
comments[-1] = comments[-1].with_suffix(''.join(whitespace))
21712171
else:
21722172
prefix = ''.join(whitespace)
21732173
whitespace = []

rewrite/tests/python/all/comment_test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,61 @@ def test_comment():
88

99
def test_windows_line_endings():
1010
rewrite_run(python("assert 1 # type: foo\r\n"))
11+
12+
13+
def test_multiline_comment():
14+
# language=python
15+
rewrite_run(python("""
16+
'''
17+
This is a
18+
multiline comment
19+
'''
20+
assert 1
21+
"""))
22+
23+
24+
def test_multiline_comment_with_code():
25+
# language=python
26+
rewrite_run(python("""
27+
# This is a
28+
# multiline comment
29+
assert 1
30+
# This is another
31+
# multiline comment
32+
"""))
33+
34+
35+
def test_multiline_comment_in_class():
36+
# language=python
37+
rewrite_run(python("""
38+
class ExampleClass:
39+
'''
40+
This is a
41+
multiline comment
42+
inside a class
43+
'''
44+
def example_method(self):
45+
def example_function():
46+
'''
47+
This is a
48+
multiline comment
49+
inside a function
50+
'''
51+
assert 1
52+
"""))
53+
54+
55+
def test_multiline_comment_with_hash():
56+
# language=python
57+
rewrite_run(python("""
58+
class ExampleClass:
59+
# This is a
60+
# multiline comment
61+
# inside a class
62+
def example_method(self):
63+
def example_function():
64+
# This is a
65+
# multiline comment
66+
# inside a function
67+
assert 1
68+
"""))

0 commit comments

Comments
 (0)