diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2023-08-06 21:47:06 +0200 |
|---|---|---|
| committer | DanielGavin <danielgavin5@hotmail.com> | 2023-08-06 21:47:06 +0200 |
| commit | 2b609359cb5a827568fcee957529d852ff7e1c40 (patch) | |
| tree | 34f50f81447d3d23c818763ed1506ee0b37996ee | |
| parent | a010fd2c8f9c3b3bd4cec75022c50aac94c0cb02 (diff) | |
Undo the control of newlines in comp_lit - it broke too many things and wasn't really compatible with the formatter design.
| -rw-r--r-- | src/odin/printer/document.odin | 36 | ||||
| -rw-r--r-- | src/odin/printer/printer.odin | 19 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 19 | ||||
| -rw-r--r-- | tools/odinfmt/snapshot/snapshot.odin | 1 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/comp_lit.odin | 39 | ||||
| -rw-r--r-- | tools/odinfmt/tests/comp_lit.odin | 25 | ||||
| -rw-r--r-- | tools/odinfmt/tests/random/.snapshots/demo.odin | 79 | ||||
| -rw-r--r-- | tools/odinfmt/tests/random/.snapshots/document.odin | 24 |
8 files changed, 86 insertions, 156 deletions
diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin index 3dd9eda..5e04181 100644 --- a/src/odin/printer/document.odin +++ b/src/odin/printer/document.odin @@ -65,6 +65,7 @@ Document_Group_Mode :: enum { Flat, Break, Fit, + Fill, } Document_Group_Options :: struct { @@ -156,6 +157,18 @@ enforce_fit :: proc( return document } +fill :: proc( + filled_document: ^Document, + allocator := context.allocator, +) -> ^Document { + document := new(Document, allocator) + document^ = Document_Group { + document = filled_document, + mode = .Fill, + } + return document +} + enforce_break :: proc( fitted_document: ^Document, options := Document_Group_Options{}, @@ -578,6 +591,17 @@ format :: proc( builder, p, ) + } else if data.mode == .Fill && consumed < width { + strings.write_string(builder, v.value) + consumed += len(v.value) + } else if data.mode == .Fill && v.newline { + format_newline( + data.indentation, + data.alignment, + &consumed, + builder, + p, + ) } else { strings.write_string(builder, v.value) consumed += len(v.value) @@ -660,7 +684,17 @@ format :: proc( }, ) } else { - if v.mode == .Fit { + if data.mode == .Fill || v.mode == .Fill { + append( + list, + Tuple{ + indentation = data.indentation, + mode = .Fill, + document = v.document, + alignment = data.alignment, + }, + ) + } else if v.mode == .Fit { append( list, Tuple{ diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index ba2b7e3..d2af255 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -38,16 +38,15 @@ Disabled_Info :: struct { } Config :: struct { - character_width: int, - spaces: int, //Spaces per indentation - newline_limit: int, //The limit of newlines between statements and declarations. - tabs: bool, //Enable or disable tabs - tabs_width: int, - convert_do: bool, //Convert all do statements to brace blocks - multiline_composite_literals: bool `json:"exp_multiline_composite_literals"`, - brace_style: Brace_Style, - indent_cases: bool, - newline_style: Newline_Style, + character_width: int, + spaces: int, //Spaces per indentation + newline_limit: int, //The limit of newlines between statements and declarations. + tabs: bool, //Enable or disable tabs + tabs_width: int, + convert_do: bool, //Convert all do statements to brace blocks + brace_style: Brace_Style, + indent_cases: bool, + newline_style: Newline_Style, } Brace_Style :: enum { diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index c23305e..76120da 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -1446,16 +1446,6 @@ contains_comments_in_range :: proc( } @(private) -comp_lit_spans_multiple_lines :: proc(lit: ast.Comp_Lit) -> bool { - for elem, i in lit.elems { - if elem.pos.line != lit.pos.line { - return true - } - } - return false -} - -@(private) contains_do_in_expression :: proc(p: ^Printer, expr: ^ast.Expr) -> bool { found_do := false @@ -2012,11 +2002,7 @@ visit_expr :: proc( } } - can_multiline := - p.config.multiline_composite_literals && - comp_lit_spans_multiple_lines(v^) should_newline := - can_multiline || comp_lit_contains_fields(p, v^) || contains_comments_in_range(p, v.pos, v.end) should_newline &= @@ -2026,7 +2012,10 @@ visit_expr :: proc( should_newline &= len(v.elems) != 0 if should_newline { - document = cons(document, visit_begin_brace(p, v.pos, .Comp_Lit)) + document = cons_with_nopl( + document, + visit_begin_brace(p, v.pos, .Comp_Lit), + ) set_source_position(p, v.open) document = cons( diff --git a/tools/odinfmt/snapshot/snapshot.odin b/tools/odinfmt/snapshot/snapshot.odin index ef6239d..23ae0b8 100644 --- a/tools/odinfmt/snapshot/snapshot.odin +++ b/tools/odinfmt/snapshot/snapshot.odin @@ -19,7 +19,6 @@ format_file :: proc( style := format.default_style style.character_width = 80 style.newline_style = .LF //We want to make sure it works on linux and windows. - style.multiline_composite_literals = true if data, ok := os.read_entire_file(filepath, allocator); ok { return format.format( diff --git a/tools/odinfmt/tests/.snapshots/comp_lit.odin b/tools/odinfmt/tests/.snapshots/comp_lit.odin index a2b8a29..b7c018c 100644 --- a/tools/odinfmt/tests/.snapshots/comp_lit.odin +++ b/tools/odinfmt/tests/.snapshots/comp_lit.odin @@ -6,50 +6,23 @@ main :: proc() { a, b, c, d: int, } _ = T{0, 0, 0, 0} - _ = T{ - 0, - 0, - 0, - 0, - } - _ = 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{ + + + _ = T { a = 0, b = 0, c = 0, d = 0, } - _ = T{ + + _ = T { a = 0, b = 0, c = 0, d = 0, } - sort.sort(sort.Interface{ + sort.sort(sort.Interface { len = proc(it: sort.Interface) -> int { c := 2 }, diff --git a/tools/odinfmt/tests/comp_lit.odin b/tools/odinfmt/tests/comp_lit.odin index 8859cb0..0b7e568 100644 --- a/tools/odinfmt/tests/comp_lit.odin +++ b/tools/odinfmt/tests/comp_lit.odin @@ -6,29 +6,10 @@ main :: proc() { a, b, c, d: int, } _ = T{0,0,0,0} - _ = T{ - 0,0,0,0, - } - _ = 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, diff --git a/tools/odinfmt/tests/random/.snapshots/demo.odin b/tools/odinfmt/tests/random/.snapshots/demo.odin index a55af51..c8cfece 100644 --- a/tools/odinfmt/tests/random/.snapshots/demo.odin +++ b/tools/odinfmt/tests/random/.snapshots/demo.odin @@ -203,7 +203,7 @@ control_flow :: proc() { } - some_map := map[string]int{ + some_map := map[string]int { "A" = 1, "C" = 9, "B" = 4, @@ -539,7 +539,7 @@ struct_type :: proc() { // You can list just a subset of the fields if you specify the // field by name (the order of the named fields does not matter): - v = Vector3{ + v = Vector3 { z = 1, y = 2, } @@ -688,7 +688,7 @@ union_type :: proc() { // See `parametric_polymorphism` procedure for details new_entity :: proc($T: typeid) -> ^Entity { t := new(Entity) - t.derived = T{ + t.derived = T { entity = t, } return t @@ -1131,29 +1131,15 @@ parametric_polymorphism :: proc() { return } - x := [2][3]f32{ - {1, 2, 3}, - {3, 2, 1}, - } - y := [3][2]f32{ - {0, 8}, - {6, 2}, - {8, 4}, - } + x := [2][3]f32{{1, 2, 3}, {3, 2, 1}} + y := [3][2]f32{{0, 8}, {6, 2}, {8, 4}} z := mul(x, y) assert(z == {{36, 24}, {20, 32}}) } } -prefix_table := [?]string{ - "White", - "Red", - "Green", - "Blue", - "Octarine", - "Black", -} +prefix_table := [?]string{"White", "Red", "Green", "Blue", "Octarine", "Black"} print_mutex := b64(false) @@ -1780,7 +1766,7 @@ ranged_fields_for_array_compound_literals :: proc() { fmt.println(foo) } { // Indexed - foo := [?]int{ + foo := [?]int { 3 = 16, 1 = 4, 2 = 9, @@ -1790,7 +1776,7 @@ ranged_fields_for_array_compound_literals :: proc() { } { // Ranges i := 2 - foo := [?]int{ + foo := [?]int { 0 = 123, 5 ..= 9 = 54, 10 ..< 16 = i * 3 + (i - 1) * 2, @@ -1800,7 +1786,7 @@ ranged_fields_for_array_compound_literals :: proc() { } { // Slice and Dynamic Array support i := 2 - foo_slice := []int{ + foo_slice := []int { 0 = 123, 5 ..= 9 = 54, 10 ..< 16 = i * 3 + (i - 1) * 2, @@ -1808,7 +1794,7 @@ ranged_fields_for_array_compound_literals :: proc() { assert(len(foo_slice) == 16) fmt.println(foo_slice) // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8] - foo_dynamic_array := [dynamic]int{ + foo_dynamic_array := [dynamic]int { 0 = 123, 5 ..= 9 = 54, 10 ..< 16 = i * 3 + (i - 1) * 2, @@ -2019,7 +2005,7 @@ constant_literal_expressions :: proc() { using c: Bar, } - FOO_CONST :: Foo{ + FOO_CONST :: Foo { b = 2, a = 1, c = {3, 4}, @@ -2036,7 +2022,7 @@ constant_literal_expressions :: proc() { fmt.println("-------") - ARRAY_CONST :: [3]int{ + ARRAY_CONST :: [3]int { 1 = 4, 2 = 9, 0 = 1, @@ -2059,7 +2045,7 @@ constant_literal_expressions :: proc() { C, D, } - ENUM_ARRAY_CONST :: [Baz]int{ + ENUM_ARRAY_CONST :: [Baz]int { .A ..= .C = 1, .D = 16, } @@ -2078,7 +2064,7 @@ constant_literal_expressions :: proc() { D = 16, } #assert(len(Sparse_Baz) < len(#sparse[Sparse_Baz]int)) - SPARSE_ENUM_ARRAY_CONST :: #sparse[Sparse_Baz]int{ + SPARSE_ENUM_ARRAY_CONST :: #sparse[Sparse_Baz]int { .A ..= .C = 1, .D = 16, } @@ -2523,12 +2509,7 @@ matrix_type :: proc() { mat2 :: distinct matrix[2, 2]f32 mat4 :: distinct matrix[4, 4]f32 - m2 := mat2{ - 1, - 3, - 2, - 4, - } + m2 := mat2{1, 3, 2, 4} m4 := mat4(m2) assert(m4[2, 2] == 1) @@ -2538,24 +2519,7 @@ matrix_type :: proc() { fmt.println("mat2(m4)", mat2(m4)) assert(mat2(m4) == m2) - b4 := mat4{ - 1, - 2, - 0, - 0, - 3, - 4, - 0, - 0, - 5, - 0, - 6, - 0, - 0, - 7, - 0, - 8, - } + b4 := mat4{1, 2, 0, 0, 3, 4, 0, 0, 5, 0, 6, 0, 0, 7, 0, 8} fmt.println("b4", matrix_flatten(b4)) } @@ -2568,16 +2532,7 @@ matrix_type :: proc() { mat2x4 :: distinct matrix[2, 4]f32 mat4x2 :: distinct matrix[4, 2]f32 - x := mat2x4{ - 1, - 3, - 5, - 7, - 2, - 4, - 6, - 8, - } + x := mat2x4{1, 3, 5, 7, 2, 4, 6, 8} y := mat4x2(x) fmt.println("x", x) diff --git a/tools/odinfmt/tests/random/.snapshots/document.odin b/tools/odinfmt/tests/random/.snapshots/document.odin index 10e13b0..e309517 100644 --- a/tools/odinfmt/tests/random/.snapshots/document.odin +++ b/tools/odinfmt/tests/random/.snapshots/document.odin @@ -81,7 +81,7 @@ empty :: proc(allocator := context.allocator) -> ^Document { text :: proc(value: string, allocator := context.allocator) -> ^Document { document := new(Document, allocator) - document^ = Document_Text{ + document^ = Document_Text { value = value, } return document @@ -89,7 +89,7 @@ text :: proc(value: string, allocator := context.allocator) -> ^Document { newline :: proc(amount: int, allocator := context.allocator) -> ^Document { document := new(Document, allocator) - document^ = Document_Newline{ + document^ = Document_Newline { amount = amount, } return document @@ -101,7 +101,7 @@ nest :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Nest{ + document^ = Document_Nest { indentation = level, document = nested_document, } @@ -115,7 +115,7 @@ nest_if_break :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Nest_If_Break{ + document^ = Document_Nest_If_Break { indentation = level, document = nested_document, group_id = group_id, @@ -129,7 +129,7 @@ hang :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Nest{ + document^ = Document_Nest { alignment = align, document = hanged_document, } @@ -141,7 +141,7 @@ enforce_fit :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Group{ + document^ = Document_Group { document = fitted_document, mode = .Fit, } @@ -153,7 +153,7 @@ enforce_break :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Group{ + document^ = Document_Group { document = fitted_document, mode = .Break, } @@ -165,7 +165,7 @@ align :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Align{ + document^ = Document_Align { document = aligned_document, } return document @@ -173,7 +173,7 @@ align :: proc( if_break :: proc(value: string, allocator := context.allocator) -> ^Document { document := new(Document, allocator) - document^ = Document_If_Break{ + document^ = Document_If_Break { value = value, } return document @@ -185,7 +185,7 @@ break_with :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Break{ + document^ = Document_Break { value = value, newline = newline, } @@ -206,7 +206,7 @@ group :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Group{ + document^ = Document_Group { document = grouped_document, options = options, } @@ -219,7 +219,7 @@ cons :: proc( allocator := context.allocator, ) -> ^Document { document := new(Document, allocator) - document^ = Document_Cons{ + document^ = Document_Cons { lhs = lhs, rhs = rhs, } |