Skip to content

Commit

Permalink
Add configurable line length section (#6371)
Browse files Browse the repository at this point in the history
Fixes #6223

---------

Co-authored-by: Parker Lougheed <[email protected]>
  • Loading branch information
MaryaBelanger and parlough authored Jan 31, 2025
1 parent 1564ef8 commit e83d34a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/content/tools/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
35 changes: 33 additions & 2 deletions src/content/tools/dart-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit e83d34a

Please sign in to comment.