This commit is contained in:
Hackerpilot 2015-03-12 12:24:47 -07:00
parent 783410390e
commit 5bca694cca
3 changed files with 66 additions and 7 deletions

View File

@ -236,6 +236,7 @@ private:
else if ((t == tok!"import" && !currentIs(tok!"import"))) else if ((t == tok!"import" && !currentIs(tok!"import")))
{ {
write("\n"); write("\n");
currentLineLength = 0;
justAddedExtraNewline = true; justAddedExtraNewline = true;
newline(); newline();
} }
@ -490,6 +491,14 @@ private:
{ {
writeToken(); writeToken();
} }
else if (assumeSorted(astInformation.funLitStartLocations)
.equalRange(tokens[index].index).length)
{
if (peekBackIs(tok!")"))
write(" ");
writeToken();
write(" ");
}
else else
{ {
if (!justAddedExtraNewline && !peekBackIs(tok!"{") if (!justAddedExtraNewline && !peekBackIs(tok!"{")
@ -513,6 +522,12 @@ private:
{ {
writeToken(); writeToken();
} }
else if (assumeSorted(astInformation.funLitEndLocations)
.equalRange(tokens[index].index).length)
{
write(" ");
writeToken();
}
else else
{ {
// Silly hack to format enums better. // Silly hack to format enums better.
@ -523,13 +538,19 @@ private:
assumeSorted(astInformation.doubleNewlineLocations) assumeSorted(astInformation.doubleNewlineLocations)
.equalRange(tokens[index].index).length && !peekIs(tok!"}")) .equalRange(tokens[index].index).length && !peekIs(tok!"}"))
{ {
output.put("\n"); write("\n");
currentLineLength = 0;
justAddedExtraNewline = true; justAddedExtraNewline = true;
} }
if (config.braceStyle == BraceStyle.otbs && currentIs(tok!"else")) if (config.braceStyle == BraceStyle.otbs && currentIs(tok!"else"))
write(" "); write(" ");
index++; if (!peekIs(tok!",") && !peekIs(tok!")"))
newline(); {
index++;
newline();
}
else
index++;
} }
break; break;
case tok!".": case tok!".":
@ -711,12 +732,11 @@ private:
{ {
if (currentIs(tok!";")) if (currentIs(tok!";"))
{ {
if (!(peekIs(tok!";") || peekIs(tok!")"))) if (!(peekIs(tok!";") || peekIs(tok!")") || peekIs(tok!"}")))
write("; "); write("; ");
else else
write(";"); write(";");
index++; index++;
continue;
} }
else if (currentIs(tok!"(")) else if (currentIs(tok!"("))
{ {
@ -731,7 +751,6 @@ private:
newline(); newline();
} }
regenLineBreakHintsIfNecessary(index - 1); regenLineBreakHintsIfNecessary(index - 1);
continue;
} }
else if (currentIs(tok!")")) else if (currentIs(tok!")"))
{ {
@ -933,8 +952,17 @@ private:
if (currentIs(tok!"comment") && current.line == tokenEndLine(tokens[index - 1])) if (currentIs(tok!"comment") && current.line == tokenEndLine(tokens[index - 1]))
return; return;
output.put("\n");
immutable bool hasCurrent = index + 1 < tokens.length; immutable bool hasCurrent = index + 1 < tokens.length;
if (hasCurrent && tokens[index].type == tok!"}" && !assumeSorted(
astInformation.funLitEndLocations).equalRange(tokens[index].index).empty)
{
write(" ");
return;
}
output.put("\n");
if (!justAddedExtraNewline && index > 0 if (!justAddedExtraNewline && index > 0
&& hasCurrent && tokens[index].line - tokenEndLine(tokens[index - 1]) > 1) && hasCurrent && tokens[index].line - tokenEndLine(tokens[index - 1]) > 1)
{ {
@ -986,6 +1014,8 @@ private:
} }
else if (currentIs(tok!"{") && assumeSorted( else if (currentIs(tok!"{") && assumeSorted(
astInformation.structInitStartLocations).equalRange( astInformation.structInitStartLocations).equalRange(
tokens[index].index).empty && assumeSorted(
astInformation.funLitStartLocations).equalRange(
tokens[index].index).empty) tokens[index].index).empty)
{ {
while (indents.length && isWrapIndent(indents.top)) while (indents.length && isWrapIndent(indents.top))
@ -1142,6 +1172,8 @@ struct ASTInformation
sort(caseEndLocations); sort(caseEndLocations);
sort(structInitStartLocations); sort(structInitStartLocations);
sort(structInitEndLocations); sort(structInitEndLocations);
sort(funLitStartLocations);
sort(funLitEndLocations);
} }
/// Locations of end braces for struct bodies /// Locations of end braces for struct bodies
@ -1164,6 +1196,12 @@ struct ASTInformation
/// Closing braces of struct initializers /// Closing braces of struct initializers
size_t[] structInitEndLocations; size_t[] structInitEndLocations;
/// Opening braces of function literals
size_t[] funLitStartLocations;
/// Closing braces of function literals
size_t[] funLitEndLocations;
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter
@ -1175,6 +1213,15 @@ final class FormatVisitor : ASTVisitor
this.astInformation = astInformation; this.astInformation = astInformation;
} }
override void visit(const FunctionLiteralExpression funcLit)
{
astInformation.funLitStartLocations ~= funcLit.functionBody
.blockStatement.startLocation;
astInformation.funLitEndLocations ~= funcLit.functionBody
.blockStatement.endLocation;
funcLit.accept(this);
}
override void visit(const DefaultStatement defaultStatement) override void visit(const DefaultStatement defaultStatement)
{ {
astInformation.caseEndLocations ~= defaultStatement.colonLocation; astInformation.caseEndLocations ~= defaultStatement.colonLocation;

6
tests/issue0094.d Normal file
View File

@ -0,0 +1,6 @@
void test()
{
fun((int x) { writeln(x); }, (int x) { writeln(x); });
return;
}

6
tests/issue0094.d.ref Normal file
View File

@ -0,0 +1,6 @@
void test()
{
fun((int x) { writeln(x); }, (int x) { writeln(x); });
return;
}