public static void DoRemoveInvocationWithFilter(this CodeFunction codeFunction, string[] classNames, List <FluentAPIEntityNode> filter) { if (classNames == null) { return; } if (codeFunction == null) { return; } EditPoint editPoint = codeFunction.StartPoint.CreateEditPoint(); editPoint.SmartFormat(codeFunction.EndPoint); editPoint = codeFunction.StartPoint.CreateEditPoint(); string buff = editPoint.GetText(codeFunction.EndPoint); SyntaxTree tree = CSharpSyntaxTree.ParseText(buff); SyntaxNode root = tree.GetRoot(); if (root == null) { return; } MethodDeclarationSyntax methodDeclaration = root.GetOnModelCreatingParameterName("DbModelBuilder", "ModelBuilder", out string parameterName); if ((methodDeclaration == null) || string.IsNullOrEmpty(parameterName)) { return; } if (methodDeclaration.Body == null) { return; } List <TextSpan> spans = new List <TextSpan>(); foreach (StatementSyntax ss in methodDeclaration.Body.Statements) { if (ss.Kind() != SyntaxKind.ExpressionStatement) { continue; } ExpressionStatementSyntax expressionStatementSyntax = ss as ExpressionStatementSyntax; if (expressionStatementSyntax.Expression == null) { continue; } if (expressionStatementSyntax.Expression.Kind() != SyntaxKind.InvocationExpression) { continue; } InvocationExpressionSyntax invocationExpressionSyntax = expressionStatementSyntax.Expression as InvocationExpressionSyntax; if (!parameterName.Equals(invocationExpressionSyntax.InvocationExpressionRootName(classNames))) { continue; } FluentAPIEntityNode faen = expressionStatementSyntax.Expression.InvocationExpressionMethods(null); if (faen == null) { continue; } if (faen.Methods == null) { continue; } if (faen.IsSatisfiedTheFilter(filter)) { spans.Insert(0, expressionStatementSyntax.Span); } } foreach (TextSpan ts in spans) { buff = buff.Remove(ts.Start, ts.Length); // // the commented code does not work : ts.Start does not correctly point to begining of the operator //editPoint.CharRight(ts.Start); //editPoint.Delete(ts.Length); //editPoint.CharLeft(ts.Start); //if (codeFunction.ProjectItem != null) //{ // codeFunction.ProjectItem.Save(); //} } buff = buff.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine); editPoint = codeFunction.StartPoint.CreateEditPoint(); editPoint.Delete(codeFunction.EndPoint); editPoint.Insert(buff); if (codeFunction.ProjectItem != null) { codeFunction.ProjectItem.Save(); } }
public static List <FluentAPIEntityNode> DoAnalyzeWithFilter(this CodeFunction codeFunction, string[] classNames, List <FluentAPIEntityNode> filter) { if (classNames == null) { return(null); } if (codeFunction == null) { return(null); } string buff = codeFunction.StartPoint.CreateEditPoint().GetText(codeFunction.EndPoint); SyntaxTree tree = CSharpSyntaxTree.ParseText(buff); SyntaxNode root = tree.GetRoot(); if (root == null) { return(null); } MethodDeclarationSyntax methodDeclaration = root.GetOnModelCreatingParameterName("DbModelBuilder", "ModelBuilder", out string parameterName); if ((methodDeclaration == null) || string.IsNullOrEmpty(parameterName)) { return(null); } if (methodDeclaration.Body == null) { return(null); } List <FluentAPIEntityNode> entityNodes = null; foreach (StatementSyntax ss in methodDeclaration.Body.Statements) { if (ss.Kind() != SyntaxKind.ExpressionStatement) { continue; } ExpressionStatementSyntax expressionStatementSyntax = ss as ExpressionStatementSyntax; if (expressionStatementSyntax.Expression == null) { continue; } if (expressionStatementSyntax.Expression.Kind() != SyntaxKind.InvocationExpression) { continue; } InvocationExpressionSyntax invocationExpressionSyntax = expressionStatementSyntax.Expression as InvocationExpressionSyntax; if (!parameterName.Equals(invocationExpressionSyntax.InvocationExpressionRootName(classNames))) { continue; } string methodBodyStr = expressionStatementSyntax.ToString().Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace(" ", ""); FluentAPIEntityNode faen = expressionStatementSyntax.Expression.InvocationExpressionMethods(null); if (faen == null) { continue; } if (faen.Methods == null) { continue; } if (faen.IsSatisfiedTheFilter(filter)) { if (entityNodes == null) { entityNodes = new List <FluentAPIEntityNode>(); } faen.MethodBodyString = methodBodyStr; entityNodes.Add(faen); } } return(entityNodes); }