-
-
Notifications
You must be signed in to change notification settings - Fork 261
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(fmt): Preserve multiple newlines between elements (#374) #919
base: main
Are you sure you want to change the base?
fix(fmt): Preserve multiple newlines between elements (#374) #919
Conversation
I found a case that I'd like to include before potentially accepting this PR:
The space should have been preserved between |
I also found that this throws an error in my own project, due to the first
EDIT: I've found that I've missed adding checks where there's normally a // Normalize whitespace for minified output. In HTML, a single space is equivalent to
// any number of spaces, tabs, or newlines.
if n == parser.SpaceVertical {
n = parser.SpaceHorizontal
} This should also do the same for Also, this switch statement must include a switch trailing {
case SpaceNone:
level = 0
case SpaceHorizontal:
level = 0
case SpaceVertical:
level = startLevel
} |
@joerdav Before I go and preserve newlines for statements like You can see how I added the logic to I believe consistency is important, so I think preserving newlines should work as you expect for all types of statements or expressions, but let me know your thoughts. |
Hey @AlexanderArvidsson , thanks for all the time on this so far, I've looked at the code here and am happy with how you have approached this. I agree with you that due to consistency I think that we should support trailing spaces for all elements. I think going ahead with this pattern is a solid approach. One to think about is that the following code block may end up repeated a bit so could maybe be made into a function (or not since I suppose it isn't a lot of code):
|
@joerdav The So far, I just followed the "don't repeat yourself more than twice" philosophy, and just copied that small piece of code twice. But for more uses, I would definitely put it in a function. I'll get on adding support for the rest of the elements and try and find all cases. Also, tests seem to be failing, even locally (I think I had only ran the parser tests, not generate ones), so I'll get on fixing that as well. |
@joerdav I've went ahead and added trailing space logic to most parsers and added relevant tests. Please take an extra look at how I unified the trailing space logic in Other than that, from my point of view, this is ready to be tested! |
Amazing, thanks! I've tested this out on a fairly large repo at my company and as you said there is no effect to existing code. I also have read the trailing space interface logic, I think it is done sensibly! One note I have is on the Going further, I wonder if we need the interface at all? If the trailing space function looked like: It could be used as:
What do you think? Also in general, I think it would be worth @a-h casting his eyes over this. |
You're absolutely right! I originally did have uses for the other values, but as I learned more about the codebase I started removing them. I think your way is much cleaner. I'll make those changes :) Good to hear that you didn't see any effect on the existing code! :) |
All good fair enough, I know the feeling! You go on a journey with the code and end up with designs based off previous iterations more than your current understanding. And yep no issues with the existing code! That's 165 templates to be exact :) |
Preserves multiple lines between elements, to allow organizing code better.
For reference and discussion, see #374.
templ fmt
is inconsistent with itself, sometimes preserving newlines (collapsing) and sometimes not.This aligns
templ fmt
withgofmt
, which also preserves newlines (by collapsing multiple standalone newlines into one), whichtempl fmt
also does in go functions.This change should be backwards compatible (as in, it shouldn't modify existing code bases since they all should already strip all newlines). But the best way to guarantee that is to run this fork on a big codebase and observe what it does.
Other than that, this PR also includes a couple of tests for this, both for the parser and the formatter output. I can add more formatter tests if needed, in case we find some edge cases.