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)); } }
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); } }
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)))); }) ); }