feat: add /*nixfmt:disable*/ and /*nixfmt:enable*/ comment directives#388
feat: add /*nixfmt:disable*/ and /*nixfmt:enable*/ comment directives#388dyegoaurelio wants to merge 2 commits into
Conversation
48901f2 to
dd2f2e4
Compare
|
I implemented this feature. Fuzzer couldn't find any issues. I will wait for a release of nixfmt-rs until the pr has been accepted in this repo. |
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/formatting-team-meeting-2026-05-12/77586/1 |
|
I reworked my code to now emit comment directives and remove the post processing code. I also noticed some idempotency issue with directives inside |
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/formatting-team-meeting-2026-05-26/77856/1 |
|
This is a really needed feature, the only thing we need to make a wider adoption for non-nixpkgs folks, where certain aligned stuff should stay as it is, due to verbosity otherwise. ❤️ |
|
This now part of nixfmt-rs 0.5, but might have subtle changes based on how nixfmt decides to implement this feature: https://github.com/Mic92/nixfmt-rs/releases/tag/0.5.0 |
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/formatting-team-meeting-2026-06-23/78492/1 |
10d239c to
7fc5a1e
Compare
Capturing the formatter output with command substitution strips trailing newlines and echo re-adds exactly one, so the expected-output files could not represent output that does not end with a newline. Write and compare through a temporary file instead.
7fc5a1e to
7006f06
Compare
MattSturgeon
left a comment
There was a problem hiding this comment.
I approve of the new AST+state-based approach. I approve of the new test coverage, which seems very thorough!
I haven't reviewed the implementation in detail yet, but the parts I've looked at seem like a good direction for the wider architecture.
Let's discuss (and hopefully merge) during Tuesday's meeting!
Allow users to disable formatting for specific regions by wrapping them in /*nixfmt:disable*/ and /*nixfmt:enable*/ block comments. Directives must be on their own line. Unclosed disable extends to end of file. The block comment syntax /*nixfmt:disable*/ was chosen because it cannot appear naturally in formatted output, making detection unambiguous. The directives are resolved during parsing: the parser state tracks the currently open region and slices its raw text from the input when it closes. The pretty printer emits a zero-width marker at the region start and the raw text at its end; the renderer lays out the region content as usual, keeping its cursor and indentation state exact, but suppresses the output between the two and prints the raw text verbatim.
7006f06 to
450f0b6
Compare
Allow users to disable formatting for specific regions by wrapping them
in /nixfmt:disable/ and /nixfmt:enable/ block comments. Directives
must be on their own line. Unclosed disable extends to end of file.
The block comment syntax /nixfmt:disable/ was chosen because it cannot
appear naturally in formatted output, making detection unambiguous.
The directives are resolved during parsing: the parser state tracks the
currently open region and slices its raw text from the input when it
closes. The pretty printer emits a zero-width marker at the region
start and the raw text at its end; the renderer lays out the region
content as usual, keeping its cursor and indentation state exact, but
suppresses the output between the two and prints the raw text verbatim.
closes #91