Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit b105647

Browse files
committed
Add a way to opt a file out of Dart formatting
1 parent 5306c02 commit b105647

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

script/tool/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
`--no-push-flags`. Releases now always tag and push.
1111
- **Breaking change**: `publish`'s `--package` flag has been replaced with the
1212
`--packages` flag used by most other packages.
13+
- Formatting now skips files that contain a line that exactly
14+
matches the string `// This file is hand-formatted`.
1315

1416
## 0.5.0
1517

script/tool/lib/src/format_command.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,21 @@ class FormatCommand extends PluginCommand {
206206

207207
final String fromPath = relativeTo.path;
208208

209+
const String handFormattedPragma = '// This file is hand-formatted.';
210+
209211
return files
212+
.where((File file) {
213+
try {
214+
return !file.readAsLinesSync().contains(handFormattedPragma);
215+
} catch FileSystemException {
216+
// If we can't open the file or can't read it as UTF-8, then we just
217+
// consider it included. It should get filtered out later when we
218+
// deal with file extensions. (We want the formatters to see the
219+
// files that we can't read, in case they have opinions about such
220+
// things as character encodings, etc.)
221+
return true;
222+
}
223+
})
210224
.map((File file) => path.relative(file.path, from: fromPath))
211225
.where((String path) =>
212226
// Ignore files in build/ directories (e.g., headers of frameworks)

script/tool/test/format_command_test.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:args/command_runner.dart';
88
import 'package:file/file.dart';
99
import 'package:file/memory.dart';
1010
import 'package:flutter_plugin_tools/src/common/core.dart';
11+
import 'package:flutter_plugin_tools/src/common/file_utils.dart';
1112
import 'package:flutter_plugin_tools/src/format_command.dart';
1213
import 'package:path/path.dart' as p;
1314
import 'package:test/test.dart';
@@ -85,12 +86,21 @@ void main() {
8586
'lib/src/b.dart',
8687
'lib/src/c.dart',
8788
];
89+
const String fileWithOptOut = 'lib/src/d.dart';
8890
final Directory pluginDir = createFakePlugin(
8991
'a_plugin',
9092
packagesDir,
91-
extraFiles: files,
93+
extraFiles: <String>[
94+
...files,
95+
fileWithOptOut,
96+
],
9297
);
9398

99+
final p.Context posixContext = p.posix;
100+
childFileWithSubcomponents(pluginDir, posixContext.split(fileWithOptOut))
101+
.writeAsStringSync(
102+
'// copyright bla bla\n// This file is hand-formatted.\ncode...');
103+
94104
await runCapturingPrint(runner, <String>['format']);
95105

96106
expect(

0 commit comments

Comments
 (0)