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] [file1 file2 ...]
PARAMETERS
--config
Specifies the configuration file to use. Defaults to `.scalafmt.conf` in the project root.
--check
Verifies that the input files are already formatted according to the configuration, exiting with a non-zero status code if any files need formatting.
--diff
Prints a diff of the changes that scalafmt would make, instead of applying them to the files.
--version
Prints the version of scalafmt.
--help
Prints help information about the available options.
Specifies the files to format. If no files are specified, scalafmt will format all files in the project.
DESCRIPTION
Scalafmt is a command-line tool and library for formatting Scala code automatically. It enforces consistent code style across projects, reducing style debates and improving code readability. It supports a wide range of Scala syntax, including Scala 2 and Scala 3, and provides configuration options to customize the formatting rules. Scalafmt integrates well with various IDEs and build tools, making it easy to incorporate into existing development workflows. The tool can be used to format individual files, directories, or entire projects. It reads a configuration file (typically `.scalafmt.conf`) to determine the formatting rules to apply.
Scalafmt is open-source and actively maintained, with regular updates and improvements.
CAVEATS
Scalafmt requires a configuration file to be effective. The default configuration may not be suitable for all projects, so customization is often necessary. Large codebases may take a significant amount of time to format. Scalafmt doesn't support all possible scala syntaxes perfectly, edge cases or complex situations can give unexpected results.
CONFIGURATION FILE
The `.scalafmt.conf` file is crucial for defining the formatting rules. It uses a specific syntax to configure options such as indentation, line width, and other style preferences. The configuration can be defined per project.
INTEGRATION
Scalafmt integrates seamlessly with popular build tools like sbt, Maven, and Gradle, and can be used as a pre-commit hook in Git repositories to ensure code is formatted before being committed.
HISTORY
Scalafmt was created to address the need for consistent code formatting in Scala projects. Initially developed as a community-driven effort, it has evolved into a mature and widely adopted tool. Its development has been driven by the growing Scala community, with contributions from numerous developers. The tool is currently maintained and improved via community effort.
SEE ALSO
clang-format(1), prettier(1)