Properly format enum member attributes and parameter attributes

This commit is contained in:
Laurent Tréguier 2018-09-27 15:27:23 +02:00 committed by The Dlang Bot
parent 0fc4149907
commit 5e6dd58502
11 changed files with 61 additions and 1 deletions

View File

@ -36,6 +36,7 @@ struct ASTInformation
sort(spaceAfterLocations);
sort(unaryLocations);
sort(attributeDeclarationLines);
sort(atAttributeStartLocations);
sort(caseEndLocations);
sort(structInitStartLocations);
sort(structInitEndLocations);
@ -67,6 +68,9 @@ struct ASTInformation
/// Lines containing attribute declarations
size_t[] attributeDeclarationLines;
/// Lines containing attribute declarations that can be followed by a new line
size_t[] atAttributeStartLocations;
/// Case statement colon locations
size_t[] caseEndLocations;
@ -335,6 +339,34 @@ final class FormatVisitor : ASTVisitor
attributeDeclaration.accept(this);
}
override void visit(const FunctionAttribute functionAttribute)
{
if (functionAttribute.atAttribute !is null)
astInformation.atAttributeStartLocations ~= functionAttribute.atAttribute.startLocation;
functionAttribute.accept(this);
}
override void visit(const MemberFunctionAttribute memberFunctionAttribute)
{
if (memberFunctionAttribute.atAttribute !is null)
astInformation.atAttributeStartLocations ~= memberFunctionAttribute.atAttribute.startLocation;
memberFunctionAttribute.accept(this);
}
override void visit(const Attribute attribute)
{
if (attribute.atAttribute !is null)
astInformation.atAttributeStartLocations ~= attribute.atAttribute.startLocation;
attribute.accept(this);
}
override void visit(const StorageClass storageClass)
{
if (storageClass.atAttribute !is null)
astInformation.atAttributeStartLocations ~= storageClass.atAttribute.startLocation;
storageClass.accept(this);
}
override void visit(const InStatement inStatement)
{
astInformation.contractLocations ~= inStatement.inTokenLocation;

View File

@ -629,6 +629,7 @@ private:
void formatAt()
{
immutable size_t atIndex = tokens[index].index;
writeToken();
if (currentIs(tok!"identifier"))
writeToken();
@ -637,7 +638,8 @@ private:
writeParens(false);
if (tokens[index].type == tok!"{")
return;
if (index < tokens.length && tokens[index - 1].line < tokens[index].line)
if (index < tokens.length && tokens[index - 1].line < tokens[index].line
&& astInformation.atAttributeStartLocations.canFindIndex(atIndex))
newline();
else
write(" ");

View File

@ -0,0 +1,6 @@
enum Foo
{
deprecated member0,
@UDA(0) member1
}

View File

@ -0,0 +1 @@
void foo(@UDA(0) @UDA(1) Bar bar);

View File

@ -0,0 +1 @@
alias fun = (@(0) @(1) int p) => ((@Foo p) { return 0; })(p);

7
tests/enum_attribs.d Normal file
View File

@ -0,0 +1,7 @@
enum Foo
{
deprecated member0 ,
@UDA(0)
member1
}

View File

@ -0,0 +1,2 @@
void foo(@UDA(0)
@UDA(1) Bar bar );

View File

@ -0,0 +1,2 @@
alias fun = (@(0)
@(1) int p) => ((@Foo p){return 0;})(p);

View File

@ -0,0 +1,5 @@
enum Foo {
deprecated member0,
@UDA(0) member1
}

View File

@ -0,0 +1 @@
void foo(@UDA(0) @UDA(1) Bar bar);

View File

@ -0,0 +1 @@
alias fun = (@(0) @(1) int p) => ((@Foo p) { return 0; })(p);