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

Fix comments with brackets #456

Merged
merged 2 commits into from
Aug 19, 2023

Conversation

flying-sheep
Copy link
Contributor

@flying-sheep flying-sheep commented Aug 11, 2023

Fixes #455

@ia0
Copy link
Collaborator

ia0 commented Aug 12, 2023

There's actually an easy way to fix this, but I don't know if there are other consequences.

Just remove this if and always execute its content:

if !formatted.ends_with(']') {
formatted
.extend(options.newlines(newline_count.saturating_sub(skip_newlines)));
}

This if looks like a hack to detect if an array just finished, but the closing bracket could be part of a comment. I don't know the situations when this if is false. It would be nice to figure out and add a test. If it can never happen then we're good. If it can happen, then we need to introduce a new variable to remember if we just closed a bracket outside a comment (probably 3 more lines of code and a bit hacky).

@flying-sheep flying-sheep marked this pull request as ready for review August 14, 2023 07:31
@flying-sheep
Copy link
Contributor Author

flying-sheep commented Aug 14, 2023

That condition seems to be never hit in any of the tests.

The intention seems to be that if an array contains an array with a trailing comment like this:

array_of_arrays = [
   [1, 2]  # comment
]

… that array is supposed to be kept in the same line. But seems like that happens in any case?

Anyway, this is a high priority fix since it actually protects data from being silently deleted, so I think not much time should be wasted to merge it speculating about possible follow up issues. If there is one, it can just be fixed later while taking care to not break the regression test I added here, no?


Other thought: then this line is also wrong, but at least it doesn’t destroy data. it just messes up formatting in the (more rare) case of a comment ending with [)

if multiline && formatted.ends_with('[') {

@panekj

This comment was marked as off-topic.

Copy link
Collaborator

@ia0 ia0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to play around to find when the if does not trigger but couldn't. So I agree we can move forward and wait until someone comes up with an example. In the absence of a test breaking, I think we can say that this is not an important feature that is being removed.

I don't think the [ case is similar, because it can't come after a comment. The only thing that can follow a comment is a newline by definition of a comment. So only the occurrence in the NEWLINE case is actually problematic.

@ia0 ia0 requested a review from panekj August 14, 2023 18:54
@flying-sheep
Copy link
Contributor Author

could we get a release with this?

@ia0
Copy link
Collaborator

ia0 commented Aug 18, 2023

could we get a release with this?

Ideally, I'd like #434 to get fixed before releasing. It's an important blocker for releasing to VS Code. Feel free to take a look at it if you're familiar with VS Code. PRs are welcome. Thanks!

@flying-sheep
Copy link
Contributor Author

I fixed that one, see #462

@panekj panekj merged commit 051ce9f into tamasfe:master Aug 19, 2023
4 checks passed
@flying-sheep flying-sheep deleted the fix-comment-with-bracket branch August 19, 2023 20:17
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.

Formatter destoys data
3 participants