Skip to content

Commit 1640ca7

Browse files
munificentCommit Queue
authored andcommitted
Add the formatter changes to the CHANGELOG.
Change-Id: Ib8e0e8e99f77ebfa00f53eb3e2b4442b60b392ff Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394571 Commit-Queue: Leaf Petersen <[email protected]> Auto-Submit: Bob Nystrom <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent bba0047 commit 1640ca7

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

CHANGELOG.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,137 @@ main() {
8282
- Add the experimental `omit_obvious_property_types` lint rule.
8383
- Deprecate the `package_api_docs` lint rule.
8484

85+
#### Dart format
86+
87+
The formatter implements a [new style][tall style] better suited for the kind of
88+
declarative code that many Dart users are writing today. The new style looks
89+
similar to the style you get when you add trailing commas to argument lists,
90+
except that now the formatter will add and remove those commas for you.
91+
92+
The `dart format` command [uses the language version][formatter lang version] of
93+
each input file to determine which style it gets. If the language version is 3.6
94+
or lower, the code is formatted with the old style. If 3.7 or later, it gets the
95+
new tall style.
96+
97+
You typically control the language version by [setting a min SDK constraint in
98+
your package's pubspec][versioning]. This means that when you update the SDK
99+
constraint in your pubspec to move to 3.7, you are also opting in to the new
100+
style.
101+
102+
In order to correctly determine the language version of each file it formats,
103+
`dart format` (like other `dart` commands) looks for a `package_config.json`
104+
file surrounding the files being formatted. This means that **you need to run
105+
`dart pub get` before formatting code in your package.** If you have format
106+
checks in your continuous integration server, you'll want to make sure it runs
107+
`dart pub get` too.
108+
109+
We don't intend to support both styles indefinitely. At some point in the
110+
future when most of the ecosystem is on 3.7 or later, support for the old style
111+
will be removed.
112+
113+
[tall style]: https://github.com/dart-lang/dart_style/issues/1253
114+
115+
[versioning]: https://dart.dev/guides/language/evolution
116+
117+
[formatter lang version]: https://github.com/dart-lang/dart_style/issues/1402
118+
119+
In addition to the new formatting style, a number of other changes are included,
120+
some of them breaking:
121+
122+
* **Project-wide page width configuration.** By long request, you can now
123+
configure your preferred formatting page width on a project-wide basis. When
124+
formatting a file, the formatter will look in the file's directory and any
125+
surrounding directories for an `analysis_options.yaml` file. If it finds one,
126+
it looks for YAML like so:
127+
128+
```yaml
129+
formatter:
130+
page_width: 123
131+
```
132+
133+
If it finds a page width matching that schema, then the file is formatted
134+
using that width. Since the formatter will walk the surrounding directories
135+
until it finds an `analysis_options.yaml` file, this can be used to globally
136+
set the page width for an entire directory, package, or even collection of
137+
packages. Since `analysis_options.yaml` files already support an `include`
138+
key to reference other `analysis_options.yaml` files, you can define a single
139+
configuration and share it across a number of packages.
140+
141+
* **Opting out a region of code from formatting.** In code formatted using the
142+
new style, you can use a pair of special marker comments to opt a region of
143+
code out of automated formatting:
144+
145+
```dart
146+
main() {
147+
this.isFormatted();
148+
// dart format off
149+
no + formatting
150+
+
151+
here;
152+
// dart format on
153+
formatting.isBackOnHere();
154+
}
155+
```
156+
157+
The comments must be exactly `// dart format off` and `// dart format on`.
158+
A file may have multiple regions, but they can't overlap or nest.
159+
160+
This can be useful for highly structured data where custom layout helps the
161+
reader understand the data, like large lists of numbers.
162+
163+
* **Overriding the page width for a single file.** In code formatted
164+
using the new tall style, you can use a special marker comment to control the
165+
page width that it's formatted using:
166+
167+
```dart
168+
// dart format width=30
169+
main() {
170+
someExpression +
171+
thatSplitsAt30;
172+
}
173+
```
174+
175+
This comment must appear before any code in the file and must match that
176+
format exactly. The width set by the comment overrides the width set by any
177+
surrounding `analysis_options.yaml` file.
178+
179+
This feature is mainly for code generators that generate and immediately
180+
format code but don't know about any surrounding `analysis_options.yaml`
181+
that might be configuring the page width. By inserting this comment in the
182+
generated code before formatting, it ensures that the code generator's
183+
behavior matches the behavior of `dart format`.
184+
185+
End users should mostly use `analysis_options.yaml` for configuring their
186+
preferred page width (or do nothing and continue to use the default page width
187+
of 80).
188+
189+
* **Breaking change: Remove support for `dart format --fix`.** Instead, use
190+
`dart fix`. It supports all of the fixes that `dart format --fix` could apply
191+
and many more.
192+
193+
* **Treat the `--stdin-name` name as a path when inferring language version.**
194+
When reading input on stdin, the formatter still needs to know its language
195+
version to know what style to apply. If the `--stdin-name` option is set, then
196+
that is treated as a file path and the formatter looks for a package config
197+
surrounding that file path to infer the language version from.
198+
199+
If you don't want that behavior, pass in an explicit language version using
200+
`--language-version=`, or use `--language-version=latest` to parse the input
201+
using the latest language version supported by the formatter.
202+
203+
If `--stdin-name` and `--language-version` are both omitted, then the
204+
formatter parses stdin using the latest supported language version.
205+
206+
* **Rename the `--line-length` option to `--page-width`.** This is consistent
207+
with the public API, internal implementation, and docs, which all use "page
208+
width" to refer to the limit that the formatter tries to fit code into.
209+
210+
The `--line-length` name is still supported for backwards compatibility, but
211+
may be removed at some point in the future. You're encouraged to move to
212+
`--page-width`. Use of this option (however it's named) is rare, and will
213+
likely be even rarer now that project-wide configuration is supported, so
214+
this shouldn't affect many users.
215+
85216
## 3.6.0
86217

87218
### Language

0 commit comments

Comments
 (0)