Exemplo n.º 1
0
        private static ImmutableArray<SyntaxKind> Fill(Array values, SyntaxKind[] exclude)
        {
            Requires.NotNull(values, nameof(values));
            Requires.NotNull(exclude, nameof(exclude));

            var SyntaxKinds = ImmutableArray.CreateBuilder<SyntaxKind>(values.Length);

            foreach (SyntaxKind SyntaxKind in values)
            {
                if (!exclude.Contains(SyntaxKind))
                {
                    SyntaxKinds.Add(SyntaxKind);
                }
            }

            return SyntaxKinds.ToImmutable();
        }
Exemplo n.º 2
0
        public PropertyDeclarationSyntax Convert(MethodDeclarationSyntax method, SemanticModel semanticModel = null)
        {
            if (method.Body == null && method.ExpressionBody != null && AreAllParentClassesPartial(method))
            {
                ISymbol methodSymbol = null;
                if (semanticModel != null)
                {
                    methodSymbol = semanticModel.GetDeclaredSymbol(method);
                }
                var parentClass    = (method.Parent as ClassDeclarationSyntax);
                var methodIsStatic = method.Modifiers.Any(SyntaxKind.StaticKeyword);
                var funcParamTypes =
                    (
                        methodIsStatic
                            ? new TypeSyntax[] { }
                            : Enumerable.Repeat(GetTypeOfClass(parentClass), 1)

                    ).Union(
                        method.ParameterList.Parameters.AsEnumerable()
                        .Select(p => p.Type.WithoutTrailingTrivia())
                        )
                    .Union(
                        Enumerable.Repeat(method.ReturnType.WithoutTrailingTrivia(), 1)
                        );

                var funcTypesSeperatedList = SyntaxFactory.SeparatedList <TypeSyntax>(funcParamTypes);
                var funcTypedArgumentList  = SyntaxFactory.TypeArgumentList(funcTypesSeperatedList);
                var funcIdentifier         = SyntaxFactory.Identifier("Func");
                var funcType = SyntaxFactory.GenericName(funcIdentifier, funcTypedArgumentList);

                var expressionIdentifier         = SyntaxFactory.Identifier("Expression");
                var expressionTypesSeperatedList = SyntaxFactory.SeparatedList <TypeSyntax>(Enumerable.Repeat(funcType, 1));
                var expressionTypedArgumentList  = SyntaxFactory.TypeArgumentList(expressionTypesSeperatedList);
                var expressionType = SyntaxFactory.GenericName(expressionIdentifier, expressionTypedArgumentList).WithTriviaFrom(method.ReturnType);

                var lambdaParameters =
                    SyntaxFactory.ParameterList(
                        methodIsStatic
                        ? RemoveThisFromExtensionMethod(method)
                        : BuildStaticParamsWith_this(method, parentClass)
                        ).WithTrailingTrivia(SyntaxFactory.Space);
                var methodExpression = (CSharpSyntaxNode) new ThisOrImplicitThisTo_ThisRewriter(semanticModel, methodSymbol).Visit(method.ExpressionBody.Expression).WithLeadingTrivia(method.ExpressionBody.Expression.GetLeadingTrivia());
                var lamdaExpression  = SyntaxFactory.ParenthesizedLambdaExpression(
                    SyntaxFactory.Token(SyntaxKind.None),
                    lambdaParameters.WithLeadingTrivia(SyntaxFactory.CarriageReturn, SyntaxFactory.LineFeed, method.GetLeadingTrivia().Last()),
                    method.ExpressionBody.ArrowToken,
                    methodExpression);
                var propertyExpressionBody = SyntaxFactory.ArrowExpressionClause
                                             (
                    SyntaxFactory.Token(SyntaxKind.EqualsGreaterThanToken),
                    lamdaExpression
                                             );
                var kindsToRemove = new SyntaxKind[] { SyntaxKind.VirtualKeyword, SyntaxKind.NewKeyword, SyntaxKind.OverrideKeyword };
                var modifiers     =
                    SyntaxFactory.TokenList(method.Modifiers.Where(t => !kindsToRemove.Contains(t.Kind())));

                if (!modifiers.Any(SyntaxKind.StaticKeyword))
                {
                    modifiers = SyntaxFactory.TokenList(modifiers.Union(Enumerable.Repeat(SyntaxFactory.Token(SyntaxKind.StaticKeyword).WithTrailingTrivia(SyntaxFactory.Space), 1)));
                }

                var propertyDec = SyntaxFactory.PropertyDeclaration(
                    method.AttributeLists,
                    modifiers,
                    expressionType,
                    null,
                    SyntaxFactory.Identifier(method.Identifier.Text + "_Expression").WithTrailingTrivia(SyntaxFactory.Space),
                    null,
                    propertyExpressionBody,
                    null,
                    SyntaxFactory.Token(SyntaxKind.SemicolonToken).WithTrailingTrivia(SyntaxFactory.CarriageReturn, SyntaxFactory.LineFeed)
                    ).WithLeadingTrivia(method.GetLeadingTrivia().Last());
                return(propertyDec);
            }
            else
            {
                return(null);
            }
        }