未验证 提交 934db51a 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #22192 from MaStr11/PreserveAttributesOnSingleLine

Keep attribute on the same line as a member if they started on a single line
...@@ -62,22 +62,23 @@ private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, Sy ...@@ -62,22 +62,23 @@ private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, Sy
if (node is MemberDeclarationSyntax memberDeclNode) if (node is MemberDeclarationSyntax memberDeclNode)
{ {
// Attempt to keep the part of a member that follows hte attributes on a single // Attempt to keep the part of a member that follows the attributes on a single
// line if that's how it's currently written. // line if that's how it's currently written.
var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes(); var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2); AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2);
// Also, If the member is on single line with its attributes on it, then keep
// it on a single line. This is for code like the following:
//
// [Import] public int Field1;
// [Import] public int Field2;
var attributes = memberDeclNode.GetAttributes(); var attributes = memberDeclNode.GetAttributes();
if (attributes.Count > 0) var endToken = node.GetLastToken(includeZeroWidth: true);
for (var i = 0; i < attributes.Count; ++i)
{ {
// Also, If the member is on single line with its attributes on it, then keep
// it on a single line. This is for code like the following:
//
// [Import] public int Field1;
// [Import] public int Field2;
AddSuppressWrappingIfOnSingleLineOperation(list, AddSuppressWrappingIfOnSingleLineOperation(list,
node.GetFirstToken(includeZeroWidth: true), attributes[i].GetFirstToken(includeZeroWidth: true),
node.GetLastToken(includeZeroWidth: true)); endToken);
} }
var propertyDeclNode = node as PropertyDeclarationSyntax; var propertyDeclNode = node as PropertyDeclarationSyntax;
......
...@@ -7946,6 +7946,64 @@ class C { ...@@ -7946,6 +7946,64 @@ class C {
}"); }");
} }
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(21789, "https://github.com/dotnet/roslyn/issues/21789")]
public async Task FormatMultipleAttributeOnSameLineAsField1()
{
await AssertFormatAsync(
@"
class C
{
[Attr1]
[Attr2]
[Attr3] [Attr4] int i;
}",
@"
class C {
[Attr1]
[Attr2]
[Attr3][Attr4] int i;
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(21789, "https://github.com/dotnet/roslyn/issues/21789")]
public async Task FormatMultipleAttributesOnSameLineAsField2()
{
await AssertFormatAsync(
@"
class C
{
[Attr1]
[Attr2]
[Attr3] [Attr4] int i;
}",
@"
class C {
[Attr1][Attr2]
[Attr3][Attr4] int i;
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(21789, "https://github.com/dotnet/roslyn/issues/21789")]
public async Task FormatMultipleAttributeOnSameLineAndFieldOnNewLine()
{
await AssertFormatAsync(
@"
class C
{
[Attr1]
[Attr2]
int i;
}",
@"
class C {
[Attr1][Attr2]
int i;
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)] [Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(6628, "https://github.com/dotnet/roslyn/issues/6628")] [WorkItem(6628, "https://github.com/dotnet/roslyn/issues/6628")]
public async Task FormatOnElseBlockBracesOnSameLineRemainsInSameLine_2() public async Task FormatOnElseBlockBracesOnSameLineRemainsInSameLine_2()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册