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

Is it possible to force a newline at the end of the file? #349

Open
mrichards42 opened this issue Feb 27, 2025 · 2 comments
Open

Is it possible to force a newline at the end of the file? #349

mrichards42 opened this issue Feb 27, 2025 · 2 comments

Comments

@mrichards42
Copy link

Hi, thanks for a great tool!

I'm wondering if there's a way to ensure that files end with a newline, while using {:style :respect-nl}. Ideally I'd love to be able to configure zprint to trim all trailing newlines except one and add a trailing newline if it's missing. Based on #100 it looks like I might be able to get this behavior if I use {:parse {:interpose "\n\n"}} but ideally I'd like to not change spacing between forms, just at the end of the file.

I couldn't find this in the docs or in other issues, but sorry if I'm missing something totally obvious. Thanks!

@kkinnear
Copy link
Owner

kkinnear commented Mar 1, 2025

Thanks for asking. There is not, at present, any way to ensure that a file ends with a newline. It would not be hard to ensure that it did. Interestingly, it was kind of hard to generate a file that didn't end with a newline to test this out. I could imagine something like {:output {:force-end-w-nl? true}} as a way to configure this. I would expect this would work regardless of the state of :respect-nl, since at present zprint does not add add a newline to the end of a file regardless of the status of :respect-nl. zprint works hard to be faithful to the incoming file.

Your second request is, I think, to ensure that there is at most one blank line at any point, and not two or more. But to not add a blank line where one does not exist today. What I am not clear on is whether or not this is just at the "top level" of the file, that is the level at which :interpose operates, or whether you mean for this restriction to a single blank line to occur anywhere in the file?

I could see implementing something which would do what you desire regarding blank lines, though the implementation would be very different based on your answer to the above question.

There have not been any requests for either of these capabilities prior to this, which is why they don't exist. My editor won't create a file that doesn't end in a newline, and I don't personally use :respect-nl, though plenty of people clearly do.

@mrichards42
Copy link
Author

Your second request is, I think, to ensure that there is at most one blank line at any point, and not two or more. But to not add a blank line where one does not exist today. What I am not clear on is whether or not this is just at the "top level" of the file, that is the level at which :interpose operates, or whether you mean for this restriction to a single blank line to occur anywhere in the file?

Almost, but I must not have been clear -- I'm looking to have files that end with exactly one newline, so if there is no newline, add one, if there are multiple newlines remove all but one. Otherwise I wouldn't want this to impact any other whitespace setting and only impact just the end of the file (or well, if for some reason the file ended with a bunch of spaces and newlines ideally I'd strip all the trailing whitespace then add a single newline). Although even just "add a newline if there are none and otherwise do nothing" would still be useful if that's easier to implement.

My editor won't create a file that doesn't end in a newline, and I don't personally use :respect-nl, though plenty of people clearly do.

Mine wont either 🙂. This came up in a work setting because we're using a few different editors and at least one of them seems to not add a final newline by default, so we keep ending up with spurious diffs on the last line depending on who edited the file last.

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

No branches or pull requests

2 participants