From 1e543822fceb303a1cf392e77dff8e5486ad48f2 Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Thu, 15 Jan 2015 19:49:55 +0100 Subject: [PATCH 1/4] refactor: extract function 'format' --- src/dfmt.d | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/dfmt.d b/src/dfmt.d index 341c91e..16af58e 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -42,8 +42,10 @@ int main(string[] args) "inplace", &inplace); File output = stdout; ubyte[] buffer; + string source_desc; if (args.length == 1) { + source_desc = "stdin"; ubyte[4096] inputBuffer; ubyte[] b; while (true) @@ -57,12 +59,19 @@ int main(string[] args) } else { + source_desc = args[1]; File f = File(args[1]); buffer = new ubyte[](cast(size_t)f.size); f.rawRead(buffer); if (inplace) output = File(args[1], "w"); } + format(source_desc, buffer, output); + return 0; +} + +void format(string source_desc, ubyte[] buffer, File output) +{ LexerConfig config; config.stringBehavior = StringBehavior.source; config.whitespaceBehavior = WhitespaceBehavior.skip; @@ -73,7 +82,7 @@ int main(string[] args) ASTInformation astInformation; FormatterConfig formatterConfig; auto parseTokens = getTokensForParser(buffer, parseConfig, &cache); - auto mod = parseModule(parseTokens, args.length > 1 ? args[1] : "stdin"); + auto mod = parseModule(parseTokens, source_desc); auto visitor = new FormatVisitor(&astInformation); visitor.visit(mod); astInformation.cleanup(); @@ -81,7 +90,6 @@ int main(string[] args) auto tokenFormatter = TokenFormatter(tokens, output, &astInformation, &formatterConfig); tokenFormatter.format(); - return 0; } struct TokenFormatter From d59e186ed9e7c0dd2481f6bc7c80d2c711b37d02 Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Thu, 15 Jan 2015 20:07:49 +0100 Subject: [PATCH 2/4] handle multiple arguments and directories --- src/dfmt.d | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/dfmt.d b/src/dfmt.d index 16af58e..f8edd30 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -33,19 +33,19 @@ import std.d.parser; import std.d.formatter; import std.d.ast; import std.array; -import std.getopt; int main(string[] args) { + import std.getopt; + bool inplace = false; getopt(args, "inplace", &inplace); File output = stdout; ubyte[] buffer; - string source_desc; - if (args.length == 1) + args.popFront(); + if (args.length == 0) { - source_desc = "stdin"; ubyte[4096] inputBuffer; ubyte[] b; while (true) @@ -56,17 +56,34 @@ int main(string[] args) else break; } + format("stdin", buffer, output); } else { - source_desc = args[1]; - File f = File(args[1]); - buffer = new ubyte[](cast(size_t)f.size); - f.rawRead(buffer); - if (inplace) - output = File(args[1], "w"); + import std.file; + if (args.length >= 2) + inplace = true; + while (args.length > 0) + { + const path = args.front; + args.popFront(); + if (isDir(path)) + { + inplace = true; + foreach (string name; dirEntries(path, "*.d", SpanMode.depth)) + { + args ~= name; + } + continue; + } + File f = File(path); + buffer = new ubyte[](cast(size_t)f.size); + f.rawRead(buffer); + if (inplace) + output = File(path, "w"); + format(path, buffer, output); + } } - format(source_desc, buffer, output); return 0; } From 2278a4c16b37524e8b2ea2b2682b6943c3d9ba2f Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Thu, 15 Jan 2015 20:30:02 +0100 Subject: [PATCH 3/4] add --help option and usage string --- src/dfmt.d | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/dfmt.d b/src/dfmt.d index f8edd30..f64e181 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -34,13 +34,29 @@ import std.d.formatter; import std.d.ast; import std.array; +immutable USAGE = "usage: %s [--inplace] [...] +Formats D code. + + --inplace change file in-place instead of outputing to stdout + (implicit in case of multiple files) + -h, --help display this help and exit +"; + int main(string[] args) { import std.getopt; bool inplace = false; + bool show_usage = false; getopt(args, + "help|h", &show_usage, "inplace", &inplace); + if (show_usage) + { + import std.path: baseName; + writef(USAGE, baseName(args[0])); + return 0; + } File output = stdout; ubyte[] buffer; args.popFront(); From 6538abd177a7a1ab1992cc26e3970c7657a7977c Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Thu, 15 Jan 2015 22:55:56 +0100 Subject: [PATCH 4/4] No spaces-only lines after imports Fixes tests/higherorder.d --- src/dfmt.d | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/dfmt.d b/src/dfmt.d index f64e181..e4d460c 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -190,9 +190,11 @@ private: { if (current.type == tok!";") { - formatStep(); + writeToken(); + tempIndent = 0; if (!(t == tok!"import" && current.type == tok!"import")) - newline(); + write("\n"); + newline(); break; } else