Formats dart files in the current project by running dartfmt.
This tool is included in the
coreConfigand is runnable by default viaddev format.
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
final config = {
'format': FormatTool() // configure as necessary
};By default this tool will run dartfmt -w . which will format all dart files in
the current project.
FormatTool can be run in 3 modes:
FormatMode.overwrite(default)- e.g.
ddev format -w
- e.g.
FormatMode.dryRun(lists files that would be changed)- e.g.
ddev format -n
- e.g.
FormatMode.check(dry-run and sets the exit code if changes are needed)- e.g.
ddev format -c
- e.g.
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
final config = {
'format': FormatTool()
..defaultMode = FormatMode.check
};Some projects like to depend on a specific version of the dart_style package
and use its format executable rather than the dartfmt provided by the Dart
SDK.
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
final config = {
'format': FormatTool()
..formatter = Formatter.dartStyle
};// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
final config = {
'format': FormatTool()
..formatterArgs = ['--fix']
};$ ddev format
[INFO] Running subprocess...
dartfmt -w --fix .
----------------------------For formatters that support --language-version, you can configure the value
used by dart_dev.
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
final config = {
'format': FormatTool()
..languageVersion = '3.0'
};If languageVersion is not configured, dart_dev will use latest only in
the cases where it decides the formatter needs an explicit language version.
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
import 'package:glob/glob.dart';
final config = {
'format': FormatTool()
..exclude = [Glob('test_fixtures/**'), Glob('**.g.dart')]
};By default, the format tool will not sort imports/export. They can be automatically
sorted by setting organizeDirectives.
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';
final config = {
'format': FormatTool()
..organizeDirectives = true
};$ ddev help format