Skip to content

Commit

Permalink
Properly format attributes on accessor declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Oct 22, 2015
1 parent f283dd5 commit fe56db5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,10 @@ public static bool IsOpenBraceOrCommaOfObjectInitializer(this SyntaxToken token)
return (token.IsKind(SyntaxKind.OpenBraceToken) || token.IsKind(SyntaxKind.CommaToken)) &&
token.Parent.IsKind(SyntaxKind.ObjectInitializerExpression);
}

public static bool IsOpenBraceOfAccessorList(this SyntaxToken token)
{
return token.IsKind(SyntaxKind.OpenBraceToken) && token.Parent.IsKind(SyntaxKind.AccessorList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Composition;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Options;
Expand Down Expand Up @@ -243,9 +244,18 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
}

// * [
if (currentToken.IsKind(SyntaxKind.OpenBracketToken) && !previousToken.IsOpenBraceOrCommaOfObjectInitializer())
if (currentToken.IsKind(SyntaxKind.OpenBracketToken) &&
!previousToken.IsOpenBraceOrCommaOfObjectInitializer())
{
return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
if (previousToken.IsOpenBraceOfAccessorList() ||
previousToken.IsLastTokenOfNode<AccessorDeclarationSyntax>())
{
return CreateAdjustSpacesOperation(1, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
}
else
{
return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
}
}

// case * :
Expand Down
15 changes: 15 additions & 0 deletions src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7079,6 +7079,21 @@ public Int32 PaymentMethodID
[System.Diagnostics.DebuggerStepThrough]
get { return 10; }
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(4720, "https://github.com/dotnet/roslyn/issues/4720")]
public void OneSpaceBetweenAccessorsAndAttributes()
{
AssertFormat(@"
class Program
{
public int SomeProperty { [SomeAttribute] get; [SomeAttribute] private set; }
}", @"
class Program
{
public int SomeProperty { [SomeAttribute] get; [SomeAttribute] private set; }
}");
}
}
Expand Down

0 comments on commit fe56db5

Please sign in to comment.