More formatting fixes

This commit is contained in:
Hackerpilot 2015-02-17 15:31:22 -08:00
parent 506e150eea
commit e39f2916b1
6 changed files with 91 additions and 28 deletions

@ -1 +1 @@
Subproject commit f73bed9279602a228933b21156d8f1f2ec5e4fdd Subproject commit 6ac2d819363eacb3cec334e9a6f3c10dfc8bb280

View File

@ -75,6 +75,7 @@ int main(string[] args)
else else
{ {
import std.file : dirEntries, isDir, SpanMode; import std.file : dirEntries, isDir, SpanMode;
if (args.length >= 2) if (args.length >= 2)
inplace = true; inplace = true;
while (args.length > 0) while (args.length > 0)
@ -91,7 +92,7 @@ int main(string[] args)
continue; continue;
} }
File f = File(path); File f = File(path);
buffer = new ubyte[](cast(size_t)f.size); buffer = new ubyte[](cast(size_t) f.size);
f.rawRead(buffer); f.rawRead(buffer);
if (inplace) if (inplace)
output = File(path, "w"); output = File(path, "w");
@ -270,7 +271,8 @@ private:
} }
else if (current.type == tok!"for" || current.type == tok!"foreach" else if (current.type == tok!"for" || current.type == tok!"foreach"
|| current.type == tok!"foreach_reverse" || current.type == tok!"while" || current.type == tok!"foreach_reverse" || current.type == tok!"while"
|| current.type == tok!"if") || current.type == tok!"if" || current.type == tok!"out"
|| current.type == tok!"catch")
{ {
currentLineLength += currentTokenLength() + 1; currentLineLength += currentTokenLength() + 1;
writeToken(); writeToken();
@ -300,7 +302,8 @@ private:
auto next = tokens[index + 1]; auto next = tokens[index + 1];
if (next.type == tok!";" || next.type == tok!"(" if (next.type == tok!";" || next.type == tok!"("
|| next.type == tok!")" || next.type == tok!"," || next.type == tok!")" || next.type == tok!","
|| next.type == tok!"{" || next.type == tok!".") || next.type == tok!"{" || next.type == tok!"."
|| next.type == tok!":")
{ {
writeToken(); writeToken();
} }
@ -358,8 +361,18 @@ private:
writeToken(); writeToken();
break; break;
case tok!":": case tok!":":
write(" : "); if (!assumeSorted(astInformation.attributeDeclarationLines)
index += 1; .equalRange(current.line).empty)
{
writeToken();
tempIndent = 0;
newline();
}
else
{
write(" : ");
index += 1;
}
break; break;
case tok!"]": case tok!"]":
writeToken(); writeToken();
@ -466,14 +479,14 @@ private:
assert (false, str(current.type)); assert (false, str(current.type));
} }
/// Pushes a temporary indent level /// Pushes a temporary indent level
void pushIndent() void pushIndent()
{ {
if (tempIndent == 0) if (tempIndent == 0)
tempIndent++; tempIndent++;
} }
/// Pops a temporary indent level /// Pops a temporary indent level
void popIndent() void popIndent()
{ {
if (tempIndent > 0) if (tempIndent > 0)
@ -511,7 +524,7 @@ private:
return l; return l;
} }
/// Writes balanced braces /// Writes balanced braces
void writeBraces() void writeBraces()
{ {
import std.range : assumeSorted; import std.range : assumeSorted;
@ -544,7 +557,7 @@ private:
depth--; depth--;
if (index < tokens.length - 1 && if (index < tokens.length - 1 &&
assumeSorted(astInformation.doubleNewlineLocations) assumeSorted(astInformation.doubleNewlineLocations)
.equalRange(tokens[index].index).length) .equalRange(tokens[index].index).length && !peekIs(tok!"}"))
{ {
output.put("\n"); output.put("\n");
} }
@ -601,14 +614,31 @@ private:
} }
else if (current.type == tok!")") else if (current.type == tok!")")
{ {
if (peekIs(tok!"identifier") || (index + 1 < tokens.length if (peekIs(tok!"identifier"))
&& (isKeyword(tokens[index + 1].type)
|| tokens[index + 1].type == tok!"@")))
{ {
writeToken(); writeToken();
if (space_afterwards) if (space_afterwards)
write(" "); write(" ");
} }
else if (index + 1 < tokens.length)
{
if (tokens[index + 1].type == tok!"in"
|| tokens[index + 1].type == tok!"out"
|| tokens[index + 1].type == tok!"body")
{
writeToken();
newline();
}
else if (isKeyword(tokens[index + 1].type)
|| tokens[index + 1].type == tok!"@")
{
writeToken();
if (space_afterwards)
write(" ");
}
else
writeToken();
}
else else
writeToken(); writeToken();
depth--; depth--;
@ -753,12 +783,18 @@ private:
void newline() void newline()
{ {
import std.range:assumeSorted;
output.put("\n"); output.put("\n");
currentLineLength = 0; currentLineLength = 0;
if (index < tokens.length) if (index < tokens.length)
{ {
if (current.type == tok!"}") if (current.type == tok!"}")
indentLevel--; indentLevel--;
else if (!assumeSorted(astInformation.attributeDeclarationLines)
.equalRange(current.line).empty)
{
tempIndent--;
}
indent(); indent();
} }
} }
@ -864,7 +900,7 @@ struct ASTInformation
sort(doubleNewlineLocations); sort(doubleNewlineLocations);
sort(spaceAfterLocations); sort(spaceAfterLocations);
sort(unaryLocations); sort(unaryLocations);
sort(ternaryColonLocations); sort(attributeDeclarationLines);
} }
/// Locations of end braces for struct bodies /// Locations of end braces for struct bodies
@ -876,8 +912,8 @@ struct ASTInformation
/// Locations of unary operators /// Locations of unary operators
size_t[] unaryLocations; size_t[] unaryLocations;
/// Locations of ':' operators in ternary expressions /// Lines containing attribute declarations
size_t[] ternaryColonLocations; size_t[] attributeDeclarationLines;
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter
@ -893,10 +929,6 @@ final class FormatVisitor : ASTVisitor
{ {
if (functionBody.blockStatement !is null) if (functionBody.blockStatement !is null)
astInformation.doubleNewlineLocations ~= functionBody.blockStatement.endLocation; astInformation.doubleNewlineLocations ~= functionBody.blockStatement.endLocation;
if (functionBody.inStatement !is null && functionBody.inStatement.blockStatement !is null)
astInformation.doubleNewlineLocations ~= functionBody.inStatement.blockStatement.endLocation;
if (functionBody.outStatement !is null && functionBody.outStatement.blockStatement !is null)
astInformation.doubleNewlineLocations ~= functionBody.outStatement.blockStatement.endLocation;
if (functionBody.bodyStatement !is null && functionBody.bodyStatement.blockStatement !is null) if (functionBody.bodyStatement !is null && functionBody.bodyStatement.blockStatement !is null)
astInformation.doubleNewlineLocations ~= functionBody.bodyStatement.blockStatement.endLocation; astInformation.doubleNewlineLocations ~= functionBody.bodyStatement.blockStatement.endLocation;
functionBody.accept(this); functionBody.accept(this);
@ -950,11 +982,10 @@ final class FormatVisitor : ASTVisitor
unary.accept(this); unary.accept(this);
} }
override void visit(const TernaryExpression ternary) override void visit(const AttributeDeclaration attributeDeclaration)
{ {
if (ternary.colon.type != tok!"") astInformation.attributeDeclarationLines ~= attributeDeclaration.line;
astInformation.ternaryColonLocations ~= ternary.colon.index; attributeDeclaration.accept(this);
ternary.accept(this);
} }
private: private:

