Exemplo n.º 1
0
        public override TDeclarationNode AddParameters <TDeclarationNode>(
            TDeclarationNode destinationMember,
            IEnumerable <IParameterSymbol> parameters,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            var memberDeclaration = destinationMember as MemberDeclarationSyntax;

            if (memberDeclaration == null)
            {
                return(destinationMember);
            }

            var currentParameterList = memberDeclaration.GetParameterList();

            if (currentParameterList == null)
            {
                return(destinationMember);
            }

            int  currentParamsCount = currentParameterList.Parameters.Count;
            bool seenOptional       = currentParamsCount > 0 && currentParameterList.Parameters[currentParamsCount - 1].Default != null;
            bool isFirstParam       = currentParamsCount == 0;

            var parameterNodesAndTokens = currentParameterList.Parameters.GetWithSeparators().ToList();

            foreach (var parameter in parameters)
            {
                if (parameterNodesAndTokens.Count > 0 && parameterNodesAndTokens.Last().Kind() != SyntaxKind.CommaToken)
                {
                    parameterNodesAndTokens.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                }

                var parameterSyntax = ParameterGenerator.GetParameter(parameter, options, isExplicit: false, isFirstParam: isFirstParam, seenOptional: seenOptional);
                parameterNodesAndTokens.Add(parameterSyntax);
                isFirstParam = false;
                seenOptional = seenOptional || parameterSyntax.Default != null;
            }

            var finalParameterList = currentParameterList.WithParameters(SyntaxFactory.SeparatedList <ParameterSyntax>(parameterNodesAndTokens));
            var finalMember        = memberDeclaration.WithParameterList(finalParameterList);

            return(Cast <TDeclarationNode>(finalMember));
        }
Exemplo n.º 2
0
        private static MemberDeclarationSyntax GenerateIndexerDeclaration(
            IPropertySymbol property,
            CodeGenerationDestination destination,
            Workspace workspace,
            CodeGenerationOptions options,
            ParseOptions parseOptions)
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

            var declaration = SyntaxFactory.IndexerDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options),
                modifiers: GenerateModifiers(property, destination, options),
                explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                parameterList: ParameterGenerator.GenerateBracketedParameterList(property.Parameters, explicitInterfaceSpecifier != null, options),
                type: property.GenerateTypeSyntax(),
                accessorList: GenerateAccessorList(property, destination, workspace, options, parseOptions));

            declaration = UseExpressionBodyIfDesired(workspace, declaration, parseOptions);

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       AddAnnotationsTo(property, declaration)));
        }
Exemplo n.º 3
0
        private static MethodDeclarationSyntax GenerateMethodDeclarationWorker(
            IMethodSymbol method, CodeGenerationDestination destination,
            Workspace workspace, CodeGenerationOptions options, ParseOptions parseOptions)
        {
            // Don't rely on destination to decide if method body should be generated.
            // Users of this service need to express their intention explicitly, either by
            // setting `CodeGenerationOptions.GenerateMethodBodies` to true, or making
            // `method` abstract. This would provide more flexibility.
            var hasNoBody = !options.GenerateMethodBodies || method.IsAbstract;

            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(method.ExplicitInterfaceImplementations);

            var methodDeclaration = SyntaxFactory.MethodDeclaration(
                attributeLists: GenerateAttributes(method, options, explicitInterfaceSpecifier != null),
                modifiers: GenerateModifiers(method, destination, options),
                returnType: method.GenerateReturnTypeSyntax(),
                explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                identifier: method.Name.ToIdentifierToken(),
                typeParameterList: GenerateTypeParameterList(method, options),
                parameterList: ParameterGenerator.GenerateParameterList(method.Parameters, explicitInterfaceSpecifier != null, options),
                constraintClauses: GenerateConstraintClauses(method),
                contractClauses: default,
Exemplo n.º 4
0
        internal static ConstructorDeclarationSyntax GenerateConstructorDeclaration(
            IMethodSymbol constructor, CodeGenerationDestination destination,
            Workspace workspace, CodeGenerationOptions options, ParseOptions parseOptions)
        {
            options = options ?? CodeGenerationOptions.Default;

            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <ConstructorDeclarationSyntax>(constructor, options);

            if (reusableSyntax != null)
            {
                return(reusableSyntax);
            }

            var hasNoBody = !options.GenerateMethodBodies;

            var declaration = SyntaxFactory.ConstructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(constructor.GetAttributes(), options),
                modifiers: GenerateModifiers(constructor, options),
                parameterList: ParameterGenerator.GenerateParameterList(constructor.Parameters, isExplicit: false, options: options),
                initializer: GenerateConstructorInitializer(constructor),
                body: hasNoBody ? null : GenerateBlock(constructor),
                expressionBody: null,
                eosToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default);