Skip to content

Commit a5196e6

Browse files
authored
fix: Don't normalize whitespace before fmt:skip comments (#4146)
Signed-off-by: RedGuy12 <[email protected]>
1 parent 59b9d85 commit a5196e6

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ release:
5656
- Format module docstrings the same as class and function docstrings (#4095)
5757
- Fix crash when using a walrus in a dictionary (#4155)
5858
- Fix unnecessary parentheses when wrapping long dicts (#4135)
59+
- Stop normalizing spaces before `# fmt: skip` comments (#4146)
5960

6061
### Configuration
6162

src/black/comments.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from functools import lru_cache
44
from typing import Collection, Final, Iterator, List, Optional, Tuple, Union
55

6-
from black.mode import Mode
6+
from black.mode import Mode, Preview
77
from black.nodes import (
88
CLOSING_BRACKETS,
99
STANDALONE_COMMENT,
@@ -46,6 +46,7 @@ class ProtoComment:
4646
newlines: int # how many newlines before the comment
4747
consumed: int # how many characters of the original leaf's prefix did we consume
4848
form_feed: bool # is there a form feed before the comment
49+
leading_whitespace: str # leading whitespace before the comment, if any
4950

5051

5152
def generate_comments(leaf: LN) -> Iterator[Leaf]:
@@ -88,7 +89,9 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
8889
form_feed = False
8990
for index, full_line in enumerate(re.split("\r?\n", prefix)):
9091
consumed += len(full_line) + 1 # adding the length of the split '\n'
91-
line = full_line.lstrip()
92+
match = re.match(r"^(\s*)(\S.*|)$", full_line)
93+
assert match
94+
whitespace, line = match.groups()
9295
if not line:
9396
nlines += 1
9497
if "\f" in full_line:
@@ -113,6 +116,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
113116
newlines=nlines,
114117
consumed=consumed,
115118
form_feed=form_feed,
119+
leading_whitespace=whitespace,
116120
)
117121
)
118122
form_feed = False
@@ -230,7 +234,11 @@ def convert_one_fmt_off_pair(
230234
standalone_comment_prefix += fmt_off_prefix
231235
hidden_value = comment.value + "\n" + hidden_value
232236
if _contains_fmt_skip_comment(comment.value, mode):
233-
hidden_value += " " + comment.value
237+
hidden_value += (
238+
comment.leading_whitespace
239+
if Preview.no_normalize_fmt_skip_whitespace in mode
240+
else " "
241+
) + comment.value
234242
if hidden_value.endswith("\n"):
235243
# That happens when one of the `ignored_nodes` ended with a NEWLINE
236244
# leaf (possibly followed by a DEDENT).

src/black/mode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class Preview(Enum):
174174
string_processing = auto()
175175
hug_parens_with_braces_and_square_brackets = auto()
176176
unify_docstring_detection = auto()
177+
no_normalize_fmt_skip_whitespace = auto()
177178
wrap_long_dict_values_in_parens = auto()
178179
multiline_string_handling = auto()
179180

tests/data/cases/fmtskip9.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# flags: --preview
2+
print () # fmt: skip
3+
print () # fmt:skip
4+
5+
6+
# output
7+
8+
print () # fmt: skip
9+
print () # fmt:skip

0 commit comments

Comments
 (0)