Formatter
Automatic code formatting with loon fmt.
Usage
Run loon fmt to format your source files in place. If you pass specific file paths it will format just those files. With no arguments it recursively formats every .loon file in the current directory.
SHELL
loon fmt [files...]Check Mode
In CI you do not want to rewrite files; you just want to know if anything is out of format. The --check flag does exactly that. It exits with code 0 if all files are already formatted, and code 1 if any file would change.
SHELL
loon fmt --check [files...]Formatting Rules
The formatter is opinionated so you do not have to be. Here is what it enforces.
Indentation
2 spaces. No tabs.
Bracket spacing
No space after [ or before ]: [fn x [+ x 1]]
Map formatting
One key-value pair per line when the map exceeds the line width.
Line width
Target of 80 characters. Long expressions are wrapped.
Trailing newline
Every file ends with exactly one newline.
Blank lines
Consecutive blank lines are collapsed to one. Sections separated by one blank line.
Comment alignment
Inline comments are not realigned. Leading comments preserved as-is.
CI Integration
Adding a format check to your CI pipeline is the best way to keep the whole team consistent. Here is a minimal GitHub Actions step.
YAML
- name: Check formatting
run: loon fmt --check
The --check flag exits with a non-zero code if any file is unformatted, which will fail the CI step and let you know exactly what needs fixing.
Editor Integration
The nicest workflow is format-on-save. Configure your editor to run loon fmt every time you save a file and you will never have to think about formatting again.
VS Code
The Loon VS Code extension supports format-on-save out of the box. Enable it in settings:
JSON
{
"editor.formatOnSave": true,
"[loon]": {
"editor.defaultFormatter": "loon-lang.loon-vscode"
}}
}}Neovim
Use an autocommand to format on save:
LUA
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*.loon',
callback = function()
vim.lsp.buf.format()
end,
}})