Skip to content

Commit

Permalink
Merge pull request #1812 from sharwell/fix-1745
Browse files Browse the repository at this point in the history
Implement the 'elementOrder' configuration property
  • Loading branch information
sharwell committed Nov 25, 2015
2 parents 2c33c5f + 6e55178 commit c7e6ed8
Show file tree
Hide file tree
Showing 27 changed files with 1,087 additions and 669 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace StyleCop.Analyzers.OrderingRules
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Settings.ObjectModel;

/// <summary>
/// Implements code fixes for element ordering rules.
Expand All @@ -30,8 +31,7 @@ internal class ElementOrderCodeFixProvider : CodeFixProvider
SA1202ElementsMustBeOrderedByAccess.DiagnosticId,
SA1203ConstantsMustAppearBeforeFields.DiagnosticId,
SA1204StaticElementsMustAppearBeforeInstanceElements.DiagnosticId,
SA1214StaticReadonlyElementsMustAppearBeforeStaticNonReadonlyElements.DiagnosticId,
SA1215InstanceReadonlyElementsMustAppearBeforeInstanceNonReadonlyElements.DiagnosticId);
SA1214ReadonlyElementsMustAppearBeforeNonReadonlyElements.DiagnosticId);

/// <inheritdoc/>
public override FixAllProvider GetFixAllProvider()
Expand All @@ -57,7 +57,8 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var orderingChecks = await GetEnabledRulesForDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var elementOrder = settings.OrderingRules.ElementOrder;
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var indentationOptions = IndentationOptions.FromDocument(document);

Expand All @@ -67,68 +68,57 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
return document;
}

syntaxRoot = UpdateSyntaxRoot(memberDeclaration, orderingChecks, syntaxRoot, indentationOptions);
syntaxRoot = UpdateSyntaxRoot(memberDeclaration, elementOrder, syntaxRoot, indentationOptions);

return document.WithSyntaxRoot(syntaxRoot);
}

private static async Task<ElementOrderingChecks> GetEnabledRulesForDocumentAsync(Document document, CancellationToken cancellationToken)
{
SemanticModel semanticModel;
if (!document.TryGetSemanticModel(out semanticModel))
{
semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
}

return ElementOrderingChecks.GetElementOrderingChecksForSemanticModel(semanticModel);
}

private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclaration, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclaration, ImmutableArray<OrderingTrait> elementOrder, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
{
var parentDeclaration = memberDeclaration.Parent;
var memberToMove = new MemberOrderHelper(memberDeclaration, checks);
var memberToMove = new MemberOrderHelper(memberDeclaration, elementOrder);

if (parentDeclaration is TypeDeclarationSyntax)
{
return HandleTypeDeclaration(memberToMove, (TypeDeclarationSyntax)parentDeclaration, checks, syntaxRoot, indentationOptions);
return HandleTypeDeclaration(memberToMove, (TypeDeclarationSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationOptions);
}

if (parentDeclaration is NamespaceDeclarationSyntax)
{
return HandleNamespaceDeclaration(memberToMove, (NamespaceDeclarationSyntax)parentDeclaration, checks, syntaxRoot, indentationOptions);
return HandleNamespaceDeclaration(memberToMove, (NamespaceDeclarationSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationOptions);
}

if (parentDeclaration is CompilationUnitSyntax)
{
return HandleCompilationUnitDeclaration(memberToMove, (CompilationUnitSyntax)parentDeclaration, checks, syntaxRoot, indentationOptions);
return HandleCompilationUnitDeclaration(memberToMove, (CompilationUnitSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationOptions);
}

return syntaxRoot;
}

private static SyntaxNode HandleTypeDeclaration(MemberOrderHelper memberOrder, TypeDeclarationSyntax typeDeclarationNode, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
private static SyntaxNode HandleTypeDeclaration(MemberOrderHelper memberOrder, TypeDeclarationSyntax typeDeclarationNode, ImmutableArray<OrderingTrait> elementOrder, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
{
return OrderMember(memberOrder, typeDeclarationNode.Members, checks, syntaxRoot, indentationOptions);
return OrderMember(memberOrder, typeDeclarationNode.Members, elementOrder, syntaxRoot, indentationOptions);
}

private static SyntaxNode HandleCompilationUnitDeclaration(MemberOrderHelper memberOrder, CompilationUnitSyntax compilationUnitDeclaration, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
private static SyntaxNode HandleCompilationUnitDeclaration(MemberOrderHelper memberOrder, CompilationUnitSyntax compilationUnitDeclaration, ImmutableArray<OrderingTrait> elementOrder, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
{
return OrderMember(memberOrder, compilationUnitDeclaration.Members, checks, syntaxRoot, indentationOptions);
return OrderMember(memberOrder, compilationUnitDeclaration.Members, elementOrder, syntaxRoot, indentationOptions);
}

private static SyntaxNode HandleNamespaceDeclaration(MemberOrderHelper memberOrder, NamespaceDeclarationSyntax namespaceDeclaration, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
private static SyntaxNode HandleNamespaceDeclaration(MemberOrderHelper memberOrder, NamespaceDeclarationSyntax namespaceDeclaration, ImmutableArray<OrderingTrait> elementOrder, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
{
return OrderMember(memberOrder, namespaceDeclaration.Members, checks, syntaxRoot, indentationOptions);
return OrderMember(memberOrder, namespaceDeclaration.Members, elementOrder, syntaxRoot, indentationOptions);
}

private static SyntaxNode OrderMember(MemberOrderHelper memberOrder, SyntaxList<MemberDeclarationSyntax> members, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
private static SyntaxNode OrderMember(MemberOrderHelper memberOrder, SyntaxList<MemberDeclarationSyntax> members, ImmutableArray<OrderingTrait> elementOrder, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
{
var memberIndex = members.IndexOf(memberOrder.Member);
MemberOrderHelper target = default(MemberOrderHelper);

for (var i = memberIndex - 1; i >= 0; --i)
{
var orderHelper = new MemberOrderHelper(members[i], checks);
var orderHelper = new MemberOrderHelper(members[i], elementOrder);
if (orderHelper.Priority < memberOrder.Priority)
{
target = orderHelper;
Expand Down Expand Up @@ -276,7 +266,8 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
}

var indentationOptions = IndentationOptions.FromDocument(document);
var orderingChecks = await GetEnabledRulesForDocumentAsync(document, fixAllContext.CancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
var elementOrder = settings.OrderingRules.ElementOrder;
var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);

var trackedDiagnosticMembers = new List<MemberDeclarationSyntax>();
Expand All @@ -296,7 +287,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
foreach (var member in trackedDiagnosticMembers)
{
var memberDeclaration = syntaxRoot.GetCurrentNode(member);
syntaxRoot = UpdateSyntaxRoot(memberDeclaration, orderingChecks, syntaxRoot, indentationOptions);
syntaxRoot = UpdateSyntaxRoot(memberDeclaration, elementOrder, syntaxRoot, indentationOptions);
}

return syntaxRoot;
Expand Down
Loading

0 comments on commit c7e6ed8

Please sign in to comment.