private static async Task RenameMethodAccoringToTypeNameAsync( RefactoringContext context, MethodDeclarationSyntax methodDeclaration) { TypeSyntax returnType = methodDeclaration.ReturnType; if (returnType?.IsVoid() == false) { SyntaxToken identifier = methodDeclaration.Identifier; if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(identifier)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); IMethodSymbol methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken); ITypeSymbol typeSymbol = GetType(returnType, semanticModel, context.CancellationToken); if (typeSymbol != null) { string newName = NameGenerator.CreateName(typeSymbol); if (!string.IsNullOrEmpty(newName)) { newName = "Get" + newName; if (methodSymbol.IsAsync) { newName += "Async"; } string oldName = identifier.ValueText; if (!string.Equals(oldName, newName, StringComparison.Ordinal) && await WorkspaceNameGenerator.IsUniqueMemberNameAsync( newName, methodSymbol, context.Solution, cancellationToken: context.CancellationToken).ConfigureAwait(false)) { context.RegisterRefactoring( $"Rename '{oldName}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Solution, methodSymbol, newName, default(OptionSet), cancellationToken)); } } } } } }
private static async Task RenameFieldAccordingToPropertyNameAsync( RefactoringContext context, IdentifierNameSyntax identifierName) { if (!IsQualified(identifierName) || IsQualifiedWithThis(identifierName)) { PropertyDeclarationSyntax propertyDeclaration = identifierName.FirstAncestor <PropertyDeclarationSyntax>(); if (propertyDeclaration != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); var fieldSymbol = semanticModel.GetSymbol(identifierName, context.CancellationToken) as IFieldSymbol; if (fieldSymbol?.IsPrivate() == true) { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (propertySymbol != null && fieldSymbol.IsStatic == propertySymbol.IsStatic && fieldSymbol.ContainingType == propertySymbol.ContainingType) { string newName = StringUtility.ToCamelCase(propertySymbol.Name, context.Settings.PrefixFieldIdentifierWithUnderscore); if (!string.Equals(fieldSymbol.Name, newName, StringComparison.Ordinal) && await WorkspaceNameGenerator.IsUniqueMemberNameAsync( newName, fieldSymbol, context.Solution, cancellationToken: context.CancellationToken).ConfigureAwait(false)) { context.RegisterRefactoring( $"Rename '{fieldSymbol.Name}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Solution, fieldSymbol, newName, default(OptionSet), cancellationToken)); } } } } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, PropertyDeclarationSyntax propertyDeclaration) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplacePropertyWithMethod) && propertyDeclaration.HeaderSpan().Contains(context.Span)) { ReplacePropertyWithMethodRefactoring.ComputeRefactoring(context, propertyDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemovePropertyInitializer) && RemovePropertyInitializerRefactoring.CanRefactor(context, propertyDeclaration)) { context.RegisterRefactoring( "Remove property initializer", cancellationToken => RemovePropertyInitializerRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ExpandProperty, RefactoringIdentifiers.ExpandPropertyAndAddBackingField) && propertyDeclaration.Span.Contains(context.Span) && ExpandPropertyRefactoring.CanRefactor(propertyDeclaration)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandProperty)) { context.RegisterRefactoring( "Expand property", cancellationToken => ExpandPropertyRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandPropertyAndAddBackingField)) { context.RegisterRefactoring( "Expand property and add backing field", cancellationToken => ExpandPropertyAndAddBackingFieldRefactoring.RefactorAsync(context.Document, propertyDeclaration, context.Settings.PrefixFieldIdentifierWithUnderscore, cancellationToken)); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.NotifyPropertyChanged) && await NotifyPropertyChangedRefactoring.CanRefactorAsync(context, propertyDeclaration).ConfigureAwait(false)) { context.RegisterRefactoring( "Notify property changed", cancellationToken => { return(NotifyPropertyChangedRefactoring.RefactorAsync( context.Document, propertyDeclaration, context.SupportsCSharp6, cancellationToken)); }); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) && propertyDeclaration.HeaderSpan().Contains(context.Span)) { MakePropertyAbstractRefactoring.ComputeRefactoring(context, propertyDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) && propertyDeclaration.HeaderSpan().Contains(context.Span)) { MakePropertyVirtualRefactoring.ComputeRefactoring(context, propertyDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) && propertyDeclaration.HeaderSpan().Contains(context.Span)) { await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, propertyDeclaration).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenamePropertyAccordingToTypeName)) { TypeSyntax type = propertyDeclaration.Type; if (type != null) { SyntaxToken identifier = propertyDeclaration.Identifier; if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(identifier)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); if (typeSymbol?.IsErrorType() == false) { string newName = NameGenerator.CreateName(typeSymbol); if (!string.IsNullOrEmpty(newName)) { string oldName = identifier.ValueText; newName = StringUtility.FirstCharToUpper(newName); if (!string.Equals(oldName, newName, StringComparison.Ordinal)) { ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (await WorkspaceNameGenerator.IsUniqueMemberNameAsync( newName, symbol, context.Solution, cancellationToken: context.CancellationToken).ConfigureAwait(false)) { context.RegisterRefactoring( $"Rename '{oldName}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Solution, symbol, newName, default(OptionSet), cancellationToken)); } } } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(propertyDeclaration.Identifier)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); AddMemberToInterfaceRefactoring.ComputeRefactoring(context, propertyDeclaration, semanticModel); } }