aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2024-04-28 16:51:21 +0200
committerDanielGavin <danielgavin5@hotmail.com>2024-04-28 16:51:21 +0200
commit49a63471d91120a23ec86f1621e99155d1be55c2 (patch)
tree3dfafeb0653c094d79b9e780b1dbfd38fa0b206b
parent0cb994a3b520b8800fa2dbe7e3b279f9496b6f59 (diff)
Fix issues with space in { on comp literals without types.
-rw-r--r--src/odin/printer/visit.odin22
-rw-r--r--tests/definition_test.odin12
-rw-r--r--tools/odinfmt/tests/.snapshots/returns.odin8
-rw-r--r--tools/odinfmt/tests/returns.odin10
4 files changed, 42 insertions, 10 deletions
diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin
index 0568026..f2e6636 100644
--- a/src/odin/printer/visit.odin
+++ b/src/odin/printer/visit.odin
@@ -574,6 +574,16 @@ is_return_stmt_ending_with_call_expr :: proc(list: []^ast.Expr) -> bool {
return true
}
+
+ return false
+}
+
+@(private)
+is_return_stmt_ending_with_comp_lit_expr :: proc(list: []^ast.Expr) -> bool {
+ if len(list) == 0 {
+ return false
+ }
+
if _, is_cmp := list[len(list) - 1].derived.(^ast.Comp_Lit); is_cmp {
return true
}
@@ -1489,8 +1499,13 @@ visit_stmt :: proc(
} else {
document = cons(document, text("return"))
-
- if !is_return_stmt_ending_with_call_expr(v.results) {
+ if is_return_stmt_ending_with_comp_lit_expr(v.results) {
+ document = cons(
+ document,
+ if_break_or_document(empty(), text(" ")),
+ visit_exprs(p, v.results, {.Add_Comma}),
+ )
+ } else if !is_return_stmt_ending_with_call_expr(v.results) {
document = cons_with_nopl(
document,
group(
@@ -2245,11 +2260,12 @@ visit_expr :: proc(
document = cons(document, newline(1), text_position(p, "}", v.end))
} else {
+ break_string := " " if v.type != nil else ""
document = cons(
document,
group(
cons(
- if_break(" "),
+ if_break(break_string),
text("{"),
nest(
cons(
diff --git a/tests/definition_test.odin b/tests/definition_test.odin
index 5c60dc8..d98d92f 100644
--- a/tests/definition_test.odin
+++ b/tests/definition_test.odin
@@ -24,7 +24,7 @@ ast_goto_comp_lit_field :: proc(t: ^testing.T) {
}
location := common.Location {
- range = {
+ range = {
start = {line = 2, character = 12},
end = {line = 2, character = 13},
},
@@ -51,7 +51,7 @@ ast_goto_comp_lit_field_indexed :: proc(t: ^testing.T) {
}
location := common.Location {
- range = {
+ range = {
start = {line = 2, character = 12},
end = {line = 2, character = 13},
},
@@ -81,7 +81,7 @@ ast_goto_untyped_comp_lit_in_proc :: proc(t: ^testing.T) {
}
location := common.Location {
- range = {
+ range = {
start = {line = 2, character = 4},
end = {line = 2, character = 7},
},
@@ -107,7 +107,7 @@ ast_goto_bit_field_definition :: proc(t: ^testing.T) {
}
location := common.Location {
- range = {
+ range = {
start = {line = 1, character = 3},
end = {line = 1, character = 15},
},
@@ -134,7 +134,7 @@ ast_goto_bit_field_field_definition :: proc(t: ^testing.T) {
}
location := common.Location {
- range = {
+ range = {
start = {line = 2, character = 4},
end = {line = 2, character = 7},
},
@@ -164,7 +164,7 @@ ast_goto_bit_field_field_in_proc :: proc(t: ^testing.T) {
}
location := common.Location {
- range = {
+ range = {
start = {line = 2, character = 4},
end = {line = 2, character = 7},
},
diff --git a/tools/odinfmt/tests/.snapshots/returns.odin b/tools/odinfmt/tests/.snapshots/returns.odin
index ddd8fef..577d5d5 100644
--- a/tools/odinfmt/tests/.snapshots/returns.odin
+++ b/tools/odinfmt/tests/.snapshots/returns.odin
@@ -42,3 +42,11 @@ return_with_call_expression_in_the_end :: proc() {
cons(text(p.indentation), line_suffix(comment.text)),
)
}
+
+return_with_comp_lit_expression_in_the_end :: proc() {
+ return {
+ alloc_fn = allocator_alloc_func,
+ free_fn = allocator_free_func,
+ user_data = cast(rawptr)context_ptr,
+ }
+}
diff --git a/tools/odinfmt/tests/returns.odin b/tools/odinfmt/tests/returns.odin
index fea7c2d..58c562f 100644
--- a/tools/odinfmt/tests/returns.odin
+++ b/tools/odinfmt/tests/returns.odin
@@ -1,4 +1,4 @@
-package odinfmt_test
+ package odinfmt_test
return_with_paren_1 :: proc() {
return(
@@ -34,5 +34,13 @@ return_with_call_expression_in_the_end :: proc() {
return newlines_before_comment, cons_with_nopl(document, cons(text(p.indentation), line_suffix(comment.text)))
}
+return_with_comp_lit_expression_in_the_end :: proc() {
+ return {
+ alloc_fn = allocator_alloc_func,
+ free_fn = allocator_free_func,
+ user_data = cast(rawptr)context_ptr,
+ }
+}
+