Skip to content

Commit f531115

Browse files
docs:documented trailing_commas (#7122)
fixes #7101 Changes made: Restructured the configuration section - Changed the heading from "Configuring formatter page width" to "Configuring the formatter" with subheadings for "Page width" and "Preserving trailing commas" Added documentation for trailing_commas: preserve @parlough @jonasfj @conooi @antfitch please review --------- Co-authored-by: Connie Ooi <connieooi@google.com>
1 parent 38ad076 commit f531115

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/content/effective-dart/style.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ an import or export), it may remain whole even if it causes the line to go over
525525
because newlines are significant inside the string and splitting the lines into
526526
shorter ones can alter the program.
527527

528-
[configure]: /tools/dart-format#configuring-formatter-page-width
528+
[configure]: /tools/dart-format#page-width
529529

530530
<a id="do-use-curly-braces-for-all-flow-control-structures"></a>
531531
### DO use curly braces for all flow control statements

src/content/resources/whats-new.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ we made the following changes to this site:
252252
[inference using bounds]: /language/type-system/#type-argument-inference
253253
[for example]: /tools/linter-rules/annotate_overrides
254254
[Bluesky]: https://bsky.app/profile/dart.dev
255-
[configurable line length]: /tools/dart-format#configuring-formatter-page-width
255+
[configurable line length]: /tools/dart-format#page-width
256256
[shared analysis options]: /tools/analysis#including-shared-options
257257
[line length]: /effective-dart/style#prefer-lines-80-characters-or-fewer
258258
[stray files]: /tools/pub/workspaces#stray-files

src/content/tools/analysis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,5 +671,5 @@ Use the following resources to learn more about static analysis in Dart:
671671
[disable individual rules]: #disabling-individual-rules
672672
[Effective Dart]: /effective-dart
673673
[`dart format`]: /tools/dart-format
674-
[Configuring formatter page width]: /tools/dart-format#configuring-formatter-page-width
674+
[Configuring formatter page width]: /tools/dart-format#page-width
675675
[Troubleshoot analyzer performance]: /tools/analyzer-performance

src/content/tools/dart-format.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@ that splits across multiple lines, and removes them from ones that don't.
106106
To learn more about best practices for writing and styling Dart code,
107107
check out the [Dart style guide][].
108108

109-
### Configuring formatter page width
109+
### Configure the formatter
110110

111111
When you run `dart format`, the formatter defaults to
112112
80 character line length or shorter.
113-
If you'd like to configure the line length for your project,
114-
you can add a top-level `formatter` section to the
115-
[`analysis_options.yaml`][] file, like so:
113+
Configure the formatter's behavior by adding a top-level
114+
`formatter` section to the [`analysis_options.yaml`][] file.
115+
116+
#### Page width
117+
118+
Configure the line length for your project:
116119

117120
```yaml title="analysis_options.yaml"
118121
formatter:
@@ -122,17 +125,36 @@ formatter:
122125
With the analysis options file typically at the root,
123126
the configured line length will apply to everything in the package.
124127
125-
You can also configure individual files' line length,
126-
overriding the analysis options file,
127-
with a marker comment at the top of the file before any other code:
128+
#### Preserve trailing commas
129+
130+
By default, `dart format` adds or removes trailing commas
131+
based on whether the construct splits across multiple lines.
132+
To force a construct to split even when it fits on one line,
133+
configure the formatter to preserve trailing commas:
134+
135+
```yaml title="analysis_options.yaml"
136+
formatter:
137+
trailing_commas: preserve
138+
```
139+
140+
When enabled, adding a trailing comma to an argument list,
141+
parameter list, or other constructs, forces the formatter to split
142+
it across multiple lines, even if it fits on one line. This provides
143+
manual control over line breaks for specific constructs.
144+
This gives you manual control over line breaks for specific constructs.
145+
146+
Configure an individual file's line length,
147+
overriding the analysis options, by adding
148+
a marker comment at the top of the file, before any other code:
128149

129150
```dart
130151
// dart format width=123
131152
```
132153

133154
:::version-note
134-
Configurable page width requires
155+
Configurable formatter options require
135156
a [language version][] of at least 3.7.
157+
The `trailing_commas` option requires at least 3.8.
136158
:::
137159

138160
## Learn more

0 commit comments

Comments
 (0)