public static Task <Document> ChangeTypeAsync( Document document, TypeSyntax type, ITypeSymbol typeSymbol, CancellationToken cancellationToken = default(CancellationToken)) { TypeSyntax newType = typeSymbol .ToTypeSyntax() .WithTriviaFrom(type); if (newType is TupleTypeSyntax tupleType) { SeparatedSyntaxList <TupleElementSyntax> newElements = tupleType .Elements .Select(tupleElement => tupleElement.WithType(tupleElement.Type.WithSimplifierAnnotation())) .ToSeparatedSyntaxList(); newType = tupleType.WithElements(newElements); } else { newType = newType.WithSimplifierAnnotation(); } return(document.ReplaceNodeAsync(type, newType, cancellationToken)); }
private static TypeSyntax ChangeType(TypeSyntax type, ITypeSymbol typeSymbol) { TypeSyntax newType = typeSymbol .ToTypeSyntax() .WithTriviaFrom(type); if (newType is TupleTypeSyntax tupleType) { SeparatedSyntaxList <TupleElementSyntax> newElements = tupleType .Elements .Select(tupleElement => tupleElement.WithType(tupleElement.Type.WithSimplifierAnnotation())) .ToSeparatedSyntaxList(); return(tupleType.WithElements(newElements)); } else { return(newType.WithSimplifierAnnotation()); } }
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); }
private static async Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclarationStatement, ExpressionSyntax value, ITypeSymbol typeSymbol, CancellationToken cancellationToken) { ExpressionSyntax defaultValue = typeSymbol.GetDefaultValueSyntax(document.GetDefaultSyntaxOptions()); LocalDeclarationStatementSyntax newNode = localDeclarationStatement.ReplaceNode( value, defaultValue.WithTriviaFrom(value)); if (defaultValue is LiteralExpressionSyntax) { TypeSyntax oldType = newNode.Declaration.Type; TypeSyntax type = typeSymbol.ToTypeSyntax(); if (!type.IsKind(SyntaxKind.NullableType) && typeSymbol.IsReferenceType) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); if ((semanticModel.GetNullableContext(localDeclarationStatement.SpanStart) & NullableContext.AnnotationsEnabled) != 0) { type = SyntaxFactory.NullableType(type); } } type = type.WithSimplifierAnnotation().WithTriviaFrom(oldType); newNode = newNode.ReplaceNode(oldType, type); } return(await document.ReplaceNodeAsync(localDeclarationStatement, newNode, cancellationToken).ConfigureAwait(false)); }