scalafmt
Format Scala source code according to standards
TLDR
Reformat all .scala files in the current directory recursively
Reformat specific files or directories with a custom formatting configuration
Check if files are correctly formatted, returning 0 if all files respect the formatting style
Exclude files or directories
Format only files that were edited against the current Git branch
SYNOPSIS
scalafmt [options] [files...]
scalafmt --stdin [options]
PARAMETERS
--help, -h
Displays a help message with available options and commands.
--version, -v
Prints the current scalafmt version.
--config
Specifies an alternative path to the .scalafmt.conf
configuration file.
--test, --check
Checks if files are formatted according to the configuration without modifying them. Exits with a non-zero code if any files are not formatted correctly.
--diff
Formats only the lines that have changed in the current Git diff, typically against the master branch.
--stdout
Prints the formatted content of the input files to standard output instead of writing back to the files.
--stdin
Reads Scala code from standard input, formats it, and prints the result to standard output.
--files
Specifies a list of files or glob patterns to format. Overrides patterns defined in the configuration file.
--exclude
Specifies files or glob patterns to exclude from formatting.
--respect-project-filters
Enables scalafmt to respect .scalafmtignore
and .gitignore
files for filtering files.
--dry-run
Shows which files would be formatted without actually writing any changes to disk.
--non-interactive
Suppresses prompts and assumes default or affirmative answers.
--quiet, -q
Suppresses most informational output, only showing errors.
DESCRIPTION
scalafmt is a powerful and opinionated code formatter for Scala. Its primary goal is to ensure consistent code style across projects and development teams, enhancing readability and maintainability. By parsing Scala source code into an Abstract Syntax Tree (AST), scalafmt applies a set of configurable rules to reformat the code, adhering to predefined stylistic guidelines.
It aims to eliminate bikeshedding over code style, allowing developers to focus on logic rather than formatting. While scalafmt comes with sensible default settings, it is highly configurable via a .scalafmt.conf
file, enabling teams to customize formatting rules to match their specific preferences or adhere to organizational standards. This configuration file allows fine-grained control over aspects like indentation, line breaks, brace style, and more.
scalafmt integrates seamlessly into various development workflows, including command-line usage, build tools like sbt and Mill, and IDEs. It supports different Scala versions and offers features like checking formatting without modifying files, formatting only changed lines (useful in Git hooks), and piping input/output for scripting. Its deterministic output ensures that running the formatter multiple times on the same input yields identical results.
CAVEATS
- Configuration Complexity: While powerful, the
.scalafmt.conf
file can become complex, requiring familiarity with HOCON syntax and scalafmt's specific options. - Performance: On very large codebases with many files, scalafmt can take a noticeable amount of time to run, especially if invoked without caching or incremental analysis.
- Scala Version Compatibility: Ensure the scalafmt version is compatible with your project's Scala version, as new Scala language features might require a more recent formatter.
- Integration Challenges: Integrating with specific IDEs or older build setups might require custom scripting or plugin configurations.
CONFIGURATION FILE
The primary way to configure scalafmt is through a .scalafmt.conf
file, typically located at the project root. This file uses the HOCON format and allows defining rules for indentation, line length, blank lines, imports, and many other stylistic aspects.
BUILD TOOL INTEGRATION
scalafmt is commonly integrated into Scala build tools. For sbt, the sbt-scalafmt
plugin provides sbt tasks like scalafmt
and scalafmtCheck
. This allows formatting code as part of the build process or before committing changes, ensuring all code adheres to the defined style.
HISTORY
scalafmt was primarily developed by VirtusLab as an opinionated code formatter for the Scala programming language. Its development began around 2016-2017, aiming to provide a robust and highly configurable tool to address the common problem of inconsistent code styles within Scala projects.
It gained significant traction within the Scala community due to its reliability, rich configuration options, and seamless integration with popular build tools like sbt and Mill. Over time, it has become a standard and widely adopted tool for maintaining code quality and consistency in Scala codebases, playing a crucial role in enabling large-scale collaborative development. Its design emphasizes determinism, ensuring that formatted code remains stable across runs and different environments.
SEE ALSO
sbt(1), mill(1), prettier(1), clang-format(1), gofmt(1)