public ReplaceNodes( IdentifierNameSyntax identifierName, SemanticModel semanticModel, CancellationToken cancellationToken) { this.identifierName = identifierName; this.semanticModel = semanticModel; this.cancellationToken = cancellationToken; this.Visit(identifierName.FirstAncestor <ClassDeclarationSyntax>()); }
internal static bool IsLocalOrParameter(IdentifierNameSyntax identifier) { if (identifier == null) { return(false); } if (identifier.Parent is MemberAccessExpressionSyntax memberAccess && memberAccess.Expression is InstanceExpressionSyntax) { return(false); } if (identifier.Identifier.ValueText == "value" && identifier.FirstAncestor <AccessorDeclarationSyntax>() is AccessorDeclarationSyntax accessor && accessor.IsKind(SyntaxKind.SetAccessorDeclaration)) { return(true); } using (var walker = BorrowAndVisit(identifier.FirstAncestor <MemberDeclarationSyntax>(), () => new IdentifierTypeWalker())) { foreach (var parameter in walker.parameters) { if (identifier.Identifier.ValueText == parameter.Identifier.ValueText) { return(true); } } foreach (var declarator in walker.variableDeclarators) { if (identifier.Identifier.ValueText == declarator.Identifier.ValueText) { return(true); } } } return(false); }
private static async Task RenameFieldAccordingToPropertyNameAsync( RefactoringContext context, IdentifierNameSyntax identifierName) { if (!identifierName.IsQualified() || identifierName.IsQualifiedWithThis()) { PropertyDeclarationSyntax propertyDeclaration = identifierName.FirstAncestor <PropertyDeclarationSyntax>(); if (propertyDeclaration != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); var fieldSymbol = semanticModel .GetSymbolInfo(identifierName, context.CancellationToken) .Symbol as IFieldSymbol; if (fieldSymbol?.IsPrivate() == true) { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (propertySymbol != null && fieldSymbol.IsStatic == propertySymbol.IsStatic && object.Equals(fieldSymbol.ContainingType, propertySymbol.ContainingType)) { string newName = TextUtility.ToCamelCase(propertySymbol.Name, context.Settings.PrefixFieldIdentifierWithUnderscore); if (!string.Equals(newName, fieldSymbol.Name, StringComparison.Ordinal)) { string fieldName = identifierName.Identifier.ValueText; context.RegisterRefactoring( $"Rename field to '{newName}'", cancellationToken => { return(SymbolRenamer.RenameAsync( context.Document, fieldSymbol, newName, 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 = Identifier.ToCamelCase(propertySymbol.Name, context.Settings.PrefixFieldIdentifierWithUnderscore); if (!string.Equals(fieldSymbol.Name, newName, StringComparison.Ordinal)) { bool isUnique = await Identifier.IsUniqueMemberNameAsync( fieldSymbol, newName, context.Solution, context.CancellationToken).ConfigureAwait(false); if (isUnique) { context.RegisterRefactoring( $"Rename '{fieldSymbol.Name}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Document, fieldSymbol, newName, cancellationToken)); } } } } } } }
private bool TryGetReplaceNode(IdentifierNameSyntax node, IdentifierNameSyntax expected, out SyntaxNode result) { result = null; if (node.Identifier.ValueText != expected.Identifier.ValueText) { return(false); } var member = node.FirstAncestor <MemberDeclarationSyntax>(); if (member == null || this.semanticModel.GetDeclaredSymbolSafe(member, this.cancellationToken)?.IsStatic != false) { return(false); } return(this.TryGetReplaceNode(node.Parent as MemberAccessExpressionSyntax, expected.Parent as MemberAccessExpressionSyntax, out result)); }
private static void RenameFieldAccordingToPropertyName( CodeRefactoringContext context, IdentifierNameSyntax identifierName, SemanticModel semanticModel) { if (!identifierName.IsQualified() || identifierName.IsQualifiedWithThis()) { PropertyDeclarationSyntax propertyDeclaration = identifierName.FirstAncestor <PropertyDeclarationSyntax>(); if (propertyDeclaration != null) { var fieldSymbol = semanticModel .GetSymbolInfo(identifierName, context.CancellationToken) .Symbol as IFieldSymbol; if (fieldSymbol?.DeclaredAccessibility == Accessibility.Private) { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (propertySymbol != null && fieldSymbol.IsStatic == propertySymbol.IsStatic && object.Equals(fieldSymbol.ContainingType, propertySymbol.ContainingType)) { string newName = NamingHelper.ToCamelCaseWithUnderscore(propertySymbol.Name); if (!string.Equals(newName, fieldSymbol.Name, StringComparison.Ordinal)) { context.RegisterRefactoring( $"Rename field to '{newName}'", cancellationToken => { return(fieldSymbol.RenameAsync( newName, context.Document, cancellationToken)); }); } } } } } }
private static async Task SyncPropertyNameAndBackingFieldNameAsync( RefactoringContext context, IdentifierNameSyntax identifierName) { if (IsQualified(identifierName) && !IsQualifiedWithThis(identifierName)) { return; } PropertyDeclarationSyntax propertyDeclaration = identifierName.FirstAncestor <PropertyDeclarationSyntax>(); if (propertyDeclaration == null) { return; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); var fieldSymbol = semanticModel.GetSymbol(identifierName, context.CancellationToken) as IFieldSymbol; if (fieldSymbol?.DeclaredAccessibility != Accessibility.Private) { return; } IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (propertySymbol == null) { return; } if (fieldSymbol.IsStatic != propertySymbol.IsStatic) { return; } if (!SymbolEqualityComparer.Default.Equals(fieldSymbol.ContainingType, propertySymbol.ContainingType)) { return; } string newName = StringUtility.ToCamelCase(propertySymbol.Name, context.PrefixFieldIdentifierWithUnderscore); if (string.Equals(fieldSymbol.Name, newName, StringComparison.Ordinal)) { return; } if (!await MemberNameGenerator.IsUniqueMemberNameAsync( newName, fieldSymbol, context.Solution, cancellationToken: context.CancellationToken) .ConfigureAwait(false)) { return; } context.RegisterRefactoring( $"Rename '{fieldSymbol.Name}' to '{newName}'", ct => Renamer.RenameSymbolAsync(context.Solution, fieldSymbol, newName, default(OptionSet), ct), RefactoringDescriptors.SyncPropertyNameAndBackingFieldName); }