Skip to content
Kavachकवच

Formatter

● Intermediate3 minBefore this: The kavach Command

kavach fmt lays out every file the same way. It has no options, so teams never argue about style.

fn main() {
const totalPrice = 5


  print( totalPrice )
}
src/main.kav, before: Kavach code
terminal
kavach fmt --check
not formatted: src/main.kav
1 file, 1 need formatting
kavach fmt
formatted src/main.kav
1 file, 1 changed
kavach fmt --check
1 file, 0 need formatting

After kavach fmt, the body is indented 4 spaces, the two blank lines in a row become one, and totalPrice is renamed total_price everywhere. It sets only the layout of lines, never what is on them:

  • 4 spaces for each open {, [ or (; several opened on one line count once.
  • One more level for a line that continues the one before, such as a line starting with . in a chain.
  • A comment line is indented like the code below it.
  • No spaces at line ends, at most one blank line in a row, none just inside { … }, and one newline at the end of the file.
  • Text written over several lines is never changed.

--check changes nothing: it lists the files that aren't formatted and fails, for a check before code is merged. It also fixes names to the language's style, renaming a declaration and every use, except a pub name, which other files may use.