From: Eddie Darling Date: Sun, 1 Oct 2023 22:35:42 +0000 (-0700) Subject: Fix comments getting removed from inside parenthesized strings (#3909) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/a91eb73064c9bef76c3a961ab662bb5f75a1543d?hp=e7c3368c1316c38338cef34fffc42ea3252b1802 Fix comments getting removed from inside parenthesized strings (#3909) Since the id of the old leaf may be the key to comments, the new leaf must adopt the old comments --- diff --git a/CHANGES.md b/CHANGES.md index a879ab3..028a01a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ +- Fix comments getting removed from inside parenthesized strings (#3909) + ### Configuration diff --git a/src/black/trans.py b/src/black/trans.py index daed264..c0cc926 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -942,6 +942,9 @@ class StringParenStripper(StringTransformer): LL[lpar_or_rpar_idx].remove() # Remove lpar. replace_child(LL[idx], string_leaf) new_line.append(string_leaf) + # replace comments + old_comments = new_line.comments.pop(id(LL[idx]), []) + new_line.comments.setdefault(id(string_leaf), []).extend(old_comments) else: LL[lpar_or_rpar_idx].remove() # This is a rpar. diff --git a/tests/data/preview/comments7.py b/tests/data/preview/comments7.py index 8b12240..0655de9 100644 --- a/tests/data/preview/comments7.py +++ b/tests/data/preview/comments7.py @@ -131,6 +131,18 @@ class C: square = Square(4) # type: Optional[Square] +# Regression test for https://github.com/psf/black/issues/3756. +[ + ( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ), +] +[ + ( # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ), +] + # output from .config import ( @@ -282,3 +294,15 @@ class C: square = Square(4) # type: Optional[Square] + +# Regression test for https://github.com/psf/black/issues/3756. +[ + ( # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + ), +] +[ + ( # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ), +]