-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathformat.dart
136 lines (120 loc) · 3.53 KB
/
format.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:dart_style/dart_style.dart';
import 'package:dart_style/src/debug.dart' as debug;
import 'package:dart_style/src/testing/test_file.dart';
void main(List<String> args) {
// Enable debugging so you can see some of the formatter's internal state.
// Normal users do not do this.
debug.traceChunkBuilder = true;
debug.traceLineWriter = true;
debug.traceSplitter = true;
debug.useAnsiColors = true;
debug.tracePieceBuilder = true;
debug.traceSolver = true;
debug.traceSolverEnqueing = true;
debug.traceSolverDequeing = true;
debug.traceSolverShowCode = true;
_formatStmt('''
1 + 2;
''');
_formatUnit('''
class C {}
''');
_runTest('other/selection.stmt', 2);
}
void _formatStmt(
String source, {
bool tall = true,
int pageWidth = 40,
TrailingCommas trailingCommas = TrailingCommas.automate,
}) {
_runFormatter(
source,
pageWidth,
tall: tall,
isCompilationUnit: false,
trailingCommas: trailingCommas,
);
}
void _formatUnit(
String source, {
bool tall = true,
int pageWidth = 40,
TrailingCommas trailingCommas = TrailingCommas.automate,
}) {
_runFormatter(
source,
pageWidth,
tall: tall,
isCompilationUnit: true,
trailingCommas: trailingCommas,
);
}
void _runFormatter(
String source,
int pageWidth, {
required bool tall,
required bool isCompilationUnit,
TrailingCommas trailingCommas = TrailingCommas.automate,
}) {
try {
var formatter = DartFormatter(
languageVersion:
tall
? DartFormatter.latestLanguageVersion
: DartFormatter.latestShortStyleLanguageVersion,
pageWidth: pageWidth,
trailingCommas: trailingCommas,
);
String result;
if (isCompilationUnit) {
result = formatter.format(source);
} else {
result = formatter.formatStatement(source);
}
_drawRuler('before', pageWidth);
print(source);
_drawRuler('after', pageWidth);
print(result);
} on FormatterException catch (error) {
print(error.message());
}
}
void _drawRuler(String label, int width) {
var padding = ' ' * (width - label.length - 1);
print('$label:$padding|');
}
/// Runs the formatter test starting on [line] at [path] inside the "test"
/// directory.
Future<void> _runTest(
String path,
int line, {
int pageWidth = 40,
bool tall = true,
}) async {
var testFile = await TestFile.read('${tall ? 'tall' : 'short'}/$path');
var formatTest = testFile.tests.firstWhere((test) => test.line == line);
var formatter = testFile.formatterForTest(formatTest);
var actual = formatter.formatSource(formatTest.input);
// The test files always put a newline at the end of the expectation.
// Statements from the formatter (correctly) don't have that, so add
// one to line up with the expected result.
var actualText = actual.textWithSelectionMarkers;
if (!testFile.isCompilationUnit) actualText += '\n';
var expectedText = formatTest.output.textWithSelectionMarkers;
print('$path ${formatTest.description}');
_drawRuler('before', pageWidth);
print(formatTest.input.textWithSelectionMarkers);
if (actualText == expectedText) {
_drawRuler('result', pageWidth);
print(actualText);
} else {
print('FAIL');
_drawRuler('expected', pageWidth);
print(expectedText);
_drawRuler('actual', pageWidth);
print(actualText);
}
}