public static async Task <Document> RefactorAsync( Document document, ConditionalExpressionSyntax conditionalExpression, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ExpressionSyntax condition = conditionalExpression.Condition; ExpressionSyntax newNode = (conditionalExpression.WhenTrue.WalkDownParentheses().IsKind(SyntaxKind.TrueLiteralExpression)) ? condition : LogicalNegationHelper.LogicallyNegate(condition, semanticModel, cancellationToken); SyntaxTriviaList trailingTrivia = conditionalExpression .DescendantTrivia(TextSpan.FromBounds(condition.Span.End, conditionalExpression.Span.End)) .ToSyntaxTriviaList() .EmptyIfWhitespace() .AddRange(conditionalExpression.GetTrailingTrivia()); newNode = newNode .WithLeadingTrivia(conditionalExpression.GetLeadingTrivia()) .WithTrailingTrivia(trailingTrivia); return(await document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken).ConfigureAwait(false)); }
public static async Task <Document> RefactorAsync( Document document, ConditionalExpressionSyntax conditionalExpression, CancellationToken cancellationToken) { ExpressionSyntax condition = conditionalExpression.Condition; ExpressionSyntax newNode = (conditionalExpression.WhenTrue.IsKind(SyntaxKind.TrueLiteralExpression)) ? condition : Negator.LogicallyNegate(condition); TextSpan span = TextSpan.FromBounds( conditionalExpression.Condition.Span.End, conditionalExpression.FullSpan.End); IEnumerable <SyntaxTrivia> trivia = conditionalExpression.DescendantTrivia(span); if (trivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { newNode = newNode.WithTrailingTrivia(trivia); } else { newNode = newNode.WithoutTrailingTrivia(); } return(await document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken).ConfigureAwait(false)); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression) { if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(conditionalExpression)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatConditionalExpression)) { if (conditionalExpression.IsSingleLine()) { context.RegisterRefactoring( "Format ?: on separate lines", ct => SyntaxFormatter.ToMultiLineAsync(context.Document, conditionalExpression, ct), RefactoringIdentifiers.FormatConditionalExpression); } else if (conditionalExpression.DescendantTrivia(conditionalExpression.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.RegisterRefactoring( "Format ?: on a single line", ct => SyntaxFormatter.ToSingleLineAsync(context.Document, conditionalExpression, ct), RefactoringIdentifiers.FormatConditionalExpression); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ConvertConditionalOperatorToIfElse)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); (CodeAction codeAction, CodeAction recursiveCodeAction) = ConvertConditionalOperatorToIfElseRefactoring.ComputeRefactoring( context.Document, conditionalExpression, new CodeActionData(ConvertConditionalOperatorToIfElseRefactoring.Title, RefactoringIdentifiers.ConvertConditionalOperatorToIfElse), new CodeActionData(ConvertConditionalOperatorToIfElseRefactoring.RecursiveTitle, ConvertConditionalOperatorToIfElseRecursiveEquivalenceKey), semanticModel, context.CancellationToken); if (codeAction != null) { context.RegisterRefactoring(codeAction); } if (recursiveCodeAction != null) { context.RegisterRefactoring(recursiveCodeAction); } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.InvertConditionalExpression) && (context.Span.IsBetweenSpans(conditionalExpression) || context.Span.IsEmptyAndContainedInSpan(conditionalExpression.QuestionToken, conditionalExpression.ColonToken)) && InvertConditionalExpressionRefactoring.CanRefactor(conditionalExpression)) { context.RegisterRefactoring( "Invert ?:", ct => InvertConditionalExpressionRefactoring.RefactorAsync(context.Document, conditionalExpression, ct), RefactoringIdentifiers.InvertConditionalExpression); } }
public static void Analyze(SyntaxNodeAnalysisContext context, ConditionalExpressionSyntax conditionalExpression) { if (conditionalExpression.Condition?.IsMissing == false && CanBeReplacedWithCoalesceExpression(conditionalExpression) && conditionalExpression .DescendantTrivia(conditionalExpression.Span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.ReportDiagnostic( DiagnosticDescriptors.UseCoalesceExpressionInsteadOfConditionalExpression, conditionalExpression); } }
public static void Analyze(SyntaxNodeAnalysisContext context, ConditionalExpressionSyntax conditionalExpression) { if (!conditionalExpression.ContainsDiagnostics && IsFixable(conditionalExpression, context.SemanticModel, context.CancellationToken) && conditionalExpression .DescendantTrivia(conditionalExpression.Span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.ReportDiagnostic( DiagnosticDescriptors.UseCoalesceExpressionInsteadOfConditionalExpression, conditionalExpression); } }
private static ExpressionSyntax CreateNewNode( ConditionalExpressionSyntax conditionalExpression, ExpressionSyntax newNode) { SyntaxTriviaList trailingTrivia = conditionalExpression .DescendantTrivia(TextSpan.FromBounds(conditionalExpression.Condition.Span.End, conditionalExpression.Span.End)) .ToSyntaxTriviaList() .EmptyIfWhitespace() .AddRange(conditionalExpression.GetTrailingTrivia()); return(newNode .WithLeadingTrivia(conditionalExpression.GetLeadingTrivia()) .WithTrailingTrivia(trailingTrivia) .WithSimplifierAnnotation()); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression) { if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(conditionalExpression)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatConditionalExpression)) { if (conditionalExpression.IsSingleLine()) { context.RegisterRefactoring( "Format ?: on separate lines", ct => SyntaxFormatter.ToMultiLineAsync(context.Document, conditionalExpression, ct), RefactoringIdentifiers.FormatConditionalExpression); } else if (conditionalExpression.DescendantTrivia(conditionalExpression.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.RegisterRefactoring( "Format ?: on a single line", ct => SyntaxFormatter.ToSingleLineAsync(context.Document, conditionalExpression, ct), RefactoringIdentifiers.FormatConditionalExpression); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithIfElse)) { await ReplaceConditionalExpressionWithIfElseRefactoring.ComputeRefactoringAsync(context, conditionalExpression).ConfigureAwait(false); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInConditionalExpression) && (context.Span.IsBetweenSpans(conditionalExpression) || context.Span.IsEmptyAndContainedInSpan(conditionalExpression.QuestionToken, conditionalExpression.ColonToken)) && SwapExpressionsInConditionalExpressionRefactoring.CanRefactor(conditionalExpression)) { context.RegisterRefactoring( "Swap expressions in ?:", ct => SwapExpressionsInConditionalExpressionRefactoring.RefactorAsync(context.Document, conditionalExpression, ct), RefactoringIdentifiers.SwapExpressionsInConditionalExpression); } }