public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            var variable     = node.ChildNodes().FirstOrDefault(x => x is VariableDeclarationSyntax);
            var variableType = variable.ChildNodes().FirstOrDefault().ToString();
            var fields       = variable.ChildNodes().Where(x => x is VariableDeclaratorSyntax);

            foreach (VariableDeclaratorSyntax field in fields)
            {
                var startLength = field.Span.Start;
                var endLength   = field.Span.End;

                var fieldName = field.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ToString();
                var fieldType = variableType;

                var parentNamespace = GetNamespace(DefineKinds.Field, startLength, endLength);

                UserDefinitions.Add(new UserDefinition
                {
                    DefineKinds    = DefineKinds.Field,
                    Namespace      = parentNamespace,
                    DefineName     = fieldName,
                    DefineFullName = $"{parentNamespace}.{fieldName}",
                    ReturnType     = RemoveNamespace(fieldType),
                    SourceFile     = SourceFile,
                    StartLength    = startLength,
                    EndLength      = endLength,
                });
            }

            base.VisitFieldDeclaration(node);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a list of fields (as variable declarations) from a class definition
        /// </summary>
        /// <param name="node">The class definition</param>
        /// <returns>The list of fields</returns>
        public List <VariableDeclarationSyntax> GetFields(ClassDeclarationSyntax node)
        {
            List <VariableDeclarationSyntax> fields = new List <VariableDeclarationSyntax>();

            IEnumerable <SyntaxNode> nodes = node.ChildNodes();

            foreach (SyntaxNode childNode in nodes)
            {
                SyntaxKind kind = childNode.Kind();

                // Found a field
                if (kind == SyntaxKind.FieldDeclaration)
                {
                    FieldDeclarationSyntax   fieldNode         = childNode as FieldDeclarationSyntax;
                    IEnumerable <SyntaxNode> fieldNodeChildren = fieldNode.ChildNodes();

                    foreach (VariableDeclarationSyntax variable in fieldNodeChildren)
                    {
                        fields.Add(variable);
                    }
                }
            }

            return(fields);
        }
Beispiel #3
0
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            //Console.WriteLine(lista["Cztery"]);

            var New = node;

            var pobierzLinie = node.ChildNodes().OfType <VariableDeclarationSyntax>().FirstOrDefault();
            // Console.WriteLine(pobierzLinie.ToString());

            var typZmiennej = pobierzLinie.ChildNodes().OfType <PredefinedTypeSyntax>().FirstOrDefault();
            //Console.WriteLine(typZmiennej);

            String typ   = typZmiennej + " ";
            String nazwa = pobierzLinie.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault().Identifier.ToString() + " = ";

            var tmp = pobierzLinie.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault();

            Console.WriteLine(tmp.ToString());
            if (typZmiennej.ToString().Equals("int") || typZmiennej.ToString().Equals("string"))
            {
                var wyjscieZmiennej = pobierzLinie.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault()
                                      .ChildNodes().OfType <EqualsValueClauseSyntax>().FirstOrDefault()
                                      .ChildNodes().OfType <InvocationExpressionSyntax>().FirstOrDefault();

                var nazwaMetody = wyjscieZmiennej.ChildNodes().OfType <IdentifierNameSyntax>().FirstOrDefault().ToString();
                // Console.WriteLine(wyjscieZmiennej);
                // Console.WriteLine(nazwaMetody);


                var pobierzNawias = wyjscieZmiennej.ChildNodes().OfType <ArgumentListSyntax>().FirstOrDefault().ToString();
                //Console.WriteLine(pobierzNawias.ToString());

                if (pobierzNawias.ToString().Equals("()"))
                {
                    if (lista.ContainsKey(wyjscieZmiennej.ToString().Substring(0, wyjscieZmiennej.ToString().Length - 2)))
                    {
                        String prawo = lista[nazwaMetody] + ";";
                        var    cale  = typ + nazwa + prawo;



                        var NowePole = SyntaxFactory.FieldDeclaration(SyntaxFactory.VariableDeclaration(typZmiennej).
                                                                      WithVariables(SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(tmp.WithInitializer(SyntaxFactory.
                                                                                                                                                                        EqualsValueClause(poRet).WithEqualsToken(SyntaxFactory.Token(SyntaxFactory.TriviaList(), SyntaxKind.
                                                                                                                                                                                                                                     EqualsToken, SyntaxFactory.TriviaList(SyntaxFactory.Space))))))).WithSemicolonToken(SyntaxFactory.
                                                                                                                                                                                                                                                                                                                         Token(SyntaxFactory.TriviaList(), SyntaxKind.SemicolonToken, SyntaxFactory.TriviaList(SyntaxFactory.LineFeed)));

                        node = node.ReplaceNode(node, NowePole);
                    }
                }
            }

            return(base.VisitFieldDeclaration(node));
        }
