From 7244cbb9b2baba44606c05ce0a5b63ac630f22c4 Mon Sep 17 00:00:00 2001 From: Majid Hajian Date: Thu, 1 Aug 2019 19:27:41 +0200 Subject: [PATCH] add line-length to flutter format commandline (#36679) --- .../lib/src/commands/format.dart | 6 +++++ .../general.shard/commands/format_test.dart | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/format.dart b/packages/flutter_tools/lib/src/commands/format.dart index 6107c0989d..fd0b331aac 100644 --- a/packages/flutter_tools/lib/src/commands/format.dart +++ b/packages/flutter_tools/lib/src/commands/format.dart @@ -28,6 +28,11 @@ class FormatCommand extends FlutterCommand { 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 @@ class FormatCommand extends FlutterCommand { 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 1817bbe2eb..fb55bc2eba 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 @@ void main() { 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 runner = createTestCommandRunner(command); + + await runner.run(['format', '--line-length', '$lineLengthLong', srcFile.path]); + final String notFormatted = srcFile.readAsStringSync(); + expect(nonFormattedWithLongLine, notFormatted); + + await runner.run(['format', '--line-length', '$lineLengthShort', srcFile.path]); + final String shouldFormatted = srcFile.readAsStringSync(); + expect(nonFormattedWithLongLine, isNot(shouldFormatted)); + }); }); }