Correctly handle fmt: skip comments without internal spaces (#2970)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Ryan Siu 2022-04-09 16:52:45 -04:00 committed by GitHub
parent 75f99bded3
commit 431bd09e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 2 deletions

View File

@ -10,6 +10,9 @@
<!-- Changes that affect Black's stable style --> <!-- Changes that affect Black's stable style -->
- Fix unstable formatting involving `# fmt: skip` comments without internal spaces
(#2970)
### Preview style ### Preview style
<!-- Changes that affect Black's preview style --> <!-- Changes that affect Black's preview style -->

View File

@ -214,8 +214,11 @@ def generate_ignored_nodes(
container: Optional[LN] = container_of(leaf) container: Optional[LN] = container_of(leaf)
if comment.value in FMT_SKIP: if comment.value in FMT_SKIP:
prev_sibling = leaf.prev_sibling prev_sibling = leaf.prev_sibling
if comment.value in leaf.prefix and prev_sibling is not None: # Need to properly format the leaf prefix to compare it to comment.value,
leaf.prefix = leaf.prefix.replace(comment.value, "") # which is also formatted
comments = list_comments(leaf.prefix, is_endmarker=False, preview=preview)
if comments and comment.value == comments[0].value and prev_sibling is not None:
leaf.prefix = ""
siblings = [prev_sibling] siblings = [prev_sibling]
while ( while (
"\n" not in prev_sibling.prefix "\n" not in prev_sibling.prefix

11
tests/data/fmtskip7.py Normal file
View File

@ -0,0 +1,11 @@
a = "this is some code"
b = 5 #fmt:skip
c = 9 #fmt: skip
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" #fmt:skip
# output
a = "this is some code"
b = 5 # fmt:skip
c = 9 # fmt: skip
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip

View File

@ -44,6 +44,7 @@
"fmtskip4", "fmtskip4",
"fmtskip5", "fmtskip5",
"fmtskip6", "fmtskip6",
"fmtskip7",
"fstring", "fstring",
"function", "function",
"function2", "function2",