Fix issue where constraints would not be detected correctly while formatting.

This commit is contained in:
Hackerpilot 2016-01-20 02:43:10 -08:00
parent cc7dab0341
commit e8a6b40432
7 changed files with 106 additions and 41 deletions

View File

@ -4,6 +4,6 @@
"targetType": "executable",
"license": "BSL-1.0",
"dependencies": {
"libdparse": "~>0.3.0"
"libdparse": "~>0.4.0"
}
}

@ -1 +1 @@
Subproject commit db1a9364b1815eec82ac853a9765d5532571db43
Subproject commit cd5f59744129649b0bc01c7b9a41293771a8229f

View File

@ -29,6 +29,7 @@ struct ASTInformation
sort(conditionalStatementLocations);
sort(arrayStartLocations);
sort(contractLocations);
sort(constraintLocations);
}
/// Locations of end braces for struct bodies
@ -69,6 +70,9 @@ struct ASTInformation
/// Locations of "in" and "out" tokens that begin contracts
size_t[] contractLocations;
/// Locations of template constraint "if" tokens
size_t[] constraintLocations;
}
/// Collects information from the AST that is useful for the formatter
@ -109,6 +113,12 @@ final class FormatVisitor : ASTVisitor
dec.accept(this);
}
override void visit(const Constraint constraint)
{
astInformation.constraintLocations ~= constraint.location;
constraint.accept(this);
}
override void visit(const ConditionalStatement statement)
{
auto condition = statement.compileCondition;

View File

@ -152,6 +152,8 @@ private:
void formatStep()
{
import std.range : assumeSorted;
assert(index < tokens.length);
if (currentIs(tok!"comment"))
{
@ -207,7 +209,11 @@ private:
else if ((isBlockHeader() || currentIs(tok!"version")
|| currentIs(tok!"debug")) && peekIs(tok!"(", false))
{
formatBlockHeader();
if (!assumeSorted(astInformation.constraintLocations)
.equalRange(current.index).empty)
formatConstrtaint();
else
formatBlockHeader();
}
else if (currentIs(tok!"do"))
{
@ -273,6 +279,47 @@ private:
writeToken();
}
void formatConstrtaint()
{
with (TemplateConstraintStyle) final switch (config.dfmt_template_constraint_style)
{
case unspecified:
assert(false, "Config was not validated properly");
case conditional_newline:
immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
if (l > config.dfmt_soft_max_line_length)
{
newline();
}
else
write(" ");
break;
case always_newline:
newline();
break;
case conditional_newline_indent:
immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
if (l > config.dfmt_soft_max_line_length)
{
pushWrapIndent(tok!"!");
newline();
}
else
write(" ");
break;
case always_newline_indent:
pushWrapIndent(tok!"!");
newline();
break;
}
// if
writeToken();
// assume that the parens are present, otherwise the parser would not
// have told is there was a constraint here
write(" ");
writeParens(false);
}
string commentText(size_t i)
{
import std.string : strip;
@ -479,6 +526,8 @@ private:
body
{
parenDepth--;
if (parenDepth == 0 && indents.topIs(tok!"!"))
indents.pop();
indents.popWrapIndents();
if (indents.topIs(tok!"("))
indents.pop();
@ -492,44 +541,6 @@ private:
if (spaceAfterParens || parenDepth > 0)
write(" ");
}
else if (peekIs(tok!"if") && !indents.topIsTemp())
{
writeToken();
if (!peekIs(tok!"("))
return;
// assume that "if" following ")" is a template constraint
with (TemplateConstraintStyle) final switch (config.dfmt_template_constraint_style)
{
case unspecified:
assert(false, "Config was not validated properly");
case conditional_newline:
immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
if (l > config.dfmt_soft_max_line_length)
{
newline();
}
else
write(" ");
break;
case always_newline:
newline();
break;
case conditional_newline_indent:
immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
if (l > config.dfmt_soft_max_line_length)
{
pushWrapIndent(tok!"!");
newline();
}
else
write(" ");
break;
case always_newline_indent:
pushWrapIndent(tok!"!");
newline();
break;
}
}
else if ((peekIsKeyword() || peekIs(tok!"@")) && spaceAfterParens
&& !peekIs(tok!"in") && !peekIs(tok!"is"))
{

View File

@ -0,0 +1,17 @@
struct SomeStructName
{
static struct InnerStruct
{
version (linux)
{
static if (condition)
{
void longFunctionName(AAAAAAAA)(AAAAAAAA a) @property
if (someThingsAreTrue!AAAAAAAA && long_condition
&& is(some < elaborate && expression))
{
}
}
}
}
}

View File

@ -0,0 +1,15 @@
struct SomeStructName
{
static struct InnerStruct
{
version (linux)
{
static if (condition)
{
void longFunctionName(AAAAAAAA)(AAAAAAAA a) @property if (someThingsAreTrue!AAAAAAAA && long_condition && is(some < elaborate && expression))
{
}
}
}
}
}

View File

@ -0,0 +1,12 @@
struct SomeStructName {
static struct InnerStruct {
version (linux) {
static if (condition) {
void longFunctionName(AAAAAAAA)(AAAAAAAA a) @property
if (someThingsAreTrue!AAAAAAAA && long_condition
&& is(some < elaborate && expression)) {
}
}
}
}
}