Skip to content

Latest commit

 

History

History
160 lines (120 loc) · 3.54 KB

File metadata and controls

160 lines (120 loc) · 3.54 KB

FormatTool

Formats dart files in the current project by running dartfmt.

Usage

This tool is included in the coreConfig and is runnable by default via ddev format.

// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';

final config = {
  'format': FormatTool() // configure as necessary
};

Default behavior

By default this tool will run dartfmt -w . which will format all dart files in the current project.

Configuration

Default mode

FormatTool can be run in 3 modes:

  • FormatMode.overwrite (default)
    • e.g. ddev format -w
  • FormatMode.dryRun (lists files that would be changed)
    • e.g. ddev format -n
  • FormatMode.check (dry-run and sets the exit code if changes are needed)
    • e.g. ddev format -c
// tool/dart_dev/config.dart
import 'package:dart_dev/dart_dev.dart';

final config = {
  'format': FormatTool()
    ..defaultMode = FormatMode.check
};

Using the dart_style package instead of dartfmt

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
};

Passing args to the formatter process

// 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 .
----------------------------

Configuring the formatter language version

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.

Excluding files from formatting

// 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')]
};

Organizing directives

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
};

Command-line options

$ ddev help format