feat: add /*nixfmt:disable*/ and /*nixfmt:enable*/ comment directives#388
feat: add /*nixfmt:disable*/ and /*nixfmt:enable*/ comment directives#388dyegoaurelio wants to merge 1 commit into
Conversation
2e2e99f to
48901f2
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 restriction to own-line-only simplifies matching between original and formatted text. Implemented as a two-step process: the pretty printer preserves directives in their correct positions, then a post-processor finds matching directive pairs in both the original and formatted output and splices the original code regions back in.
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. |
| go (Just start) ((i, False) : rest) = (start, Just i) : go Nothing rest | ||
| go (Just start) ((_, True) : rest) = go (Just start) rest -- nested disable, ignore | ||
|
|
||
| -- | Advance the scan state by processing a line character by character. |
There was a problem hiding this comment.
Pair reviewed this during the formatting team meeting. We (@MattSturgeon and I) are not in love with re-implementing parsing logic here, given that we already have a CST parser in the codebase.
Have you investigated alternate implementations? For example, tracking the original position of these tokens and then tweaking the pretty printer to honor them?
|
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 |
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 restriction to own-line-only simplifies matching between original and formatted text.
Implemented as a two-step process: the pretty printer preserves directives in their correct positions, then a post-processor finds matching directive pairs in both the original and formatted output and splices the original code regions back in.
I tried to disable this feature on strict mode.
Not applying the post processor using the cli option was fine, however it was very tricky to make sure the special directive syntax (
/*nixfmt:disable*/) gets formatted into regular comments.I thought it looked weird to allow the directive to stay but format stuff bellow it, so I kept it on both modes.
closes #91