From 490c163ca462991992157268a3dcfa379add3a69 Mon Sep 17 00:00:00 2001 From: DanielGavin Date: Sun, 28 Apr 2024 12:12:41 +0200 Subject: [PATCH] Split case clause when it's too big. --- src/odin/printer/document.odin | 46 ++++++++++++++++++---------------- src/odin/printer/visit.odin | 33 +++++++++++++++--------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin index 5e04181e..27195856 100644 --- a/src/odin/printer/document.odin +++ b/src/odin/printer/document.odin @@ -377,7 +377,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { for i := len(v.elements) - 1; i >= 0; i -= 1 { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.elements[i], @@ -388,18 +388,19 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { case Document_Align: append( list, - Tuple{ + Tuple { indentation = 0, mode = data.mode, document = v.document, alignment = start_width - width, }, ) + case Document_Nest: if v.alignment != 0 { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.document, @@ -410,7 +411,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { } else { append( list, - Tuple{ + Tuple { indentation = data.indentation + (v.negate ? -1 : 1), mode = data.mode, document = v.document, @@ -430,7 +431,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { if data.mode == .Break { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.break_document, @@ -440,7 +441,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { } else if v.fit_document != nil { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.fit_document, @@ -451,7 +452,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { case Document_Group: append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = (v.mode == .Break ? .Break : data.mode), document = v.document, @@ -537,7 +538,7 @@ format :: proc( for i := len(v.elements) - 1; i >= 0; i -= 1 { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.elements[i], @@ -549,7 +550,7 @@ format :: proc( if v.alignment != 0 { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.document, @@ -560,7 +561,7 @@ format :: proc( } else { append( list, - Tuple{ + Tuple { indentation = data.indentation + (v.negate ? -1 : 1), mode = data.mode, document = v.document, @@ -569,13 +570,14 @@ format :: proc( ) } case Document_Align: + align := consumed - data.indentation * p.indentation_width append( list, - Tuple{ - indentation = 0, + Tuple { + indentation = data.indentation, mode = data.mode, document = v.document, - alignment = consumed, + alignment = align, }, ) case Document_Text: @@ -611,7 +613,7 @@ format :: proc( if mode == .Break { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.break_document, @@ -621,7 +623,7 @@ format :: proc( } else if v.fit_document != nil { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = data.mode, document = v.fit_document, @@ -633,7 +635,7 @@ format :: proc( if data.mode == .Flat && !recalculate { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = v.mode, document = v.document, @@ -651,7 +653,7 @@ format :: proc( append( &list_fits, - Tuple{ + Tuple { indentation = data.indentation, mode = .Flat, document = v.document, @@ -664,7 +666,7 @@ format :: proc( if data.mode == .Fit { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = .Fit, document = v.document, @@ -676,7 +678,7 @@ format :: proc( v.mode != .Fit { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = .Flat, document = v.document, @@ -687,7 +689,7 @@ format :: proc( if data.mode == .Fill || v.mode == .Fill { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = .Fill, document = v.document, @@ -697,7 +699,7 @@ format :: proc( } else if v.mode == .Fit { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = .Fit, document = v.document, @@ -707,7 +709,7 @@ format :: proc( } else { append( list, - Tuple{ + Tuple { indentation = data.indentation, mode = .Break, document = v.document, diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index f1d67e05..c75d4704 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -727,7 +727,9 @@ visit_bit_field_fields :: proc( document := empty() - name_alignment, type_alignment := get_possible_bit_field_alignment(bit_field_type.fields) + name_alignment, type_alignment := get_possible_bit_field_alignment( + bit_field_type.fields, + ) for field, i in bit_field_type.fields { if i == 0 && .Enforce_Newline in options { @@ -756,7 +758,8 @@ visit_bit_field_fields :: proc( cons_with_nopl( cons( repeat_space( - type_alignment - get_node_length(field.type), + type_alignment - + get_node_length(field.type), ), text_position(p, "|", field.type.end), ), @@ -791,7 +794,8 @@ visit_bit_field_fields :: proc( document = cons(document, text(",")) } - if (i != len(bit_field_type.fields) - 1 && .Enforce_Newline in options) { + if (i != len(bit_field_type.fields) - 1 && + .Enforce_Newline in options) { comment, _ := visit_comments(p, bit_field_type.fields[i + 1].pos) document = cons(document, comment, newline(1)) } else if .Enforce_Newline in options { @@ -1094,7 +1098,7 @@ visit_stmt :: proc( set_source_position(p, v.pos) - block := visit_block_stmts(p, v.stmts, len(v.stmts) > 1) + block := visit_block_stmts(p, v.stmts) comment_end, _ := visit_comments( p, @@ -1238,7 +1242,7 @@ visit_stmt :: proc( if v.list != nil { document = cons_with_nopl( document, - visit_exprs(p, v.list, {.Add_Comma}), + align(group(visit_exprs(p, v.list, {.Add_Comma}))), ) } @@ -1948,7 +1952,11 @@ visit_expr :: proc( document = cons_with_nopl(document, text("{")) document = cons(document, text("}")) } else { - document = cons(document, break_with_space(), visit_begin_brace(p, v.pos, .Generic)) + document = cons( + document, + break_with_space(), + visit_begin_brace(p, v.pos, .Generic), + ) set_source_position(p, v.fields[0].pos) document = cons( document, @@ -2420,11 +2428,7 @@ visit_end_brace :: proc( } @(private) -visit_block_stmts :: proc( - p: ^Printer, - stmts: []^ast.Stmt, - split := false, -) -> ^Document { +visit_block_stmts :: proc(p: ^Printer, stmts: []^ast.Stmt) -> ^Document { document := empty() for stmt, i in stmts { @@ -2965,7 +2969,12 @@ get_possible_enum_alignment :: proc(exprs: []^ast.Expr) -> int { } @(private) -get_possible_bit_field_alignment :: proc(fields: []^ast.Bit_Field_Field) -> (longest_name: int, longest_type: int) { +get_possible_bit_field_alignment :: proc( + fields: []^ast.Bit_Field_Field, +) -> ( + longest_name: int, + longest_type: int, +) { for field in fields { longest_name = max(longest_name, get_node_length(field.name)) longest_type = max(longest_type, get_node_length(field.type))