Store the result of version tests instead of comparing each time Because style is language versioned, the formatter very often checks to see if the language version is above some threshold where a style change was shipped. Prior to this PR, we did that by invoking the comparison operator on the Version object each time. It turns out those tests are often enough that doing the check each time is slow. Instead, this calculates the predicates we need ahead of time and stores those directly. ``` Benchmark (tall) fastest median slowest average baseline ----------------------------- -------- ------- ------- ------- -------- block 0.083 0.099 0.182 0.107 108.4% chain 0.703 0.727 0.755 0.726 100.6% collection 0.413 0.422 0.454 0.425 103.5% collection_large 1.778 1.813 2.045 1.819 104.6% conditional 0.084 0.085 0.102 0.087 105.6% curry 0.175 0.176 0.207 0.180 106.6% curry_2 0.348 0.354 0.393 0.359 106.0% ffi 0.161 0.163 0.183 0.166 103.1% flutter_popup_menu_test 0.567 0.586 0.607 0.585 104.7% flutter_scrollbar_test 2.003 2.026 2.075 2.030 103.5% function_call 1.793 1.837 1.952 1.837 104.4% infix_large 0.739 0.757 0.787 0.760 103.0% infix_small 0.186 0.191 0.234 0.194 103.5% interpolation 0.145 0.149 0.174 0.151 107.0% interpolation_1516 0.128 0.131 0.151 0.133 106.7% large 4.366 4.433 4.687 4.425 104.5% top_level 0.161 0.164 0.193 0.166 104.9% ```
The dart_style package defines an opinionated, minimally configurable automated formatter for Dart code.
It replaces the whitespace in your program with what it deems to be the best formatting for it. It also makes minor changes around non-semantic punctuation like trailing commas and brackets in parameter lists.
The resulting code should follow the Dart style guide and look nice to most human readers, most of the time.
The formatter handles indentation, inline whitespace, and (by far the most difficult) intelligent line wrapping. It has no problems with nested collections, function expressions, long argument lists, or otherwise tricky code.
The formatter turns code like this:
process = await Process.start(path.join(p.pubCacheBinPath,Platform.isWindows ?'${command.first}.bat':command.first,),[...command.sublist(1),'web:0', // Allow for binding to a random available port. ],workingDirectory:workingDir,environment:{'PUB_CACHE':p.pubCachePath,'PATH': path.dirname(Platform.resolvedExecutable)+(Platform.isWindows?';':':')+ Platform.environment['PATH']!,},);
into:
process = await Process.start( path.join( p.pubCacheBinPath, Platform.isWindows ? '${command.first}.bat' : command.first, ), [ ...command.sublist(1), 'web:0', // Allow for binding to a random available port. ], workingDirectory: workingDir, environment: { 'PUB_CACHE': p.pubCachePath, 'PATH': path.dirname(Platform.resolvedExecutable) + (Platform.isWindows ? ';' : ':') + Platform.environment['PATH']!, }, );
The formatter will never break your code—you can safely invoke it automatically from build and presubmit scripts.
The formatter is part of the unified dart developer tool included in the Dart SDK, so most users run it directly from there using dart format.
IDEs and editors that support Dart usually provide easy ways to run the formatter. For example, in Visual Studio Code, formatting Dart code will use the dart_style formatter by default. Most users have it set to reformat every time they save a file.
Here's a simple example of using the formatter on the command line:
$ dart format my_file.dart
This command formats the my_file.dart file and writes the result back to the same file.
The dart format command takes a list of paths, which can point to directories or files. If the path is a directory, it processes every .dart file in that directory and all of its subdirectories.
By default, dart format formats each file and writes the result back to the same files. If you pass --output show, it prints the formatted code to stdout and doesn't modify the files.
If you want to use the formatter in something like a presubmit script or commit hook, you can pass flags to omit writing formatting changes to disk and to update the exit code to indicate success/failure:
$ dart format --output=none --set-exit-if-changed .
The dart_style package exposes a simple library API for formatting code. Basic usage looks like this:
import 'package:dart_style/dart_style.dart'; main() { final formatter = DartFormatter( languageVersion: DartFormatter.latestLanguageVersion, ); try { print(formatter.format(""" library an_entire_compilation_unit; class SomeClass {} """)); print(formatter.formatStatement("aSingle(statement);")); } on FormatterException catch (ex) { print(ex); } }
Before sending an email, see if you are asking a frequently asked question.
Before filing a bug, or if you want to understand how work on the formatter is managed, see how we track issues.