zprint icon indicating copy to clipboard operation
zprint copied to clipboard

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

Open mrichards42 opened this issue 10 months ago • 4 comments

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 https://github.com/kkinnear/zprint/issues/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!

mrichards42 avatar Feb 27 '25 14:02 mrichards42

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.

kkinnear avatar Mar 01 '25 18:03 kkinnear

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.

mrichards42 avatar Mar 04 '25 01:03 mrichards42

Ok, thanks. My takeaway from this is that ideally you would like a feature that ensures that there is exactly one newline after the last printable character in the file. Add that newline if necessary, and remove any additional newlines or spaces after that last printable character. I'm thinking {:output {:end-w-single-nl? true}} as a possible signal to do this.

kkinnear avatar Mar 04 '25 17:03 kkinnear

Yep, exactly, and that configuration looks reasonable to me. Thanks for considering it!

mrichards42 avatar Mar 04 '25 22:03 mrichards42