提交 e657cde3 编写于 作者: C CyrusNajmabadi

Merge pull request #11685 from CyrusNajmabadi/preserveAttributeOnSingleLine

Keep attribute on the same line as a member if they started on a single line

Fixes #11572 
......@@ -182,6 +182,17 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
return AdjustSpacesOperationZeroOrOne(optionSet, CSharpFormattingOptions.SpaceWithinSquareBrackets);
}
// attribute case ] *
// Place a space between the attribute and the next member if they're on the same line.
if (previousKind == SyntaxKind.CloseBracketToken && previousToken.Parent.IsKind(SyntaxKind.AttributeList))
{
var attributeOwner = previousToken.Parent?.Parent;
if (attributeOwner is MemberDeclarationSyntax)
{
return CreateAdjustSpacesOperation(1, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
}
}
// For spacing delimiters - after colon
if (previousToken.IsColonInTypeBaseList())
{
......
......@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Options;
......@@ -64,8 +65,24 @@ private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, Sy
var memberDeclNode = node as MemberDeclarationSyntax;
if (memberDeclNode != null)
{
// Attempt to keep the part of a member that follows hte attributes on a single
// line if that's how it's currently written.
var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2);
var attributes = memberDeclNode.GetAttributes();
if (attributes.Count > 0)
{
// 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,
node.GetFirstToken(includeZeroWidth: true),
node.GetLastToken(includeZeroWidth: true));
}
var propertyDeclNode = node as PropertyDeclarationSyntax;
if (propertyDeclNode?.Initializer != null && propertyDeclNode?.AccessorList != null)
{
......
......@@ -3317,8 +3317,7 @@ public async Task Attributes1()
{
await AssertFormatAsync(@"class Program
{
[Flags]
public void Method() { }
[Flags] public void Method() { }
}", @"class Program
{
[ Flags ] public void Method ( ) { }
......@@ -7401,6 +7400,22 @@ public void M()
await AssertFormatAsync(code, code);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(11572, "https://github.com/dotnet/roslyn/issues/11572")]
public async Task FormatAttributeOnSameLineAsField()
{
await AssertFormatAsync(
@"
class C
{
[Attr] int i;
}",
@"
class C {
[Attr] int i;
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(6628, "https://github.com/dotnet/roslyn/issues/6628")]
public async Task FormatOnElseBlockBracesOnSameLineRemainsInSameLine_2()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册