From ac78202eea46ef9b1f10cceed3dd693c0d1a5ff1 Mon Sep 17 00:00:00 2001 From: flga Date: Tue, 4 Jul 2023 02:19:09 +0100 Subject: Force composite literals with comments to be multi line again --- src/odin/printer/document.odin | 13 +++++++++---- src/odin/printer/visit.odin | 8 ++++---- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin index 3dd9eda..89edd4b 100644 --- a/src/odin/printer/document.odin +++ b/src/odin/printer/document.odin @@ -290,11 +290,11 @@ cons_with_opl :: proc( rhs: ^Document, allocator := context.allocator, ) -> ^Document { - if _, ok := lhs.(Document_Nil); ok { + if is_empty(lhs) { return rhs } - if _, ok := rhs.(Document_Nil); ok { + if is_empty(rhs) { return lhs } @@ -309,11 +309,11 @@ cons_with_nopl :: proc( rhs: ^Document, allocator := context.allocator, ) -> ^Document { - if _, ok := lhs.(Document_Nil); ok { + if is_empty(lhs) { return rhs } - if _, ok := rhs.(Document_Nil); ok { + if is_empty(rhs) { return lhs } @@ -323,6 +323,11 @@ cons_with_nopl :: proc( ) } +is_empty :: proc(doc: ^Document) -> bool { + _, ok := doc.(Document_Nil) + return ok +} + Tuple :: struct { indentation: int, alignment: int, diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 61bbad3..9a73e3d 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -818,7 +818,7 @@ visit_comp_lit_exprs :: proc( document = cons( document, newline(1), - group(visit_expr(p, expr, .Generic, options)), + visit_expr(p, expr, .Generic, options), ) } else { document = group( @@ -2022,9 +2022,10 @@ visit_expr :: proc( } } + contains_comments := contains_comments_in_range(p, v.pos, v.end) should_newline := + contains_comments || comp_lit_spans_multiple_lines(v) || - contains_comments_in_range(p, v.pos, v.end) || (called_from == .Call_Expr && comp_lit_contains_blocks(p, v^)) should_newline &= len(v.elems) != 0 @@ -2032,12 +2033,11 @@ visit_expr :: proc( document = cons(document, visit_begin_brace(p, v.pos, .Comp_Lit)) options: List_Options = {.Add_Comma, .Trailing} - if comp_lit_contains_fields(p, v^) { + if contains_comments || comp_lit_contains_fields(p, v^) { options |= {.Enforce_Newline} } else { options |= {.Group} } - set_source_position(p, v.open) document = cons( document, -- cgit v1.2.3