private static async Task AddCastExpressionAsync( RefactoringContext context, VariableDeclarationSyntax variableDeclaration) { if (variableDeclaration.Type?.IsVar == false) { VariableDeclaratorSyntax declarator = variableDeclaration.Variables .FirstOrDefault(f => f.Initializer?.Value?.Span.Contains(context.Span) == true); if (declarator != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol declarationType = semanticModel.GetTypeSymbol(variableDeclaration.Type, context.CancellationToken); if (declarationType?.IsErrorType() == false) { ITypeSymbol expressionType = semanticModel.GetTypeSymbol(declarator.Initializer.Value, context.CancellationToken); if (expressionType?.IsErrorType() == false && !declarationType.Equals(expressionType)) { ModifyExpressionRefactoring.ComputeRefactoring(context, declarator.Initializer.Value, declarationType, semanticModel); } } } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod) && yieldStatement.Kind() == SyntaxKind.YieldReturnStatement) { ExpressionSyntax expression = yieldStatement.Expression; if (expression?.IsMissing == false && expression.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); (ISymbol memberSymbol, ITypeSymbol memberTypeSymbol) = ReturnExpressionRefactoring.GetContainingSymbolAndType(expression, semanticModel, context.CancellationToken); if (memberSymbol != null && (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol) && namedTypeSymbol.SpecialType != SpecialType.System_Collections_IEnumerable && namedTypeSymbol.OriginalDefinition.IsIEnumerableOfT()) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; ITypeSymbol expressionTypeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken); if (argumentSymbol != expressionTypeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, expression, argumentSymbol, semanticModel, addCastExpression: false); } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ConvertReturnToIf) && (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword) || context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword) || context.Span.IsBetweenSpans(yieldStatement))) { await ConvertReturnToIfRefactoring.ConvertYieldReturnToIfElse.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield) && yieldStatement.IsYieldReturn() && context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword)) { SyntaxNode declaration = yieldStatement.FirstAncestor(SyntaxKind.MethodDeclaration, SyntaxKind.LocalFunctionStatement, SyntaxKind.GetAccessorDeclaration, ascendOutOfTrivia: false); Debug.Assert(declaration != null); if (declaration != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, declaration, semanticModel); } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ArgumentSyntax argument) { if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod)) { ExpressionSyntax expression = argument.Expression; if (expression?.IsMissing == false) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(expression).ConvertedType; if (typeSymbol?.IsErrorType() == false) { IEnumerable <ITypeSymbol> newTypes = DetermineParameterTypes(argument, semanticModel, context.CancellationToken) .Where(f => !typeSymbol.Equals(f)); ModifyExpressionRefactoring.ComputeRefactoring(context, expression, newTypes, semanticModel); } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodGroupWithLambda)) { await ReplaceMethodGroupWithLambdaRefactoring.ComputeRefactoringAsync(context, argument).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, CaseSwitchLabelSyntax caseLabel) { if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod)) { ExpressionSyntax value = caseLabel.Value; if (value?.Span.Contains(context.Span) == true) { var switchStatement = caseLabel.Parent?.Parent as SwitchStatementSyntax; if (switchStatement != null) { ExpressionSyntax expression = switchStatement.Expression; if (expression?.IsMissing == false) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken); if (typeSymbol?.IsErrorType() == false) { ModifyExpressionRefactoring.ComputeRefactoring(context, value, typeSymbol, semanticModel); } } } } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, AssignmentExpressionSyntax assignmentExpression) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandAssignmentExpression) && assignmentExpression.OperatorToken.Span.Contains(context.Span) && ExpandAssignmentExpressionRefactoring.CanRefactor(assignmentExpression)) { context.RegisterRefactoring( "Expand assignment", cancellationToken => { return(ExpandAssignmentExpressionRefactoring.RefactorAsync( context.Document, assignmentExpression, cancellationToken)); }); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && assignmentExpression.IsKind(SyntaxKind.SimpleAssignmentExpression)) { ExpressionSyntax left = assignmentExpression.Left; ExpressionSyntax right = assignmentExpression.Right; if (left?.IsMissing == false && right?.IsMissing == false && right.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol leftSymbol = semanticModel.GetTypeSymbol(left, context.CancellationToken); if (leftSymbol?.IsErrorType() == false) { ITypeSymbol rightSymbol = semanticModel.GetTypeSymbol(right, context.CancellationToken); if (rightSymbol?.IsErrorType() == false && !leftSymbol.Equals(rightSymbol)) { ModifyExpressionRefactoring.ComputeRefactoring(context, right, leftSymbol, semanticModel); } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodGroupWithLambda) && context.SupportsSemanticModel) { await ReplaceMethodGroupWithLambdaRefactoring.ComputeRefactoringAsync(context, assignmentExpression).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, AssignmentExpressionSyntax assignmentExpression) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandCompoundAssignmentOperator) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(assignmentExpression.OperatorToken) && CSharpFacts.IsCompoundAssignmentExpression(assignmentExpression.Kind()) && SyntaxInfo.AssignmentExpressionInfo(assignmentExpression).Success) { context.RegisterRefactoring( $"Expand {assignmentExpression.OperatorToken}", ct => ExpandCompoundAssignmentOperatorRefactoring.RefactorAsync(context.Document, assignmentExpression, ct)); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && assignmentExpression.IsKind(SyntaxKind.SimpleAssignmentExpression)) { SimpleAssignmentExpressionInfo simpleAssignment = SyntaxInfo.SimpleAssignmentExpressionInfo(assignmentExpression); ExpressionSyntax right = simpleAssignment.Right; if (simpleAssignment.Success && right.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol leftSymbol = semanticModel.GetTypeSymbol(simpleAssignment.Left, context.CancellationToken); if (leftSymbol?.IsErrorType() == false) { ITypeSymbol rightSymbol = semanticModel.GetTypeSymbol(right, context.CancellationToken); if (rightSymbol?.IsErrorType() == false && !leftSymbol.Equals(rightSymbol)) { ModifyExpressionRefactoring.ComputeRefactoring(context, right, leftSymbol, semanticModel); } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodGroupWithLambda) && context.SupportsSemanticModel) { await ReplaceMethodGroupWithLambdaRefactoring.ComputeRefactoringAsync(context, assignmentExpression).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod) && yieldStatement.Kind() == SyntaxKind.YieldReturnStatement) { ExpressionSyntax expression = yieldStatement.Expression; if (expression?.IsMissing == false && expression.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); (ISymbol memberSymbol, ITypeSymbol memberTypeSymbol) = ReturnExpressionRefactoring.GetContainingSymbolAndType(expression, semanticModel, context.CancellationToken); if (memberSymbol != null && (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol) && namedTypeSymbol.SpecialType != SpecialType.System_Collections_IEnumerable && namedTypeSymbol.OriginalDefinition.IsIEnumerableOfT()) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; ITypeSymbol expressionTypeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken); if (argumentSymbol != expressionTypeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, expression, argumentSymbol, semanticModel, addCastExpression: false); } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfElse) && (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword) || context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword) || context.Span.IsBetweenSpans(yieldStatement))) { await ReplaceStatementWithIfStatementRefactoring.ReplaceYieldReturnWithIfElse.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression) { if (expression == null) { return; } if (expression.IsMissing) { return; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); (ISymbol memberSymbol, ITypeSymbol memberTypeSymbol) = GetContainingSymbolAndType(expression, semanticModel, context.CancellationToken); if (memberSymbol == null) { return; } if (memberTypeSymbol?.IsErrorType() != false) { return; } ITypeSymbol expressionSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken); if (expressionSymbol?.IsErrorType() != false) { return; } ITypeSymbol castTypeSymbol = GetCastTypeSymbol(memberSymbol, memberTypeSymbol, expressionSymbol); if (castTypeSymbol == null) { return; } ModifyExpressionRefactoring.ComputeRefactoring(context, expression, castTypeSymbol, semanticModel, addCastExpression: false); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ArgumentSyntax argument) { if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.AddToMethodInvocation) && argument.Expression?.IsMissing == false && context.SupportsSemanticModel) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(argument.Expression).ConvertedType; if (typeSymbol?.IsErrorType() == false) { IEnumerable <ITypeSymbol> newTypes = argument .DetermineParameterTypes(semanticModel, context.CancellationToken) .Where(f => !typeSymbol.Equals(f)); ModifyExpressionRefactoring.ComputeRefactoring(context, argument.Expression, newTypes, semanticModel); } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression) { if (expression != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol memberSymbol = GetContainingMethodOrPropertySymbol(expression, semanticModel, context.CancellationToken); if (memberSymbol != null) { SyntaxNode node = await memberSymbol .DeclaringSyntaxReferences[0] .GetSyntaxAsync(context.CancellationToken) .ConfigureAwait(false); var declaration = node as MemberDeclarationSyntax; if (declaration != null) { TypeSyntax memberType = GetMemberType(declaration); if (memberType != null) { ITypeSymbol memberTypeSymbol = semanticModel.GetTypeSymbol(memberType, context.CancellationToken); if (memberTypeSymbol != null) { ITypeSymbol expressionSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken); if (expressionSymbol?.IsErrorType() == false) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToReturnExpression)) { ITypeSymbol newType = GetMemberNewType(memberSymbol, memberTypeSymbol, expression, expressionSymbol, semanticModel, context.CancellationToken); if (newType?.IsErrorType() == false && !memberTypeSymbol.Equals(newType) && !memberSymbol.IsOverride && !memberSymbol.ImplementsInterfaceMember()) { if (newType.IsNamedType() && memberTypeSymbol.IsNamedType()) { var newNamedType = (INamedTypeSymbol)newType; INamedTypeSymbol orderedEnumerableSymbol = semanticModel.GetTypeByMetadataName(MetadataNames.System_Linq_IOrderedEnumerable_T); if (newNamedType.ConstructedFrom == orderedEnumerableSymbol) { INamedTypeSymbol enumerableSymbol = semanticModel.GetTypeByMetadataName(MetadataNames.System_Collections_Generic_IEnumerable_T); if (enumerableSymbol != null && ((INamedTypeSymbol)memberTypeSymbol).ConstructedFrom != enumerableSymbol) { RegisterChangeType(context, declaration, memberType, enumerableSymbol.Construct(newNamedType.TypeArguments.ToArray()), semanticModel); } } } RegisterChangeType(context, declaration, memberType, newType, semanticModel); } } 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, YieldStatementSyntax yieldStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.IsYieldReturn() && yieldStatement.Expression != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol memberSymbol = ReturnExpressionRefactoring.GetContainingMethodOrPropertySymbol(yieldStatement.Expression, semanticModel, context.CancellationToken); if (memberSymbol != null) { SyntaxNode node = await memberSymbol .DeclaringSyntaxReferences[0] .GetSyntaxAsync(context.CancellationToken) .ConfigureAwait(false); TypeSyntax type = ReturnExpressionRefactoring.GetTypeOrReturnType(node); if (type != null) { ITypeSymbol memberTypeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); if (memberTypeSymbol?.SpecialType != SpecialType.System_Collections_IEnumerable) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(yieldStatement.Expression, context.CancellationToken); if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) && typeSymbol?.IsErrorType() == false && !typeSymbol.IsVoid() && !memberSymbol.IsOverride && (memberTypeSymbol == null || memberTypeSymbol.IsErrorType() || !memberTypeSymbol.IsConstructedFromIEnumerableOfT() || !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol))) { INamedTypeSymbol newTypeSymbol = semanticModel .Compilation .GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T) .Construct(typeSymbol); TypeSyntax newType = newTypeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart); context.RegisterRefactoring( $"Change {ReturnExpressionRefactoring.GetText(node)} type to '{SymbolDisplay.GetMinimalString(newTypeSymbol, semanticModel, type.SpanStart)}'", cancellationToken => { return ChangeTypeRefactoring.ChangeTypeAsync( context.Document, type, newType, cancellationToken); }); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.Expression.Span.Contains(context.Span) && memberTypeSymbol?.IsNamedType() == true) { var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol; if (namedTypeSymbol.IsConstructedFromIEnumerableOfT()) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; if (argumentSymbol != typeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, yieldStatement.Expression, argumentSymbol, semanticModel); } } } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfElse) && (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword) || context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword) || context.Span.IsBetweenSpans(yieldStatement))) { await ReplaceStatementWithIfStatementRefactoring.ReplaceYieldReturnWithIfElse.ComputeRefactoringAsync(context, yieldStatement).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, YieldStatementSyntax yieldStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod, RefactoringIdentifiers.CreateConditionFromBooleanExpression) && yieldStatement.IsYieldReturn() && yieldStatement.Expression != null && context.SupportsSemanticModel) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod)) { MemberDeclarationSyntax containingMember = ReturnExpressionRefactoring.GetContainingMethodOrPropertyOrIndexer(yieldStatement.Expression); if (containingMember != null) { TypeSyntax memberType = ReturnExpressionRefactoring.GetMemberType(containingMember); if (memberType != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol memberTypeSymbol = semanticModel .GetTypeInfo(memberType, context.CancellationToken) .Type; if (memberTypeSymbol?.SpecialType != SpecialType.System_Collections_IEnumerable) { ITypeSymbol typeSymbol = semanticModel .GetTypeInfo(yieldStatement.Expression, context.CancellationToken) .Type; if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) && typeSymbol?.IsErrorType() == false && (memberTypeSymbol == null || memberTypeSymbol.IsErrorType() || !SyntaxAnalyzer.IsGenericIEnumerable(memberTypeSymbol) || !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol))) { TypeSyntax newType = QualifiedName( ParseName("System.Collections.Generic"), GenericName( Identifier("IEnumerable"), TypeArgumentList( SingletonSeparatedList( CSharpFactory.Type(typeSymbol))))); context.RegisterRefactoring( $"Change {ReturnExpressionRefactoring.GetText(containingMember)} type to 'IEnumerable<{typeSymbol.ToDisplayString(SyntaxUtility.DefaultSymbolDisplayFormat)}>'", cancellationToken => { return(ChangeTypeRefactoring.ChangeTypeAsync( context.Document, memberType, newType, cancellationToken)); }); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.Expression.Span.Contains(context.Span) && memberTypeSymbol?.IsNamedType() == true) { var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol; if (namedTypeSymbol.ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; if (argumentSymbol != typeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, yieldStatement.Expression, argumentSymbol, semanticModel); } } } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CreateConditionFromBooleanExpression)) { await CreateConditionFromBooleanExpressionRefactoring.ComputeRefactoringAsync(context, yieldStatement.Expression).ConfigureAwait(false); } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.IsYieldReturn() && yieldStatement.Expression != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); MemberDeclarationSyntax containingMember = await ReturnExpressionRefactoring.GetContainingMethodOrPropertyOrIndexerAsync(yieldStatement.Expression, semanticModel, context.CancellationToken).ConfigureAwait(false); if (containingMember != null) { TypeSyntax memberType = ReturnExpressionRefactoring.GetMemberType(containingMember); if (memberType != null) { ITypeSymbol memberTypeSymbol = semanticModel.GetTypeSymbol(memberType, context.CancellationToken); if (memberTypeSymbol?.SpecialType != SpecialType.System_Collections_IEnumerable) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(yieldStatement.Expression, context.CancellationToken); if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) && typeSymbol?.IsErrorType() == false && !typeSymbol.IsVoid() && (memberTypeSymbol == null || memberTypeSymbol.IsErrorType() || !memberTypeSymbol.IsConstructedFromIEnumerableOfT() || !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol))) { INamedTypeSymbol newTypeSymbol = semanticModel .Compilation .GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T) .Construct(typeSymbol); TypeSyntax newType = CSharpFactory.Type(newTypeSymbol, semanticModel, memberType.SpanStart); context.RegisterRefactoring( $"Change {ReturnExpressionRefactoring.GetText(containingMember)} type to '{SymbolDisplay.GetMinimalDisplayString(newTypeSymbol, memberType.SpanStart, semanticModel)}'", cancellationToken => { return(ChangeTypeRefactoring.ChangeTypeAsync( context.Document, memberType, newType, cancellationToken)); }); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.Expression.Span.Contains(context.Span) && memberTypeSymbol?.IsNamedType() == true) { var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol; if (namedTypeSymbol.IsConstructedFromIEnumerableOfT()) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; if (argumentSymbol != typeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, yieldStatement.Expression, argumentSymbol, semanticModel); } } } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfStatement) && context.Span.IsBetweenSpans(yieldStatement)) { var refactoring = new ReplaceYieldStatementWithIfStatementRefactoring(); await refactoring.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false); } }