Esc

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.

Before
main :: () {
for i: 0..2 {
if i == 1 {
          print("%\n", i);
}
}
}
After
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.

Before
main :: () {
    if ready { start(); }
    point := Vector2.{ 1, 2 };
}
After
main :: () {
    if ready {
        start();
    }
    point := Vector2.{ 1, 2 };
}

One statement per line

Statements on one line separated by ; go on lines of their own.

Before
main :: () {
    x := 1; y := 2; z := x + y;
}
After
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.

Before
main :: () {
    x:=add(1 ,2)*3;
    name   :   string = "jai";
}
After
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.

Before
main :: () {
    if ok
    {
        go();
    }
    else
    {
        stop();
    }
}
After
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.

Before
main :: () {
    a();   




    b();
}
After
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).

Before
main :: () {
    // jaifmt: off
    table := int.[1,0,
                   0,1];
    // jaifmt: on
    x:=1;   // kept
}
After
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 case lines sit inside if x == {, 0 to 16. Default: indent_width.
  • case_body_indent: How far statements under a case sit, 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; false turns 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 a case that 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, for or while. 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 jai fenced blocks inside // comments, such as the examples in doc comments. On by default.
  • max_width: The line length jaifmt --verbose reports lines over. Default 100. Lines are never re-wrapped, so it changes no output.
  • ignore: Globs, relative to the jacket.toml, of files jacket fmt skips: ignore = ["generated/**"].