-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsdfmt.d
85 lines (70 loc) · 1.71 KB
/
sdfmt.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module driver.sdfmt;
int main(string[] args) {
bool dbg = false;
bool inPlace = false;
import std.getopt;
try {
auto help_info = getopt(
// sdfmt off
args, std.getopt.config.caseSensitive,
"debug", "Enable debug", &dbg,
"i", "Format files in place", &inPlace,
// sdfmt on
);
if (help_info.helpWanted || args.length == 1) {
import std.stdio;
writeln("The Snazzy D Compiler - Code Formatter");
writeln("Usage: sdfmt [options] file.d");
writeln("Options:");
foreach (option; help_info.options) {
writefln(
" %-16s %s",
// bug : optShort is empty if there is no long version
option.optShort.length
? option.optShort
: (option.optLong.length == 3)
? option.optLong[1 .. $]
: option.optLong,
option.help
);
}
return 0;
}
} catch (GetOptException ex) {
import std.stdio;
writefln("%s", ex.msg);
writeln("Please use -h to get a list of valid options.");
return 1;
}
auto files = args[1 .. $];
import source.context;
auto context = new Context();
foreach (filename; files) {
import format.config;
Config conf;
import config.build;
conf.buildLocalConfig("sdfmt", context, filename);
import source.location;
auto base = context.registerFile(Location.init, filename, "");
import format.parser;
auto chunks = Parser(base, context).parse();
if (dbg) {
import std.stdio;
writeln(chunks);
}
import format.writer;
auto o = chunks.write(conf);
if (inPlace) {
// Remove the null terminator.
auto s = base.getFullPosition(context).getSource();
if (o != s.getContent()) {
import std.file;
filename.write(o);
}
} else {
import std.stdio;
write(o);
}
}
return 0;
}