private static void ChangeType( RefactoringContext context, VariableDeclarationSyntax variableDeclaration, ITypeSymbol typeSymbol, SemanticModel semanticModel, CancellationToken cancellationToken) { TypeSyntax type = variableDeclaration.Type; if (variableDeclaration.SingleVariableOrDefault()?.Initializer?.Value != null && typeSymbol.IsConstructedFromTaskOfT(semanticModel)) { ISymbol enclosingSymbol = semanticModel.GetEnclosingSymbol(variableDeclaration.SpanStart, cancellationToken); if (enclosingSymbol.IsAsyncMethod()) { ITypeSymbol typeArgument = ((INamedTypeSymbol)typeSymbol).TypeArguments[0]; context.RegisterRefactoring( $"Change type to '{SymbolDisplay.GetMinimalString(typeArgument, semanticModel, type.SpanStart)}' and insert 'await'", c => ChangeTypeAndAddAwaitAsync(context.Document, variableDeclaration, typeArgument, c)); } } context.RegisterRefactoring( $"Change type to '{SymbolDisplay.GetMinimalString(typeSymbol, semanticModel, type.Span.Start)}'", c => ChangeTypeRefactoring.ChangeTypeAsync(context.Document, type, typeSymbol, c)); }
private static async Task ChangeTypeAccordingToExpressionAsync( RefactoringContext context, VariableDeclarationSyntax variableDeclaration) { TypeSyntax type = variableDeclaration.Type; if (type?.IsVar == false) { ExpressionSyntax initializerValue = variableDeclaration.SingleVariableOrDefault()?.Initializer?.Value; if (initializerValue != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol initializerTypeSymbol = semanticModel.GetTypeSymbol(initializerValue); if (initializerTypeSymbol?.IsErrorType() == false) { ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(type).ConvertedType; if (!initializerTypeSymbol.Equals(typeSymbol)) { ChangeType(context, variableDeclaration, initializerTypeSymbol, semanticModel, context.CancellationToken); } } } } }
public async Task ComputeRefactoringAsync(RefactoringContext context, StatementContainerSelection selectedStatements) { StatementSyntax statement = selectedStatements.FirstOrDefault(); if (statement?.IsKind(SyntaxKind.LocalDeclarationStatement) == true) { var localDeclaration = (LocalDeclarationStatementSyntax)statement; VariableDeclarationSyntax declaration = localDeclaration.Declaration; if (declaration != null) { VariableDeclaratorSyntax variable = declaration.SingleVariableOrDefault(); if (variable?.Initializer?.Value?.IsKind(SyntaxKind.ObjectCreationExpression) == true && declaration.Type != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(declaration.Type, context.CancellationToken); if (typeSymbol?.IsNamedType() == true && ((INamedTypeSymbol)typeSymbol).Implements(SpecialType.System_IDisposable)) { context.RegisterRefactoring( $"Using '{variable.Identifier.ValueText}'", cancellationToken => RefactorAsync(context.Document, selectedStatements, cancellationToken)); } } } } }
private static async Task RenameVariableAccordingToTypeNameAsync( RefactoringContext context, VariableDeclarationSyntax variableDeclaration) { TypeSyntax type = variableDeclaration.Type; if (type != null && !variableDeclaration.IsParentKind(SyntaxKind.EventFieldDeclaration)) { VariableDeclaratorSyntax variable = variableDeclaration.SingleVariableOrDefault(); if (variable != null) { SyntaxToken identifier = variable.Identifier; if (identifier.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol symbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken); if (symbol != null) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); if (typeSymbol?.IsErrorType() == false) { string newName = Identifier.CreateName( typeSymbol, FirstCharToLower(symbol)); if (!string.IsNullOrEmpty(newName)) { if (context.Settings.PrefixFieldIdentifierWithUnderscore && symbol.IsPrivate() && symbol.IsField() && !((IFieldSymbol)symbol).IsConst) { newName = Identifier.ToCamelCase(newName, prefixWithUnderscore: true); } string oldName = identifier.ValueText; if (!string.Equals(oldName, newName, StringComparison.Ordinal)) { newName = Identifier.EnsureUniqueLocalName(newName, variable.SpanStart, semanticModel, context.CancellationToken); context.RegisterRefactoring( $"Rename '{oldName}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Document, symbol, newName, cancellationToken)); } } } } } } } }
private static async Task RenameVariableAccordingToTypeNameAsync( RefactoringContext context, VariableDeclarationSyntax variableDeclaration) { TypeSyntax type = variableDeclaration.Type; if (type != null && !variableDeclaration.IsParentKind(SyntaxKind.EventFieldDeclaration)) { VariableDeclaratorSyntax variable = variableDeclaration.SingleVariableOrDefault(); if (variable != null) { SyntaxToken identifier = variable.Identifier; if (identifier.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol symbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken); if (symbol?.IsLocal() == true) { var localSymbol = (ILocalSymbol)symbol; string oldName = identifier.ValueText; string newName = NameGenerator.Default.CreateUniqueLocalName( localSymbol.Type, oldName, semanticModel, variable.SpanStart, cancellationToken: context.CancellationToken); if (newName != null) { context.RegisterRefactoring( $"Rename '{oldName}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Solution, symbol, newName, default(OptionSet), cancellationToken)); } } } } } }
internal static async Task ComputeRefactoringAsync(RefactoringContext context, VariableDeclarationSyntax variableDeclaration) { SyntaxNode parent = variableDeclaration.Parent; if (parent?.IsKind(SyntaxKind.LocalDeclarationStatement) == true) { TypeSyntax type = variableDeclaration.Type; if (type != null) { VariableDeclaratorSyntax variableDeclarator = variableDeclaration.SingleVariableOrDefault(); ExpressionSyntax value = variableDeclarator?.Initializer?.Value; if (value?.IsKind(SyntaxKind.NullLiteralExpression) == false && CanBeEqualToNull(value)) { SyntaxToken identifier = variableDeclarator.Identifier; if (context.Span.IsContainedInSpanOrBetweenSpans(identifier)) { IdentifierNameSyntax identifierName = IdentifierName(identifier); var localDeclaration = (StatementSyntax)parent; if (!NullCheckExists(identifierName, localDeclaration)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (semanticModel .GetTypeSymbol(type, context.CancellationToken)? .IsReferenceType == true) { RegisterRefactoring(context, identifierName, localDeclaration); } } } } } } }
private static async Task ComputeRefactoringAsync( RefactoringContext context, LocalDeclarationStatementSyntax localDeclaration, StatementContainerSelection selectedStatements) { VariableDeclarationSyntax variableDeclaration = localDeclaration.Declaration; if (variableDeclaration != null) { TypeSyntax type = variableDeclaration.Type; if (type != null) { VariableDeclaratorSyntax variableDeclarator = variableDeclaration.SingleVariableOrDefault(); ExpressionSyntax value = variableDeclarator?.Initializer?.Value; if (value?.IsKind(SyntaxKind.NullLiteralExpression) == false && CanBeEqualToNull(value)) { SyntaxToken identifier = variableDeclarator.Identifier; IdentifierNameSyntax identifierName = IdentifierName(identifier); if (!NullCheckExists(identifierName, localDeclaration)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (semanticModel .GetTypeSymbol(type, context.CancellationToken)? .IsReferenceType == true) { RegisterRefactoring(context, identifierName, localDeclaration, selectedStatements.Count - 1); } } } } } }