Formatting
What jaifmt (and jacket fmt) changes, with the code before and after. The formatter moves line breaks, indentation and spaces, and never changes what a program means: output whose tokens differ from the input is refused. Lines are never re-wrapped. jaifmt says how to run it and where settings live.
What every run does is on this page. The settings change the details; they go in the [format] table of the nearest jacket.toml:
[format]
indent_width = 2
blank_line_before_return = true
Indentation
Each line is indented by how deeply it is nested in { }, ( ) and [ ]. Continuation lines keep their offset from the line they continue.
main :: () {
for i: 0..2 {
if i == 1 {
print("%\n", i);
}
}
}main :: () {
for i: 0..2 {
if i == 1 {
print("%\n", i);
}
}
}Bodies on their own lines
A non-empty { } body is split onto its own lines. Literals (.{ }, .[ ]) stay as written.
main :: () {
if ready { start(); }
point := Vector2.{ 1, 2 };
}main :: () {
if ready {
start();
}
point := Vector2.{ 1, 2 };
}One statement per line
Statements on one line separated by ; go on lines of their own.
main :: () {
x := 1; y := 2; z := x + y;
}main :: () {
x := 1;
y := 2;
z := x + y;
}Spacing
Between two tokens there is either no space or one, following the standard library's style. Nothing is aligned with runs of spaces.
main :: () {
x:=add(1 ,2)*3;
name : string = "jai";
}main :: () {
x := add(1, 2) * 3;
name: string = "jai";
}Braces
A { alone on a line joins its header, and else joins the } before it. brace_style turns this off.
main :: () {
if ok
{
go();
}
else
{
stop();
}
}main :: () {
if ok {
go();
} else {
stop();
}
}Blank lines
Trailing spaces go, a run of blank lines is cut to max_blank_lines, and a file ends with one newline. Blank lines between statements are kept, never removed.
main :: () {
a();
b();
}main :: () {
a();
b();
}Comments and strings
Comments keep their place and text. Strings, here-strings, #asm bodies and everything between // jaifmt: off and // jaifmt: on are left exactly as written, except that blanks ending a here-string's header lines are dropped (they are outside the literal). The one exception is the Jai in fenced blocks inside // comments, which is formatted too (see format_doc_examples).
main :: () {
// jaifmt: off
table := int.[1,0,
0,1];
// jaifmt: on
x:=1; // kept
}main :: () {
// jaifmt: off
table := int.[1,0,
0,1];
// jaifmt: on
x := 1; // kept
}Settings
Each key of [format], with an example of what it does.
- indent_width: Spaces per level of nesting, 1 to 16. Default 4.
- case_indent: How far
caselines sit insideif x == {, 0 to 16. Default:indent_width. - case_body_indent: How far statements under a
casesit, 0 to 16. Default:indent_width. - max_blank_lines: The longest run of blank lines kept, 0 to 100. Default 2.
- brace_style: main :: () { if ok { go(); } } ```
- blank_lines_between_items: A blank line around file-level items that span several lines, and around groups of imports. On by default;
falseturns it off. - blank_line_after_declarations: In code, a blank line after a run of declarations. Off by default.
- blank_lines_around_blocks: In code, a blank line before and after a statement with a block body. Off by default.
- blank_line_between_cases: In an
if x == {switch, a blank line before acasethat follows a multi-line arm. Off by default. - blank_line_before_comments: In code, a blank line above a comment that follows code. Off by default.
- blank_line_after_guards: In code, a blank line after a one-line
if,fororwhile. Off by default. - blank_line_before_return: In code, a blank line above a
return. Off by default. - format_doc_examples: Formats the code in
jaifenced blocks inside//comments, such as the examples in doc comments. On by default. - max_width: The line length
jaifmt --verbosereports lines over. Default 100. Lines are never re-wrapped, so it changes no output. - ignore: Globs, relative to the
jacket.toml, of filesjacket fmtskips:ignore = ["generated/**"].