Skip to content

Commit

Permalink
Add "-i" to command line to specify leading indent.
Browse files Browse the repository at this point in the history
Fix #464.

R=sigmund@google.com

Review URL: https://codereview.chromium.org//1470263004 .
  • Loading branch information
munificent committed Nov 25, 2015
1 parent e6155f2 commit ef9db2d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
20 changes: 17 additions & 3 deletions bin/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void main(List<String> 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",
Expand Down Expand Up @@ -118,7 +120,6 @@ void main(List<String> args) {
}

var pageWidth;

try {
pageWidth = int.parse(argResults["line-length"]);
} on FormatException catch (_) {
Expand All @@ -128,10 +129,22 @@ void main(List<String> 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);
Expand Down Expand Up @@ -168,7 +181,8 @@ void formatStdin(FormatterOptions options, List<int> 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, "<stdin>");
var source = new SourceCode(input.toString(),
Expand Down
5 changes: 4 additions & 1 deletion lib/src/formatter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
57 changes: 40 additions & 17 deletions test/command_line_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down Expand Up @@ -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),
Expand All @@ -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"]);
Expand Down Expand Up @@ -153,25 +153,25 @@ 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);

process = runFormatter(["--preserve", "1:2:3"]);
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();
Expand All @@ -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();
Expand Down

0 comments on commit ef9db2d

Please sign in to comment.