Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent squeezing of hanging comments inside expressions #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

effigies
Copy link
Contributor

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 effigies changed the title Track whitespace and preserve it by default Prevent squeezing of hanging comments inside expressions Sep 15, 2022
@effigies
Copy link
Contributor Author

effigies commented Sep 19, 2022

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

@effigies
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hanging comments inside multi-line expressions are squeezed
1 participant