diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2023-07-29 19:47:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-29 19:47:30 +0200 |
| commit | a010fd2c8f9c3b3bd4cec75022c50aac94c0cb02 (patch) | |
| tree | 9a297e0dffcafdac93f2f4a27b0bd34b5f529699 /src | |
| parent | b994162b8a04175f5aae94fd59ce46f388c1f179 (diff) | |
| parent | 75a14dcc796c1f97a98660381b77828183c345e6 (diff) | |
Merge pull request #223 from flga/linebreak_control
Allow a little more line break control in composite literals
Diffstat (limited to 'src')
| -rw-r--r-- | src/odin/printer/printer.odin | 19 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 41 |
2 files changed, 34 insertions, 26 deletions
diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index d2af255..ba2b7e3 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -38,15 +38,16 @@ 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 - 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 + multiline_composite_literals: bool `json:"exp_multiline_composite_literals"`, + 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 3e71b0a..c23305e 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -966,10 +966,7 @@ visit_stmt :: proc( } if !uses_do { - document = cons( - document, - visit_begin_brace(p, v.pos, block_type, len(v.stmts)), - ) + document = cons(document, visit_begin_brace(p, v.pos, block_type)) } else { document = cons(document, text("do"), break_with(" ", false)) } @@ -1449,6 +1446,16 @@ 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 @@ -1978,9 +1985,9 @@ visit_expr :: proc( if matrix_type, ok := v.type.derived.(^ast.Matrix_Type); ok && len(v.elems) > 0 && is_matrix_type_constant(matrix_type) { - document = cons_with_nopl( + document = cons( document, - visit_begin_brace(p, v.pos, .Generic), + visit_begin_brace(p, v.pos, .Comp_Lit), ) set_source_position(p, v.open) @@ -2005,7 +2012,11 @@ 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 &= @@ -2015,10 +2026,7 @@ visit_expr :: proc( should_newline &= len(v.elems) != 0 if should_newline { - document = cons_with_nopl( - document, - visit_begin_brace(p, v.pos, .Generic), - ) + document = cons(document, visit_begin_brace(p, v.pos, .Comp_Lit)) set_source_position(p, v.open) document = cons( @@ -2205,8 +2213,6 @@ visit_begin_brace :: proc( p: ^Printer, begin: tokenizer.Pos, type: Block_Type, - count := 0, - same_line_spaces_before := 1, ) -> ^Document { set_source_position(p, begin) set_comment_option(p, begin.line, .Indent) @@ -2216,12 +2222,13 @@ visit_begin_brace :: proc( newline_braced &= p.config.brace_style != ._1TBS if newline_braced { - document := newline(1) - document = cons(document, text("{")) - return document - } else { - return text("{") + if type == .Comp_Lit { + return cons(text("\\"), newline(1), text("{")) + } + return cons(newline(1), text("{")) } + + return text("{") } @(private) |