diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-07-18 18:40:43 +0200 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-07-18 18:40:43 +0200 |
| commit | 0bce9200239fed06dd71ab82a7e0de254a146cff (patch) | |
| tree | 128e041a7ffeeaf99bfa5461a5d8789841344291 | |
| parent | b0f127175d05d00d14cd4fee43a50718e83664a7 (diff) | |
Added new suffix type to handle newline comments in odinfmt.
| -rw-r--r-- | src/odin/printer/document.odin | 41 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 16 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/calls.odin | 9 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/comments.odin | 5 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/proc_types.odin | 5 | ||||
| -rw-r--r-- | tools/odinfmt/tests/calls.odin | 10 | ||||
| -rw-r--r-- | tools/odinfmt/tests/comments.odin | 6 | ||||
| -rw-r--r-- | tools/odinfmt/tests/proc_types.odin | 5 |
8 files changed, 78 insertions, 19 deletions
diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin index 9be7da1..db39cde 100644 --- a/src/odin/printer/document.odin +++ b/src/odin/printer/document.odin @@ -14,6 +14,8 @@ Document :: union { Document_If_Break, Document_Align, Document_Nest_If_Break, + Document_Break_Parent, + Document_Line_Suffix, } Document_Nil :: struct { @@ -28,6 +30,10 @@ Document_Text :: struct { value: string, } +Document_Line_Suffix :: struct { + value: string, +} + Document_Nest :: struct { indentation: int, alignment: int, @@ -75,6 +81,9 @@ Document_Group_Options :: struct { id: string, } +Document_Break_Parent :: struct { +} + empty :: proc(allocator := context.allocator) -> ^Document { document := new(Document, allocator) document^ = Document_Nil {} @@ -169,6 +178,21 @@ break_with :: proc(value: string, newline := true, allocator := context.allocato return document } +break_parent :: proc(allocator := context.allocator) -> ^Document { + document := new(Document, allocator) + document^ = Document_Break_Parent { + } + return document +} + +line_suffix :: proc(value: string, allocator := context.allocator) -> ^Document { + document := new(Document, allocator) + document^ = Document_Line_Suffix { + value = value, + } + return document +} + break_with_space :: proc(allocator := context.allocator) -> ^Document { return break_with(" ", true, allocator) } @@ -247,6 +271,9 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { switch v in data.document { case Document_Nil: + case Document_Line_Suffix: + case Document_Break_Parent: + return false case Document_Newline: if v.amount > 0 { return true @@ -296,6 +323,11 @@ format_newline :: proc(indentation: int, alignment: int, consumed: ^int, builder consumed^ = indentation * p.indentation_width + alignment } +flush_line_suffix :: proc(builder: ^strings.Builder, suffix_builder: ^strings.Builder) { + strings.write_string(builder, strings.to_string(suffix_builder^)) + strings.builder_reset(suffix_builder) +} + format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p: ^Printer) { assert(list != nil) assert(builder != nil) @@ -303,13 +335,19 @@ format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p: consumed := 0 recalculate := false; + suffix_builder := strings.builder_make() + for len(list) != 0 { data: Tuple = pop(list) switch v in data.document { case Document_Nil: + case Document_Line_Suffix: + strings.write_string(&suffix_builder, v.value) + case Document_Break_Parent: case Document_Newline: if v.amount > 0 { + flush_line_suffix(builder, &suffix_builder) for i := 0; i < v.amount; i += 1 { strings.write_string(builder, p.newline) } @@ -337,12 +375,13 @@ format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p: consumed += len(v.value) case Document_Break: if data.mode == .Break && v.newline { + flush_line_suffix(builder, &suffix_builder) format_newline(data.indentation, data.alignment, &consumed, builder, p) } else { strings.write_string(builder, v.value) consumed += len(v.value) } - case Document_If_Break: + case Document_If_Break: if data.mode == .Break { strings.write_string(builder, v.value) consumed += len(v.value) diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 6e07d57..d57c54a 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -116,10 +116,10 @@ visit_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> (int, ^Document) return 1, empty() } else if comment.pos.line == p.source_position.line && p.source_position.column != 1 { p.source_position = comment.pos - return newlines_before_comment, cons_with_nopl(document, text(comment.text)) + return newlines_before_comment, cons_with_nopl(document, line_suffix(comment.text)) } else { p.source_position = comment.pos - return newlines_before_comment, cons(document, text(comment.text)) + return newlines_before_comment, cons(document, line_suffix(comment.text)) } } else { newlines := strings.count(comment.text, "\n") @@ -329,12 +329,14 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) -> ^Do @(private) is_call_expr_nestable :: proc(list: []^ast.Expr) -> bool { - if len(list) == 1 { - if _, is_comp := list[0].derived.(^ast.Comp_Lit); is_comp { - return false - } + if len(list) == 0 { + return true } + #partial switch v in list[len(list)-1].derived { + case ^ast.Comp_Lit, ^ast.Proc_Type, ^ast.Proc_Lit: + return false + } return true } @@ -1707,7 +1709,7 @@ visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank := if i != len(list.list) - 1 { document = cons(document, cons(text(","), break_with_space())) } else { - document = cons(document, if_break(",")) + document = len(list.list) > 1 ? cons(document, if_break(",")) : document } } diff --git a/tools/odinfmt/tests/.snapshots/calls.odin b/tools/odinfmt/tests/.snapshots/calls.odin index d169f9b..184c53f 100644 --- a/tools/odinfmt/tests/.snapshots/calls.odin +++ b/tools/odinfmt/tests/.snapshots/calls.odin @@ -2,8 +2,6 @@ package odinfmt_test calls :: proc() { - - aaaaaaaaaaaaa44444444777aaesult := vk.CreateInsaaaaaadafaddddadwadawdwadawdawddgddaaaknce( my_really_cool_call( @@ -14,7 +12,6 @@ calls :: proc() { ), ) - aaaaaaaaaaaaa44444444777aaesult = vk.CreateInsaaaaaadafaddddadwadawdwadawdawddgddaaaknce( my_really_cool_call( @@ -34,7 +31,6 @@ calls :: proc() { ), ) - result = vk.CreateInsance( my_really_cool_call( aaaaaaaaaaaaaaaaaaaaa, @@ -44,7 +40,6 @@ calls :: proc() { ), ) - test_2( Foo{ field1 = 1, @@ -58,5 +53,9 @@ calls :: proc() { }, ) + slice.sort_by(fis, proc(a, b: os.File_Info) -> bool { + return a.name < b.name + }) + } diff --git a/tools/odinfmt/tests/.snapshots/comments.odin b/tools/odinfmt/tests/.snapshots/comments.odin new file mode 100644 index 0000000..c9fc4db --- /dev/null +++ b/tools/odinfmt/tests/.snapshots/comments.odin @@ -0,0 +1,5 @@ +package odinfmt_test + +//Comments are really important + +GetPhysicsBody :: proc(index: int) -> PhysicsBody // Returns a physics body of the bodies pool at a specific index diff --git a/tools/odinfmt/tests/.snapshots/proc_types.odin b/tools/odinfmt/tests/.snapshots/proc_types.odin new file mode 100644 index 0000000..da6e334 --- /dev/null +++ b/tools/odinfmt/tests/.snapshots/proc_types.odin @@ -0,0 +1,5 @@ +package odinfmt_test + + +GetPhysicsBody :: proc(index: int) -> + ReallllllllllllllllllllllllllllllllllllllyThiccccccccccccccccccccccccPhysicsBody diff --git a/tools/odinfmt/tests/calls.odin b/tools/odinfmt/tests/calls.odin index ae9837b..38a848d 100644 --- a/tools/odinfmt/tests/calls.odin +++ b/tools/odinfmt/tests/calls.odin @@ -2,20 +2,14 @@ package odinfmt_test calls :: proc() { - - aaaaaaaaaaaaa44444444777aaesult := vk.CreateInsaaaaaadafaddddadwadawdwadawdawddgddaaaknce(my_really_cool_call(aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) - aaaaaaaaaaaaa44444444777aaesult = vk.CreateInsaaaaaadafaddddadwadawdwadawdawddgddaaaknce(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(aaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddddd)) - - test_2( Foo{field1 = 1, field2 = "hello", @@ -27,6 +21,10 @@ calls :: proc() { field8 = 1, }, ) + + slice.sort_by(fis, proc(a, b: os.File_Info) -> bool { + return a.name < b.name + }) } diff --git a/tools/odinfmt/tests/comments.odin b/tools/odinfmt/tests/comments.odin new file mode 100644 index 0000000..e22e2d9 --- /dev/null +++ b/tools/odinfmt/tests/comments.odin @@ -0,0 +1,6 @@ +package odinfmt_test + +//Comments are really important + +GetPhysicsBody :: proc(index: int) -> PhysicsBody // Returns a physics body of the bodies pool at a specific index + diff --git a/tools/odinfmt/tests/proc_types.odin b/tools/odinfmt/tests/proc_types.odin new file mode 100644 index 0000000..53dafa3 --- /dev/null +++ b/tools/odinfmt/tests/proc_types.odin @@ -0,0 +1,5 @@ +package odinfmt_test + + +GetPhysicsBody :: proc(index: int) -> ReallllllllllllllllllllllllllllllllllllllyThiccccccccccccccccccccccccPhysicsBody + |