private static IEnumerable <SwitchSectionSyntax> CreateSwitchSections(IfStatementSyntax ifStatement) { foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { if (ifOrElse.IsIf) { ifStatement = ifOrElse.AsIf(); var condition = ifStatement.Condition.WalkDownParentheses() as BinaryExpressionSyntax; List <SwitchLabelSyntax> labels = CreateSwitchLabels(condition, new List <SwitchLabelSyntax>()); labels.Reverse(); SwitchSectionSyntax section = SwitchSection( List(labels), AddBreakStatementIfNecessary(ifStatement.Statement)); yield return(section); } else { yield return(DefaultSwitchSection(AddBreakStatementIfNecessary(ifOrElse.Statement))); } } }
private static HashSet <AwaitExpressionSyntax> GetAwaitExpressionsFromIfStatement( IfStatementSyntax ifStatement, bool endsWithElse) { HashSet <AwaitExpressionSyntax> awaitExpressions = null; foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { if (ifOrElse.IsElse && !endsWithElse) { return(null); } AwaitExpressionSyntax awaitExpression = GetLastReturnAwaitExpressionOfDefault(ifOrElse.Statement); if (awaitExpression != null) { (awaitExpressions ?? (awaitExpressions = new HashSet <AwaitExpressionSyntax>())).Add(awaitExpression); } else { return(null); } } return(awaitExpressions); }
public static async Task ComputeRefactoringAsync(RefactoringContext context, IfStatementSyntax ifStatement) { if (!ifStatement.IsTopmostIf()) { return; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { if (ifOrElse.IsIf) { if (!IsFixable(ifOrElse.AsIf(), semanticModel, context.CancellationToken)) { return; } } else if (ContainsBreakStatementThatBelongsToParentLoop(ifOrElse.AsElse().Statement)) { return; } } string title = (ifStatement.IsSimpleIf()) ? "Replace if with switch" : "Replace if-else with switch"; context.RegisterRefactoring( title, cancellationToken => RefactorAsync(context.Document, ifStatement, cancellationToken)); }
public static BracesAnalysisResult AnalyzeBraces(IfStatementSyntax ifStatement) { if (ifStatement == null) { throw new ArgumentNullException(nameof(ifStatement)); } bool anyHasEmbedded = false; bool anyHasBlock = false; bool allSupportsEmbedded = true; int cnt = 0; foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { cnt++; StatementSyntax statement = ifOrElse.Statement; if (!anyHasEmbedded && !statement.IsKind(SyntaxKind.Block)) { anyHasEmbedded = true; } if (!anyHasBlock && statement.IsKind(SyntaxKind.Block)) { anyHasBlock = true; } if (allSupportsEmbedded && !SupportsEmbedded(statement)) { allSupportsEmbedded = false; } if (cnt > 1 && anyHasEmbedded && !allSupportsEmbedded) { return(BracesAnalysisResult.AddBraces); } } if (cnt > 1 && allSupportsEmbedded && anyHasBlock) { if (anyHasEmbedded) { return(BracesAnalysisResult.AddBraces | BracesAnalysisResult.RemoveBraces); } else { return(BracesAnalysisResult.RemoveBraces); } } return(BracesAnalysisResult.None); }
private static IEnumerable <IfStatementSyntax> GetIfStatements(IfStatementSyntax ifStatement) { foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { if (ifOrElse.IsIf) { yield return(ifOrElse.AsIf()); } } }
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.GetChain()) { 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 IEnumerable <BlockSyntax> GetBlockStatements(IfStatementSyntax ifStatement) { foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { StatementSyntax statement = ifOrElse.Statement; if (statement?.IsKind(SyntaxKind.Block) == true) { yield return((BlockSyntax)statement); } } }
private static IEnumerable <StatementSyntax> GetEmbeddedStatements(IfStatementSyntax topmostIf) { foreach (IfStatementOrElseClause ifOrElse in topmostIf.GetChain()) { StatementSyntax statement = ifOrElse.Statement; if (statement?.IsKind(SyntaxKind.Block) == false) { yield return(statement); } } }
public static void ComputeRefactoring(RefactoringContext context, IfStatementSyntax ifStatement) { if (ifStatement.IsParentKind(SyntaxKind.ElseClause)) { return; } if (ifStatement.Else == null) { return; } foreach (IfStatementOrElseClause ifOrElse in ifStatement.GetChain()) { StatementSyntax statement = ifOrElse.Statement; if (statement is BlockSyntax block) { statement = block.Statements.LastOrDefault(); } if (statement == null) { return; } if (!statement.IsKind( SyntaxKind.ReturnStatement, SyntaxKind.ContinueStatement, SyntaxKind.BreakStatement, SyntaxKind.ThrowStatement)) { return; } } context.RegisterRefactoring( "Split if-else", cancellationToken => RefactorAsync(context.Document, ifStatement, cancellationToken)); }