Beispiel #4
0
        public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            var variables = node.ChildNodes().OfType <VariableDeclaratorSyntax>();

            foreach (var variable in variables)
            {
                // Public i1, i2 As Integer
                // Public i3() As Integer, i4 As Integer

                // 先に型を取得
                var asNode       = variable.ChildNodes().FirstOrDefault(x => x is SimpleAsClauseSyntax);
                var variableType = asNode.ChildNodes().FirstOrDefault().ToString();

                // フィールド名を取得
                var fields = variable.ChildNodes().OfType <ModifiedIdentifierSyntax>();

                foreach (var field in fields)
                {
                    var startLength = field.Span.Start;
                    var endLength   = field.Span.End;

                    var fieldName = field.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ToString();
                    var fieldType = variableType;

                    var hasArrayRank = field.ChildNodes().Any();
                    if (hasArrayRank)
                    {
                        var arrayRank = field.ChildNodes().FirstOrDefault().ToString();
                        fieldType = $"{fieldType}{arrayRank}";
                    }

                    var parentNamespace = GetNamespace(DefineKinds.Field, startLength, endLength);

                    UserDefinitions.Add(new UserDefinition
                    {
                        DefineKinds    = DefineKinds.Field,
                        Namespace      = parentNamespace,
                        DefineName     = fieldName,
                        DefineFullName = $"{parentNamespace}.{fieldName}",
                        ReturnType     = ConvertCSharpType(fieldType),
                        SourceFile     = SourceFile,
                        StartLength    = startLength,
                        EndLength      = endLength,
                    });
                }
            }

            base.VisitFieldDeclaration(node);
        }
Beispiel #5
0
        public static FieldSpec LoadFromSyntaxNode(FieldDeclarationSyntax node)
        {
            FieldSpec fieldSpec = new FieldSpec();

            fieldSpec.IsConst    = node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ConstKeyword));
            fieldSpec.IsReadonly = node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ReadOnlyKeyword));
            fieldSpec.IsStatic   = node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));

            // Null access will throw and be caught in our overall analyzer handler.
            VariableDeclarationSyntax variable = node.ChildNodes()
                                                 .FirstOrDefault(childNode => childNode.IsKind(SyntaxKind.VariableDeclaration)) as VariableDeclarationSyntax;

            fieldSpec.Type = variable.Type.ToString();
            fieldSpec.Name = DocumentedElement.LoadFromSyntaxNode(variable, node.GetLeadingTrivia());
            return(fieldSpec);
        }
Beispiel #6
0
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            var firstNode = node.ChildNodes().OfType <VariableDeclarationSyntax>().FirstOrDefault();
            var tmpNode   = firstNode.ChildNodes().OfType <PredefinedTypeSyntax>().FirstOrDefault();
            var tmpString = tmpNode.ChildTokens().FirstOrDefault();

            if (tmpString.Kind() == SyntaxKind.StringKeyword)
            {
                var tmpDeclarator = firstNode.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault();

                var tmpId = tmpDeclarator.ChildTokens().FirstOrDefault();

                var ifNull = tmpDeclarator.ChildNodes().OfType <EqualsValueClauseSyntax>().FirstOrDefault();

                if (ifNull == null)
                {
                    SyntaxNode newNode = SyntaxFactory.VariableDeclaration(SyntaxFactory.PredefinedType(tmpString)).WithVariables(SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(SyntaxFactory.TriviaList(), tmpId.ToString(), SyntaxFactory.TriviaList(SyntaxFactory.Space))).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal("Pusty string"))).WithEqualsToken(SyntaxFactory.Token(SyntaxFactory.TriviaList(), SyntaxKind.EqualsToken, SyntaxFactory.TriviaList(SyntaxFactory.Space))))));

                    node = node.ReplaceNode(node, node.ReplaceNode(firstNode, newNode));
                }
            }

            return(base.VisitFieldDeclaration(node));
        }
