internal static void ComputeRefactorings(RefactoringContext context, UnsafeStatementSyntax node) { if (context.IsRefactoringEnabled(RefactoringDescriptors.MoveUnsafeContextToContainingDeclaration)) { MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, node); } }
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)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember)) { context.RegisterRefactoring( "Duplicate local function", cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken)); } 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?.Span.Contains(context.Span) == true && UseExpressionBodiedMemberAnalysis.IsFixable(localFunctionStatement)) { context.RegisterRefactoring( "Use expression-bodied member", cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken)); } 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, 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.ChangeMethodReturnTypeToVoid) && context.Span.IsEmptyAndContainedInSpan(localFunctionStatement)) { await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, localFunctionStatement).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter)) { AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) && UseExpressionBodiedMemberRefactoring.CanRefactor(localFunctionStatement, context.Span)) { context.RegisterRefactoring( UseExpressionBodiedMemberRefactoring.Title, cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken), RefactoringIdentifiers.UseExpressionBodiedMember); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MoveUnsafeContextToContainingDeclaration)) { MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, localFunctionStatement); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, MemberDeclarationSyntax member) { SyntaxKind kind = member.Kind(); switch (kind) { case SyntaxKind.MethodDeclaration: case SyntaxKind.IndexerDeclaration: case SyntaxKind.PropertyDeclaration: case SyntaxKind.OperatorDeclaration: case SyntaxKind.ConversionOperatorDeclaration: case SyntaxKind.ConstructorDeclaration: case SyntaxKind.EventDeclaration: case SyntaxKind.NamespaceDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.EnumDeclaration: { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.RemoveMember, RefactoringIdentifiers.DuplicateMember, RefactoringIdentifiers.CommentOutMember) && BraceContainsSpan(member, context.Span)) { if (member.IsParentKind( SyntaxKind.NamespaceDeclaration, SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.CompilationUnit)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveMember)) { context.RegisterRefactoring(CodeActionFactory.RemoveMemberDeclaration(context.Document, member, equivalenceKey: RefactoringIdentifiers.RemoveMember)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember)) { context.RegisterRefactoring( $"Duplicate {CSharpFacts.GetTitle(member)}", cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, member, cancellationToken), RefactoringIdentifiers.DuplicateMember); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CommentOutMember)) { CommentOutRefactoring.RegisterRefactoring(context, member); } } break; } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveAllStatements)) { RemoveAllStatementsRefactoring.ComputeRefactoring(context, member); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveAllMemberDeclarations)) { RemoveAllMemberDeclarationsRefactoring.ComputeRefactoring(context, member); } if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.SwapMemberDeclarations, RefactoringIdentifiers.RemoveMemberDeclarations) && !member.Span.IntersectsWith(context.Span)) { MemberDeclarationsRefactoring.ComputeRefactoring(context, member); } switch (kind) { case SyntaxKind.NamespaceDeclaration: { var namespaceDeclaration = (NamespaceDeclarationSyntax)member; NamespaceDeclarationRefactoring.ComputeRefactorings(context, namespaceDeclaration); if (MemberDeclarationListSelection.TryCreate(namespaceDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.ClassDeclaration: { var classDeclaration = (ClassDeclarationSyntax)member; await ClassDeclarationRefactoring.ComputeRefactoringsAsync(context, classDeclaration).ConfigureAwait(false); if (MemberDeclarationListSelection.TryCreate(classDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.StructDeclaration: { var structDeclaration = (StructDeclarationSyntax)member; await StructDeclarationRefactoring.ComputeRefactoringsAsync(context, structDeclaration).ConfigureAwait(false); if (MemberDeclarationListSelection.TryCreate(structDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.InterfaceDeclaration: { var interfaceDeclaration = (InterfaceDeclarationSyntax)member; InterfaceDeclarationRefactoring.ComputeRefactorings(context, interfaceDeclaration); if (MemberDeclarationListSelection.TryCreate(interfaceDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.EnumDeclaration: { await EnumDeclarationRefactoring.ComputeRefactoringAsync(context, (EnumDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.EnumMemberDeclaration: { await EnumMemberDeclarationRefactoring.ComputeRefactoringAsync(context, (EnumMemberDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.DelegateDeclaration: { DelegateDeclarationRefactoring.ComputeRefactorings(context, (DelegateDeclarationSyntax)member); break; } case SyntaxKind.MethodDeclaration: { await MethodDeclarationRefactoring.ComputeRefactoringsAsync(context, (MethodDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.ConstructorDeclaration: { await ConstructorDeclarationRefactoring.ComputeRefactoringsAsync(context, (ConstructorDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.DestructorDeclaration: { DestructorDeclarationRefactoring.ComputeRefactorings(context, (DestructorDeclarationSyntax)member); break; } case SyntaxKind.IndexerDeclaration: { await IndexerDeclarationRefactoring.ComputeRefactoringsAsync(context, (IndexerDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.PropertyDeclaration: { await PropertyDeclarationRefactoring.ComputeRefactoringsAsync(context, (PropertyDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.OperatorDeclaration: { ComputeRefactorings(context, (OperatorDeclarationSyntax)member); break; } case SyntaxKind.ConversionOperatorDeclaration: { ComputeRefactorings(context, (ConversionOperatorDeclarationSyntax)member); break; } case SyntaxKind.FieldDeclaration: { await FieldDeclarationRefactoring.ComputeRefactoringsAsync(context, (FieldDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.EventDeclaration: { await EventDeclarationRefactoring.ComputeRefactoringsAsync(context, (EventDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.EventFieldDeclaration: { await EventFieldDeclarationRefactoring.ComputeRefactoringsAsync(context, (EventFieldDeclarationSyntax)member).ConfigureAwait(false); break; } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MoveUnsafeContextToContainingDeclaration)) { MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, member); } }
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(RefactoringDescriptors.RemoveMemberDeclaration)) { context.RegisterRefactoring(CodeActionFactory.RemoveStatement(context.Document, localFunctionStatement, equivalenceKey: EquivalenceKey.Create(RefactoringDescriptors.RemoveMemberDeclaration))); } if (context.IsRefactoringEnabled(RefactoringDescriptors.CopyMemberDeclaration)) { context.RegisterRefactoring( "Copy local function", ct => CopyMemberDeclarationRefactoring.RefactorAsync( context.Document, localFunctionStatement, copyAfter: body.CloseBraceToken.Span.Contains(context.Span), ct), RefactoringDescriptors.CopyMemberDeclaration); } if (context.IsRefactoringEnabled(RefactoringDescriptors.CommentOutMemberDeclaration)) { CommentOutRefactoring.RegisterRefactoring(context, localFunctionStatement); } } } } if (context.IsRefactoringEnabled(RefactoringDescriptors.ChangeMethodReturnTypeToVoid) && context.Span.IsEmptyAndContainedInSpan(localFunctionStatement)) { await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, localFunctionStatement).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.AddGenericParameterToDeclaration)) { AddGenericParameterToDeclarationRefactoring.ComputeRefactoring(context, localFunctionStatement); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertBlockBodyToExpressionBody) && ConvertBlockBodyToExpressionBodyRefactoring.CanRefactor(localFunctionStatement, context.Span)) { context.RegisterRefactoring( ConvertBlockBodyToExpressionBodyRefactoring.Title, ct => ConvertBlockBodyToExpressionBodyRefactoring.RefactorAsync(context.Document, localFunctionStatement, ct), RefactoringDescriptors.ConvertBlockBodyToExpressionBody); } if (context.IsRefactoringEnabled(RefactoringDescriptors.MoveUnsafeContextToContainingDeclaration)) { MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, localFunctionStatement); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, MemberDeclarationSyntax member) { SyntaxKind kind = member.Kind(); switch (kind) { case SyntaxKind.MethodDeclaration: case SyntaxKind.IndexerDeclaration: case SyntaxKind.PropertyDeclaration: case SyntaxKind.OperatorDeclaration: case SyntaxKind.ConversionOperatorDeclaration: case SyntaxKind.ConstructorDeclaration: case SyntaxKind.EventDeclaration: case SyntaxKind.NamespaceDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.RecordDeclaration: case SyntaxKind.StructDeclaration: case SyntaxKind.RecordStructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.EnumDeclaration: { if (context.IsAnyRefactoringEnabled( RefactoringDescriptors.RemoveMemberDeclaration, RefactoringDescriptors.CopyMemberDeclaration, RefactoringDescriptors.CommentOutMemberDeclaration)) { (SyntaxToken openBrace, SyntaxToken closeBrace) = GetBraces(member); if ((!openBrace.IsKind(SyntaxKind.None) && openBrace.Span.Contains(context.Span)) || (!closeBrace.IsKind(SyntaxKind.None) && closeBrace.Span.Contains(context.Span))) { if (member.Parent != null && CSharpFacts.CanHaveMembers(member.Parent.Kind())) { if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveMemberDeclaration)) { context.RegisterRefactoring(CodeActionFactory.RemoveMemberDeclaration(context.Document, member, equivalenceKey: EquivalenceKey.Create(RefactoringDescriptors.RemoveMemberDeclaration))); } if (context.IsRefactoringEnabled(RefactoringDescriptors.CopyMemberDeclaration)) { context.RegisterRefactoring( $"Copy {CSharpFacts.GetTitle(member)}", ct => CopyMemberDeclarationRefactoring.RefactorAsync( context.Document, member, copyAfter: closeBrace.Span.Contains(context.Span), ct), RefactoringDescriptors.CopyMemberDeclaration); } } if (context.IsRefactoringEnabled(RefactoringDescriptors.CommentOutMemberDeclaration)) { CommentOutRefactoring.RegisterRefactoring(context, member); } } } break; } } if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveAllStatements)) { RemoveAllStatementsRefactoring.ComputeRefactoring(context, member); } if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveAllMemberDeclarations)) { RemoveAllMemberDeclarationsRefactoring.ComputeRefactoring(context, member); } if (context.IsAnyRefactoringEnabled( RefactoringDescriptors.SwapMemberDeclarations, RefactoringDescriptors.RemoveMemberDeclarations) && !member.Span.IntersectsWith(context.Span)) { MemberDeclarationsRefactoring.ComputeRefactoring(context, member); } switch (kind) { case SyntaxKind.NamespaceDeclaration: { var namespaceDeclaration = (NamespaceDeclarationSyntax)member; NamespaceDeclarationRefactoring.ComputeRefactorings(context, namespaceDeclaration); if (MemberDeclarationListSelection.TryCreate(namespaceDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.ClassDeclaration: { var classDeclaration = (ClassDeclarationSyntax)member; await ClassDeclarationRefactoring.ComputeRefactoringsAsync(context, classDeclaration).ConfigureAwait(false); if (MemberDeclarationListSelection.TryCreate(classDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.RecordDeclaration: case SyntaxKind.RecordStructDeclaration: { var recordDeclaration = (RecordDeclarationSyntax)member; await RecordDeclarationRefactoring.ComputeRefactoringsAsync(context, recordDeclaration).ConfigureAwait(false); if (MemberDeclarationListSelection.TryCreate(recordDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.StructDeclaration: { var structDeclaration = (StructDeclarationSyntax)member; await StructDeclarationRefactoring.ComputeRefactoringsAsync(context, structDeclaration).ConfigureAwait(false); if (MemberDeclarationListSelection.TryCreate(structDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.InterfaceDeclaration: { var interfaceDeclaration = (InterfaceDeclarationSyntax)member; InterfaceDeclarationRefactoring.ComputeRefactorings(context, interfaceDeclaration); if (MemberDeclarationListSelection.TryCreate(interfaceDeclaration, context.Span, out MemberDeclarationListSelection selectedMembers)) { await SelectedMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, selectedMembers).ConfigureAwait(false); } break; } case SyntaxKind.EnumDeclaration: { await EnumDeclarationRefactoring.ComputeRefactoringAsync(context, (EnumDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.EnumMemberDeclaration: { await EnumMemberDeclarationRefactoring.ComputeRefactoringAsync(context, (EnumMemberDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.DelegateDeclaration: { DelegateDeclarationRefactoring.ComputeRefactorings(context, (DelegateDeclarationSyntax)member); break; } case SyntaxKind.MethodDeclaration: { await MethodDeclarationRefactoring.ComputeRefactoringsAsync(context, (MethodDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.ConstructorDeclaration: { await ConstructorDeclarationRefactoring.ComputeRefactoringsAsync(context, (ConstructorDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.DestructorDeclaration: { DestructorDeclarationRefactoring.ComputeRefactorings(context, (DestructorDeclarationSyntax)member); break; } case SyntaxKind.IndexerDeclaration: { await IndexerDeclarationRefactoring.ComputeRefactoringsAsync(context, (IndexerDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.PropertyDeclaration: { await PropertyDeclarationRefactoring.ComputeRefactoringsAsync(context, (PropertyDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.OperatorDeclaration: { ComputeRefactorings(context, (OperatorDeclarationSyntax)member); break; } case SyntaxKind.ConversionOperatorDeclaration: { ComputeRefactorings(context, (ConversionOperatorDeclarationSyntax)member); break; } case SyntaxKind.FieldDeclaration: { await FieldDeclarationRefactoring.ComputeRefactoringsAsync(context, (FieldDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.EventDeclaration: { await EventDeclarationRefactoring.ComputeRefactoringsAsync(context, (EventDeclarationSyntax)member).ConfigureAwait(false); break; } case SyntaxKind.EventFieldDeclaration: { await EventFieldDeclarationRefactoring.ComputeRefactoringsAsync(context, (EventFieldDeclarationSyntax)member).ConfigureAwait(false); break; } } if (context.IsRefactoringEnabled(RefactoringDescriptors.MoveUnsafeContextToContainingDeclaration)) { MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, member); } }