Scala Code Formatter
Re-indent Scala source code by tracking brace, paren and bracket nesting depth — object, class, trait, def, match/case, if/else and for/yield blocks get consistent indentation, while comments, string and triple-quoted literals are left untouched.
Input
Formatting Options
Output
Guides
The Scala Code Formatter re-indents messy or hand-pasted Scala source so every block lines up consistently. Paste your code (or upload a .scala file), choose an indent size, and the formatted result appears instantly with copy and download buttons.
It works by tracking nesting depth through the braces, parentheses and brackets in your code. Each object, class, trait, def, val/var block, match/case expression, if/else block and for/yield block is indented one level deeper than the construct that contains it, and a line that begins with a closing delimiter is lined up with the line that opened its block. Whatever indentation the input had is discarded and rebuilt from scratch. Because case arms live inside a match { … } block's braces, they are indented automatically without any special handling.
What it does — and what it doesn't
This is a structural re-indenter, not a drop-in replacement for scalafmt. The real scalafmt runs on the JVM and parses the full Scala grammar with scalameta; it can't run in your browser. This tool deliberately does one thing well — fix indentation — and leaves everything else on each line untouched:
- It does recompute the leading whitespace of every line from brace/paren/bracket depth, so nested code is cleanly stepped in and out.
- It does not rewrite spacing around operators, wrap long lines to a column limit, organize imports, or insert or remove trailing commas.
If you need canonical, style-guide formatting for a commit, run scalafmt locally. If you just want to make a chunk of copied Scala readable again, this tool is faster and needs nothing installed.
Comments and string literals are safe
A naive re-indenter breaks the moment a brace appears inside a string or a comment. This formatter uses a Scala-aware scanner, so brackets are only counted when they are real code. It correctly skips over:
- line comments (
// …), - block comments (
/* … */), including the nested block comments Scala allows, - regular and interpolated string literals (
"…",s"…",f"…"), where a${ … }interpolation is skipped as a balanced-brace region so braces inside the interpolated expression are never counted, - triple-quoted strings (
"""…"""), including interpolated ones, which may span several lines, - character literals (
'a','\n') and Scala 2 symbol literals ('name).
Text inside a multi-line triple-quoted string or block comment is emitted exactly as you typed it, so a formatter run never changes the value of a string.
Which indent size should I use?
Two spaces is the Scala community standard and the default here (it matches the Scala style guide and scalafmt's default). Four spaces and tabs are offered for codebases or snippets that prefer them.
Is my code sent anywhere?
No. All formatting runs entirely in your browser — your code never leaves your device.