Formatting.
silk format rewrites Silk source in one standard layout, so a project reads the same everywhere and diffs show real changes. It changes only layout: indentation, line breaks and spacing. Comments stay where you wrote them, and a file that compiled before still compiles after.
Format a package#
Run it anywhere inside a package. It finds the nearest silk.toml and formats every .silk file under the package's source root, in path order:
silk format/home/you/tidy/src/main.silk: formatted
/home/you/tidy/src/util/One.silk: unchangedHere is src/main.silk before:
import silk.core.effect { Effect }
import silk.io.logger { LogError }
import silk.io.logger_native { StdoutLogger }
struct Point { x: i32,
y: i32 }
fn sum(p: &Point) -> i32 { return p.x + p.y }
pub effect fn main() -> () ! LogError {
let mut logger = StdoutLogger.make()
let p = Point {
x: 1,
y: 2,
}
run Effect.log("sum {s}\n", &.{ s: sum(&p) }) |> Effect.provideMut(&mut logger)
}And after:
import silk.core.effect { Effect }
import silk.io.logger { LogError }
import silk.io.logger_native { StdoutLogger }
struct Point {
x: i32,
y: i32
}
fn sum(p: &Point) -> i32 {
return p.x + p.y
}
pub effect fn main() -> ()
! LogError {
let mut logger = StdoutLogger.make()
let p = Point { x: 1, y: 2 }
run Effect.log("sum {s}\n", &.{ s: sum(&p) }) |> Effect.provideMut(&mut logger)
}Each struct field gets its own line, a one-line function body is split over three lines, a struct literal that fits joins onto one line, and the failure row of an effect signature moves to its own line. A comma after the last field stays as you wrote it: y: i32 had none, so it gets none.
A pipeline with one |> stays on one line when it fits, as the Effect.log line does, and moves the step to the next line, indented, when it does not. A chain of two or more |> steps always puts each step on its own line:
let piped = 10 |> subtract(3) |> negatebecomes
let piped = 10
|> subtract(3)
|> negateDirectives go one per line before the declaration they apply to. A // comment after a directive stays on its line, and so does a /// doc comment, which may sit before or after the directives:
@inline // hot path
@nonParking fn f() -> i32 { return 1 }becomes
@inline // hot path
@nonParking
fn f() -> i32 {
return 1
}Array columns#
A long array literal breaks one element per line. To lay a table out in rows instead, add a column count after a second semicolon in the array type of the let or const it initializes:
const REVERSE_BITS: [u8; 8; 4] = [0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0]becomes
const REVERSE_BITS: [u8; 8; 4] = [
0x00, 0x80, 0x40, 0xC0,
0x20, 0xA0, 0x60, 0xE0,
]The count is a formatter hint, not part of the type: [u8; 8; 4] is the type [u8; 8], and the length may still be an expression, as in [u8; 256 / 4; 4]. A literal with more elements than the count always breaks, with that many elements on each line and the last row holding the rest. One with the count or fewer stays on one line when it fits. Each column is right-aligned to its widest element, so numbers line up:
const STEPS: [i32; 6; 3] = [
1, 200, 3,
4000, 5, -60,
]A literal with a comment inside it, or an element that spans several lines, keeps one space between elements instead.
The hint applies only to the array literal that is the declaration's own value, and only when the type is written there. Nothing carries it further, so a later assignment such as table = [...] is laid out as an ordinary list. The count is a decimal literal of at least 1, written only on the outermost array type of a let or const; anywhere else, such as a parameter type or an element type, it is a syntax error.
Format some files#
Name files or directories to format only those. Paths are relative to the current directory. A directory must be inside the package's source root. A .silk file can be anywhere, even in a folder with no silk.toml, so an editor can format a scratch file:
silk format --check src/util/One.silk src/main.silkA directory selects every .silk file below it. A directory outside the source root, or a file that is not .silk, is refused with exit status 2 before any file is touched:
silk format: /home/you/tidy/scripts: selection is outside the package source rootPut -- before a file name that starts with -. --manifest-path <path> selects the package by its manifest instead of the search upward.
Check in CI#
--check reports instead of rewriting. A file that differs from the standard layout is listed as needs formatting and left alone:
silk format --check/home/you/tidy/src/main.silk: needs formatting
/home/you/tidy/src/util/One.silk: unchangedThe exit status is 1 when any file needs formatting, so silk format --check fails a CI job until someone runs silk format.
Outcomes and exit status#
Each selected file gets one line, <path>: <outcome>. unchanged and formatted lines go to standard output; the others go to standard error.
| Outcome | Meaning | Status |
|---|---|---|
unchanged | Already in the standard layout | 0 |
formatted | Rewritten in place | 0 |
needs formatting | --check only: would be rewritten; left untouched | 1 |
cannot format damaged source (CODES) | The file has lexical or syntax errors, listed by code; left untouched | 1 |
| a failure message | The file could not be read or written, or formatting would have changed its meaning | 2 |
The command exits with the worst status over all files. Problems that stop it before any file, such as no silk.toml for a directory, an unknown option or a directory outside the source root, print one silk format: line and exit 2.
A damaged file is skipped, not guessed at. Fix the syntax error first; silk check shows where it is.
/home/you/tidy/src/main.silk: unchanged
/home/you/tidy/src/util/One.silk: cannot format damaged source (PAR0001)Workspaces#
At a workspace root, silk format and silk format --check cover the source root of every member, and so does silk format .; at a package root, . selects its source root. Inside a member, they cover only that member. See Workspaces in Projects and packages.
Summary#
silk formatrewrites every.silkfile under the source root; name files or directories to narrow it.silk format --checkchanges nothing and exits 1 when any file needs formatting.const T: [u8; 64; 4] = [...]lays the literal out four elements per line; the hint is not part of the type.Files with syntax errors are reported with their codes and left untouched.
Exit status 2 means the command or a file operation failed, not the layout.