public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison)) { await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractExpressionFromCondition) && context.Span.IsBetweenSpans(expression)) { ExtractConditionRefactoring.ComputeRefactoring(context, expression); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ParenthesizeExpression) && context.Span.IsBetweenSpans(expression) && ParenthesizeExpressionRefactoring.CanRefactor(context, expression)) { context.RegisterRefactoring( $"Parenthesize '{expression}'", cancellationToken => ParenthesizeExpressionRefactoring.RefactorAsync(context.Document, expression, cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceNullLiteralExpressionWithDefaultExpression)) { await ReplaceNullLiteralExpressionWithDefaultExpressionRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithExpression)) { ReplaceConditionalExpressionWithExpressionRefactoring.ComputeRefactoring(context, expression); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckExpressionForNull) && context.Span.IsContainedInSpanOrBetweenSpans(expression)) { await CheckExpressionForNullRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceExpressionWithConstantValue) && context.Span.IsBetweenSpans(expression)) { await ReplaceExpressionWithConstantValueRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, WhileStatementSyntax whileStatement) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison) && whileStatement.Condition != null && whileStatement.Condition.Span.Contains(context.Span) && context.SupportsSemanticModel) { await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, whileStatement.Condition).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceWhileStatementWithDoStatement) && (whileStatement.WhileKeyword.Span.Contains(context.Span))) { context.RegisterRefactoring( "Replace while with do", cancellationToken => { return(ReplaceWhileStatementWithDoStatementRefactoring.RefactorAsync( context.Document, whileStatement, cancellationToken)); }); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.SwapStatementsInIfElse, RefactoringIdentifiers.ReplaceIfElseWithConditionalExpression) && IfElseAnalysis.IsTopmostIf(ifStatement) && context.Span.IsBetweenSpans(ifStatement)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithConditionalExpression)) { ReplaceIfElseWithConditionalExpressionRefactoring.ComputeRefactoring(context, ifStatement); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse) && SwapStatementInIfElseRefactoring.CanRefactor(ifStatement)) { context.RegisterRefactoring( "Swap statements in if-else", cancellationToken => { return(SwapStatementInIfElseRefactoring.RefactorAsync( context.Document, ifStatement, cancellationToken)); }); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison) && ifStatement.Condition != null && ifStatement.Condition.Span.Contains(context.Span) && context.SupportsSemanticModel) { await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, ifStatement.Condition).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression) { if (expression != null && context.SupportsSemanticModel) { MemberDeclarationSyntax declaration = GetContainingMethodOrPropertyOrIndexer(expression); if (declaration != null) { TypeSyntax memberType = GetMemberType(declaration); if (memberType != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol memberTypeSymbol = semanticModel .GetTypeInfo(memberType, context.CancellationToken) .Type; if (memberTypeSymbol != null) { ITypeSymbol expressionSymbol = semanticModel .GetTypeInfo(expression, context.CancellationToken) .Type; if (expressionSymbol?.IsErrorType() == false) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison) && memberTypeSymbol.IsBoolean() && expressionSymbol.IsNamedType()) { var namedTypeSymbol = (INamedTypeSymbol)expressionSymbol; if (namedTypeSymbol?.IsNullableOf(SpecialType.System_Boolean) == true) { AddBooleanComparisonRefactoring.RegisterRefactoring(context, expression); } } ISymbol memberSymbol = semanticModel.GetDeclaredSymbol(declaration, context.CancellationToken); if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToReturnExpression)) { ITypeSymbol newType = GetMemberNewType(memberSymbol, memberTypeSymbol, expression, expressionSymbol, semanticModel, context.CancellationToken); if (newType?.IsErrorType() == false && !memberTypeSymbol.Equals(newType)) { if (newType.IsNamedType() && memberTypeSymbol.IsNamedType()) { var newNamedType = (INamedTypeSymbol)newType; INamedTypeSymbol orderedEnumerableSymbol = semanticModel.Compilation.GetTypeByMetadataName("System.Linq.IOrderedEnumerable`1"); if (newNamedType.ConstructedFrom == orderedEnumerableSymbol) { INamedTypeSymbol enumerableSymbol = semanticModel.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1"); if (enumerableSymbol != null && ((INamedTypeSymbol)memberTypeSymbol).ConstructedFrom != enumerableSymbol) { RegisterChangeType(context, declaration, memberType, enumerableSymbol.Construct(newNamedType.TypeArguments.ToArray())); } } } RegisterChangeType(context, declaration, memberType, newType); } } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && !memberTypeSymbol.IsErrorType()) { ITypeSymbol castTypeSymbol = GetCastTypeSymbol(memberSymbol, memberTypeSymbol, expressionSymbol, semanticModel); if (castTypeSymbol != null) { ModifyExpressionRefactoring.ComputeRefactoring( context, expression, castTypeSymbol, semanticModel); } } } } } } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, BinaryExpressionSyntax binaryExpression) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.NegateOperator)) { SyntaxToken operatorToken = binaryExpression.OperatorToken; if (operatorToken.Span.Contains(context.Span) && NegateOperatorRefactoring.CanBeNegated(operatorToken)) { context.RegisterRefactoring( "Negate operator", cancellationToken => NegateOperatorRefactoring.RefactorAsync(context.Document, operatorToken, cancellationToken)); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison) && binaryExpression.IsKind(SyntaxKind.LogicalAndExpression, SyntaxKind.LogicalOrExpression) && binaryExpression.Left?.IsMissing == false && binaryExpression.Right?.IsMissing == false && context.SupportsSemanticModel) { if (binaryExpression.Left.Span.Contains(context.Span)) { await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, binaryExpression.Left).ConfigureAwait(false); } else if (binaryExpression.Right.Span.Contains(context.Span)) { await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, binaryExpression.Right).ConfigureAwait(false); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatBinaryExpression)) { FormatBinaryExpressionRefactoring.ComputeRefactorings(context, binaryExpression); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.NegateBinaryExpression)) { NegateBinaryExpressionRefactoring.ComputeRefactoring(context, binaryExpression); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandCoalesceExpression) && binaryExpression.OperatorToken.Span.Contains(context.Span) && ExpandCoalesceExpressionRefactoring.CanRefactor(binaryExpression)) { context.RegisterRefactoring( "Expand ??", cancellationToken => { return(ExpandCoalesceExpressionRefactoring.RefactorAsync( context.Document, binaryExpression, cancellationToken)); }); } if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.MergeStringLiterals, RefactoringIdentifiers.MergeStringLiteralsIntoMultilineStringLiteral) && MergeStringLiteralsRefactoring.CanRefactor(context, binaryExpression)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeStringLiterals)) { context.RegisterRefactoring( "Merge string literals", cancellationToken => MergeStringLiteralsRefactoring.RefactorAsync(context.Document, binaryExpression, cancellationToken: cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeStringLiteralsIntoMultilineStringLiteral) && binaryExpression .DescendantTrivia(binaryExpression.Span) .Any(f => f.IsKind(SyntaxKind.EndOfLineTrivia))) { context.RegisterRefactoring( "Merge string literals into multiline string literal", cancellationToken => MergeStringLiteralsRefactoring.RefactorAsync(context.Document, binaryExpression, multiline: true, cancellationToken: cancellationToken)); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInBinaryExpression) && SwapExpressionsRefactoring.CanRefactor(binaryExpression) && context.Span.IsBetweenSpans(binaryExpression)) { context.RegisterRefactoring( "Swap expressions", cancellationToken => { return(SwapExpressionsRefactoring.RefactorAsync( context.Document, binaryExpression, cancellationToken)); }); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceAsWithCast)) { ReplaceAsWithCastRefactoring.ComputeRefactoring(context, binaryExpression); } if (context.Span.IsContainedInSpanOrBetweenSpans(binaryExpression.OperatorToken) && context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ReplaceEqualsExpressionWithStringEquals, RefactoringIdentifiers.ReplaceEqualsExpressionWithStringIsNullOrWhiteSpace)) { await ReplaceEqualsExpressionRefactoring.ComputeRefactoringsAsync(context, binaryExpression).ConfigureAwait(false); } }