From 54d41706bff77584fafb6342d1af4748b3a40a11 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 21 Dec 2025 22:26:10 -0300 Subject: 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 345b04c..bc8dfef 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 a55872d..f30c999 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 698abb4..48897b8 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 99af62e..40d3f83 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( -- cgit v1.2.3