Пример #1
0
        public override Ust VisitArrayType(ArrayTypeSyntax node)
        {
            var arrayType = node.ToString().Replace(",", "][");
            var result    = new TypeToken(arrayType, node.GetTextSpan());

            return(result);
        }
Пример #2
0
        private static async Task <Document> ChangeArrayTypeToImplicitAsync(
            Document document,
            ArrayCreationExpressionSyntax arrayCreation,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ArrayTypeSyntax arrayType = arrayCreation.Type;
            SyntaxList <ArrayRankSpecifierSyntax> rankSpecifiers = arrayType.RankSpecifiers;
            InitializerExpressionSyntax           initializer    = arrayCreation.Initializer;

            ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(arrayType.ElementType, cancellationToken);
            TypeSyntax  castType;

            if (rankSpecifiers.Count > 1)
            {
                castType = ParseTypeName(arrayType.ToString().Remove(rankSpecifiers.Last().SpanStart - arrayType.SpanStart));
            }
            else
            {
                castType = arrayType.ElementType;
            }

            InitializerExpressionSyntax newInitializer = initializer.ReplaceNodes(
                initializer.Expressions,
                (node, _) => CastExpression(castType, node.WithoutTrivia())
                .WithTriviaFrom(node)
                .WithSimplifierAnnotation());

            ImplicitArrayCreationExpressionSyntax implicitArrayCreation = ImplicitArrayCreationExpression(
                arrayCreation.NewKeyword.WithTrailingTrivia(arrayCreation.NewKeyword.TrailingTrivia.EmptyIfWhitespace()),
                rankSpecifiers[0].OpenBracketToken,
                default(SyntaxTokenList),
                rankSpecifiers.Last().CloseBracketToken,
                newInitializer);

            return(await document.ReplaceNodeAsync(arrayCreation, implicitArrayCreation, cancellationToken).ConfigureAwait(false));
        }