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)
@ -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!":":
if (!assumeSorted(astInformation.attributeDeclarationLines)
.equalRange(current.line).empty)
{
writeToken();
tempIndent = 0;
newline();
}
else
{
write(" : "); write(" : ");
index += 1; index += 1;
}
break; break;
case tok!"]": case tok!"]":
writeToken(); writeToken();
@ -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()

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);
}
}
}