How to use the command "scalafmt" (with examples)
“scalafmt” is a code formatter specifically designed for Scala programming language. It allows developers to automatically format their Scala code according to predefined or custom formatting configurations. The configurations are stored in the .scalafmt.conf
file. This command helps in maintaining consistent code formatting across projects and improves code readability.
Use case 1: Reformat all .scala
files in the current directory recursively
Code:
scalafmt
Motivation:
Reformatting all .scala
files in the current directory recursively can be useful when there are multiple files that need to have consistent formatting. This use case ensures that all Scala files within the project are formatted correctly.
Explanation:
Running the command scalafmt
without any additional arguments will recursively reformat all the .scala
files in the current directory and its subdirectories using the default .scalafmt.conf
configuration file, if it exists.
Example output:
Reformatted file1.scala
Reformatted subdirectory/file2.scala
...
Use case 2: Reformat specific files or directories with a custom formatting configuration
Code:
scalafmt --config path/to/.scalafmt.conf path/to/file_or_directory path/to/file_or_directory ...
Motivation: In some cases, it might be necessary to have different formatting configurations for specific files or directories within a project. Using a custom formatting configuration for selected files or directories allows developers to adhere to different code style requirements.
Explanation:
To reformat specific files or directories with a custom formatting configuration, the --config
flag is used to specify the path to the .scalafmt.conf
file. You can provide multiple paths to files or directories that you want to reformat.
Example output:
Reformatted path/to/file1.scala
Reformatted path/to/file2.scala
...
Use case 3: Check if files are correctly formatted
Code:
scalafmt --config path/to/.scalafmt.conf --test
Motivation: Ensuring that all code is correctly formatted according to a specified style guide is essential for maintaining a consistent codebase. By running this command, developers can quickly check and validate if all files respect the specified formatting style.
Explanation:
The --test
flag is used to check if the files are correctly formatted. It returns 0
if all files adhere to the formatting style defined in the .scalafmt.conf
configuration file.
Example output:
All files are correctly formatted.
Use case 4: Exclude files or directories
Code:
scalafmt --exclude path/to/file_or_directory ...
Motivation: There might be instances where it is necessary to exclude specific files or directories from being formatted. This use case allows developers to exclude files or directories that should not be reformatted.
Explanation:
The --exclude
flag is used to specify the files or directories that should be excluded from the formatting process. You can provide multiple paths to files or directories that you want to exclude.
Example output:
Skipping path/to/file1.scala
Reformatted path/to/file2.scala
...
Use case 5: Format only files that were edited against the current Git branch
Code:
scalafmt --config path/to/.scalafmt.conf --mode diff
Motivation: When working with version control systems like Git, it can be beneficial to format only the files that have been edited against the current branch. This use case allows developers to focus on only the modified files when formatting code, saving time on files that have not been edited.
Explanation:
The --mode diff
flag is used to format only the files that have been edited against the current Git branch. It compares the changes made to each file against the branch and applies formatting only to the modified files.
Example output:
Reformatted path/to/modified_file.scala
Skipping path/to/unmodified_file.scala
...
Conclusion:
“scalafmt” is a powerful command-line tool for automatically formatting Scala code. By using the different options and flags provided by the command, developers can easily achieve consistent code formatting across projects and ensure that their code adheres to the specified style guide. Use cases such as reformatting all Scala files, reformatting specific files with custom configurations, checking formatting correctness, excluding files or directories, and formatting only modified files against the Git branch provide developers with flexibility and control over code formatting.