private static async Task <Document> SortEnumMembersAsync( Document document, EnumDeclarationSyntax enumDeclaration, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); SpecialType enumSpecialType = semanticModel.GetDeclaredSymbol(enumDeclaration).EnumUnderlyingType.SpecialType; SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members; SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members .OrderBy(f => GetConstantValue(f, semanticModel, cancellationToken), EnumValueComparer.GetInstance(enumSpecialType)) .ToSeparatedSyntaxList(); if (AreSeparatedWithEmptyLine(members)) { for (int i = 0; i < newMembers.Count; i++) { newMembers = newMembers.ReplaceAt(i, newMembers[i].TrimLeadingTrivia()); } for (int i = 0; i < newMembers.Count - 1; i++) { SyntaxToken separator = newMembers.GetSeparator(i); newMembers = newMembers.ReplaceSeparator( separator, separator.TrimTrailingTrivia().AppendToTrailingTrivia(new SyntaxTrivia[] { NewLine(), NewLine() })); } } if (newMembers.SeparatorCount == members.SeparatorCount - 1) { SyntaxNodeOrTokenList newMembersWithSeparators = newMembers.GetWithSeparators(); newMembersWithSeparators = newMembersWithSeparators.Add(CommaToken()); newMembers = newMembersWithSeparators.ToSeparatedSyntaxList <EnumMemberDeclarationSyntax>(); } MemberDeclarationSyntax newNode = enumDeclaration .WithMembers(newMembers) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false)); }
private SeparatedSyntaxList <EnumMemberDeclarationSyntax> SortMembers(SeparatedSyntaxList <EnumMemberDeclarationSyntax> members) { return(members .OrderBy(f => f, _enumMemberComparer) .ToSeparatedSyntaxList()); }