Formatter
The Draft formatter (draft fmt) enforces consistent code style.
Format Files
Section titled “Format Files”@// Format single filedraft fmt main.draft
@// Format directorydraft fmt src/
@// Format all files in projectdraft fmtCheck Formatting
Section titled “Check Formatting”draft fmt --checkExit code 1 if files need formatting.
Show Diff
Section titled “Show Diff”draft fmt --diffConfiguration
Section titled “Configuration”draft.toml
Section titled “draft.toml”[format]line_width = 100indent_size = 4use_tabs = falsetrailing_comma = truenewline_style = "Unix".draftfmt
Section titled “.draftfmt”Create a .draftfmt file in the project root:
line_width = 100indent_size = 4use_tabs = falsetrailing_comma = truesingle_line_if = falseFormatting Rules
Section titled “Formatting Rules”Indentation
Section titled “Indentation”Default: 4 spaces
action example() if @condition do_something() else do_other() fin if.fin action.Line Width
Section titled “Line Width”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)Blank Lines
Section titled “Blank Lines”@// Between top-level definitionsaction first() @// ...fin action.
action second() @// ...fin action.Trailing Commas
Section titled “Trailing Commas”@// Trailing commas in multi-line@list = [ 1, 2, 3,]
@map = { "a", 1, "b", 2,}String Quotes
Section titled “String Quotes”@// Consistent string quotes@name = "Alice"@greeting = "Hello, World!"Comment Formatting
Section titled “Comment Formatting”@// Single-line comments have space after //
@/* * Multi-line comments * are aligned */Import Sorting
Section titled “Import Sorting”@// Imports are sorted alphabeticallyimport collectionsimport fsimport httpimport ioimport jsonimport mathimport processimport stringsimport timeImport Grouping
Section titled “Import Grouping”@// Standard library importsimport ioimport fsimport math
@// Third-party importsimport http
@// Local importsimport { User } from ./models/userRecord Formatting
Section titled “Record Formatting”record User name: String age: Int email: Stringfin record.Action Formatting
Section titled “Action Formatting”action add(@a: Int, @b: Int) -> Int return @a + @bfin action.If-Else Formatting
Section titled “If-Else Formatting”if @condition do_something()else if @other_condition do_other()else fallback()fin if.Loop Formatting
Section titled “Loop Formatting”for @item in @items process(@item)fin for.
while @condition update()fin while.Match Formatting
Section titled “Match Formatting”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.Ignoring Code
Section titled “Ignoring Code”Skip Formatting
Section titled “Skip Formatting”@// draftfmt:off@x=1@y=2@z=3@// draftfmt:onSkip File
Section titled “Skip File”@// draftfmt:file:offEditor Integration
Section titled “Editor Integration”VS Code
Section titled “VS Code”{ "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)))CI Integration
Section titled “CI Integration”GitHub Actions
Section titled “GitHub Actions”name: Format Checkon: [push, pull_request]jobs: format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: draft-lang/setup-draft@v1 - run: draft fmt --checkPre-commit Hook
Section titled “Pre-commit Hook”repos: - repo: https://github.com/draft-lang/draft rev: v1.4.0 hooks: - id: draft-fmtExit Codes
Section titled “Exit Codes”| Code | Meaning |
|---|---|
| 0 | Success / Already formatted |
| 1 | Files need formatting (with –check) |
| 2 | Error |
