Created
January 27, 2021 20:47
-
-
Save jeppevammenkristensen/54b05471b03d6f0cae65666132804e06 to your computer and use it in GitHub Desktop.
Sort episerver class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Define other methods and classes here | |
| public class Rewriter : CSharpSyntaxRewriter | |
| { | |
| private string LastGroup = null; | |
| private int? Index = null; | |
| public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) | |
| { | |
| var display = node.AttributeLists.SelectMany(x => x.Attributes.Where(y => y.Name.ToString() == "Display")).FirstOrDefault(); | |
| if (display == null){ | |
| return base.VisitPropertyDeclaration(node); | |
| } | |
| var order = display.ArgumentList.Arguments.FirstOrDefault(x => x.NameEquals.Name.ToString() == "Order"); | |
| var groupname = display.ArgumentList.Arguments.FirstOrDefault(x => x.NameEquals.Name.ToString() == "GroupName"); | |
| int thisIndex; | |
| if (LastGroup == null || (groupname != null && groupname.Expression.ToString() != LastGroup)){ | |
| thisIndex = 100; | |
| Index = 110; | |
| } | |
| else { | |
| thisIndex = Index.Value; | |
| Index += 10; | |
| } | |
| if (groupname == null){ | |
| if (LastGroup == null){ | |
| LastGroup = "Dummy"; | |
| } | |
| } else { | |
| LastGroup = groupname.Expression.ToString().Trim(); | |
| } | |
| var orderLiteral = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(thisIndex)); | |
| if (Index == null){ | |
| node = node.ReplaceNode(display, display.ArgumentList.AddArguments(SyntaxFactory.AttributeArgument(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(thisIndex))))); | |
| } | |
| else { | |
| node = node.ReplaceNode(order, order.WithExpression(orderLiteral)); | |
| } | |
| return base.VisitPropertyDeclaration(node); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment