What it changes
The formatter changes layout and separators, and keeps what the file does.
.kscript files
- Blocks are indented by nesting, and so are the lines inside a
( ),[ ]or{ }that spans several lines. A statement continued on the next line (after+,,or=, or before|>or->) goes one level further in. - Each statement goes on its own line: a one-line block such as
for i in 1..3 { echo $i }is opened up, and statements joined with;are split. else,elif,catchandfinallygo on the line of the}before them:} else {.- Spacing inside a line: a space is added where one is missing after a comma or a colon (
add(1, 2),{ a: 1 }), around a statement's=and a=>, before a block's{and before a//comment at the end of a line. Spaces you wrote are kept, so$total = 1and a comment lined up with the ones around it stay as they are, and so does the inside of a parameter list or an expression ($b: int=2,$a+$b). - In a type,
|is written without spaces:int|uuid,status: draft|live,id|slug: int. In a parameter list, the|that starts the next member of a one-of group (a|followed byname:) has a space on each side:(as: draft|live | template: Template). - A line that runs past
print_width(80 columns unless you set it) is wrapped, as described in Long lines. - An enum's values go on one line, with commas, when they fit in
print_width:enum Size { small, medium, large }. A longer enum gets one value per line, with no commas, the way a type holds one field per line. Values with a comment among them are left as written. - Model options go one per line when the header is long or already split. Each option gets a comma, including the last one, so adding an option changes one line. A trailing comma before
)on the same line is an error: it only goes after the last item of a list written one per line. - Trailing spaces are removed, runs of blank lines shrink to
max_blank_lines(and go entirely at the start and end of a block), and the file ends with exactly one newline.
<let name="x" value="[3 ,1,2]" />
<for each="i" in="$x">
<echo value="$i" />
</for>
<if $x->count() > 2>
<echo>big</echo>
</if>
<else>
<echo>small</echo>
</else>$x=[3 ,1,2]
for i in $x { echo $i }
if $x->count() > 2{
echo 'big'
}
else {
echo 'small'
}becomes
<let name="x" value="[3, 1, 2]" />
<for each="i" in="$x">
<echo value="$i" />
</for>
<if $x->count() > 2>
<echo>big</echo>
</if>
<else>
<echo>small</echo>
</else>$x = [3, 1, 2]
for i in $x {
echo $i
}
if $x->count() > 2 {
echo 'big'
} else {
echo 'small'
}Long lines
A line longer than print_width is broken where the code joins most loosely, one step at a time, until it fits, the way Prettier and dart format do. Each step is tried in this order:
- A one-line function's body moves to the next line, after the
=>. - A chain of
->calls, or a pipe of|>steps, puts each call on its own line, one level in from where the chain starts. One call on a plain name ($user->save(...)) is left for its arguments to wrap, and a name no wider than an indent keeps its first call ($s->trim()). - A call, array or map puts one item per line.
- A lambda that is a call's only argument breaks after its
=>. - A string is broken between words, with a
\at the end of the line so the break isn't part of it (thewrap_stringsrule).
A // or /// comment that runs past print_width is broken between words, and the words that move down join the next comment line when it carries on the same paragraph. In a doc comment, the rest of a tag (@param) or a list item hangs under its text. @example code, ``` fences, tables, headings and words too long to fit (like links) are left alone, and a line is never broken just before a word that would start something new on its own line: a tag such as @deprecated, a list item or a heading.
<import package="KScript.IO" />
<import package="KScript.Cache" />
<fn name="cached" params="key: string, compute: fn" description="The value kept under the key, or else what compute gives, kept for cache.ttl, which says how long." value="@cache('memory', name: 'app')->remember($key, $compute, ttl: $config.cache.ttl ?? '5m')" />
<fn name="adults" params="people: list" value="$people->filter(fn(p) => $p.age >= 18)->sort(by: .name)->map(.name)->take(20)" />
<let name="people" value="@read_json(./people.json)" />
<let name="names" value="$people |> filter(fn(p) => $p.age > 18) |> map(.name->upper()) |> sort()" />import KScript.IO
import KScript.Cache
/// The value kept under the key, or else what compute gives, kept for cache.ttl, which says how long.
fn cached(key: string, compute: fn) => @cache('memory', name: 'app')->remember($key, $compute, ttl: $config.cache.ttl ?? '5m')
fn adults(people: list) => $people->filter(fn(p) => $p.age >= 18)->sort(by: .name)->map(.name)->take(20)
$people = @read_json(./people.json)
$names = $people |> filter(fn(p) => $p.age > 18) |> map(.name->upper()) |> sort()becomes
<import package="KScript.IO" />
<import package="KScript.Cache" />
<!---
The value kept under the key, or else what compute gives, kept for
cache.ttl, which says how long.
-->
<fn name="cached" params="key: string, compute: fn" value="
@cache('memory', name: 'app')
->remember($key, $compute, ttl: $config.cache.ttl ?? '5m')" />
<fn name="adults" params="people: list" value="
$people
->filter(fn(p) => $p.age >= 18)
->sort(by: .name)
->map(.name)
->take(20)" />
<let name="people" value="@read_json(./people.json)" />
<let name="names">$people
|> filter(fn(p) => $p.age > 18)
|> map(.name->upper())
|> sort()</let>import KScript.IO
import KScript.Cache
/// The value kept under the key, or else what compute gives, kept for
/// cache.ttl, which says how long.
fn cached(key: string, compute: fn) =>
@cache('memory', name: 'app')
->remember($key, $compute, ttl: $config.cache.ttl ?? '5m')
fn adults(people: list) =>
$people
->filter(fn(p) => $p.age >= 18)
->sort(by: .name)
->map(.name)
->take(20)
$people = @read_json(./people.json)
$names = $people
|> filter(fn(p) => $p.age > 18)
|> map(.name->upper())
|> sort()Lines are only broken, never joined, so a short line you split on purpose stays split. Set print_width in kscript.json (or your standard) to choose the width, and turn any step off with the rules below.
Text in quotes, /* */ comments and the arms of a match used as a value ($label = match $n { 1 => 'one', _ => 'many' }) are kept as they are, apart from the long-line wrapping above. The arms of a match statement are laid out as statements: each arm keeps its line (1, 2 => echo 'low'), and an arm's { } block is opened up like any other block. After formatting, kscript fmt reads the file again and checks that it is the same program, statement for statement; if it isn't, only the line layout (indentation) is tidied and kscript fmt prints a warning saying what the full rules would have changed, or, if even that would change the program, the file is left alone with an error. Either way, please report it.
HTML fences returned from a function declared as Component use the page formatter. This indents nested markup and wraps long tags, with the same whitespace rules as an HTML page. Fences used as string values keep their contents.
XML scripts and pages
In XML scripts and pages:
- Elements are indented by how deeply they're nested, and each statement in a script goes on its own line.
- Start tags are tidied: one space between attributes, and
<let name="x"/>becomes<let name="x" />. A tag with several attributes that would run pastprint_widthgets one attribute per line, and so does a tag you already wrote that way. - Trailing spaces are removed, runs of blank lines shrink to
max_blank_lines, and the file ends with exactly one newline.
<kscript>
<let name="n" value="3"/>
<for each="i" in="1..$n"><echo>$i</echo></for>
</kscript>$n = 3
for i in 1..$n {
echo $i
}becomes
<kscript>
<let name="n" value="3" />
<for each="i" in="1..$n">
<echo>$i</echo>
</for>
</kscript>$n = 3
for i in 1..$n {
echo $i
}