commit 3171199b5fc37431d7048ac2ea57b7649d217271
parent a26beeb7f8a1b60746d1d9b8f89296bf1d9fb75e
Author: Friedel Schön <[email protected]>
Date: Tue, 11 Oct 2022 11:39:10 +0200
"--special" becomes "--attribute" and refactoring code
Diffstat:
M | src/main.d | | | 149 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
1 file changed, 79 insertions(+), 70 deletions(-)
diff --git a/src/main.d b/src/main.d
@@ -12,11 +12,12 @@ import std.string : format, indexOf, split, strip, stripLeft;
import std.typecons : Tuple, Yes, tuple;
struct Identifier {
+ bool byOriginal;
string original;
string alias_;
string sortBy() {
- if (sortOriginal)
+ if (byOriginal)
return original;
else
return hasAlias ? alias_ : original;
@@ -28,6 +29,7 @@ struct Identifier {
}
struct Import {
+ bool byAttribute;
string line;
bool public_;
@@ -38,7 +40,7 @@ struct Import {
string end;
string sortBy() {
- if (special && (public_ || static_))
+ if (byAttribute && (public_ || static_))
return '\0' ~ name.sortBy;
return name.sortBy;
}
@@ -58,26 +60,25 @@ Options:
-i, --inline .......... writes to the input
-o, --out <path> ...... writes to `path` instead of stdout
- -s, --special ......... public and static imports first
+ -a, --attribute ....... public and static imports first
-r, --original ........ sort by original not by binding
-h, --help ............ prints this message
-v, --verbose ......... prints useful messages";
-bool inline = false;
-bool keep = false;
-bool special = false;
-string output = null;
-string path = null;
-bool sortOriginal = false;
+struct SortConfig {
+ bool keepLine = false;
+ bool byAttribute = false;
+ bool byOriginal = false;
+}
-void writeImports(File outfile, Import[] matches) {
+void writeImports(File outfile, SortConfig config, Import[] matches) {
if (!matches)
return;
matches.sort!((a, b) => a.sortBy < b.sortBy);
foreach (m; matches) {
- if (keep) {
+ if (config.keepLine) {
outfile.write(m.line);
} else {
outfile.write(m.begin);
@@ -103,7 +104,69 @@ void writeImports(File outfile, Import[] matches) {
}
}
+void sortImports(SortConfig config, File infile, File outfile) {
+ string softEnd = null;
+ Import[] matches;
+
+ foreach (line; infile.byLine(Yes.keepTerminator)) {
+ auto linestr = line.idup;
+ if (auto match = matchFirst(linestr, pattern)) { // is import
+ if (softEnd) {
+ if (!matches)
+ outfile.write(softEnd);
+ softEnd = null;
+ }
+
+ auto im = Import(config.byAttribute, linestr);
+ if (match[3]) {
+ im.name = Identifier(config.byOriginal, match[4], match[3]);
+ } else {
+ im.name = Identifier(config.byOriginal, match[4]);
+ }
+ im.begin = match[1];
+ im.end = match[6];
+
+ if (match[2] == "static")
+ im.static_ = true;
+ else if (match[2] == "public")
+ im.public_ = true;
+
+ if (match[5]) {
+ foreach (id; match[5][1 .. $].split(",")) {
+ if (auto pair = id.findSplit("=")) { // has alias
+ im.idents ~= Identifier(config.byOriginal, pair[2].strip, pair[0].strip);
+ } else {
+ im.idents ~= Identifier(config.byOriginal, id.strip);
+ }
+ }
+ im.idents.sort!((a, b) => a.sortBy < b.sortBy);
+ }
+ matches ~= im;
+ } else {
+ if (!softEnd && linestr.stripLeft == "") {
+ softEnd = linestr;
+ } else {
+ if (matches) {
+ outfile.writeImports(config, matches);
+ matches = [];
+ }
+ if (softEnd) {
+ outfile.write(softEnd);
+ softEnd = null;
+ }
+ outfile.write(line);
+ }
+ }
+ }
+ outfile.writeImports(config, matches);
+}
+
void main(string[] args) {
+ SortConfig config;
+ bool inline = false;
+ string output = null;
+ string path = null;
+
bool nextout = false;
foreach (arg; args[1 .. $]) {
@@ -115,13 +178,13 @@ void main(string[] args) {
stdout.writeln(help);
return;
} else if (arg == "--keep" || arg == "-k") {
- keep = true;
- } else if (arg == "--special" || arg == "-s") {
- special = true;
+ config.keepLine = true;
+ } else if (arg == "--attribute" || arg == "-a") {
+ config.byAttribute = true;
} else if (arg == "--inline" || arg == "-i") {
inline = true;
} else if (arg == "--original" || arg == "-r") {
- sortOriginal = true;
+ config.byOriginal = true;
} else if (arg == "--out" || arg == "-o") {
if (output != null) {
stderr.writeln("error: output already specified");
@@ -179,62 +242,8 @@ void main(string[] args) {
} else {
outfile = stdout;
}
- string softEnd = null;
- Import[] matches;
-
- foreach (line; infile.byLine(Yes.keepTerminator)) {
- auto linestr = line.idup;
- if (auto match = matchFirst(linestr, pattern)) { // is import
- if (softEnd) {
- if (!matches)
- outfile.write(softEnd);
- softEnd = null;
- }
-
- auto im = Import(linestr);
- if (match[3]) {
- im.name = Identifier(match[4], match[3]);
- } else {
- im.name = Identifier(match[4]);
- }
- im.begin = match[1];
- im.end = match[6];
-
- if (match[2] == "static")
- im.static_ = true;
- else if (match[2] == "public")
- im.public_ = true;
-
- if (match[5]) {
- foreach (id; match[5][1 .. $].split(",")) {
- if (auto pair = id.findSplit("=")) { // has alias
- im.idents ~= Identifier(pair[2].strip, pair[0].strip);
- } else {
- im.idents ~= Identifier(id.strip);
- }
- }
- im.idents.sort!((a, b) => a.sortBy < b.sortBy);
- }
- matches ~= im;
- } else {
- if (!softEnd && linestr.stripLeft == "") {
- softEnd = linestr;
- } else {
- if (matches) {
- outfile.writeImports(matches);
- matches = [];
- }
-
- if (softEnd) {
- outfile.write(softEnd);
- softEnd = null;
- }
- outfile.write(line);
- }
- }
- }
- outfile.writeImports(matches);
+ sortImports(config, infile, outfile);
infile.close();
outfile.close();