1- The dart_style package defines an automatic, opinionated formatter for Dart
2- code. It replaces the whitespace in your program with what it deems to be the
3- best formatting for it. Resulting code should follow the [ Dart style guide] [ ]
4- but, moreso, should look nice to most human readers, most of the time.
1+ The dart_style package defines an opinionated automated formatter for Dart code.
2+ It replaces the whitespace in your program with what it deems to be the
3+ best formatting for it. It also makes minor changes around non-semantic
4+ punctuation like trailing commas and brackets in parameter lists.
5+
6+ The resulting code should follow the [ Dart style guide] [ ] and look nice to most
7+ human readers, most of the time.
58
69[ dart style guide ] : https://dart.dev/guides/language/effective-dart/style
710
@@ -14,80 +17,89 @@ The formatter turns code like this:
1417
1518``` dart
1619// BEFORE formatting
17- if (tag=='style'||tag=='script'&&(type==null||type == TYPE_JS
18- ||type==TYPE_DART)||
19- tag=='link'&&(rel=='stylesheet'||rel=='import')) {}
20+ process = await Process.start(path.join(p.pubCacheBinPath,Platform.isWindows
21+ ?'${command.first}.bat':command.first,),[...command.sublist(1),'web:0',
22+ // Allow for binding to a random available port.
23+ ],workingDirectory:workingDir,environment:{'PUB_CACHE':p.pubCachePath,'PATH':
24+ path.dirname(Platform.resolvedExecutable)+(Platform.isWindows?';':':')+
25+ Platform.environment['PATH']!,},);
2026```
2127
2228into:
2329
2430``` dart
2531// AFTER formatting
26- if (tag == 'style' ||
27- tag == 'script' &&
28- (type == null || type == TYPE_JS || type == TYPE_DART) ||
29- tag == 'link' && (rel == 'stylesheet' || rel == 'import')) {}
32+ process = await Process.start(
33+ path.join(
34+ p.pubCacheBinPath,
35+ Platform.isWindows ? '${command.first}.bat' : command.first,
36+ ),
37+ [
38+ ...command.sublist(1), 'web:0',
39+ // Allow for binding to a random available port.
40+ ],
41+ workingDirectory: workingDir,
42+ environment: {
43+ 'PUB_CACHE': p.pubCachePath,
44+ 'PATH':
45+ path.dirname(Platform.resolvedExecutable) +
46+ (Platform.isWindows ? ';' : ':') +
47+ Platform.environment['PATH']!,
48+ },
49+ );
3050```
3151
3252The formatter will never break your code&mdash ; you can safely invoke it
3353automatically from build and presubmit scripts.
3454
35- ## Using the formatter
55+ ## Formatting files
3656
3757The formatter is part of the unified [ ` dart ` ] [ ] developer tool included in the
38- Dart SDK, so most users get it directly from there. That has the latest version
39- of the formatter that was available when the SDK was released.
58+ Dart SDK, so most users run it directly from there using ` dart format ` .
4059
4160[ `dart` ] : https://dart.dev/tools/dart-tool
4261
4362IDEs and editors that support Dart usually provide easy ways to run the
44- formatter. For example, in WebStorm you can right-click a .dart file and then
45- choose ** Reformat with Dart Style** .
63+ formatter. For example, in Visual Studio Code, formatting Dart code will use
64+ the dart_style formatter by default. Most users have it set to reformat every
65+ time they save a file.
4666
4767Here's a simple example of using the formatter on the command line:
4868
49- $ dart format test.dart
69+ ``` sh
70+ $ dart format test.dart
71+ ```
5072
51- This command formats the ` test.dart ` file and writes the result to the
73+ This command formats the ` test.dart ` file and writes the result back to the same
5274file.
5375
54- ` dart format ` takes a list of paths, which can point to directories or files. If
55- the path is a directory, it processes every ` .dart ` file in that directory or
56- any of its subdirectories.
76+ The ` dart format ` command takes a list of paths, which can point to directories
77+ or files. If the path is a directory, it processes every ` .dart ` file in that
78+ directory and all of its subdirectories.
5779
58- By default, it formats each file and write the formatting changes to the files.
59- If you pass ` --output show ` , it prints the formatted code to stdout.
80+ By default, ` dart format ` formats each file and writes the result back to the
81+ same files. If you pass ` --output show ` , it prints the formatted code to stdout
82+ and doesn't modify the files.
6083
61- You may pass a ` -l ` option to control the width of the page that it wraps lines
62- to fit within, but you're strongly encouraged to keep the default line length of
63- 80 columns.
64-
65- ### Validating files
84+ ## Validating formatting
6685
6786If you want to use the formatter in something like a [ presubmit script] [ ] or
6887[ commit hook] [ ] , you can pass flags to omit writing formatting changes to disk
6988and to update the exit code to indicate success/failure:
7089
71- $ dart format --output=none --set-exit-if-changed .
90+ ``` sh
91+ $ dart format --output=none --set-exit-if-changed .
92+ ```
7293
7394[ presubmit script ] : https://www.chromium.org/developers/how-tos/depottools/presubmit-scripts
7495[ commit hook ] : https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
7596
76- ## Running other versions of the formatter CLI command
77-
78- If you need to run a different version of the formatter, you can
79- [ globally activate] [ ] the package from the dart_style package on
80- pub.dev:
81-
82- [ globally activate ] : https://dart.dev/tools/pub/cmd/pub-global
83-
84- $ pub global activate dart_style
85- $ pub global run dart_style:format ...
97+ ## Using the formatter as a library
8698
87- ## Using the dart_style API
99+ The `dart_style package exposes a simple [ library API] [ ] for formatting code.
100+ Basic usage looks like this:
88101
89- The package also exposes a single dart_style library containing a programmatic
90- API for formatting code. Simple usage looks like this:
102+ [ library api ] : https://pub.dev/documentation/dart_style/latest/
91103
92104``` dart
93105import 'package:dart_style/dart_style.dart';
0 commit comments