Code cleanup
This commit is contained in:
parent
9284f1afb6
commit
eb2f783333
36
src/dfmt.d
36
src/dfmt.d
|
@ -34,6 +34,7 @@ import std.d.formatter;
|
||||||
import std.d.ast;
|
import std.d.ast;
|
||||||
import std.array;
|
import std.array;
|
||||||
|
|
||||||
|
/// Help text
|
||||||
immutable USAGE = "usage: %s [--inplace] [<path>...]
|
immutable USAGE = "usage: %s [--inplace] [<path>...]
|
||||||
Formats D code.
|
Formats D code.
|
||||||
|
|
||||||
|
@ -44,16 +45,26 @@ Formats D code.
|
||||||
|
|
||||||
int main(string[] args)
|
int main(string[] args)
|
||||||
{
|
{
|
||||||
import std.getopt;
|
import std.getopt : getopt;
|
||||||
|
import std.path: baseName;
|
||||||
|
import std.file : isDir, dirEntries, SpanMode;
|
||||||
|
|
||||||
bool inplace = false;
|
bool inplace = false;
|
||||||
bool show_usage = false;
|
bool show_usage = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
getopt(args,
|
getopt(args,
|
||||||
"help|h", &show_usage,
|
"help|h", &show_usage,
|
||||||
"inplace", &inplace);
|
"inplace", &inplace);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
writef(USAGE, baseName(args[0]));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
if (show_usage)
|
if (show_usage)
|
||||||
{
|
{
|
||||||
import std.path: baseName;
|
|
||||||
writef(USAGE, baseName(args[0]));
|
writef(USAGE, baseName(args[0]));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -76,7 +87,6 @@ int main(string[] args)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
import std.file;
|
|
||||||
if (args.length >= 2)
|
if (args.length >= 2)
|
||||||
inplace = true;
|
inplace = true;
|
||||||
while (args.length > 0)
|
while (args.length > 0)
|
||||||
|
@ -103,6 +113,12 @@ int main(string[] args)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* source_desc =
|
||||||
|
* buffer =
|
||||||
|
* output =
|
||||||
|
*/
|
||||||
void format(string source_desc, ubyte[] buffer, File output)
|
void format(string source_desc, ubyte[] buffer, File output)
|
||||||
{
|
{
|
||||||
LexerConfig config;
|
LexerConfig config;
|
||||||
|
@ -125,8 +141,16 @@ void format(string source_desc, ubyte[] buffer, File output)
|
||||||
tokenFormatter.format();
|
tokenFormatter.format();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Contains formatting logic
|
||||||
struct TokenFormatter
|
struct TokenFormatter
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* tokens = the tokens to format
|
||||||
|
* output = the file that the code will be formatted to
|
||||||
|
* astInformation = information about the AST used to inform formatting
|
||||||
|
* decisions.
|
||||||
|
*/
|
||||||
this(const(Token)[] tokens, File output, ASTInformation* astInformation,
|
this(const(Token)[] tokens, File output, ASTInformation* astInformation,
|
||||||
FormatterConfig* config)
|
FormatterConfig* config)
|
||||||
{
|
{
|
||||||
|
@ -136,6 +160,7 @@ struct TokenFormatter
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Runs the foramtting process
|
||||||
void format()
|
void format()
|
||||||
{
|
{
|
||||||
while (index < tokens.length)
|
while (index < tokens.length)
|
||||||
|
@ -169,7 +194,7 @@ private:
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
}
|
||||||
writeToken();
|
writeToken();
|
||||||
if (i >= tokens.length-1)
|
if (i + 1 >= tokens.length)
|
||||||
newline();
|
newline();
|
||||||
else if (tokens[i + 1].line > tokens[i].line)
|
else if (tokens[i + 1].line > tokens[i].line)
|
||||||
newline();
|
newline();
|
||||||
|
@ -455,7 +480,7 @@ private:
|
||||||
newline();
|
newline();
|
||||||
write("}");
|
write("}");
|
||||||
depth--;
|
depth--;
|
||||||
if (index < tokens.length-1 &&
|
if (index + 1 < tokens.length &&
|
||||||
assumeSorted(astInformation.doubleNewlineLocations)
|
assumeSorted(astInformation.doubleNewlineLocations)
|
||||||
.equalRange(tokens[index].index).length)
|
.equalRange(tokens[index].index).length)
|
||||||
{
|
{
|
||||||
|
@ -770,6 +795,7 @@ struct ASTInformation
|
||||||
sort(doubleNewlineLocations);
|
sort(doubleNewlineLocations);
|
||||||
sort(spaceAfterLocations);
|
sort(spaceAfterLocations);
|
||||||
sort(unaryLocations);
|
sort(unaryLocations);
|
||||||
|
sort(ternaryColonLocations);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locations of end braces for struct bodies
|
/// Locations of end braces for struct bodies
|
||||||
|
|
Loading…
Reference in New Issue