diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2025-12-23 18:05:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-23 18:05:49 +0100 |
| commit | 5872154f56d21074f0e77950612bc769351094aa (patch) | |
| tree | fbe13888d1c105d94eceec2602ce9c30b43418b5 | |
| parent | 96f27dfa9738d0c425d6ceebce40dfca847f50c5 (diff) | |
| parent | 54d41706bff77584fafb6342d1af4748b3a40a11 (diff) | |
Merge pull request #1230 from lucypero/add-options
Add config option to not align struct fields
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | misc/odinfmt.schema.json | 10 | ||||
| -rw-r--r-- | src/odin/printer/printer.odin | 7 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 32 |
4 files changed, 37 insertions, 16 deletions
@@ -158,6 +158,10 @@ 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. + +- `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 6c82dde..f30c999 100644 --- a/misc/odinfmt.schema.json +++ b/misc/odinfmt.schema.json @@ -70,6 +70,16 @@ "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" + }, + "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 227f909..48897b8 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -54,6 +54,8 @@ 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, + align_struct_values: bool, } Brace_Style :: enum { @@ -90,7 +92,6 @@ Line_Suffix_Option :: enum { Indent, } - when ODIN_OS == .Windows { default_style := Config { spaces = 4, @@ -104,6 +105,8 @@ when ODIN_OS == .Windows { character_width = 100, sort_imports = true, spaces_around_colons = false, + align_struct_fields = true, + align_struct_values = true, } } else { default_style := Config { @@ -118,6 +121,8 @@ when ODIN_OS == .Windows { character_width = 100, 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 6294dbe..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( @@ -2081,22 +2081,24 @@ visit_struct_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := L } name_options := List_Options{.Add_Comma} - - if (.Enforce_Newline in options) { - 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 { |