private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { StatementSyntax statement = ifStatement.Statement.WithoutTrivia(); ExpressionSyntax condition = ifStatement.Condition; BinaryExpressionChainInfo info = SyntaxInfo.BinaryExpressionChainInfo((BinaryExpressionSyntax)condition); var ifStatements = new List <IfStatementSyntax>(); foreach (ExpressionSyntax expression in info.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 (ifStatement.IsEmbedded()) { 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, MemberInvocationExpressionInfo invocationInfo, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); InvocationExpressionSyntax invocation = invocationInfo.InvocationExpression; InvocationExpressionSyntax newInvocation = null; bool isAppendLine = string.Equals(invocationInfo.NameText, "AppendLine", StringComparison.Ordinal); ExpressionSyntax expression = argument.Expression; switch (expression.Kind()) { case SyntaxKind.InterpolatedStringExpression: { newInvocation = ConvertInterpolatedStringExpressionToInvocationExpression((InterpolatedStringExpressionSyntax)argument.Expression, invocationInfo, semanticModel); break; } case SyntaxKind.AddExpression: { ImmutableArray <ExpressionSyntax> expressions = SyntaxInfo.BinaryExpressionChainInfo((BinaryExpressionSyntax)expression).Expressions; newInvocation = invocation .ReplaceNode(invocationInfo.Name, IdentifierName("Append").WithTriviaFrom(invocationInfo.Name)) .WithArgumentList(invocation.ArgumentList.WithArguments(SingletonSeparatedList(Argument(expressions[0]))).WithoutTrailingTrivia()); for (int i = 1; i < expressions.Length; i++) { ExpressionSyntax argumentExpression = expressions[i]; string methodName; if (i == expressions.Length - 1 && isAppendLine && semanticModel .GetTypeInfo(argumentExpression, cancellationToken) .ConvertedType? .SpecialType == SpecialType.System_String) { methodName = "AppendLine"; } else { methodName = "Append"; } newInvocation = SimpleMemberInvocationExpression( newInvocation, IdentifierName(methodName), ArgumentList(Argument(argumentExpression))); if (i == expressions.Length - 1 && isAppendLine && !string.Equals(methodName, "AppendLine", StringComparison.Ordinal)) { newInvocation = SimpleMemberInvocationExpression( newInvocation, IdentifierName("AppendLine"), ArgumentList()); } } 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)); }