Merge pull request #307 from stefan-koch-sociomantic/sbfp-fix

Sbfp fix
merged-on-behalf-of: Brian Schott <Hackerpilot@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2017-12-05 12:18:13 +01:00 committed by GitHub
commit 23bd2b408c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -30,6 +30,7 @@ struct ASTInformation
sort(arrayStartLocations); sort(arrayStartLocations);
sort(contractLocations); sort(contractLocations);
sort(constraintLocations); sort(constraintLocations);
sort(constructorDestructorLocations);
} }
/// Locations of end braces for struct bodies /// Locations of end braces for struct bodies
@ -73,6 +74,9 @@ struct ASTInformation
/// Locations of template constraint "if" tokens /// Locations of template constraint "if" tokens
size_t[] constraintLocations; size_t[] constraintLocations;
/// Locations of constructor/destructor "this" tokens ?
size_t[] constructorDestructorLocations;
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter
@ -93,6 +97,18 @@ final class FormatVisitor : ASTVisitor
arrayInitializer.accept(this); arrayInitializer.accept(this);
} }
override void visit(const Constructor constructor)
{
astInformation.constructorDestructorLocations ~= constructor.location;
constructor.accept(this);
}
override void visit(const Destructor destructor)
{
astInformation.constructorDestructorLocations ~= destructor.index;
destructor.accept(this);
}
override void visit(const ConditionalDeclaration dec) override void visit(const ConditionalDeclaration dec)
{ {
if (dec.hasElse) if (dec.hasElse)

View File

@ -1,3 +1,4 @@
// Copyright Brian Schott 2015. // Copyright Brian Schott 2015.
// Distributed under the Boost Software License, Version 1.0. // Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at // (See accompanying file LICENSE.txt or copy at
@ -263,6 +264,17 @@ private:
inAsm = false; inAsm = false;
} }
} }
else if (currentIs(tok!"this"))
{
const thisIndex = current.index;
formatKeyword();
if (config.dfmt_space_before_function_parameters
&& astInformation.constructorDestructorLocations
.canFindIndex(thisIndex))
{
write(" ");
}
}
else if (isKeyword(current.type)) else if (isKeyword(current.type))
{ {
formatKeyword(); formatKeyword();
@ -280,12 +292,19 @@ private:
else if (currentIs(tok!"identifier")) else if (currentIs(tok!"identifier"))
{ {
writeToken(); writeToken();
if (index < tokens.length && (currentIs(tok!"identifier") //dfmt off
|| ( index > 1 && ( isBasicType(peekBack(2).type) || peekBack2Is(tok!"identifier") ) && if (index < tokens.length && ( currentIs(tok!"identifier")
currentIs(tok!("(")) && config.dfmt_space_before_function_parameters ) || ( index > 1 && config.dfmt_space_before_function_parameters
&& ( isBasicType(peekBack(2).type)
|| peekBack2Is(tok!"identifier")
|| peekBack2Is(tok!")")
|| peekBack2Is(tok!"]") )
&& currentIs(tok!("(") )
|| isBasicType(current.type) || currentIs(tok!"@") || currentIs(tok!"if") || isBasicType(current.type) || currentIs(tok!"@") || currentIs(tok!"if")
|| isNumberLiteral(tokens[index].type) || (inAsm || isNumberLiteral(tokens[index].type)
&& peekBack2Is(tok!";") && currentIs(tok!"[")))) || (inAsm && peekBack2Is(tok!";") && currentIs(tok!"["))
)))
//dfmt on
{ {
write(" "); write(" ");
} }