Skip to content
Draft

Formatter

The Draft formatter (draft fmt) enforces consistent code style.

Terminal window
@// Format single file
draft fmt main.draft
@// Format directory
draft fmt src/
@// Format all files in project
draft fmt
Terminal window
draft fmt --check

Exit code 1 if files need formatting.

Terminal window
draft fmt --diff
[format]
line_width = 100
indent_size = 4
use_tabs = false
trailing_comma = true
newline_style = "Unix"

Create a .draftfmt file in the project root:

line_width = 100
indent_size = 4
use_tabs = false
trailing_comma = true
single_line_if = false

Default: 4 spaces

action example()
if @condition
do_something()
else
do_other()
fin if.
fin action.

Default: 100 characters

Lines exceeding the limit are wrapped:

@// Before formatting
@result = @math#sqrt(@a * @a + @b * @b + @c * @c + @d * @d)
@// After formatting
@result = @math#sqrt(
@a * @a + @b * @b + @c * @c + @d * @d
)
@// Between top-level definitions
action first()
@// ...
fin action.
action second()
@// ...
fin action.
@// Trailing commas in multi-line
@list = [
1,
2,
3,
]
@map = {
"a", 1,
"b", 2,
}
@// Consistent string quotes
@name = "Alice"
@greeting = "Hello, World!"
@// Single-line comments have space after //
@/*
* Multi-line comments
* are aligned
*/
@// Imports are sorted alphabetically
import collections
import fs
import http
import io
import json
import math
import process
import strings
import time
@// Standard library imports
import io
import fs
import math
@// Third-party imports
import http
@// Local imports
import { User } from ./models/user
record User
name: String
age: Int
email: String
fin record.
action add(@a: Int, @b: Int) -> Int
return @a + @b
fin action.
if @condition
do_something()
else if @other_condition
do_other()
else
fallback()
fin if.
for @item in @items
process(@item)
fin for.
while @condition
update()
fin while.
match @value
when String then handle_string(@value)
when Int then handle_int(@value)
when Boolean then handle_bool(@value)
else handle_other(@value)
fin match.
@// draftfmt:off
@x=1
@y=2
@z=3
@// draftfmt:on
@// draftfmt:file:off
{
"editor.formatOnSave": true,
"[draft]": {
"editor.defaultFormatter": "draft-lang.draft"
}
}
autocmd FileType draft setlocal formatprg=draft\ fmt\ -
(add-hook 'draft-mode-hook
(lambda () (setq format-all-mode t)))
name: Format Check
on: [push, pull_request]
jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: draft-lang/setup-draft@v1
- run: draft fmt --check
repos:
- repo: https://github.com/draft-lang/draft
rev: v1.4.0
hooks:
- id: draft-fmt
Code Meaning
0 Success / Already formatted
1 Files need formatting (with –check)
2 Error