public static async Task ComputeRefactoringsAsync(RefactoringContext context, LocalFunctionStatementSyntax localFunctionStatement) { if (localFunctionStatement.IsParentKind(SyntaxKind.Block)) { BlockSyntax body = localFunctionStatement.Body; if (body != null) { if (body.OpenBraceToken.Span.Contains(context.Span) || body.CloseBraceToken.Span.Contains(context.Span)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveMember)) { context.RegisterRefactoring( "Remove local function", cancellationToken => context.Document.RemoveStatementAsync(localFunctionStatement, cancellationToken), RefactoringIdentifiers.RemoveMember); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember)) { context.RegisterRefactoring( "Duplicate local function", cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken), RefactoringIdentifiers.DuplicateMember); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CommentOutMember)) { CommentOutRefactoring.RegisterRefactoring(context, localFunctionStatement); } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter)) { AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) && localFunctionStatement.Body != null && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(localFunctionStatement.Body) && UseExpressionBodiedMemberAnalysis.IsFixable(localFunctionStatement)) { context.RegisterRefactoring( "Use expression-bodied member", cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken), RefactoringIdentifiers.UseExpressionBodiedMember); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield) && localFunctionStatement.Identifier.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, localFunctionStatement, semanticModel); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MoveUnsafeContextToContainingDeclaration)) { MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, localFunctionStatement); } }
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.ReplaceStatementWithIfStatement) && context.Span.IsBetweenSpans(yieldStatement)) { var refactoring = new ReplaceYieldStatementWithIfStatementRefactoring(); await refactoring.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, yieldStatement, semanticModel); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, MethodDeclarationSyntax methodDeclaration) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMethodReturnTypeToVoid) && context.Span.IsEmptyAndContainedInSpan(methodDeclaration)) { await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, methodDeclaration).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter)) { AddTypeParameterRefactoring.ComputeRefactoring(context, methodDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodWithProperty) && methodDeclaration.HeaderSpan().Contains(context.Span) && ReplaceMethodWithPropertyRefactoring.CanRefactor(methodDeclaration)) { context.RegisterRefactoring( $"Replace '{methodDeclaration.Identifier.ValueText}' with property", cancellationToken => ReplaceMethodWithPropertyRefactoring.RefactorAsync(context.Document, methodDeclaration, cancellationToken), RefactoringIdentifiers.ReplaceMethodWithProperty); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) && context.SupportsCSharp6 && methodDeclaration.Body != null && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(methodDeclaration.Body) && UseExpressionBodiedMemberAnalysis.GetExpression(methodDeclaration.Body) != null) { context.RegisterRefactoring( UseExpressionBodiedMemberRefactoring.Title, cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, methodDeclaration, cancellationToken), RefactoringIdentifiers.UseExpressionBodiedMember); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) && methodDeclaration.HeaderSpan().Contains(context.Span)) { MakeMethodAbstractRefactoring.ComputeRefactoring(context, methodDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) && methodDeclaration.HeaderSpan().Contains(context.Span)) { MakeMethodVirtualRefactoring.ComputeRefactoring(context, methodDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) && methodDeclaration.HeaderSpan().Contains(context.Span)) { await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, methodDeclaration).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameMethodAccordingToTypeName)) { await RenameMethodAccoringToTypeNameAsync(context, methodDeclaration).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(methodDeclaration.Identifier)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); AddMemberToInterfaceRefactoring.ComputeRefactoring(context, methodDeclaration, semanticModel); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield) && methodDeclaration.Identifier.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, methodDeclaration, semanticModel); } }