@@ -55,9 +55,12 @@ class ProtoComment:
5555 consumed : int # how many characters of the original leaf's prefix did we consume
5656 form_feed : bool # is there a form feed before the comment
5757 leading_whitespace : str # leading whitespace before the comment, if any
58+ original_value : str # original comment text, before normalization
5859
5960
60- def generate_comments (leaf : LN , mode : Mode ) -> Iterator [Leaf ]:
61+ def generate_comments (
62+ leaf : LN , mode : Mode , * , preserve_comment_formatting : bool = False
63+ ) -> Iterator [Leaf ]:
6164 """Clean the prefix of the `leaf` and generate comments from it, if any.
6265
6366 Comments in lib2to3 are shoved into the whitespace prefix. This happens
@@ -82,7 +85,10 @@ def generate_comments(leaf: LN, mode: Mode) -> Iterator[Leaf]:
8285 ):
8386 total_consumed = pc .consumed
8487 prefix = make_simple_prefix (pc .newlines , pc .form_feed )
85- yield Leaf (pc .type , pc .value , prefix = prefix )
88+ value = pc .value
89+ if preserve_comment_formatting :
90+ value = pc .leading_whitespace + pc .original_value
91+ yield Leaf (pc .type , value , prefix = prefix )
8692 normalize_trailing_prefix (leaf , total_consumed )
8793
8894
@@ -127,6 +133,7 @@ def list_comments(prefix: str, *, is_endmarker: bool, mode: Mode) -> list[ProtoC
127133 consumed = consumed ,
128134 form_feed = form_feed ,
129135 leading_whitespace = whitespace ,
136+ original_value = line .rstrip (),
130137 )
131138 )
132139 form_feed = False
0 commit comments