Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ Options:

- `multiline_composite_literals`: When enabled, composite literals that were written across multiple lines are kept multiline.

- `preserve_struct_blank_lines`: Preserve blank lines between struct fields, up to `newline_limit`.

## Features

Support Language server features:
Expand Down
5 changes: 5 additions & 0 deletions misc/odinfmt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
"type": "boolean",
"default": false,
"description": "Composite literals that span multiple lines are not forcibly inlined"
},
"preserve_struct_blank_lines": {
"type": "boolean",
"default": false,
"description": "Preserve blank lines between struct fields, up to newline_limit"
}
},
"required": []
Expand Down
3 changes: 3 additions & 0 deletions src/odin/printer/printer.odin
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Config :: struct {
align_constant_definitions: bool,
align_comments: bool, //Align trailing line comments to the same column.
multiline_composite_literals: bool,
preserve_struct_blank_lines: bool,
}

Brace_Style :: enum {
Expand Down Expand Up @@ -127,6 +128,7 @@ when ODIN_OS == .Windows {
align_constant_definitions = false,
align_comments = false,
multiline_composite_literals = false,
preserve_struct_blank_lines = false,
}
} else {
default_style := Config {
Expand All @@ -147,6 +149,7 @@ when ODIN_OS == .Windows {
align_constant_definitions = false,
align_comments = false,
multiline_composite_literals = false,
preserve_struct_blank_lines = false,
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/odin/printer/visit.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2219,8 +2219,12 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L
}

if i != len(list.list) - 1 && .Enforce_Newline in options {
comment, _ := visit_comments(p, list.list[i + 1].pos)
document = cons(document, comment, newline(1))
if p.config.preserve_struct_blank_lines {
document = cons(document, move_line(p, list.list[i + 1].pos))
} else {
comment, _ := visit_comments(p, list.list[i + 1].pos)
document = cons(document, comment, newline(1))
}
} else {
comment, _ := visit_comments(p, list.end)
document = cons(document, comment)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package collapse_struct_field_blank_lines

Pair :: struct {
first: int,
second: int,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package collapse_struct_field_blank_lines

Pair :: struct {
first: int,

second: int,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package preserve_struct_field_blank_lines

Settings :: struct {
window_width: int,
window_height: int,
target_fps: int,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder with this if we should align each section separately? Similarly to how gofmt handles it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I agree! That behavior is better.

I've updated the PR (& PR body) to use the gofmt style, see:


fullscreen: bool,
vertical_sync: bool,
}

With_Comments :: struct {
first: int, // trailing comment

// The second group.
second: int,

// The third group follows this comment without a blank line.
third: int,
}

Capped_By_Newline_Limit :: struct {
first: int,


second: int,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"newline_limit": 2,
"preserve_struct_blank_lines": true
}
29 changes: 29 additions & 0 deletions tools/odinfmt/tests/preserve_struct_field_blank_lines/structs.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package preserve_struct_field_blank_lines

Settings :: struct {
window_width: int,
window_height: int,
target_fps: int,

fullscreen: bool,
vertical_sync: bool,
}

With_Comments :: struct {
first: int, // trailing comment

// The second group.
second: int,

// The third group follows this comment without a blank line.
third: int,
}

Capped_By_Newline_Limit :: struct {
first: int,




second: int,
}
Loading