diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-08-19 00:03:47 +0200 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-08-19 00:03:47 +0200 |
| commit | d8ec8c5c098ae2829e4916d7a33b6dbe7d515df7 (patch) | |
| tree | 18fafd4ebda4100cdfb404048a22e6f1a3340f31 | |
| parent | 599c906489bab115bf79cbde30e28a46eba2d4d2 (diff) | |
Fix ternary expression not breaking correctly.
| -rw-r--r-- | src/main.odin | 2 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 42 | ||||
| -rw-r--r-- | tools/odinfmt/snapshot/snapshot.odin | 2 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/attributes.odin | 2 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/returns.odin | 9 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/ternary.odin | 17 | ||||
| -rw-r--r-- | tools/odinfmt/tests/attributes.odin | 2 | ||||
| -rw-r--r-- | tools/odinfmt/tests/returns.odin | 9 | ||||
| -rw-r--r-- | tools/odinfmt/tests/ternary.odin | 12 |
9 files changed, 82 insertions, 15 deletions
diff --git a/src/main.odin b/src/main.odin index 29bfd41..ac9cf72 100644 --- a/src/main.odin +++ b/src/main.odin @@ -112,7 +112,7 @@ main :: proc() { context.logger = log.create_file_logger(fh, log.Level.Info) */ - when ODIN_DEBUG { + when ODIN_DEBUG && ODIN_OS == .Windows { set_stacktrace() } diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 875b230..ad123b1 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -1089,18 +1089,35 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr, called_from: Expr_Called_Type = document = cons_with_nopl(text_token(p, v.op), visit_expr(p, v.expr)) case ^Ternary_If_Expr: if v.op1.text == "if" { - document = group(visit_expr(p, v.x)) - document = cons_with_nopl(document, text_token(p, v.op1)) - document = cons_with_opl(document, group(visit_expr(p, v.cond))) - document = cons_with_nopl(document, text_token(p, v.op2)) - document = cons_with_opl(document, group(visit_expr(p, v.y))) + document = cons( + group(visit_expr(p, v.x)), + if_break(" \\"), + break_with_space(), + text_token(p, v.op1), + break_with_no_newline(), + group(visit_expr(p, v.cond)), + if_break(" \\"), + break_with_space(), + text_token(p, v.op2), + break_with_no_newline(), + group(visit_expr(p, v.y)), + ) } else { - document = group(visit_expr(p, v.cond)) - document = cons_with_nopl(document, text_token(p, v.op1)) - document = cons_with_opl(document, group(visit_expr(p, v.x))) - document = cons_with_nopl(document, text_token(p, v.op2)) - document = cons_with_opl(document, group(visit_expr(p, v.y))) - } + document = cons( + group(visit_expr(p, v.cond)), + if_break(" \\"), + break_with_space(), + text_token(p, v.op1), + break_with_no_newline(), + group(visit_expr(p,v.x)), + if_break(" \\"), + break_with_space(), + text_token(p, v.op2), + break_with_no_newline(), + group(visit_expr(p, v.y)), + ) + } + document = group(document) case ^Ternary_When_Expr: document = visit_expr(p, v.x) document = cons_with_nopl(document, text_token(p, v.op1)) @@ -1264,6 +1281,7 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr, called_from: Expr_Called_Type = document = cons_with_nopl(document, visit_proc_type(p, v.type^, v.body != nil)) document = cons_with_nopl(document, push_where_clauses(p, v.where_clauses)) + document = cons_with_opl(document, visit_proc_tags(p, v.tags)) if v.body != nil { set_source_position(p, v.body.pos) @@ -1710,8 +1728,6 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type, contains_body: bo document = cons_with_nopl(document, text("!")) } - document = cons_with_opl(document, visit_proc_tags(p, proc_type.tags)) - return group(document) } diff --git a/tools/odinfmt/snapshot/snapshot.odin b/tools/odinfmt/snapshot/snapshot.odin index ed84e39..2f658a5 100644 --- a/tools/odinfmt/snapshot/snapshot.odin +++ b/tools/odinfmt/snapshot/snapshot.odin @@ -67,7 +67,7 @@ snapshot_file :: proc(path: string) -> bool { for { s_ch := scanner.next(&snapshot_scanner) f_ch := scanner.next(&formatted_scanner) - if s_ch == scanner.EOF || f_ch == scanner.EOF { + if s_ch == scanner.EOF && f_ch == scanner.EOF { break } diff --git a/tools/odinfmt/tests/.snapshots/attributes.odin b/tools/odinfmt/tests/.snapshots/attributes.odin index a6eaa65..58dd5f8 100644 --- a/tools/odinfmt/tests/.snapshots/attributes.odin +++ b/tools/odinfmt/tests/.snapshots/attributes.odin @@ -6,3 +6,5 @@ main :: proc() { #no_bounds_check buf = buf[8:] } + +procedure_no_bounds_check :: proc() where 1 == 1 #no_bounds_check {} diff --git a/tools/odinfmt/tests/.snapshots/returns.odin b/tools/odinfmt/tests/.snapshots/returns.odin index 85996b7..0b6dd10 100644 --- a/tools/odinfmt/tests/.snapshots/returns.odin +++ b/tools/odinfmt/tests/.snapshots/returns.odin @@ -17,3 +17,12 @@ return_without_paren_1 :: proc() { GRADIENTS_4D[gi | 3] * delta.w \ ) } + +returns_with_call_expression :: proc() { + return resolve_type_comp_literal( + ast_context, + position_context, + symbol, + cast(^ast.Comp_Lit)field_value.value, + ) +} diff --git a/tools/odinfmt/tests/.snapshots/ternary.odin b/tools/odinfmt/tests/.snapshots/ternary.odin new file mode 100644 index 0000000..50d48fb --- /dev/null +++ b/tools/odinfmt/tests/.snapshots/ternary.odin @@ -0,0 +1,17 @@ +package odinfmt_test + + +main :: proc() { + a := + .Group in options \ + ? group(visit_expr(p, expr, called_from, options)) \ + : visit_expr(p, expr, called_from, options) + + + document = cons( + document, + .Group in options \ + ? group(visit_expr(p, expr, called_from, options)) \ + : visit_expr(p, expr, called_from, options), + ) +} diff --git a/tools/odinfmt/tests/attributes.odin b/tools/odinfmt/tests/attributes.odin index a6eaa65..6e55c21 100644 --- a/tools/odinfmt/tests/attributes.odin +++ b/tools/odinfmt/tests/attributes.odin @@ -6,3 +6,5 @@ main :: proc() { #no_bounds_check buf = buf[8:] } + +procedure_no_bounds_check :: proc() where 1 == 1 #no_bounds_check {}
\ No newline at end of file diff --git a/tools/odinfmt/tests/returns.odin b/tools/odinfmt/tests/returns.odin index f4ff400..2359308 100644 --- a/tools/odinfmt/tests/returns.odin +++ b/tools/odinfmt/tests/returns.odin @@ -13,4 +13,13 @@ return_without_paren_1 :: proc() { return GRADIENTS_4D[gi] * delta.x + GRADIENTS_4D[gi | 1] * delta.y + GRADIENTS_4D[gi | 2] * delta.z + GRADIENTS_4D[gi | 3] * delta.w +} + +returns_with_call_expression :: proc() { + return resolve_type_comp_literal( + ast_context, + position_context, + symbol, + cast(^ast.Comp_Lit)field_value.value, + ) }
\ No newline at end of file diff --git a/tools/odinfmt/tests/ternary.odin b/tools/odinfmt/tests/ternary.odin new file mode 100644 index 0000000..5eb044e --- /dev/null +++ b/tools/odinfmt/tests/ternary.odin @@ -0,0 +1,12 @@ +package odinfmt_test + + +main :: proc() { + a := .Group in options ? group(visit_expr(p, expr, called_from, options)) : visit_expr(p, expr, called_from, options) + + + document = cons( + document, + .Group in options ? group(visit_expr(p, expr, called_from, options)) : visit_expr(p, expr, called_from, options), + ) +}
\ No newline at end of file |