View File

@ -4,7 +4,6 @@ class U0 : Exception
{ {
super("U0 error message"); super("U0 error message");
} }
} }
class U1 : Exception class U1 : Exception
@ -13,7 +12,6 @@ class U1 : Exception
{ {
super("U1 error message"); super("U1 error message");
} }
} }
void foo() void foo()
@ -26,7 +24,7 @@ void foo()
{ {
i.bar; i.bar;
} }
catch(U0) catch (U0)
{ {
"Function foo caught exception U0".writeln; "Function foo caught exception U0".writeln;
} }

13
tests/contracts.d Normal file
View File

@ -0,0 +1,13 @@
void main(string[] args)
{
struct SomeStruct {
private:
int a; int b;
void doStuff(int q)
in {
assert(q);
} out (result) {}
body
{
writeln(q);
}}}

21
tests/contracts.d.ref Normal file
View File

@ -0,0 +1,21 @@
void main(string[] args)
{
struct SomeStruct
{
private:
int a;
int b;
void doStuff(int q)
in
{
assert(q);
}
out (result)
{
}
body
{
writeln(q);
}
}
}

View File

@ -15,7 +15,7 @@ void main()
{ {
answer = txt.to!int; answer = txt.to!int;
} }
catch(ConvException e) catch (ConvException e)
{ {
writefln(" I don't understand your input '%s'", txt); writefln(" I don't understand your input '%s'", txt);
continue; continue;