From b0721f1e0c51f3f738459db9dc29534161553d4c Mon Sep 17 00:00:00 2001 From: Daniel Gavin Date: Wed, 14 Apr 2021 16:31:31 +0200 Subject: [PATCH] aligning structs now work --- core/odin/printer/printer.odin | 90 ++++++++++++++++++++++++++++++++-- core/odin/printer/visit.odin | 15 +++--- 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/core/odin/printer/printer.odin b/core/odin/printer/printer.odin index c47227d42..6d591a6ae 100644 --- a/core/odin/printer/printer.odin +++ b/core/odin/printer/printer.odin @@ -208,7 +208,7 @@ align_switch_smt :: proc(p: ^Printer, index: int) { if format_token.kind == .Open_Brace && switch_found { brace_token = format_token; - brace_line = line_index; + brace_line = line_index+index; break found_switch_brace; } else if format_token.kind == .Open_Brace { break; @@ -294,7 +294,7 @@ align_switch_smt :: proc(p: ^Printer, index: int) { if case_count > brace_token.parameter_count { break; } - + } } @@ -302,6 +302,88 @@ align_switch_smt :: proc(p: ^Printer, index: int) { align_struct :: proc(p: ^Printer, index: int) { + struct_found := false; + brace_token: Format_Token; + brace_line: int; + + found_struct_brace: for line, line_index in p.lines[index:] { + + for format_token in line.format_tokens { + + if format_token.kind == .Open_Brace && struct_found { + brace_token = format_token; + brace_line = line_index+index; + break found_struct_brace; + } else if format_token.kind == .Open_Brace { + break; + } else if format_token.kind == .Struct { + struct_found = true; + } + + } + + } + + if !struct_found { + return; + } + + largest := 0; + colon_count := 0; + + + + for line, line_index in p.lines[brace_line+1:] { + + length := 0; + + for format_token in line.format_tokens { + + if format_token.kind == .Comment { + continue; + } + + if format_token.kind == .Colon { + colon_count += 1; + largest = max(length, largest); + break; + } + + length += len(format_token.text) + format_token.spaces_before; + } + + if colon_count > brace_token.parameter_count { + break; + } + } + + colon_count = 0; + + for line, line_index in p.lines[brace_line+1:] { + + length := 0; + + for format_token, i in line.format_tokens { + + if format_token.kind == .Comment { + continue; + } + + if format_token.kind == .Colon { + colon_count += 1; + line.format_tokens[i+1].spaces_before += (largest - length) - 1; + break; + } + + length += len(format_token.text) + format_token.spaces_before; + } + + if colon_count > brace_token.parameter_count { + break; + } + } + + } align_blocks :: proc(p: ^Printer) { @@ -314,7 +396,9 @@ align_blocks :: proc(p: ^Printer) { if .Switch_Stmt in line.types && p.config.align_switch { align_switch_smt(p, line_index); - } else if .Struct in line.types && p.config.align_structs { + } + + if .Struct in line.types && p.config.align_structs { align_struct(p, line_index); } diff --git a/core/odin/printer/visit.odin b/core/odin/printer/visit.odin index 6afc04a61..adbe68fb5 100644 --- a/core/odin/printer/visit.odin +++ b/core/odin/printer/visit.odin @@ -1028,6 +1028,8 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) { case Struct_Type: push_generic_token(p, .Struct, 1); + hint_current_line(p, {.Struct}); + if v.is_packed { push_ident_token(p, "#packed", 1); } @@ -1058,11 +1060,10 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) { set_source_position(p, v.fields.pos); visit_field_list(p, v.fields, true); push_generic_token(p, .Close_Brace, 0); - } else { - visit_begin_brace(p, v.pos, .Generic); - newline_position(p, 1); + } else if v.fields != nil { + visit_begin_brace(p, v.pos, .Generic, len(v.fields.list)); set_source_position(p, v.fields.pos); - visit_field_list(p, v.fields, true, true); + visit_field_list(p, v.fields, true, true, true); visit_end_brace(p, v.end); } @@ -1256,7 +1257,7 @@ visit_block_stmts :: proc(p: ^Printer, stmts: []^ast.Stmt, newline_each := false } } -visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, add_comma := false, trailing := false) { +visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, add_comma := false, trailing := false, enforce_newline := false) { if list.list == nil { return; @@ -1264,7 +1265,9 @@ visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, add_comma := false, for field, i in list.list { - move_line_limit(p, field.pos, 1); + if !move_line_limit(p, field.pos, 1) && enforce_newline { + newline_position(p, 1); + } if .Using in field.flags { push_generic_token(p, .Using, 0);