diff options
| author | Bradley Lewis <22850972+BradLewis@users.noreply.github.com> | 2025-10-15 17:22:39 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-15 17:22:39 -0400 |
| commit | 5c2f9e18eda3421d252be59c734e34651c885faf (patch) | |
| tree | a0ecd28f399a4fd08e6a44aa4c134e2f95feea52 | |
| parent | 9f7ba16c9c9afeb49f2a8b1729141afe5011bc88 (diff) | |
| parent | e0bbb1b7ea5c7bc24763d1863a94ef74f0966ccb (diff) | |
Merge pull request #1096 from BradLewis/fix/odinfmt-matrix-comp-lit-with-ranges
Format matrix comp lits that use ranges
| -rwxr-xr-x | odinfmt.sh | 2 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 21 | ||||
| -rw-r--r-- | tools/odinfmt/tests/.snapshots/matrix_comp_lit.odin | 5 | ||||
| -rw-r--r-- | tools/odinfmt/tests/matrix_comp_lit.odin | 7 |
4 files changed, 28 insertions, 7 deletions
@@ -4,7 +4,7 @@ if [[ $1 == "debug" ]] then shift - odin build tools/odinfmt/main.odin -file -show-timings -collection:src=src -out:odinfmt -o:none + odin build tools/odinfmt/main.odin -file -show-timings -collection:src=src -out:odinfmt -o:none -debug exit 0 fi diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 734a3a3..8c820d4 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -1808,13 +1808,13 @@ visit_expr :: proc( document = cons_with_nopl(document, group(visit_expr(p, v.type))) if matrix_type, ok := v.type.derived.(^ast.Matrix_Type); - ok && len(v.elems) > 0 && is_matrix_type_constant(matrix_type) { + ok && is_matrix_type_constant(matrix_type) && is_matrix_filled_comp_lit(matrix_type, v) { document = cons(document, visit_begin_brace(p, v.pos, .Comp_Lit)) set_source_position(p, v.open) document = cons( document, - nest(cons(newline_position(p, 1, v.elems[0].pos), visit_matrix_comp_lit(p, v, matrix_type))), + nest(cons(newline_position(p, 1, v.elems[0].pos), visit_matrix_comp_lit(p, matrix_type, v))), ) set_source_position(p, v.end) @@ -1911,7 +1911,6 @@ visit_expr :: proc( case ^ast.Tag_Expr: document = cons( text(v.op.text), - break_with_no_newline(), text(v.name), break_with_no_newline(), visit_expr(p, v.expr), @@ -1945,14 +1944,24 @@ is_matrix_type_constant :: proc(matrix_type: ^ast.Matrix_Type) -> bool { } @(private) -visit_matrix_comp_lit :: proc(p: ^Printer, comp_lit: ^ast.Comp_Lit, matrix_type: ^ast.Matrix_Type) -> ^Document { - document := empty() - +is_matrix_filled_comp_lit :: proc(matrix_type: ^ast.Matrix_Type, comp_lit: ^ast.Comp_Lit) -> bool { //these values have already been validated row_count, _ := strconv.parse_int(matrix_type.row_count.derived.(^ast.Basic_Lit).tok.text) column_count, _ := strconv.parse_int(matrix_type.column_count.derived.(^ast.Basic_Lit).tok.text) if row_count * column_count > len(comp_lit.elems) { + return false + } + return true +} + +@(private) +visit_matrix_comp_lit :: proc(p: ^Printer, matrix_type: ^ast.Matrix_Type, comp_lit: ^ast.Comp_Lit) -> ^Document { + document := empty() + row_count, _ := strconv.parse_int(matrix_type.row_count.derived.(^ast.Basic_Lit).tok.text) + column_count, _ := strconv.parse_int(matrix_type.column_count.derived.(^ast.Basic_Lit).tok.text) + + if row_count * column_count > len(comp_lit.elems) { p.errored_out = true return document } diff --git a/tools/odinfmt/tests/.snapshots/matrix_comp_lit.odin b/tools/odinfmt/tests/.snapshots/matrix_comp_lit.odin new file mode 100644 index 0000000..a90e451 --- /dev/null +++ b/tools/odinfmt/tests/.snapshots/matrix_comp_lit.odin @@ -0,0 +1,5 @@ +package odinfmt_test + +main :: proc() { + foo := #row_major matrix[4, 4]int{4 ..< 8 = 1, 1 ..< 2 = 2} +} diff --git a/tools/odinfmt/tests/matrix_comp_lit.odin b/tools/odinfmt/tests/matrix_comp_lit.odin new file mode 100644 index 0000000..74e71d2 --- /dev/null +++ b/tools/odinfmt/tests/matrix_comp_lit.odin @@ -0,0 +1,7 @@ +package odinfmt_test + +main :: proc() { + foo := #row_major matrix[4,4]int { + 4..<8 = 1, 1..<2 = 2, + } +} |