grantjenks / blue

The slightly less uncompromising Python code formatter.
https://blue.readthedocs.io/
Other
387 stars 21 forks source link

Prevent squeezing of hanging comments inside expressions #83

Open effigies opened 2 years ago

effigies commented 2 years ago

Closes #77 and fixes the CI.

This takes us one step further out from list_comments. The idea here is that the initial parser considers comments and whitespace-only lines a "prefix" to an actual token (significant newlines get their own token). generate_comments() turns each comment into its own Leaf node. The previous code worked because expression-terminating newlines are a token in their own right in Python, so comments that hung on the end of expressions had no newline before the next token.

With this proposal, instead of trying to figure out when it's safe to preserve the whitespace in front of a comment, we just note how much whitespace there was (adjusting for Black's auto-added two spaces). Then, we add that to the prefix (after any newlines), black has the option of respecting it or blowing it away, if it's going to break up the line.

As in the previous approach, this makes no attempt to compare the alignment across lines and only preserve extra whitespace when it actually does do alignment.

effigies commented 1 year ago

@warsaw Just wanted to make sure you saw this, and I would be interested in your thoughts. Absolutely no rush, though.

effigies commented 1 year ago

Gentle bump on this. I often have some more free time over the holidays, so would be able to iterate fairly quickly if there are changes that need to be made.