public static void ComputeRefactorings(RefactoringContext context, VariableDeclaratorSyntax variableDeclarator) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.InitializeFieldFromConstructor) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(variableDeclarator.Identifier)) { InitializeFieldFromConstructorRefactoring.ComputeRefactoring(context, variableDeclarator); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, VariableDeclaratorSyntax variableDeclarator) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.InitializeFieldFromConstructor) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(variableDeclarator.Identifier)) { InitializeFieldFromConstructorRefactoring.ComputeRefactoring(context, variableDeclarator); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodGroupWithLambda)) { await ReplaceMethodGroupWithLambdaRefactoring.ComputeRefactoringAsync(context, variableDeclarator).ConfigureAwait(false); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, FieldDeclarationSyntax fieldDeclaration) { if (fieldDeclaration.Modifiers.Contains(SyntaxKind.ConstKeyword)) { if (context.IsRefactoringEnabled(RefactoringDescriptors.UseReadOnlyFieldInsteadOfConstant) && fieldDeclaration.Span.Contains(context.Span)) { context.RegisterRefactoring( "Replace constant with field", ct => UseReadOnlyFieldInsteadOfConstantRefactoring.RefactorAsync(context.Document, fieldDeclaration, ct), RefactoringDescriptors.UseReadOnlyFieldInsteadOfConstant); } if (context.IsRefactoringEnabled(RefactoringDescriptors.InlineConstantDeclaration) && !fieldDeclaration.ContainsDiagnostics) { VariableDeclaratorSyntax variableDeclarator = fieldDeclaration .Declaration? .Variables .FirstOrDefault(f => context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(f.Identifier)); if (variableDeclarator != null) { context.RegisterRefactoring( "Inline constant", ct => InlineConstantDeclarationRefactoring.RefactorAsync(context.Document, fieldDeclaration, variableDeclarator, ct), RefactoringDescriptors.InlineConstantDeclaration); } } } else if (context.IsRefactoringEnabled(RefactoringDescriptors.UseConstantInsteadOfReadOnlyField) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(fieldDeclaration)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (UseConstantInsteadOfFieldAnalysis.IsFixable(fieldDeclaration, semanticModel, onlyPrivate: false, cancellationToken: context.CancellationToken)) { context.RegisterRefactoring( "Use constant instead of field", ct => UseConstantInsteadOfReadOnlyFieldRefactoring.RefactorAsync(context.Document, fieldDeclaration, ct), RefactoringDescriptors.UseConstantInsteadOfReadOnlyField); } } if (context.IsRefactoringEnabled(RefactoringDescriptors.InitializeFieldFromConstructor)) { InitializeFieldFromConstructorRefactoring.ComputeRefactoring(context, fieldDeclaration); } }
public static async Task ComputeRefactoringAsync(RefactoringContext context, MemberDeclarationListSelection selectedMembers) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeAccessibility) && !selectedMembers.Parent.IsKind(SyntaxKind.InterfaceDeclaration)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); AccessibilityFilter validAccessibilities = ChangeAccessibilityAnalysis.GetValidAccessibilityFilter(selectedMembers, semanticModel, context.CancellationToken); if (validAccessibilities != AccessibilityFilter.None) { bool canHaveMultipleDeclarations = CanHaveMultipleDeclarations(); TryRegisterRefactoring(validAccessibilities, Accessibility.Public, canHaveMultipleDeclarations); TryRegisterRefactoring(validAccessibilities, Accessibility.Internal, canHaveMultipleDeclarations); TryRegisterRefactoring(validAccessibilities, Accessibility.Protected, canHaveMultipleDeclarations); TryRegisterRefactoring(validAccessibilities, Accessibility.Private, canHaveMultipleDeclarations); } } if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ConvertBlockBodyToExpressionBody, RefactoringIdentifiers.ConvertExpressionBodyToBlockBody)) { ConvertBodyAndExpressionBodyRefactoring.ComputeRefactoring(context, selectedMembers); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.InitializeFieldFromConstructor) && !selectedMembers.Parent.IsKind(SyntaxKind.InterfaceDeclaration)) { InitializeFieldFromConstructorRefactoring.ComputeRefactoring(context, selectedMembers); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddEmptyLineBetweenDeclarations)) { AddEmptyLineBetweenDeclarationsRefactoring.ComputeRefactoring(context, selectedMembers); } void TryRegisterRefactoring(AccessibilityFilter accessibilities, Accessibility accessibility, bool canHaveMultipleDeclarations) { if ((accessibilities & accessibility.GetAccessibilityFilter()) != 0) { if (canHaveMultipleDeclarations) { context.RegisterRefactoring( ChangeAccessibilityRefactoring.GetTitle(accessibility), async cancellationToken => { SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); return(await ChangeAccessibilityRefactoring.RefactorAsync(context.Document.Solution(), selectedMembers, accessibility, semanticModel, cancellationToken).ConfigureAwait(false)); }, _accessiblityIdentifierMap[accessibility]); } else { context.RegisterRefactoring( ChangeAccessibilityRefactoring.GetTitle(accessibility), cancellationToken => ChangeAccessibilityRefactoring.RefactorAsync(context.Document, selectedMembers, accessibility, cancellationToken), EquivalenceKey.Join(RefactoringIdentifiers.ChangeAccessibility, accessibility.ToString())); } } } bool CanHaveMultipleDeclarations() { foreach (MemberDeclarationSyntax member in selectedMembers) { switch (member.Kind()) { case SyntaxKind.ClassDeclaration: { if (((ClassDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.InterfaceDeclaration: { if (((InterfaceDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.RecordDeclaration: { if (((RecordDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.StructDeclaration: { if (((StructDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.MethodDeclaration: { if (((MethodDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.PartialKeyword, SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.PropertyDeclaration: { if (((PropertyDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.IndexerDeclaration: { if (((IndexerDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.EventDeclaration: { if (((EventDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.EventFieldDeclaration: { if (((EventFieldDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } } } return(false); } }
public static void ComputeRefactoring(RefactoringContext context, MemberDeclarationSelection selectedMembers) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeAccessibility)) { AccessibilityFlags accessibilityFlags = ChangeAccessibilityRefactoring.GetAllowedAccessibilityFlags(selectedMembers, allowOverride: true); if (accessibilityFlags != AccessibilityFlags.None) { bool canHaveMultipleDeclarations = CanHaveMultipleDeclarations(); TryRegisterRefactoring(accessibilityFlags, Accessibility.Public, canHaveMultipleDeclarations); TryRegisterRefactoring(accessibilityFlags, Accessibility.Internal, canHaveMultipleDeclarations); TryRegisterRefactoring(accessibilityFlags, Accessibility.Protected, canHaveMultipleDeclarations); TryRegisterRefactoring(accessibilityFlags, Accessibility.Private, canHaveMultipleDeclarations); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.InitializeFieldFromConstructor)) { InitializeFieldFromConstructorRefactoring.ComputeRefactoring(context, selectedMembers); } void TryRegisterRefactoring(AccessibilityFlags accessibilityFlags, Accessibility accessibility, bool canHaveMultipleDeclarations) { if ((accessibilityFlags & accessibility.GetAccessibilityFlag()) != 0) { if (canHaveMultipleDeclarations) { context.RegisterRefactoring( ChangeAccessibilityRefactoring.GetTitle(accessibility), async cancellationToken => { SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); return(await ChangeAccessibilityRefactoring.RefactorAsync(context.Document.Solution(), selectedMembers, accessibility, semanticModel, cancellationToken).ConfigureAwait(false)); }); } else { context.RegisterRefactoring( ChangeAccessibilityRefactoring.GetTitle(accessibility), cancellationToken => ChangeAccessibilityRefactoring.RefactorAsync(context.Document, selectedMembers, accessibility, cancellationToken)); } } } bool CanHaveMultipleDeclarations() { foreach (MemberDeclarationSyntax member in selectedMembers) { switch (member.Kind()) { case SyntaxKind.ClassDeclaration: { if (((ClassDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.InterfaceDeclaration: { if (((InterfaceDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.StructDeclaration: { if (((StructDeclarationSyntax)member).Modifiers.Contains(SyntaxKind.PartialKeyword)) { return(true); } break; } case SyntaxKind.MethodDeclaration: { if (((MethodDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.PartialKeyword, SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.PropertyDeclaration: { if (((PropertyDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.IndexerDeclaration: { if (((IndexerDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.EventDeclaration: { if (((EventDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } case SyntaxKind.EventFieldDeclaration: { if (((EventFieldDeclarationSyntax)member).Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword)) { return(true); } break; } } } return(false); } }