private static void ChangeType( CodeRefactoringContext context, SemanticModel semanticModel, VariableDeclarationSyntax variableDeclaration) { TypeAnalysisResult result = VariableDeclarationAnalysis.AnalyzeType( variableDeclaration, semanticModel, context.CancellationToken); switch (result) { case TypeAnalysisResult.Explicit: case TypeAnalysisResult.ExplicitButShouldBeImplicit: { context.RegisterRefactoring( "Change type to 'var'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToImplicitAsync(context.Document, variableDeclaration.Type, cancellationToken)); break; } case TypeAnalysisResult.Implicit: case TypeAnalysisResult.ImplicitButShouldBeExplicit: { ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(variableDeclaration.Type, context.CancellationToken).Type; context.RegisterRefactoring( $"Change type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, variableDeclaration.Type, typeSymbol, cancellationToken)); break; } } }
private static void ChangeTypeAccordingToExpression( CodeRefactoringContext context, SemanticModel semanticModel, VariableDeclarationSyntax variableDeclaration) { if (variableDeclaration.Parent?.IsKind(SyntaxKind.FieldDeclaration) != false) { return; } TypeSyntax type = variableDeclaration.Type; if (type == null) { return; } if (type.IsVar) { return; } if (!type.Span.Contains(context.Span)) { return; } if (variableDeclaration.Variables.Count != 1) { return; } EqualsValueClauseSyntax initializer = variableDeclaration.Variables[0].Initializer; if (initializer == null) { return; } if (initializer.Value == null) { return; } ITypeSymbol initializerTypeSymbol = semanticModel.GetTypeInfo(initializer.Value).Type; if (initializerTypeSymbol == null || initializerTypeSymbol.IsKind(SymbolKind.ErrorType)) { return; } ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(type).ConvertedType; if (!initializerTypeSymbol.Equals(typeSymbol)) { context.RegisterRefactoring( $"Change type to '{initializerTypeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, variableDeclaration.Type, initializerTypeSymbol, cancellationToken)); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); VariableDeclarationSyntax variableDeclaration = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <VariableDeclarationSyntax>(); if (variableDeclaration == null) { return; } if (variableDeclaration.Type.IsVar) { SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken); if (semanticModel == null) { return; } ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(variableDeclaration.Type).Type; CodeAction codeAction = CodeAction.Create( $"Change type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, variableDeclaration.Type, typeSymbol, cancellationToken), DiagnosticIdentifiers.DeclareExplicitType + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); } }
private static void ChangeTypeAccordingToExpression( CodeRefactoringContext context, SemanticModel semanticModel, ForEachStatementSyntax forEachStatement) { if (forEachStatement.Type?.IsVar != false) { return; } if (!forEachStatement.Type.Span.Contains(context.Span)) { return; } ForEachStatementInfo forEachInfo = semanticModel.GetForEachStatementInfo(forEachStatement); ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(forEachStatement.Type).ConvertedType; if (forEachInfo.ElementType?.Equals(typeSymbol) != false) { return; } context.RegisterRefactoring( $"Change type to '{forEachInfo.ElementType.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, forEachStatement.Type, forEachInfo.ElementType, cancellationToken)); }
private static void ChangeType( CodeRefactoringContext context, SemanticModel semanticModel, ForEachStatementSyntax forEachStatement) { TypeSyntax type = forEachStatement.Type; if (type == null || !type.Span.Contains(context.Span)) { return; } TypeAnalysisResult result = ForEachStatementAnalysis.AnalyzeType( forEachStatement, semanticModel, context.CancellationToken); switch (result) { case TypeAnalysisResult.Explicit: { context.RegisterRefactoring( "Change type to 'var'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToImplicitAsync(context.Document, type, cancellationToken)); break; } case TypeAnalysisResult.ImplicitButShouldBeExplicit: { ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(type, context.CancellationToken).Type; context.RegisterRefactoring( $"Change type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'", cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, type, typeSymbol, cancellationToken)); break; } } }