Skip to content

Commit dd1f7d9

Browse files
authored
Allow disabling formatting for a region of code. (#1522)
Allow disabling formatting for a region of code. There are two special line comments: ``` // dart format off // dart format on ``` Any code between those keeps its original formatting completely, including indentation. It just splats the original unformatted code into the output.
1 parent 8c0e44e commit dd1f7d9

19 files changed

+611
-133
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* Allow passing a language version to `DartFomatter()`. Formatted code will be
44
parsed at that version. If omitted, defaults to the latest version. In a
55
future release, this parameter will become required.
6+
* Allow opting out of formatting for a region of code using `// dart format off`
7+
and `// dart format on` comments. Note: This only works using the new tall
8+
style and requires passing the `--enable-experiment=tall-style` experiment
9+
flag (#361).
610
* Preserve type parameters on old-style function-typed formals that also use
711
`this.` or `super.` (#1321).
812
* Remove temporary work around for analyzer 6.2.0 from dart_style 2.3.6.

benchmark/run.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ double _runTrial(DartFormatter formatter, ParseStringResult parseResult,
160160
result = visitor.run(parseResult.unit).text;
161161
} else {
162162
var visitor = AstNodeVisitor(formatter, parseResult.lineInfo, source);
163-
result = visitor.run(parseResult.unit).text;
163+
result = visitor.run(source, parseResult.unit).text;
164164
}
165165
}
166166

example/format.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main(List<String> args) {
2525
class C {}
2626
''');
2727

28-
_runTest('selection/selection.stmt', 2);
28+
_runTest('other/selection.stmt', 2);
2929
}
3030

3131
void _formatStmt(String source, {bool tall = true, int pageWidth = 40}) {
@@ -68,7 +68,7 @@ void _drawRuler(String label, int width) {
6868
/// directory.
6969
Future<void> _runTest(String path, int line,
7070
{int pageWidth = 40, bool tall = true}) async {
71-
var testFile = await TestFile.read(path);
71+
var testFile = await TestFile.read('${tall ? 'tall' : 'short'}/$path');
7272
var formatTest = testFile.tests.firstWhere((test) => test.line == line);
7373

7474
var formatter = DartFormatter(

0 commit comments

Comments
 (0)