From ef9db2d553d03ea6a1e6c3667487400c73fa34b8 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Tue, 24 Nov 2015 17:21:34 -0800 Subject: [PATCH] Add "-i" to command line to specify leading indent. Fix #464. R=sigmund@google.com Review URL: https://codereview.chromium.org//1470263004 . --- bin/format.dart | 20 ++++++++++-- lib/src/formatter_options.dart | 5 ++- lib/src/io.dart | 3 +- test/command_line_test.dart | 57 ++++++++++++++++++++++++---------- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/bin/format.dart b/bin/format.dart index 9df6265b..8867f184 100644 --- a/bin/format.dart +++ b/bin/format.dart @@ -25,6 +25,8 @@ void main(List args) { negatable: false, help: "Shows version information."); parser.addOption("line-length", abbr: "l", help: "Wrap lines longer than this.", defaultsTo: "80"); + parser.addOption("indent", + abbr: "i", help: "Spaces of leading indentation.", defaultsTo: "0"); parser.addOption("preserve", help: 'Selection to preserve, formatted as "start:length".'); parser.addFlag("dry-run", @@ -118,7 +120,6 @@ void main(List args) { } var pageWidth; - try { pageWidth = int.parse(argResults["line-length"]); } on FormatException catch (_) { @@ -128,10 +129,22 @@ void main(List args) { '"${argResults['line-length']}".'); } + var indent; + + try { + indent = int.parse(argResults["indent"]); + if (indent < 0 || indent.toInt() != indent) throw new FormatException(); + } on FormatException catch (_) { + usageError( + parser, + '--indent must be a non-negative integer, was ' + '"${argResults['indent']}".'); + } + var followLinks = argResults["follow-links"]; var options = new FormatterOptions(reporter, - pageWidth: pageWidth, followLinks: followLinks); + indent: indent, pageWidth: pageWidth, followLinks: followLinks); if (argResults.rest.isEmpty) { formatStdin(options, selection); @@ -168,7 +181,8 @@ void formatStdin(FormatterOptions options, List selection) { var input = new StringBuffer(); stdin.transform(new Utf8Decoder()).listen(input.write, onDone: () { - var formatter = new DartFormatter(pageWidth: options.pageWidth); + var formatter = + new DartFormatter(indent: options.indent, pageWidth: options.pageWidth); try { options.reporter.beforeFile(null, ""); var source = new SourceCode(input.toString(), diff --git a/lib/src/formatter_options.dart b/lib/src/formatter_options.dart index aeb02e40..69a46ffa 100644 --- a/lib/src/formatter_options.dart +++ b/lib/src/formatter_options.dart @@ -14,6 +14,9 @@ class FormatterOptions { /// The [OutputReporter] used to show the formatting results. final OutputReporter reporter; + /// The number of spaces of indentation to prefix the output with. + final int indent; + /// The number of columns that formatted output should be constrained to fit /// within. final int pageWidth; @@ -22,7 +25,7 @@ class FormatterOptions { final bool followLinks; FormatterOptions(this.reporter, - {this.pageWidth: 80, this.followLinks: false}); + {this.indent: 0, this.pageWidth: 80, this.followLinks: false}); } /// How the formatter reports the results it produces. diff --git a/lib/src/io.dart b/lib/src/io.dart index e435ace7..51f38058 100644 --- a/lib/src/io.dart +++ b/lib/src/io.dart @@ -67,7 +67,8 @@ bool processDirectory(FormatterOptions options, Directory directory) { bool processFile(FormatterOptions options, File file, {String label}) { if (label == null) label = file.path; - var formatter = new DartFormatter(pageWidth: options.pageWidth); + var formatter = + new DartFormatter(indent: options.indent, pageWidth: options.pageWidth); try { var source = new SourceCode(file.readAsStringSync(), uri: file.path); options.reporter.beforeFile(file, label); diff --git a/test/command_line_test.dart b/test/command_line_test.dart index 9117d565..425143d0 100644 --- a/test/command_line_test.dart +++ b/test/command_line_test.dart @@ -16,54 +16,54 @@ import 'utils.dart'; void main() { setUpTestSuite(); - test("Exits with 0 on success.", () { + test("exits with 0 on success", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatterOnDir(); process.shouldExit(0); }); - test("Exits with 64 on a command line argument error.", () { + test("exits with 64 on a command line argument error", () { var process = runFormatterOnDir(["-wat"]); process.shouldExit(64); }); - test("Exits with 65 on a parse error.", () { + test("exits with 65 on a parse error", () { d.dir("code", [d.file("a.dart", "herp derp i are a dart")]).create(); var process = runFormatterOnDir(); process.shouldExit(65); }); - test("Errors if --dry-run and --overwrite are both passed.", () { + test("errors if --dry-run and --overwrite are both passed", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatterOnDir(["--dry-run", "--overwrite"]); process.shouldExit(64); }); - test("Errors if --dry-run and --machine are both passed.", () { + test("errors if --dry-run and --machine are both passed", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatterOnDir(["--dry-run", "--machine"]); process.shouldExit(64); }); - test("Errors if --machine and --overwrite are both passed.", () { + test("errors if --machine and --overwrite are both passed", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatterOnDir(["--machine", "--overwrite"]); process.shouldExit(64); }); - test("Errors if --dry-run and --machine are both passed.", () { + test("errors if --dry-run and --machine are both passed", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatter(["--dry-run", "--machine"]); process.shouldExit(64); }); - test("Errors if --machine and --overwrite are both passed.", () { + test("errors if --machine and --overwrite are both passed", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatter(["--machine", "--overwrite"]); @@ -94,7 +94,7 @@ void main() { }); group("--dry-run", () { - test("prints names of files that would change.", () { + test("prints names of files that would change", () { d.dir("code", [ d.file("a_bad.dart", unformattedSource), d.file("b_good.dart", formattedSource), @@ -113,7 +113,7 @@ void main() { process.shouldExit(); }); - test("does not modify files.", () { + test("does not modify files", () { d.dir("code", [d.file("a.dart", unformattedSource)]).create(); var process = runFormatterOnDir(["--dry-run"]); @@ -153,12 +153,12 @@ void main() { }); group("--preserve", () { - test("errors if given paths.", () { + test("errors if given paths", () { var process = runFormatter(["--preserve", "path", "another"]); process.shouldExit(64); }); - test("errors on wrong number of components.", () { + test("errors on wrong number of components", () { var process = runFormatter(["--preserve", "1"]); process.shouldExit(64); @@ -166,12 +166,12 @@ void main() { process.shouldExit(64); }); - test("errors on non-integer component.", () { + test("errors on non-integer component", () { var process = runFormatter(["--preserve", "1:2.3"]); process.shouldExit(64); }); - test("updates selection.", () { + test("updates selection", () { var process = runFormatter(["--preserve", "6:10", "-m"]); process.writeLine(unformattedSource); process.closeStdin(); @@ -187,20 +187,43 @@ void main() { }); }); + group("--indent", () { + test("sets the leading indentation of the output", () { + var process = runFormatter(["--indent", "3"]); + process.writeLine("main() {'''"); + process.writeLine("a flush left multi-line string''';}"); + process.closeStdin(); + + process.stdout.expect(" main() {"); + process.stdout.expect(" '''"); + process.stdout.expect("a flush left multi-line string''';"); + process.stdout.expect(" }"); + process.shouldExit(0); + }); + + test("errors if the indent is not a non-negative number", () { + var process = runFormatter(["--indent", "notanum"]); + process.shouldExit(64); + + process = runFormatter(["--preserve", "-4"]); + process.shouldExit(64); + }); + }); + group("with no paths", () { - test("errors on --overwrite.", () { + test("errors on --overwrite", () { var process = runFormatter(["--overwrite"]); process.shouldExit(64); }); - test("exits with 65 on parse error.", () { + test("exits with 65 on parse error", () { var process = runFormatter(); process.writeLine("herp derp i are a dart"); process.closeStdin(); process.shouldExit(65); }); - test("reads from stdin.", () { + test("reads from stdin", () { var process = runFormatter(); process.writeLine(unformattedSource); process.closeStdin();