Skip to content

Commit

Permalink
Split case clause when it's too big.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGavin committed Apr 28, 2024
1 parent d15f438 commit 490c163
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
46 changes: 24 additions & 22 deletions src/odin/printer/document.odin
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
for i := len(v.elements) - 1; i >= 0; i -= 1 {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.elements[i],
Expand All @@ -388,18 +388,19 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
case Document_Align:
append(
list,
Tuple{
Tuple {
indentation = 0,
mode = data.mode,
document = v.document,
alignment = start_width - width,
},
)

case Document_Nest:
if v.alignment != 0 {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.document,
Expand All @@ -410,7 +411,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
} else {
append(
list,
Tuple{
Tuple {
indentation = data.indentation + (v.negate ? -1 : 1),
mode = data.mode,
document = v.document,
Expand All @@ -430,7 +431,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
if data.mode == .Break {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.break_document,
Expand All @@ -440,7 +441,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
} else if v.fit_document != nil {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.fit_document,
Expand All @@ -451,7 +452,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
case Document_Group:
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = (v.mode == .Break ? .Break : data.mode),
document = v.document,
Expand Down Expand Up @@ -537,7 +538,7 @@ format :: proc(
for i := len(v.elements) - 1; i >= 0; i -= 1 {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.elements[i],
Expand All @@ -549,7 +550,7 @@ format :: proc(
if v.alignment != 0 {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.document,
Expand All @@ -560,7 +561,7 @@ format :: proc(
} else {
append(
list,
Tuple{
Tuple {
indentation = data.indentation + (v.negate ? -1 : 1),
mode = data.mode,
document = v.document,
Expand All @@ -569,13 +570,14 @@ format :: proc(
)
}
case Document_Align:
align := consumed - data.indentation * p.indentation_width
append(
list,
Tuple{
indentation = 0,
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.document,
alignment = consumed,
alignment = align,
},
)
case Document_Text:
Expand Down Expand Up @@ -611,7 +613,7 @@ format :: proc(
if mode == .Break {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.break_document,
Expand All @@ -621,7 +623,7 @@ format :: proc(
} else if v.fit_document != nil {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.fit_document,
Expand All @@ -633,7 +635,7 @@ format :: proc(
if data.mode == .Flat && !recalculate {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = v.mode,
document = v.document,
Expand All @@ -651,7 +653,7 @@ format :: proc(

append(
&list_fits,
Tuple{
Tuple {
indentation = data.indentation,
mode = .Flat,
document = v.document,
Expand All @@ -664,7 +666,7 @@ format :: proc(
if data.mode == .Fit {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = .Fit,
document = v.document,
Expand All @@ -676,7 +678,7 @@ format :: proc(
v.mode != .Fit {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = .Flat,
document = v.document,
Expand All @@ -687,7 +689,7 @@ format :: proc(
if data.mode == .Fill || v.mode == .Fill {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = .Fill,
document = v.document,
Expand All @@ -697,7 +699,7 @@ format :: proc(
} else if v.mode == .Fit {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = .Fit,
document = v.document,
Expand All @@ -707,7 +709,7 @@ format :: proc(
} else {
append(
list,
Tuple{
Tuple {
indentation = data.indentation,
mode = .Break,
document = v.document,
Expand Down
33 changes: 21 additions & 12 deletions src/odin/printer/visit.odin
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,9 @@ visit_bit_field_fields :: proc(

document := empty()

name_alignment, type_alignment := get_possible_bit_field_alignment(bit_field_type.fields)
name_alignment, type_alignment := get_possible_bit_field_alignment(
bit_field_type.fields,
)

for field, i in bit_field_type.fields {
if i == 0 && .Enforce_Newline in options {
Expand Down Expand Up @@ -756,7 +758,8 @@ visit_bit_field_fields :: proc(
cons_with_nopl(
cons(
repeat_space(
type_alignment - get_node_length(field.type),
type_alignment -
get_node_length(field.type),
),
text_position(p, "|", field.type.end),
),
Expand Down Expand Up @@ -791,7 +794,8 @@ visit_bit_field_fields :: proc(
document = cons(document, text(","))
}

if (i != len(bit_field_type.fields) - 1 && .Enforce_Newline in options) {
if (i != len(bit_field_type.fields) - 1 &&
.Enforce_Newline in options) {
comment, _ := visit_comments(p, bit_field_type.fields[i + 1].pos)
document = cons(document, comment, newline(1))
} else if .Enforce_Newline in options {
Expand Down Expand Up @@ -1094,7 +1098,7 @@ visit_stmt :: proc(

set_source_position(p, v.pos)

block := visit_block_stmts(p, v.stmts, len(v.stmts) > 1)
block := visit_block_stmts(p, v.stmts)

comment_end, _ := visit_comments(
p,
Expand Down Expand Up @@ -1238,7 +1242,7 @@ visit_stmt :: proc(
if v.list != nil {
document = cons_with_nopl(
document,
visit_exprs(p, v.list, {.Add_Comma}),
align(group(visit_exprs(p, v.list, {.Add_Comma}))),
)
}

Expand Down Expand Up @@ -1948,7 +1952,11 @@ visit_expr :: proc(
document = cons_with_nopl(document, text("{"))
document = cons(document, text("}"))
} else {
document = cons(document, break_with_space(), visit_begin_brace(p, v.pos, .Generic))
document = cons(
document,
break_with_space(),
visit_begin_brace(p, v.pos, .Generic),
)
set_source_position(p, v.fields[0].pos)
document = cons(
document,
Expand Down Expand Up @@ -2420,11 +2428,7 @@ visit_end_brace :: proc(
}

@(private)
visit_block_stmts :: proc(
p: ^Printer,
stmts: []^ast.Stmt,
split := false,
) -> ^Document {
visit_block_stmts :: proc(p: ^Printer, stmts: []^ast.Stmt) -> ^Document {
document := empty()

for stmt, i in stmts {
Expand Down Expand Up @@ -2965,7 +2969,12 @@ get_possible_enum_alignment :: proc(exprs: []^ast.Expr) -> int {
}

@(private)
get_possible_bit_field_alignment :: proc(fields: []^ast.Bit_Field_Field) -> (longest_name: int, longest_type: int) {
get_possible_bit_field_alignment :: proc(
fields: []^ast.Bit_Field_Field,
) -> (
longest_name: int,
longest_type: int,
) {
for field in fields {
longest_name = max(longest_name, get_node_length(field.name))
longest_type = max(longest_type, get_node_length(field.type))
Expand Down

0 comments on commit 490c163

Please sign in to comment.