public static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { ReturnStatementSyntax newReturnStatement = CreateReturnStatement(ifStatement); if (ifStatement.Else != null) { newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement); return(document.ReplaceNodeAsync(ifStatement, newReturnStatement, cancellationToken)); } else { var block = (BlockSyntax)ifStatement.Parent; SyntaxList <StatementSyntax> statements = block.Statements; int index = statements.IndexOf(ifStatement); var returnStatement = (ReturnStatementSyntax)statements[index + 1]; newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newReturnStatement); return(document.ReplaceNodeAsync(block, block.WithStatements(newStatements), cancellationToken)); } }
private static SyntaxNode GetNewRoot( SyntaxNode root, IfStatementSyntax ifStatement, ReturnStatementSyntax newReturnStatement) { if (ifStatement.Else != null) { newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement); return(root.ReplaceNode(ifStatement, newReturnStatement)); } else { var block = (BlockSyntax)ifStatement.Parent; int index = block.Statements.IndexOf(ifStatement); var returnStatement = (ReturnStatementSyntax)block.Statements[index + 1]; newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()); SyntaxList <StatementSyntax> statements = block.Statements .RemoveAt(index); statements = statements .Replace(statements[index], newReturnStatement); return(root.ReplaceNode(block, block.WithStatements(statements))); } }
public static async Task <Document> RefactorAsync( Document document, StatementContainer container, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement, CancellationToken cancellationToken) { ExpressionSyntax expression = ReplaceIfWithStatementRefactoring.GetExpression( ifStatement.Condition, ReplaceIfWithStatementRefactoring.GetReturnExpression(ifStatement), returnStatement.Expression); ReturnStatementSyntax newReturnStatement = ReturnStatement(expression); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatement); newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newReturnStatement); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); }
private async Task <Solution> SwitchReturnToRandomInt( Document document, MethodDeclarationSyntax methodDecl, CancellationToken cancellationToken) { var newBody = methodDecl.Body; var newBodyStatements = newBody.ChildNodes().ToArray(); var endsWithReturn = newBodyStatements.LastOrDefault() is ReturnStatementSyntax; if (endsWithReturn == false) { newBodyStatements = newBody .ChildNodes() .Concat(new[] { SF.ReturnStatement() }) .ToArray(); newBody = newBody.WithStatements(SF.List(newBodyStatements)); } var returns = newBody.DescendantNodes() .OfType <ReturnStatementSyntax>() .ToArray(); newBody = newBody.ReplaceNodes(returns, (original, updated) => { return(ReturnRandomIntSyntax .WithLeadingTrivia(original.GetLeadingTrivia()) .WithTrailingTrivia(original.GetTrailingTrivia())); }); var intTypeSyntax = SF.PredefinedType( SF.Token(SyntaxKind.IntKeyword)); var newMethodDecl = methodDecl .WithReturnType(intTypeSyntax) .WithBody(newBody); var oldClass = (ClassDeclarationSyntax)methodDecl.Parent; var newClass = oldClass.ReplaceNode(methodDecl, newMethodDecl); var newRoot = (await document.GetSyntaxRootAsync()) .ReplaceNode(oldClass, newClass); var newSolution = document.Project.Solution .RemoveDocument(document.Id) .AddDocument(document.Id, document.Name, newRoot); return(newSolution); }
public static async Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { ExpressionSyntax returnExpression = ifStatement.Condition; if (GetBooleanLiteral(ifStatement.Statement).Kind() == SyntaxKind.FalseLiteralExpression) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); returnExpression = CSharpUtility.LogicallyNegate(returnExpression, semanticModel, cancellationToken); } ReturnStatementSyntax newReturnStatement = ReturnStatement( ReturnKeyword().WithTrailingTrivia(Space), returnExpression, SemicolonToken()); if (ifStatement.Else != null) { newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement); return(await document.ReplaceNodeAsync(ifStatement, newReturnStatement, cancellationToken).ConfigureAwait(false)); } StatementContainer container = StatementContainer.Create(ifStatement); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatement); var returnStatement = (ReturnStatementSyntax)statements[index + 1]; newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newReturnStatement); //TODO: ReplaceStatementsAsync return(await document.ReplaceNodeAsync(container.Node, container.WithStatements(newStatements).Node, cancellationToken).ConfigureAwait(false)); }
private static SyntaxNode GetNewRoot( SyntaxNode root, IfStatementSyntax ifStatement, ReturnStatementSyntax newReturnStatement) { if (ifStatement.Else != null) { ReturnStatementSyntax returnStatement = SimplifyIfStatementToReturnStatementAnalyzer.GetReturnStatement(ifStatement.Statement); LiteralExpressionSyntax booleanLiteral = SimplifyIfStatementToReturnStatementAnalyzer.GetBooleanLiteral(returnStatement); newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement); return(root.ReplaceNode(ifStatement, newReturnStatement)); } else { var block = (BlockSyntax)ifStatement.Parent; int index = block.Statements.IndexOf(ifStatement); var returnStatement = (ReturnStatementSyntax)block.Statements[index + 1]; LiteralExpressionSyntax booleanLiteral = SimplifyIfStatementToReturnStatementAnalyzer.GetBooleanLiteral(returnStatement); newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()); SyntaxList <StatementSyntax> statements = block.Statements .RemoveAt(index); statements = statements .Replace(statements[index], newReturnStatement); return(root.ReplaceNode(block, block.WithStatements(statements))); } }