private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { return(document.ReplaceNodeAsync(ifStatement, GetNewStatements(), cancellationToken)); IEnumerable <StatementSyntax> GetNewStatements() { ElseClauseSyntax parentElse = null; foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade()) { if (ifOrElse.IsIf) { IfStatementSyntax newIfStatement = ifOrElse.AsIf(); ElseClauseSyntax elseClause = newIfStatement.Else; newIfStatement = newIfStatement.WithElse(null); if (parentElse != null) { newIfStatement = newIfStatement.PrependToLeadingTrivia(parentElse.GetLeadingTrivia()); } if (elseClause != null) { newIfStatement = newIfStatement.AppendToTrailingTrivia(CSharpFactory.NewLine()); } yield return(newIfStatement.WithFormatterAnnotation()); parentElse = ifStatement.Else; } else { StatementSyntax statement = ifOrElse.Statement; if (statement is BlockSyntax block) { foreach (StatementSyntax newStatement in block.Statements.Select(f => f.WithFormatterAnnotation())) { yield return(newStatement); } } else { yield return(statement); } } } } }
private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { var statements = new List <StatementSyntax>(); IfStatementSyntax topIfStatement = ifStatement; ElseClauseSyntax elseClause = null; while (true) { IfStatementSyntax newIfStatement = ifStatement.WithElse(null); if (elseClause != null) { newIfStatement = newIfStatement.PrependToLeadingTrivia(elseClause.GetLeadingTrivia()); } newIfStatement = newIfStatement .AppendToTrailingTrivia(CSharpFactory.NewLine()) .WithFormatterAnnotation(); statements.Add(newIfStatement); elseClause = ifStatement.Else; StatementSyntax statement = elseClause.Statement; SyntaxKind kind = statement.Kind(); if (kind != SyntaxKind.IfStatement) { if (kind == SyntaxKind.Block) { statements.AddRange(((BlockSyntax)statement).Statements.Select(f => f.WithFormatterAnnotation())); } else { statements.Add(statement); } break; } ifStatement = ((IfStatementSyntax)statement); } return(document.ReplaceNodeAsync(topIfStatement, statements, cancellationToken)); }
private Decisions TraverseElseClauses(ElseClauseSyntax ecs, ref int exitPoints, bool nested = false) { Decisions retDecision = new Decisions(); ElseStatement elseStm = new ElseStatement(); if (ecs.HasLeadingTrivia) { SetOuterComments(elseStm, ecs.GetLeadingTrivia().ToFullString()); } if (ecs.HasTrailingTrivia) { SetInnerComments(elseStm, ecs.GetTrailingTrivia().ToFullString()); } elseStm.IsNested = nested; var binaryExpressions = from aBinaryExpression in ecs.ChildNodes().OfType<BinaryExpressionSyntax>() select aBinaryExpression; foreach (BinaryExpressionSyntax bes in binaryExpressions) { Method tempMethod = TraverseBinaryExpression(bes); elseStm.AccessedVars.AddRange(tempMethod.AccessedVariables); elseStm.InvokedMethods.AddRange(tempMethod.InvokedMethods); } var catches = from aCatch in ecs.ChildNodes().OfType<CatchClauseSyntax>() select aCatch; foreach (CatchClauseSyntax ccs in catches) { Decisions tempCatch = TraverseCatchClauses(ccs, ref exitPoints, true); elseStm.Nested.AddRange(tempCatch.Catches); } var elses = from aElse in ecs.ChildNodes().OfType<ElseClauseSyntax>() select aElse; foreach (ElseClauseSyntax ecs2 in elses) { Decisions tempElse = TraverseElseClauses(ecs2, ref exitPoints, true); elseStm.Nested.AddRange(tempElse.ElseStatements); } #region nested stuff var statements = from aStatement in ecs.ChildNodes().OfType<StatementSyntax>() select aStatement; foreach (StatementSyntax ss in statements) { if (ss is DoStatementSyntax) { Decisions dwl = TraverseDoStatements(ss as DoStatementSyntax, ref exitPoints, true); elseStm.Nested.AddRange(dwl.DoWhileLoops); } else if (ss is ExpressionStatementSyntax) { Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax); elseStm.AccessedVars.AddRange(tempMethod.AccessedVariables); elseStm.InvokedMethods.AddRange(tempMethod.InvokedMethods); } else if (ss is ForEachStatementSyntax) { Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true); elseStm.Nested.AddRange(fes.ForEachStatements); } else if (ss is ForStatementSyntax) { Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true); elseStm.Nested.AddRange(fs.ForStatements); } else if (ss is IfStatementSyntax) { Decisions decision = TraverseIfStatements(ss as IfStatementSyntax, ref exitPoints, true); elseStm.Nested.AddRange(decision.IfStatements); } else if (ss is LocalDeclarationStatementSyntax) { Model.Type tempType = new Model.Type(); LocalDeclarationStatementSyntax ldss = ss as LocalDeclarationStatementSyntax; if (ldss.Declaration != null) { VariableDeclarationSyntax vds = ldss.Declaration; tempType.Name = vds.Type.ToString(); tempType.IsKnownType = true; tempType.IsNotUserDefined = true; } Method tempMethod = TransverseAccessVars(ss as LocalDeclarationStatementSyntax); //NOT SURE if this will work but here goes tempMethod.AccessedVariables[0].Type = tempType; elseStm.AccessedVars.AddRange(tempMethod.AccessedVariables); elseStm.InvokedMethods.AddRange(tempMethod.InvokedMethods); } else if (ss is ReturnStatementSyntax) { exitPoints++; } else if (ss is SwitchStatementSyntax) { Decisions switchStm = TraverseSwitchStatements(ss as SwitchStatementSyntax, ref exitPoints, true); elseStm.Nested.AddRange(switchStm.SwitchStatements); } else if (ss is WhileStatementSyntax) { Decisions wl = TraverseWhileLoops(ss as WhileStatementSyntax, ref exitPoints, true); elseStm.Nested.AddRange(wl.WhileLoops); } } #endregion retDecision.ElseStatements.Add(elseStm); return retDecision; }