Beispiel #7
0
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            var varDec = node.ChildNodes().OfType <VariableDeclarationSyntax>().FirstOrDefault();

            var predefType = varDec.ChildNodes().OfType <PredefinedTypeSyntax>().FirstOrDefault();

            var expr = varDec.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault()
                       .ChildNodes().OfType <EqualsValueClauseSyntax>().FirstOrDefault()
                       .ChildNodes().OfType <InvocationExpressionSyntax>().FirstOrDefault();

            var decl = varDec.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault();

            var exprName = expr.ChildNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();

            var newRight = SyntaxFactory.TriviaList();

            if (predefType.ToString().Equals("int") && exprName.ToString().Equals("Range"))
            {
                var argList = expr.ChildNodes().OfType <ArgumentListSyntax>().FirstOrDefault();

                var argListCount = argList.ChildNodes().OfType <ArgumentSyntax>().Count();
                if (argListCount == 2)
                {
                    var arg1 = argList.ChildNodes().OfType <ArgumentSyntax>().FirstOrDefault();
                    var arg2 = argList.ChildNodes().OfType <ArgumentSyntax>().Last();

                    var right = new SyntaxNodeOrToken[(int.Parse(arg2.ToString()) - int.Parse(arg1.ToString()) + 1) * 2 - 1];

                    int count = 0;
                    for (int i = int.Parse(arg1.ToString()); i <= int.Parse(arg2.ToString()); i++)
                    {
                        var val = SyntaxFactory.LiteralExpression(
                            SyntaxKind.NumericLiteralExpression,
                            SyntaxFactory.Literal(i));

                        right[count] = val;
                        count++;
                        if (i < int.Parse(arg2.ToString()))
                        {
                            right[count] = SyntaxFactory.Token(SyntaxKind.CommaToken);
                            count++;
                        }
                    }

                    for (int i = 0; i < count; i++)
                    {
                        Console.Write(right[i]);
                    }
                    Console.WriteLine();



                    var newFieldDecl = SyntaxFactory.FieldDeclaration(
                        SyntaxFactory.VariableDeclaration(
                            SyntaxFactory.ArrayType(
                                SyntaxFactory.PredefinedType(
                                    SyntaxFactory.Token(SyntaxKind.IntKeyword)))
                            .WithRankSpecifiers(
                                SyntaxFactory.SingletonList <ArrayRankSpecifierSyntax>(
                                    SyntaxFactory.ArrayRankSpecifier(
                                        SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(
                                            SyntaxFactory.OmittedArraySizeExpression())))))
                        .WithVariables(
                            SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                                SyntaxFactory.VariableDeclarator(
                                    SyntaxFactory.Identifier(decl.Identifier.ToString()))
                                .WithInitializer(
                                    SyntaxFactory.EqualsValueClause(
                                        SyntaxFactory.ImplicitArrayCreationExpression(
                                            SyntaxFactory.InitializerExpression(
                                                SyntaxKind.ArrayInitializerExpression,
                                                SyntaxFactory.SeparatedList <ExpressionSyntax>(
                                                    right))))))))
                                       .NormalizeWhitespace();

                    Console.WriteLine(newFieldDecl);

                    node = node.ReplaceNode(node, newFieldDecl);
                }
            }
            return(base.VisitFieldDeclaration(node).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
        }
Beispiel #8
0
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            var varDec = node.ChildNodes().OfType <VariableDeclarationSyntax>().FirstOrDefault();

            PredefinedTypeSyntax predefType = null;

            try
            {
                predefType = varDec.ChildNodes().OfType <PredefinedTypeSyntax>().Last();
            }
            catch (InvalidOperationException)
            {
                return(base.VisitFieldDeclaration(node).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
            }

            var trivia = predefType.GetLeadingTrivia();

            var predefTypeKind = predefType.ChildTokens().FirstOrDefault().Kind();

            if (kindList.Contains(predefTypeKind))
            {
                Console.WriteLine("Zawiera");
            }

            var expr = varDec.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault()
                       .ChildNodes().OfType <EqualsValueClauseSyntax>().FirstOrDefault()
                       .ChildNodes().OfType <InvocationExpressionSyntax>().FirstOrDefault();

            var decl = varDec.ChildNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault();

            var exprName = expr.ChildNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();

            var newRight = SyntaxFactory.TriviaList();

            if (kindList.Contains(predefTypeKind) && exprName.ToString().Equals("Range"))
            {
                var argList = expr.ChildNodes().OfType <ArgumentListSyntax>().FirstOrDefault();

                var argListCount = argList.ChildNodes().OfType <ArgumentSyntax>().Count();
                if (argListCount == 2)
                {
                    var arg1 = argList.ChildNodes().OfType <ArgumentSyntax>().FirstOrDefault();
                    var arg2 = argList.ChildNodes().OfType <ArgumentSyntax>().Last();

                    var right = new SyntaxNodeOrToken[5];

                    Console.WriteLine(arg1 + " : " + arg2);
                    int count = 0;

                    try
                    {
                        ExpandSnippet(arg1, arg2, ref right, ref count);

                        var newFieldDecl = SyntaxFactory.FieldDeclaration(
                            SyntaxFactory.VariableDeclaration(
                                SyntaxFactory.ArrayType(
                                    SyntaxFactory.PredefinedType(
                                        SyntaxFactory.Token(predefTypeKind)))
                                .WithRankSpecifiers(
                                    SyntaxFactory.SingletonList <ArrayRankSpecifierSyntax>(
                                        SyntaxFactory.ArrayRankSpecifier(
                                            SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(
                                                SyntaxFactory.OmittedArraySizeExpression())))))
                            .WithVariables(
                                SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                                    SyntaxFactory.VariableDeclarator(
                                        SyntaxFactory.Identifier(decl.Identifier.ToString()))
                                    .WithInitializer(
                                        SyntaxFactory.EqualsValueClause(
                                            SyntaxFactory.ImplicitArrayCreationExpression(
                                                SyntaxFactory.InitializerExpression(
                                                    SyntaxKind.ArrayInitializerExpression,
                                                    SyntaxFactory.SeparatedList <ExpressionSyntax>(
                                                        right)))))))).NormalizeWhitespace();


                        node = node.ReplaceNode(node, newFieldDecl);
                    }
                    catch (FormatException)
                    {
                        return(base.VisitFieldDeclaration(node).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
                    }
                }
            }
            return(base.VisitFieldDeclaration(node).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithLeadingTrivia(SyntaxFactory.ElasticWhitespace(trivia.ToString())));
        }