static bool IsLastStatement(SyntaxList<StatementSyntax> statements, MultiLineIfBlockSyntax ifBlock) { return statements.IndexOf(ifBlock) + 1 >= statements.Count; }
static IEnumerable<SyntaxNode> GetStatements(MultiLineIfBlockSyntax ifBlock) { foreach (var stmt in ifBlock.Statements) yield return stmt.WithAdditionalAnnotations(Formatter.Annotation); }
static IEnumerable<SyntaxNode> GenerateNewScript(MultiLineIfBlockSyntax ifBlock) { var ifStatement = GenerateInvertedIfStatement(ifBlock.IfStatement); yield return SyntaxFactory.MultiLineIfBlock(ifStatement) .WithStatements(GenerateNewTrueStatement(ifBlock.ElseBlock.Statements)) .WithLeadingTrivia(ifBlock.GetLeadingTrivia()) .WithTrailingTrivia(ifBlock.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); foreach (var stmt in ifBlock.Statements) { yield return stmt.WithAdditionalAnnotations(Formatter.Annotation); } }
internal static bool CollectCaseBlocks(List<CaseBlockSyntax> result, SemanticModel context, MultiLineIfBlockSyntax ifBlock, ExpressionSyntax switchExpr) { // if var labels = new List<CaseClauseSyntax>(); if (!CollectCaseLabels(labels, context, ifBlock.IfStatement.Condition, switchExpr)) return false; result.Add(SyntaxFactory.CaseBlock(SyntaxFactory.CaseStatement(labels.ToArray()), ifBlock.Statements)); foreach (var block in ifBlock.ElseIfBlocks) { labels = new List<CaseClauseSyntax>(); if (!CollectCaseLabels(labels, context, block.ElseIfStatement.Condition, switchExpr)) return false; result.Add(SyntaxFactory.CaseBlock(SyntaxFactory.CaseStatement(labels.ToArray()), block.Statements)); } // else if (ifBlock.ElseBlock != null) { result.Add(SyntaxFactory.CaseElseBlock(SyntaxFactory.CaseElseStatement(SyntaxFactory.ElseCaseClause()), ifBlock.ElseBlock.Statements)); } return true; }
private Decisions TraverseMultiIfStatement(MultiLineIfBlockSyntax mlib, ref int returnCnt, bool nested = false) { Decisions retDecisions = new Decisions(); foreach (SyntaxNode sn in mlib.ChildNodes()) { if (sn is IfPartSyntax) { retDecisions.IfStatements.Add(TraverseIfStatement(sn as IfPartSyntax, ref returnCnt, nested)); } else if (sn is ElsePartSyntax) { retDecisions.ElseStatements.Add(TraverseElseStatement(sn as ElsePartSyntax, ref returnCnt, nested)); } } return retDecisions; }
public override void VisitMultiLineIfBlock(MultiLineIfBlockSyntax node) => counter.CheckNesting(node.IfStatement.IfKeyword, () => base.VisitMultiLineIfBlock(node));
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { var document = context.Document; if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles) { return; } var span = context.Span; if (!span.IsEmpty) { return; } var cancellationToken = context.CancellationToken; if (cancellationToken.IsCancellationRequested) { return; } var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); if (model.IsFromGeneratedCode(cancellationToken)) { return; } var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); var selectStatement = root.FindNode(span) as SelectStatementSyntax; if (selectStatement == null) { return; } var node = selectStatement.Parent as SelectBlockSyntax; if (node == null || node.CaseBlocks.Count == 0) { return; } if (node.CaseBlocks.Count == 1 && node.CaseBlocks[0].CaseStatement.Cases.OfType <ElseCaseClauseSyntax>().Any()) { return; } foreach (var block in node.CaseBlocks) { var lastStatement = block.Statements.LastOrDefault() as ExitStatementSyntax; if (HasNonTrailingExitSelects(block, lastStatement != null && lastStatement.BlockKeyword.IsKind(SyntaxKind.SelectKeyword) ? lastStatement : null)) { return; } } context.RegisterRefactoring( CodeActionFactory.Create( span, DiagnosticSeverity.Info, GettextCatalog.GetString("To 'if'"), t2 => { MultiLineIfBlockSyntax ifBlock = null; List <ElseIfBlockSyntax> elseIfs = new List <ElseIfBlockSyntax>(); ElseBlockSyntax defaultElse = null; foreach (var block in node.CaseBlocks) { var condition = CollectCondition(node.SelectStatement.Expression, block.CaseStatement.Cases.ToArray()); var statements = block.Statements; var lastStatement = block.Statements.LastOrDefault() as ExitStatementSyntax; if (lastStatement != null && lastStatement.BlockKeyword.IsKind(SyntaxKind.SelectKeyword)) { statements = SyntaxFactory.List(statements.Take(statements.Count - 1)); } if (condition == null) { defaultElse = SyntaxFactory.ElseBlock() .AddStatements(statements.ToArray()) .WithLeadingTrivia(block.GetLeadingTrivia()) .WithTrailingTrivia(block.GetTrailingTrivia()); break; } else if (ifBlock == null) { ifBlock = SyntaxFactory.MultiLineIfBlock(SyntaxFactory.IfStatement(condition).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword))) .WithStatements(statements); } else { elseIfs.Add(SyntaxFactory.ElseIfBlock(SyntaxFactory.ElseIfStatement(condition).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)), statements)); } } ifBlock = ifBlock.WithElseIfBlocks(SyntaxFactory.List(elseIfs)); if (defaultElse != null) { ifBlock = ifBlock.WithElseBlock(defaultElse); } ifBlock = ifBlock.WithLeadingTrivia(node.GetLeadingTrivia().Concat(ifBlock.GetLeadingTrivia())).WithTrailingTrivia(node.GetTrailingTrivia()) .WithElseBlock(defaultElse).WithAdditionalAnnotations(Formatter.Annotation); return(Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(node, ifBlock)))); }) ); }