Skip to content

Commit

Permalink
Add config for inline single case stmts
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGavin committed Aug 28, 2024
1 parent 1c62e7c commit bd60025
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
5 changes: 5 additions & 0 deletions misc/odinfmt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"type": "string",
"enum": ["CRLF", "LF"],
"default": "CRLF"
},
"inline_single_stmt_case": {
"type": "boolean",
"default": false,
"description": "When statement in the clause contains one simple statement, it will inline the case and statement in one line"
}
},
"required": []
Expand Down
21 changes: 11 additions & 10 deletions src/odin/printer/printer.odin
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ 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,
sort_imports: bool,
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,
sort_imports: bool,
inline_single_stmt_case: bool,
}

Brace_Style :: enum {
Expand Down
4 changes: 2 additions & 2 deletions src/odin/printer/visit.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ visit_stmt :: proc(
if count := len(v.body); count > 0 {
set_source_position(p, v.body[0].pos)
fst_stmt, is_assign := v.body[0].derived_stmt.(^Assign_Stmt)
if is_assign && count == 1 {
if is_assign && count == 1 && p.config.inline_single_stmt_case {
document = cons_with_opl(document, nest(visit_stmt(p, fst_stmt)))
} else {
document = cons(document, nest(cons(newline(1), visit_block_stmts(p, v.body))))
Expand Down Expand Up @@ -1550,7 +1550,7 @@ visit_expr :: proc(
document = cons_with_nopl(document, text("#raw_union"))
}

if v.is_no_copy {
if v.is_no_copy {
document = cons_with_nopl(document, text("#no_copy"))
}

Expand Down

0 comments on commit bd60025

Please sign in to comment.