add line-length to flutter format commandline (#36679)
diff --git a/packages/flutter_tools/lib/src/commands/format.dart b/packages/flutter_tools/lib/src/commands/format.dart index 6107c09..fd0b331 100644 --- a/packages/flutter_tools/lib/src/commands/format.dart +++ b/packages/flutter_tools/lib/src/commands/format.dart
@@ -28,6 +28,11 @@ defaultsTo: false, negatable: false, ); + argParser.addOption('line-length', + abbr: 'l', + help: 'Wrap lines longer than this length. Defaults to 80 characters.', + defaultsTo: '80', + ); } @override @@ -65,6 +70,7 @@ dartfmt, if (argResults['dry-run']) '-n', if (argResults['machine']) '-m', + if (argResults['line-length'] != null) '-l ${argResults['line-length']}', if (!argResults['dry-run'] && !argResults['machine']) '-w', if (argResults['set-exit-if-changed']) '--set-exit-if-changed', ...argResults.rest,
diff --git a/packages/flutter_tools/test/general.shard/commands/format_test.dart b/packages/flutter_tools/test/general.shard/commands/format_test.dart index 1817bbe..fb55bc2 100644 --- a/packages/flutter_tools/test/general.shard/commands/format_test.dart +++ b/packages/flutter_tools/test/general.shard/commands/format_test.dart
@@ -74,5 +74,30 @@ final String shouldNotFormatted = srcFile.readAsStringSync(); expect(shouldNotFormatted, nonFormatted); }); + + testUsingContext('line-length', () async { + const int lineLengthShort = 50; + const int lineLengthLong = 120; + final String projectPath = await createProject(tempDir); + + final File srcFile = fs.file( + fs.path.join(projectPath, 'lib', 'main.dart')); + final String nonFormatted = srcFile.readAsStringSync(); + srcFile.writeAsStringSync( + nonFormatted.replaceFirst('main()', + 'main(anArgument1, anArgument2, anArgument3, anArgument4, anArgument5)')); + + final String nonFormattedWithLongLine = srcFile.readAsStringSync(); + final FormatCommand command = FormatCommand(); + final CommandRunner<void> runner = createTestCommandRunner(command); + + await runner.run(<String>['format', '--line-length', '$lineLengthLong', srcFile.path]); + final String notFormatted = srcFile.readAsStringSync(); + expect(nonFormattedWithLongLine, notFormatted); + + await runner.run(<String>['format', '--line-length', '$lineLengthShort', srcFile.path]); + final String shouldFormatted = srcFile.readAsStringSync(); + expect(nonFormattedWithLongLine, isNot(shouldFormatted)); + }); }); }