Feature: optionally preserve blank lines between struct fields - #1657
Open
giuinktse7 wants to merge 6 commits into
Open
Feature: optionally preserve blank lines between struct fields#1657giuinktse7 wants to merge 6 commits into
giuinktse7 wants to merge 6 commits into
Conversation
BradLewis
reviewed
Sep 2, 2026
| Settings :: struct { | ||
| window_width: int, | ||
| window_height: int, | ||
| target_fps: int, |
Collaborator
There was a problem hiding this comment.
I wonder with this if we should align each section separately? Similarly to how gofmt handles it.
Contributor
Author
There was a problem hiding this comment.
Ah, I agree! That behavior is better.
I've updated the PR (& PR body) to use the gofmt style, see:
- new changes: 157cf11..1580cff
- The main functionality change: feat: use section-scoped struct field alignment across blank lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
odinfmtcurrently collapses blank lines between struct fields, but I often want to separate fields visually into logical groups.For example, this:
becomes:
It is possible to preserve the visual separation by adding a comment, but a comment should not be necessary solely for formatting:
Proposed Solution
This PR adds an opt-in
preserve_struct_blank_linessetting:{ "preserve_struct_blank_lines": true }When enabled, the formatter preserves blank lines between struct fields while respecting the existing
newline_limitsetting.
Each blank-line-separated section is also aligned independently, similar to
gofmt. A long field name in one logicalsection therefore does not add padding to fields in another section. This applies to both
align_struct_fieldsandalign_struct_declarations.Comments attached to fields are treated as part of those fields when identifying section boundaries. Blank lines inside multiline block comments do not create sections, while an actual blank line between a field and the next field or its documentation does.
When
newline_limitis zero, blank lines are removed and alignment continues across the whole struct because no visible section boundary remains.The option defaults to false, so existing formatting behavior remains unchanged.
Example
Fields previously aligned across the entire struct:
With
preserve_struct_blank_linesenabled, each blank-line-separated section now has its own alignment width:Alignment Improvements
This PR also fixes an inconsistency where
#subtypewas included in declaration alignment widths but omitted from field-type alignment widths. For example, withalign_struct_fieldsenabled:Previously,
#subtypewas omitted when calculating the maximum width, producing:After including
#subtypein the shared width calculation:Verification
Added snapshot coverage for:
align_struct_fieldsandalign_struct_declarations#subtypeprefixes during alignmentnewline_limitnewline_limitis zeroThe
odinfmtsnapshot suite passes.