diff --git a/src/content/tools/analysis.md b/src/content/tools/analysis.md index eaa85a801a..292077fca2 100644 --- a/src/content/tools/analysis.md +++ b/src/content/tools/analysis.md @@ -583,6 +583,13 @@ analyzer: dead_code: info ``` +## Configuring `dart format` + +You can configure the behavior of [`dart format`][] by adding a +`formatter` section to the analysis options file +specifying your preferred `page_width`. + +For more information, read [Configuring formatter page width][]. ## Resources @@ -603,3 +610,5 @@ Use the following resources to learn more about static analysis in Dart: [dead_code]: /tools/diagnostic-messages#dead_code [disable individual rules]: #disabling-individual-rules [Effective Dart]: /effective-dart +[`dart format`]: /tools/dart-format +[Configuring formatter page width]: /tools/dart-format#configuring-formatter-page-width diff --git a/src/content/tools/dart-format.md b/src/content/tools/dart-format.md index 397cfb5dca..1c74f7a577 100644 --- a/src/content/tools/dart-format.md +++ b/src/content/tools/dart-format.md @@ -72,13 +72,42 @@ $ dart format -o none --set-exit-if-changed bin/my_app.dart * Removes whitespace. * Wraps every line to 80 characters long or shorter. -* Adds trailing commas to any argument or parameter list. +* Adds trailing commas to any argument or parameter list that splits across multiple lines, and removes them from ones that don't. * Might move comments before or after a comma. To learn more about best practices for writing and styling Dart code, check out the [Dart style guide][]. +### Configuring formatter page width + +When you run `dart format`, the formatter defaults to +80 character line length or shorter. +If you'd like to configure the line length for your project, +you can add a top-level `formatter` section to the +[`analysis_options.yaml`][] file, like so: + +```yaml title="analysis_options.yaml" +formatter: + page_width: 123 +``` + +With the analysis options file typically at the root, +the configured line length will apply to everything in the package. + +You can also configure individual files' line length, +overriding the analysis options file, +with a marker comment at the top of the file before any other code: + +```dart +// dart format width=123 +``` + +:::version-note +Configurable page width requires +a [language version][] of at least 3.7. +::: + ## Learn more To learn about additional command-line options, @@ -94,4 +123,6 @@ Check out the [formatter FAQ][] for more context behind formatting decisions. [Dart style guide]: /effective-dart/style [dart_style]: {{site.pub-pkg}}/dart_style [dart-guidelines]: /effective-dart/style#formatting -[formatter FAQ]: {{site.repo.dart.org}}/dart_style/wiki/FAQ +[`analysis_options.yaml`]: /tools/analysis +[language version]: /resources/language/evolution#language-versioning +[formatter FAQ]: {{site.repo.dart.org}}/dart_style/wiki/FAQ \ No newline at end of file