public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); ImplicitArrayCreationExpressionSyntax expression = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <ImplicitArrayCreationExpressionSyntax>(); if (expression != null && context.Document.SupportsSemanticModel) { SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); var typeSymbol = semanticModel .GetTypeInfo(expression, context.CancellationToken) .Type as IArrayTypeSymbol; if (typeSymbol?.ElementType?.IsErrorType() == false) { var arrayType = CSharpFactory.Type(typeSymbol) as ArrayTypeSyntax; if (arrayType != null) { CodeAction codeAction = CodeAction.Create( $"Declare explicit type '{typeSymbol.ToMinimalDisplayString(semanticModel, expression.Span.Start, SyntaxUtility.DefaultSymbolDisplayFormat)}'", cancellationToken => RefactorAsync(context.Document, expression, arrayType, cancellationToken), DiagnosticIdentifiers.AvoidImplicitlyTypedArray + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); } } } }
private static async Task <Document> ChangeTypeAndAddAwaitAsync( Document document, VariableDeclarationSyntax variableDeclaration, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); ExpressionSyntax initializerValue = variableDeclaration.Variables[0].Initializer.Value; AwaitExpressionSyntax newInitializerValue = SyntaxFactory.AwaitExpression(initializerValue) .WithTriviaFrom(initializerValue); VariableDeclarationSyntax newNode = variableDeclaration.ReplaceNode(initializerValue, newInitializerValue); newNode = newNode .WithType( CSharpFactory.Type(typeSymbol) .WithTriviaFrom(variableDeclaration.Type) .WithSimplifierAnnotation()); SyntaxNode newRoot = oldRoot.ReplaceNode(variableDeclaration, newNode); return(document.WithSyntaxRoot(newRoot)); }
public static async Task <Document> RefactorAsync( Document document, SyntaxNode node, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { TypeSyntax newType = CSharpFactory.Type(typeSymbol) .WithTriviaFrom(node) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(node, newType, cancellationToken).ConfigureAwait(false)); }
public static async Task <Document> RefactorAsync( Document document, ExpressionSyntax expression, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); TypeSyntax type = CSharpFactory.Type(typeSymbol, semanticModel, expression.SpanStart); DefaultExpressionSyntax defaultExpression = SyntaxFactory.DefaultExpression(type) .WithTriviaFrom(expression); return(await document.ReplaceNodeAsync(expression, defaultExpression, cancellationToken).ConfigureAwait(false)); }
private static async Task <Document> ChangeTypeAsync( Document document, TypeSyntax type, ITypeSymbol typeSymbol, CancellationToken cancellationToken) { SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); TypeSyntax newType = CSharpFactory.Type(typeSymbol) .WithTriviaFrom(type) .WithSimplifierAnnotation(); SyntaxNode newRoot = oldRoot.ReplaceNode(type, newType); return(document.WithSyntaxRoot(newRoot)); }
private static async Task <Document> RefactorAsync( Document document, SyntaxNode node, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); TypeSyntax newType = CSharpFactory.Type(typeSymbol) .WithTriviaFrom(node) .WithFormatterAnnotation(); SyntaxNode newRoot = root.ReplaceNode(node, newType); return(document.WithSyntaxRoot(newRoot)); }
public static async Task <Document> RefactorAsync( Document document, ExpressionSyntax expression, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); TypeSyntax type = CSharpFactory.Type(typeSymbol) .WithSimplifierAnnotation(); DefaultExpressionSyntax defaultExpression = SyntaxFactory.DefaultExpression(type) .WithTriviaFrom(expression); SyntaxNode newRoot = root.ReplaceNode(expression, defaultExpression); return(document.WithSyntaxRoot(newRoot)); }
public static async Task <Document> RefactorAsync( Document document, ExpressionSyntax expression, ITypeSymbol destinationType, CancellationToken cancellationToken = default(CancellationToken)) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); TypeSyntax type = CSharpFactory.Type(destinationType, semanticModel, expression.SpanStart); ExpressionSyntax newExpression = expression .WithoutTrivia() .Parenthesize() .WithSimplifierAnnotation(); CastExpressionSyntax castExpression = SyntaxFactory.CastExpression(type, newExpression) .WithTriviaFrom(expression); return(await document.ReplaceNodeAsync(expression, castExpression, cancellationToken).ConfigureAwait(false)); }
public static async Task <Document> RefactorAsync( Document document, ImplicitArrayCreationExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken)) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, cancellationToken); var arrayType = CSharpFactory.Type(typeSymbol, semanticModel, expression.SpanStart) as ArrayTypeSyntax; ArrayCreationExpressionSyntax newNode = SyntaxFactory.ArrayCreationExpression( expression.NewKeyword, arrayType.WithTrailingTrivia(expression.CloseBracketToken.TrailingTrivia), expression.Initializer); newNode = newNode.WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(expression, newNode, cancellationToken).ConfigureAwait(false)); }
private static async Task <Document> ChangeTypeAndAddAwaitAsync( Document document, VariableDeclarationSyntax variableDeclaration, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { TypeSyntax type = variableDeclaration.Type; ExpressionSyntax value = variableDeclaration.Variables[0].Initializer.Value; AwaitExpressionSyntax newInitializerValue = SyntaxFactory.AwaitExpression(value) .WithTriviaFrom(value); VariableDeclarationSyntax newNode = variableDeclaration.ReplaceNode(value, newInitializerValue); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); newNode = newNode.WithType( CSharpFactory.Type(typeSymbol, semanticModel, type.SpanStart).WithTriviaFrom(type)); return(await document.ReplaceNodeAsync(variableDeclaration, newNode, cancellationToken).ConfigureAwait(false)); }
private static List <SwitchSectionSyntax> CreateSwitchSections(INamedTypeSymbol enumTypeSymbol) { SyntaxList <StatementSyntax> statements = SingletonList <StatementSyntax>(BreakStatement()); ImmutableArray <ISymbol> members = enumTypeSymbol.GetMembers(); var sections = new List <SwitchSectionSyntax>(members.Length); TypeSyntax enumType = CSharpFactory.Type(enumTypeSymbol); if (members.Length <= 128) { enumType = enumType.WithSimplifierAnnotation(); } foreach (ISymbol memberSymbol in members) { if (memberSymbol.IsField()) { sections.Add( SwitchSection( SingletonList <SwitchLabelSyntax>( CaseSwitchLabel( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, enumType, IdentifierName(memberSymbol.Name)))), statements)); } } sections.Add(SwitchSection( SingletonList <SwitchLabelSyntax>(DefaultSwitchLabel()), statements)); return(sections); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod, RefactoringIdentifiers.CreateConditionFromBooleanExpression) && yieldStatement.IsYieldReturn() && yieldStatement.Expression != null && context.SupportsSemanticModel) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod)) { MemberDeclarationSyntax containingMember = ReturnExpressionRefactoring.GetContainingMethodOrPropertyOrIndexer(yieldStatement.Expression); if (containingMember != null) { TypeSyntax memberType = ReturnExpressionRefactoring.GetMemberType(containingMember); if (memberType != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol memberTypeSymbol = semanticModel .GetTypeInfo(memberType, context.CancellationToken) .Type; if (memberTypeSymbol?.SpecialType != SpecialType.System_Collections_IEnumerable) { ITypeSymbol typeSymbol = semanticModel .GetTypeInfo(yieldStatement.Expression, context.CancellationToken) .Type; if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) && typeSymbol?.IsErrorType() == false && (memberTypeSymbol == null || memberTypeSymbol.IsErrorType() || !SyntaxAnalyzer.IsGenericIEnumerable(memberTypeSymbol) || !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol))) { TypeSyntax newType = QualifiedName( ParseName("System.Collections.Generic"), GenericName( Identifier("IEnumerable"), TypeArgumentList( SingletonSeparatedList( CSharpFactory.Type(typeSymbol))))); context.RegisterRefactoring( $"Change {ReturnExpressionRefactoring.GetText(containingMember)} type to 'IEnumerable<{typeSymbol.ToDisplayString(SyntaxUtility.DefaultSymbolDisplayFormat)}>'", cancellationToken => { return(ChangeTypeRefactoring.ChangeTypeAsync( context.Document, memberType, newType, cancellationToken)); }); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.Expression.Span.Contains(context.Span) && memberTypeSymbol?.IsNamedType() == true) { var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol; if (namedTypeSymbol.ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; if (argumentSymbol != typeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, yieldStatement.Expression, argumentSymbol, semanticModel); } } } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CreateConditionFromBooleanExpression)) { await CreateConditionFromBooleanExpressionRefactoring.ComputeRefactoringAsync(context, yieldStatement.Expression).ConfigureAwait(false); } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression, RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.IsYieldReturn() && yieldStatement.Expression != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); MemberDeclarationSyntax containingMember = await ReturnExpressionRefactoring.GetContainingMethodOrPropertyOrIndexerAsync(yieldStatement.Expression, semanticModel, context.CancellationToken).ConfigureAwait(false); if (containingMember != null) { TypeSyntax memberType = ReturnExpressionRefactoring.GetMemberType(containingMember); if (memberType != null) { ITypeSymbol memberTypeSymbol = semanticModel.GetTypeSymbol(memberType, context.CancellationToken); if (memberTypeSymbol?.SpecialType != SpecialType.System_Collections_IEnumerable) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(yieldStatement.Expression, context.CancellationToken); if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) && typeSymbol?.IsErrorType() == false && !typeSymbol.IsVoid() && (memberTypeSymbol == null || memberTypeSymbol.IsErrorType() || !memberTypeSymbol.IsConstructedFromIEnumerableOfT() || !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol))) { INamedTypeSymbol newTypeSymbol = semanticModel .Compilation .GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T) .Construct(typeSymbol); TypeSyntax newType = CSharpFactory.Type(newTypeSymbol, semanticModel, memberType.SpanStart); context.RegisterRefactoring( $"Change {ReturnExpressionRefactoring.GetText(containingMember)} type to '{SymbolDisplay.GetMinimalDisplayString(newTypeSymbol, memberType.SpanStart, semanticModel)}'", cancellationToken => { return(ChangeTypeRefactoring.ChangeTypeAsync( context.Document, memberType, newType, cancellationToken)); }); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) && yieldStatement.Expression.Span.Contains(context.Span) && memberTypeSymbol?.IsNamedType() == true) { var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol; if (namedTypeSymbol.IsConstructedFromIEnumerableOfT()) { ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0]; if (argumentSymbol != typeSymbol) { ModifyExpressionRefactoring.ComputeRefactoring( context, yieldStatement.Expression, argumentSymbol, semanticModel); } } } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfStatement) && context.Span.IsBetweenSpans(yieldStatement)) { var refactoring = new ReplaceYieldStatementWithIfStatementRefactoring(); await refactoring.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false); } }