This commit is contained in:
Hackerpilot 2015-03-13 02:59:21 -07:00
parent 80b853113f
commit 2410311022
3 changed files with 79 additions and 41 deletions

View File

@ -166,8 +166,7 @@ private:
void formatStep() void formatStep()
{ {
import std.range : assumeSorted; import std.algorithm : canFind;
import std.algorithm : canFind, startsWith;
assert (index < tokens.length); assert (index < tokens.length);
if (currentIs(tok!"comment")) if (currentIs(tok!"comment"))
@ -310,9 +309,9 @@ private:
&& peekIs(tok!"(", false)) && peekIs(tok!"(", false))
{ {
immutable bool shouldPushIndent = (!currentIs(tok!"version") immutable bool shouldPushIndent = (!currentIs(tok!"version")
&& !currentIs(tok!"debug")) || !assumeSorted( && !currentIs(tok!"debug")) || astInformation.conditionalWithElseLocations
astInformation.conditionalWithElseLocations).equalRange( .canFindIndex(current.index) || astInformation.conditionalStatementLocations.canFindIndex(
current.index).empty; current.index);
if (shouldPushIndent) if (shouldPushIndent)
indents.push(current.type); indents.push(current.type);
writeToken(); writeToken();
@ -396,8 +395,7 @@ private:
switch (current.type) switch (current.type)
{ {
case tok!"*": case tok!"*":
if (!assumeSorted(astInformation.spaceAfterLocations) if (astInformation.spaceAfterLocations.canFindIndex(current.index))
.equalRange(current.index).empty)
{ {
writeToken(); writeToken();
if (!currentIs(tok!"*") && !currentIs(tok!")") && !currentIs(tok!"[") if (!currentIs(tok!"*") && !currentIs(tok!")") && !currentIs(tok!"[")
@ -407,7 +405,7 @@ private:
} }
break; break;
} }
else if (assumeSorted(astInformation.unaryLocations).equalRange(current.index).empty) else if (!astInformation.unaryLocations.canFindIndex(current.index))
goto binary; goto binary;
else else
writeToken(); writeToken();
@ -428,8 +426,7 @@ private:
case tok!"&": case tok!"&":
case tok!"+": case tok!"+":
case tok!"-": case tok!"-":
if (!assumeSorted(astInformation.unaryLocations) if (astInformation.unaryLocations.canFindIndex(current.index))
.equalRange(current.index).empty)
{ {
writeToken(); writeToken();
break; break;
@ -451,10 +448,9 @@ private:
writeToken(); writeToken();
break; break;
case tok!":": case tok!":":
if (!assumeSorted(astInformation.caseEndLocations) if (astInformation.caseEndLocations.canFindIndex(current.index)
.equalRange(current.index).empty || !assumeSorted( || astInformation.attributeDeclarationLines.canFindIndex(
astInformation.attributeDeclarationLines).equalRange( current.line))
current.line).empty)
{ {
writeToken(); writeToken();
if (!currentIs(tok!"{")) if (!currentIs(tok!"{"))
@ -496,13 +492,13 @@ private:
newline(); newline();
break; break;
case tok!"{": case tok!"{":
if (assumeSorted(astInformation.structInitStartLocations) if (astInformation.structInitStartLocations.canFindIndex(
.equalRange(tokens[index].index).length) tokens[index].index))
{ {
writeToken(); writeToken();
} }
else if (assumeSorted(astInformation.funLitStartLocations) else if (astInformation.funLitStartLocations.canFindIndex(
.equalRange(tokens[index].index).length) tokens[index].index))
{ {
if (peekBackIs(tok!")")) if (peekBackIs(tok!")"))
write(" "); write(" ");
@ -527,13 +523,13 @@ private:
} }
break; break;
case tok!"}": case tok!"}":
if (assumeSorted(astInformation.structInitEndLocations) if (astInformation.structInitEndLocations.canFindIndex(
.equalRange(tokens[index].index).length) tokens[index].index))
{ {
writeToken(); writeToken();
} }
else if (assumeSorted(astInformation.funLitEndLocations) else if (astInformation.funLitEndLocations.canFindIndex(
.equalRange(tokens[index].index).length) tokens[index].index))
{ {
write(" "); write(" ");
writeToken(); writeToken();
@ -545,8 +541,8 @@ private:
newline(); newline();
write("}"); write("}");
if (index < tokens.length - 1 && if (index < tokens.length - 1 &&
assumeSorted(astInformation.doubleNewlineLocations) astInformation.doubleNewlineLocations.canFindIndex(
.equalRange(tokens[index].index).length && !peekIs(tok!"}")) tokens[index].index) && !peekIs(tok!"}"))
{ {
write("\n"); write("\n");
currentLineLength = 0; currentLineLength = 0;
@ -735,8 +731,6 @@ private:
} }
body body
{ {
import std.range : assumeSorted;
int depth = 0; int depth = 0;
do do
{ {
@ -752,10 +746,8 @@ private:
{ {
writeToken(); writeToken();
depth++; depth++;
if (!assumeSorted(linebreakHints).equalRange(index - 1).empty if (linebreakHints.canFindIndex(index - 1) || (linebreakHints.length == 0
|| (linebreakHints.length == 0 && currentLineLength > config.columnSoftLimit && !currentIs(tok!")")))
&& currentLineLength > config.columnSoftLimit
&& !currentIs(tok!")")))
{ {
indents.push(tok!"("); indents.push(tok!"(");
newline(); newline();
@ -1025,11 +1017,9 @@ private:
if (l != -1) if (l != -1)
indentLevel = l; indentLevel = l;
} }
else if (currentIs(tok!"{") && assumeSorted( else if (currentIs(tok!"{")
astInformation.structInitStartLocations).equalRange( && !astInformation.structInitStartLocations.canFindIndex(tokens[index].index)
tokens[index].index).empty && assumeSorted( && !astInformation.funLitStartLocations.canFindIndex(tokens[index].index))
astInformation.funLitStartLocations).equalRange(
tokens[index].index).empty)
{ {
while (indents.length && isWrapIndent(indents.top)) while (indents.length && isWrapIndent(indents.top))
indents.pop(); indents.pop();
@ -1056,8 +1046,7 @@ private:
indents.pop(); indents.pop();
} }
} }
else if ((!assumeSorted(astInformation.attributeDeclarationLines) else if (astInformation.attributeDeclarationLines.canFindIndex(current.line))
.equalRange(current.line).empty))
{ {
auto l = indents.indentToMostRecent(tok!"{"); auto l = indents.indentToMostRecent(tok!"{");
if (l != -1) if (l != -1)
@ -1172,6 +1161,12 @@ struct FormatterConfig
BraceStyle braceStyle = BraceStyle.allman; BraceStyle braceStyle = BraceStyle.allman;
} }
bool canFindIndex(const size_t[] items, size_t index)
{
import std.range : assumeSorted;
return !assumeSorted(items).equalRange(index).empty;
}
/// ///
struct ASTInformation struct ASTInformation
{ {
@ -1219,6 +1214,8 @@ struct ASTInformation
size_t[] funLitEndLocations; size_t[] funLitEndLocations;
size_t[] conditionalWithElseLocations; size_t[] conditionalWithElseLocations;
size_t[] conditionalStatementLocations;
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter
@ -1230,11 +1227,11 @@ final class FormatVisitor : ASTVisitor
this.astInformation = astInformation; this.astInformation = astInformation;
} }
override void visit(const ConditionalDeclaration conditionalDeclaration) override void visit(const ConditionalDeclaration dec)
{ {
if (conditionalDeclaration.falseDeclaration !is null) if (dec.falseDeclaration !is null)
{ {
auto condition = conditionalDeclaration.compileCondition; auto condition = dec.compileCondition;
if (condition.versionCondition !is null) if (condition.versionCondition !is null)
{ {
astInformation.conditionalWithElseLocations ~= astInformation.conditionalWithElseLocations ~=
@ -1248,7 +1245,23 @@ final class FormatVisitor : ASTVisitor
// Skip "static if" because the formatting for normal "if" handles // Skip "static if" because the formatting for normal "if" handles
// it properly // it properly
} }
conditionalDeclaration.accept(this); dec.accept(this);
}
override void visit(const ConditionalStatement statement)
{
auto condition = statement.compileCondition;
if (condition.versionCondition !is null)
{
astInformation.conditionalStatementLocations ~=
condition.versionCondition.versionIndex;
}
else if (condition.debugCondition !is null)
{
astInformation.conditionalStatementLocations ~=
condition.debugCondition.debugIndex;
}
statement.accept(this);
} }
override void visit(const FunctionLiteralExpression funcLit) override void visit(const FunctionLiteralExpression funcLit)

11
tests/issue0042.d Normal file
View File

@ -0,0 +1,11 @@
unittest
{
version (Windows) __locale_decpoint = save;
}
unittest
{
version (Windows) __locale_decpoint = save;
else __locale_decpoint = save;
version (Win32) int x;
}

14
tests/issue0042.d.ref Normal file
View File

@ -0,0 +1,14 @@
unittest
{
version (Windows)
__locale_decpoint = save;
}
unittest
{
version (Windows)
__locale_decpoint = save;
else
__locale_decpoint = save;
version (Win32) int x;
}