From 47f9c665dc12c3b702e2d2e8506f44367b344e24 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 05:12:21 -0300 Subject: [PATCH 1/7] Remove field type alignment --- src/odin/printer/visit.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 6294dbec..6bd08007 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -2096,7 +2096,7 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L length += 9 } } - align = repeat_space(alignment - length) + // align = repeat_space(alignment - length) } document = cons(document, visit_exprs(p, field.names, name_options)) } else { From 55c3d1831d40eb6cd17990049c5487b9fef1a4a3 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 05:25:19 -0300 Subject: [PATCH 2/7] Add config flag to align struct fields --- src/odin/printer/printer.odin | 3 +++ src/odin/printer/visit.odin | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 227f9093..698abb43 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -54,6 +54,7 @@ Config :: struct { inline_single_stmt_case: bool, spaces_around_colons: bool, //Put spaces to the left of a colon as well as the right. `foo: bar` => `foo : bar` space_single_line_blocks: bool, + align_struct_fields: bool, } Brace_Style :: enum { @@ -104,6 +105,7 @@ when ODIN_OS == .Windows { character_width = 100, sort_imports = true, spaces_around_colons = false, + align_struct_fields = true, } } else { default_style := Config { @@ -118,6 +120,7 @@ when ODIN_OS == .Windows { character_width = 100, sort_imports = true, spaces_around_colons = false, + align_struct_fields = true, } } diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 6bd08007..6e3e852e 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -2081,8 +2081,8 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L } name_options := List_Options{.Add_Comma} - - if (.Enforce_Newline in options) { + + if (.Enforce_Newline in options) && p.config.align_struct_fields { alignment := get_possible_field_alignment(list.list) if alignment > 0 { @@ -2096,10 +2096,10 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L length += 9 } } - // align = repeat_space(alignment - length) + align = repeat_space(alignment - length) } document = cons(document, visit_exprs(p, field.names, name_options)) - } else { + } else if (.Enforce_Newline in options) { document = cons_with_opl(document, visit_exprs(p, field.names, name_options)) } From cdb08995ff28f2188b92378d13c65db095481589 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 05:36:39 -0300 Subject: [PATCH 3/7] Fix bug --- src/odin/printer/visit.odin | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 6e3e852e..9db83cd1 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -2082,24 +2082,27 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L name_options := List_Options{.Add_Comma} - if (.Enforce_Newline in options) && p.config.align_struct_fields { - alignment := get_possible_field_alignment(list.list) - - if alignment > 0 { - length := 0 - for name in field.names { - length += get_node_length(name) + 2 - if .Using in field.flags { - length += 6 - } - if .Subtype in field.flags { - length += 9 + if (.Enforce_Newline in options) { + if p.config.align_struct_fields { + alignment := get_possible_field_alignment(list.list) + + if alignment > 0 { + length := 0 + for name in field.names { + length += get_node_length(name) + 2 + if .Using in field.flags { + length += 6 + } + if .Subtype in field.flags { + length += 9 + } } + align = repeat_space(alignment - length) } - align = repeat_space(alignment - length) } document = cons(document, visit_exprs(p, field.names, name_options)) - } else if (.Enforce_Newline in options) { + } + else { document = cons_with_opl(document, visit_exprs(p, field.names, name_options)) } From f9f7cdcd6be4eb86a62a1fd2e9ec1e24867e5cb2 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 05:40:46 -0300 Subject: [PATCH 4/7] Fix formatting --- src/odin/printer/visit.odin | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 9db83cd1..99af62eb 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -2101,8 +2101,7 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L } } document = cons(document, visit_exprs(p, field.names, name_options)) - } - else { + } else { document = cons_with_opl(document, visit_exprs(p, field.names, name_options)) } From 6dbfafab72eb46239415250a56a40f936d685cac Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 05:42:09 -0300 Subject: [PATCH 5/7] Add docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 44a2ac21..345b04c0 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,8 @@ Options: - `space_single_line_blocks`: Put spaces around braces of single-line blocks: `{return 0}` => `{ return 0 }` +- `align_struct_fields`: Align the types of struct fields so they all start at the same column. + ## Features Support Language server features: From 93e39dcad2ad4aa5c07a0d2ff822b76d5f0e4ac5 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 12:40:39 -0300 Subject: [PATCH 6/7] Add config parameter to schema --- misc/odinfmt.schema.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/misc/odinfmt.schema.json b/misc/odinfmt.schema.json index 6c82dde3..a55872dd 100644 --- a/misc/odinfmt.schema.json +++ b/misc/odinfmt.schema.json @@ -70,6 +70,11 @@ "type": "boolean", "default": false, "description": "Put spaces around braces of single-line blocks: `{return 0}` => `{ return 0 }`" + }, + "align_struct_fields": { + "type": "boolean", + "default": true, + "description": "Align the types of struct fields so they all start at the same column" } }, "required": [] From 54d41706bff77584fafb6342d1af4748b3a40a11 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 22:26:10 -0300 Subject: [PATCH 7/7] Add option to align struct value assignment --- README.md | 2 ++ misc/odinfmt.schema.json | 5 +++++ src/odin/printer/printer.odin | 4 +++- src/odin/printer/visit.odin | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 345b04c0..bc8dfef8 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,8 @@ Options: - `align_struct_fields`: Align the types of struct fields so they all start at the same column. +- `align_struct_values`: Align the values of struct fields when assigning a struct value to a variable so they all start at the same column. + ## Features Support Language server features: diff --git a/misc/odinfmt.schema.json b/misc/odinfmt.schema.json index a55872dd..f30c999b 100644 --- a/misc/odinfmt.schema.json +++ b/misc/odinfmt.schema.json @@ -75,6 +75,11 @@ "type": "boolean", "default": true, "description": "Align the types of struct fields so they all start at the same column" + }, + "align_struct_values": { + "type": "boolean", + "default": true, + "description": "Align the values of struct fields when assigning a struct value to a variable so they all start at the same column" } }, "required": [] diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 698abb43..48897b89 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -55,6 +55,7 @@ Config :: struct { spaces_around_colons: bool, //Put spaces to the left of a colon as well as the right. `foo: bar` => `foo : bar` space_single_line_blocks: bool, align_struct_fields: bool, + align_struct_values: bool, } Brace_Style :: enum { @@ -91,7 +92,6 @@ Line_Suffix_Option :: enum { Indent, } - when ODIN_OS == .Windows { default_style := Config { spaces = 4, @@ -106,6 +106,7 @@ when ODIN_OS == .Windows { sort_imports = true, spaces_around_colons = false, align_struct_fields = true, + align_struct_values = true, } } else { default_style := Config { @@ -121,6 +122,7 @@ when ODIN_OS == .Windows { sort_imports = true, spaces_around_colons = false, align_struct_fields = true, + align_struct_values = true, } } diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 99af62eb..40d3f83b 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -798,7 +798,7 @@ visit_comp_lit_exprs :: proc(p: ^Printer, comp_lit: ast.Comp_Lit, options := Lis alignment := get_possible_comp_lit_alignment(comp_lit.elems) if value, ok := expr.derived.(^ast.Field_Value); ok && alignment > 0 { align := empty() - if should_align_comp_lit(p, comp_lit) { + if should_align_comp_lit(p, comp_lit) && p.config.align_struct_values { align = repeat_space(alignment - get_node_length(value.field)) } document = cons(