private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { StatementSyntax statement = ifStatement.Statement.WithoutTrivia(); ExpressionSyntax condition = ifStatement.Condition; var logicalOr = (BinaryExpressionSyntax)condition; BinaryExpressionChain chain = BinaryExpressionChain.Create((BinaryExpressionSyntax)condition); var ifStatements = new List <IfStatementSyntax>(); foreach (ExpressionSyntax expression in chain.Expressions) { ifStatements.Add(SyntaxFactory.IfStatement(expression.TrimTrivia(), statement).WithFormatterAnnotation()); } ifStatements[0] = ifStatements[0].WithLeadingTrivia(ifStatement.GetLeadingTrivia()); ifStatements[ifStatements.Count - 1] = ifStatements[ifStatements.Count - 1].WithTrailingTrivia(ifStatement.GetTrailingTrivia()); if (EmbeddedStatementHelper.IsEmbeddedStatement(ifStatement)) { BlockSyntax block = SyntaxFactory.Block(ifStatements); return(document.ReplaceNodeAsync(ifStatement, block, cancellationToken)); } else { return(document.ReplaceNodeAsync(ifStatement, ifStatements, cancellationToken)); } }
public static async Task <Document> RefactorAsync( Document document, ArgumentSyntax argument, MemberInvocationExpression memberInvocation, CancellationToken cancellationToken) { InvocationExpressionSyntax invocation = memberInvocation.InvocationExpression; InvocationExpressionSyntax newInvocation = null; bool isAppendLine = string.Equals(memberInvocation.NameText, "AppendLine", StringComparison.Ordinal); ExpressionSyntax expression = argument.Expression; switch (expression.Kind()) { case SyntaxKind.InterpolatedStringExpression: { newInvocation = ConvertInterpolatedStringExpressionToInvocationExpression((InterpolatedStringExpressionSyntax)argument.Expression, memberInvocation); break; } case SyntaxKind.AddExpression: { ImmutableArray <ExpressionSyntax> expressions = BinaryExpressionChain.Create((BinaryExpressionSyntax)expression).Expressions; newInvocation = invocation .ReplaceNode(memberInvocation.Name, IdentifierName("Append").WithTriviaFrom(memberInvocation.Name)) .WithArgumentList(invocation.ArgumentList.WithArguments(SingletonSeparatedList(Argument(expressions[0]))).WithoutTrailingTrivia()); SemanticModel semanticModel = null; for (int i = 1; i < expressions.Length; i++) { string name = (i == expressions.Length - 1 && isAppendLine) ? "AppendLine" : "Append"; ExpressionSyntax argumentExpression = expressions[i]; if (isAppendLine) { if (semanticModel == null) { semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); } if (semanticModel .GetTypeInfo(argumentExpression, cancellationToken) .ConvertedType? .IsString() != true) { argumentExpression = SimpleMemberInvocationExpression(argumentExpression.Parenthesize(), IdentifierName("ToString")); } } newInvocation = SimpleMemberInvocationExpression( newInvocation, IdentifierName(name), ArgumentList(Argument(argumentExpression))); } break; } default: { newInvocation = CreateInvocationExpression( (InvocationExpressionSyntax)expression, invocation); if (isAppendLine) { newInvocation = SimpleMemberInvocationExpression(newInvocation, IdentifierName("AppendLine"), ArgumentList()); } break; } } newInvocation = newInvocation .WithTriviaFrom(invocation) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(invocation, newInvocation, cancellationToken).ConfigureAwait(false)); }