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:

shell
silk format
TEXT
/home/you/tidy/src/main.silk: formatted
/home/you/tidy/src/util/One.silk: unchanged

Here is src/main.silk before:

silk
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:

silk
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:

silk
let piped = 10 |> subtract(3) |> negate

becomes

silk
let piped = 10
  |> subtract(3)
  |> negate

Directives 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:

silk
@inline // hot path
@nonParking fn f() -> i32 { return 1 }

becomes

silk
@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:

silk
const REVERSE_BITS: [u8; 8; 4] = [0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0]

becomes

silk
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:

silk
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:

shell
silk format --check src/util/One.silk src/main.silk

A 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:

TEXT
silk format: /home/you/tidy/scripts: selection is outside the package source root

Put -- 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:

shell
silk format --check
TEXT
/home/you/tidy/src/main.silk: needs formatting
/home/you/tidy/src/util/One.silk: unchanged

The 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.

OutcomeMeaningStatus
unchangedAlready in the standard layout0
formattedRewritten in place0
needs formatting--check only: would be rewritten; left untouched1
cannot format damaged source (CODES)The file has lexical or syntax errors, listed by code; left untouched1
a failure messageThe file could not be read or written, or formatting would have changed its meaning2

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.

TEXT
/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 format rewrites every .silk file under the source root; name files or directories to narrow it.

  • silk format --check changes 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.

View the source of this page