-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Formatter fails on string interpolation #6565
Comments
|
@makenowjust It's ok if you try and succeed. However, I don't think it's really important to fix this, there's no point in interpolating a string literal. This is just an edge-case. |
@asterite I understand what you said as that you must not add a complex change to fix an edge-case. I'd keep in mind. Thank you. |
Sure, this is definitely a corner case. It's one of those things that could really trip people up and give them a bad experience for the language. I ran into the problem because I was writing some test code that would be refactored later. The interpolated string was just a placeholder that would be substituted out later. And, having the formatter blowing up or not formatting code on a perfectly valid syntax is just not a very good experience. It took me a good 15-20 minutes to track down the problem. I agree that this is probably not the highest priority bug, but it probably should be fixed at some point. |
We should probably fix this by keeping the interpolation instead of just generating a string literal. I'll fix it later today. |
…ions Fixed crystal-lang#6565 Currently `"foo#{"bar"}baz"` is parsed as `StringLiteral` with joining all parts. It is bad for formatting, so this changes it is parsed as `StringInterpolation`. Above example's `expressions` property is now `["foo", "bar", "baz"]`. In addition, this changes `StringInterpolation` ensure to interleave string literals with interpolations. In other words, it ensures `expressions` odd-index elements are string literals and even-index elements are interpolations. For example `"#{123}"` is parsed as `StringInterpolation` and its `expressions` property is `["", 123, ""]`. It is good for formatting. Other changes in this are for keeping back-compatibility and formatter improvement.
I implemented two solutions for this:
The first diff has 101 additions and 74 deletions, but the second diff has 6 additions and 4 deletions. Modifying parser to keep interpolation affects in many places unfortunately, for example macro expansion. @asterite I guess you would fix like the first, however I can't think it is better than the simple solution. Additional comment by implementer: this is just formatter bug, not related to parser. I'm sure it cannot be fixed without formatter fix, even if the parser keeps interpolations. We can fix parser to keep interpolation after fixed this. I prefer to apply simple and perfect solution first, then consider to merge complex solution after. |
@makenowjust thank you! I vote to keep the simpler fix. Does it work with a string interpolation that has many string literals interpolated, with string content too? For example |
I strongly disagree. It is valid code and the formatter should support all valid code. |
@asterite Of course, yes. I'll add this to spec then open pull request. |
* Format: fix formatting StringLiteral in StringInterpolation Fixed #6565 * Add more complex specs
A simple running Crystal program (
bug.cr
):However, the formatter will fail when it tries to format
bug.cr
:Crystal version:
The text was updated successfully, but these errors were encountered: