diff options
| -rw-r--r-- | src/odin/printer/document.odin | 13 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 8 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/calls.odin | 151 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/comp_lit.odin | 51 | ||||
| -rw-r--r-- | tools/odinfmt/tests/calls.odin | 22 | ||||
| -rw-r--r-- | tools/odinfmt/tests/comp_lit.odin | 45 |
6 files changed, 211 insertions, 79 deletions
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, diff --git a/tools/odinfmt/tests/.snapshots/calls.odin b/tools/odinfmt/tests/.snapshots/calls.odin index 5ad8249..72e37fb 100644 --- a/tools/odinfmt/tests/.snapshots/calls.odin +++ b/tools/odinfmt/tests/.snapshots/calls.odin @@ -39,13 +39,96 @@ calls :: proc() { ddddddddddddddddddddddddddddddddddddd, ), ) + result = vk.CreateInsance( + my_really_cool_call( + T{1, 2, 3}, + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccccccccc, + ddddddddddddddddddddddddddddddddddddd, + ), + ) + result = vk.CreateInsance( + my_really_cool_call( + T{a = 1, b = 2, c = 3}, + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccccccccc, + ddddddddddddddddddddddddddddddddddddd, + ), + ) + result = vk.CreateInsance(my_really_cool_call( + T{ + 1, 2, 3, + }, + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccccccccc, + ddddddddddddddddddddddddddddddddddddd, + )) + result = vk.CreateInsance(my_really_cool_call( + T{ + a = 1, + b = 2, + c = 3, + }, + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccccccccc, + ddddddddddddddddddddddddddddddddddddd, + )) + + + result = vk.CreateInsance( + my_really_cool_call( + T{ + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccccccccc, + ddddddddddddddddddddddddddddddddddddd, + }, + ), + ) + result = vk.CreateInsance( + my_really_cool_call( + T{ + a = aaaaaaaaaaaaaaaaaaaaa, + b = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + c = cccccccccccccccccccccccccccccccc, + d = ddddddddddddddddddddddddddddddddddddd, + }, + ), + ) + result = vk.CreateInsance(my_really_cool_call(T{ + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd, + })) + result = vk.CreateInsance(my_really_cool_call(T{ + a = aaaaaaaaaaaaaaaaaaaaa, + b = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + c = cccccccccccccccccccccccccccccccc, + d = ddddddddddddddddddddddddddddddddddddd + + ddddddddddddddddddddddddddddddddddddd, + })) + result = vk.CreateInsance(my_really_cool_call(T{ + a = aaaaaaaaaaaaaaaaaaaaa, + b = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + c = cccccccccccccccccccccccccccccccc, + d = ddddddddddddddddddddddddddddddddddddd + 1, + })) _ = vk.CreateInsance(my_really_cool_call(1, 2, 3)) _ = vk.CreateInsance(my_really_cool_call( - 1, - 2, - 3, - )) + 1, + 2, + 3, + )) + _ = vk.CreateInsance(my_really_cool_call( + 1, + 2, + 3, + )) _ = vk.CreateInsance(1, 2, 3) _ = vk.CreateInsance( 1, @@ -56,47 +139,47 @@ calls :: proc() { _ = vk.CreateInsance(Composite{a = 1, b = 2}) _ = vk.CreateInsance(Composite{a = 1, b = 2}) _ = vk.CreateInsance(Composite{ - a = 1, - b = 2, - }) + a = 1, + b = 2, + }) _ = vk.CreateInsance(Composite{1, 2, 3, 4}) _ = vk.CreateInsance(Composite{ - 1, 2, - 3, 4, - }) + 1, 2, + 3, 4, + }) _ = vk.CreateInsance(matrix[2, 2]i32{ - 1, 2, - 3, 4, - }) + 1, 2, + 3, 4, + }) _ = vk.CreateInsance(matrix[2, 2]i32{ - 1, 2, - 3, 4, - }) + 1, 2, + 3, 4, + }) _ = vk.CreateInsance(Composite{ - p = proc(_: int) -> int {return 42}, - }) + p = proc(_: int) -> int {return 42}, + }) _ = vk.CreateInsance(Composite{ - p = proc(_: int) -> int { - return 42}, - }) + p = proc(_: int) -> int { + return 42}, + }) _ = vk.CreateInsance(Composite{ - p = proc(_: int) -> int { - return 42 - }, - }) + p = proc(_: int) -> int { + return 42 + }, + }) _ = vk.CreateInsance(Composite{ - p = proc(_: int) -> int { - return 42}, - }) + p = proc(_: int) -> int { + return 42}, + }) _ = vk.CreateInsance(Composite{ - p = proc(_: int) -> int { - return 42 - }, - }) + p = proc(_: int) -> int { + return 42 + }, + }) _ = vk.CreateInsance(Composite{ - p = proc(_: int) -> int {return 42}, - }) + p = proc(_: int) -> int {return 42}, + }) test_2(Foo{ field1 = 1, diff --git a/tools/odinfmt/tests/.snapshots/comp_lit.odin b/tools/odinfmt/tests/.snapshots/comp_lit.odin index fae3a4b..bbef149 100644 --- a/tools/odinfmt/tests/.snapshots/comp_lit.odin +++ b/tools/odinfmt/tests/.snapshots/comp_lit.odin @@ -2,30 +2,41 @@ package odinfmt_test main :: proc() { - _ = SameLine{a = 1, b = 2, c = 3} - _ = MultiLine{ - a = 1, - b = 2, - c = 3, + T :: struct { + a, b, c, d: int, } - _ = array_like{0, 1, 2, 3} - _ = array_like{ - 0, 1, - 2, 3, + _ = T{0, 0, 0, 0} + _ = T{ + 0, 0, 0, 0, } - _ = array_like{ - 0, 1, - 2, 3, + _ = T{ + 0, + 0, + 0, + 0, } - _ = array_like{ - 0, 1, - 2, 3, + _ = T{ + 0, 0, + 0, 0, } - _ = array_like{ - 0, 1, - 2, - 3, 4, 5, 6, 7, - 8, + _ = T{a = 0, b = 0, c = 0, d = 0} + _ = T{ + a = 0, + b = 0, + c = 0, + d = 0, + } + _ = T{ + a = 0, + b = 0, + c = 0, + d = 0, + } + _ = T{ + a = 0, + b = 0, + c = 0, + d = 0, } sort.sort(sort.Interface{ diff --git a/tools/odinfmt/tests/calls.odin b/tools/odinfmt/tests/calls.odin index b9fe588..febe4de 100644 --- a/tools/odinfmt/tests/calls.odin +++ b/tools/odinfmt/tests/calls.odin @@ -9,10 +9,32 @@ calls :: proc() { result := vk.CreateInsance(my_really_cool_call(aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) result = vk.CreateInsance(my_really_cool_call(aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) + result = vk.CreateInsance(my_really_cool_call(T{1,2,3},aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) + result = vk.CreateInsance(my_really_cool_call(T{a=1,b=2,c=3},aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) + result = vk.CreateInsance(my_really_cool_call(T{ + 1,2,3},aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) + result = vk.CreateInsance(my_really_cool_call(T{ + a=1,b=2,c=3},aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) + + result = vk.CreateInsance(my_really_cool_call(T{aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd})) + result = vk.CreateInsance(my_really_cool_call(T{a=aaaaaaaaaaaaaaaaaaaaa,b=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,c=cccccccccccccccccccccccccccccccc,d=ddddddddddddddddddddddddddddddddddddd})) + result = vk.CreateInsance(my_really_cool_call(T{ + aaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd})) + result = vk.CreateInsance(my_really_cool_call(T{ + a=aaaaaaaaaaaaaaaaaaaaa, + b=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,c=cccccccccccccccccccccccccccccccc,d=ddddddddddddddddddddddddddddddddddddd+ddddddddddddddddddddddddddddddddddddd})) + result = vk.CreateInsance(my_really_cool_call(T{ + a=aaaaaaaaaaaaaaaaaaaaa, + b=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,c=cccccccccccccccccccccccccccccccc,d=ddddddddddddddddddddddddddddddddddddd+1})) + _ = vk.CreateInsance(my_really_cool_call(1,2,3)) _ = vk.CreateInsance(my_really_cool_call(1, 2,3)) + _ = vk.CreateInsance(my_really_cool_call(1, + 2,3,), +) _ = vk.CreateInsance(1,2,3) _ = vk.CreateInsance(1, 2,3) diff --git a/tools/odinfmt/tests/comp_lit.odin b/tools/odinfmt/tests/comp_lit.odin index dc4ee7b..8859cb0 100644 --- a/tools/odinfmt/tests/comp_lit.odin +++ b/tools/odinfmt/tests/comp_lit.odin @@ -2,25 +2,36 @@ package odinfmt_test main :: proc() { - _ = SameLine{a=1 ,b=2,c=3} - _ = MultiLine{a=1 , - b=2,c=3} - _ = array_like{0,1,2,3} - _ = array_like{ - 0,1, - 2,3} - _ = array_like{ - 0,1, - 2,3, + T :: struct { + a, b, c, d: int, } - _ = array_like{0,1, - 2,3, + _ = T{0,0,0,0} + _ = T{ + 0,0,0,0, } - _ = array_like { - 0,1, - 2, - 3,4,5,6,7, - 8, + _ = T{ + 0, + 0, + 0, + 0, + } + _ = T{ + 0,0, + 0,0, + } + _ = T{a=0,b=0,c=0,d=0} + _ = T{ + a=0,b=0,c=0,d=0, + } + _ = T{ + a=0, + b=0, + c=0, + d=0, + } + _ = T{ + a=0,b=0, + c=0,d=0, } sort.sort(sort.Interface{len = proc(it: sort.Interface) -> int { |