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 /src | |
| parent | b0f127175d05d00d14cd4fee43a50718e83664a7 (diff) | |
Added new suffix type to handle newline comments in odinfmt.
Diffstat (limited to 'src')
| -rw-r--r-- | src/odin/printer/document.odin | 41 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 16 |
2 files changed, 49 insertions, 8 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 } } |