Beispiel #1
0
        private void CheckDirectCall(int count)
        {
            if (_callMethodSyntax is not null)
            {
                TypeSyntax[] typeArguments = _callMethodSyntax.TypeArgumentList.Arguments.ToArray();
                typeArguments[count] = _callTypeArguments.Dequeue();
                _callMethodSyntax    = _callMethodSyntax.WithTypeArgumentList(SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(typeArguments)));

                InvocationExpressionSyntax inv = SyntaxFactory.InvocationExpression(_callMethodSyntax, _callArguments !);
                StatementSyntax            statement;

                if (_applyReturnSyntax)
                {
                    statement = SyntaxFactory.ReturnStatement(SyntaxFactory.Token(SyntaxKind.ReturnKeyword).WithTrailingTrivia(SyntaxFactory.Space), inv, SyntaxFactory.Token(SyntaxKind.SemicolonToken));
                }
                else
                {
                    statement = SyntaxFactory.ExpressionStatement(inv);
                }

                statement = statement.WithLeadingTrivia(DefaultParamUtilities.GetTabs(_indentLevel + 1)).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

                CurrentDeclaration = CurrentDeclaration.WithBody(SyntaxFactory.Block(CurrentDeclaration.Body !.OpenBraceToken, SyntaxFactory.SingletonList(statement), CurrentDeclaration.Body.CloseBraceToken));
            }
        }
        /// <summary>
        /// Determines how many type parameters of the <see cref="OriginalDeclaration"/> should the <see cref="CurrentDeclaration"/> have.
        /// </summary>
        /// <param name="count">Number of type parameters to take.</param>
        public void WithTypeParameters(int count)
        {
            if (DefaultParamUtilities.TryUpdateTypeParameters(CurrentDeclaration.TypeParameterList, count, out TypeParameterListSyntax? updated))
            {
                CurrentDeclaration = CurrentDeclaration.WithTypeParameterList(updated);
            }

            SyntaxTokenList modifiers = CurrentDeclaration.Modifiers;

            if (!modifiers.Any())
            {
                SyntaxTriviaList trivia = CurrentDeclaration.DelegateKeyword.LeadingTrivia;

                if (trivia.Any())
                {
                    trivia             = trivia.RemoveAt(trivia.Count - 1);
                    CurrentDeclaration = CurrentDeclaration.WithDelegateKeyword(CurrentDeclaration.DelegateKeyword.WithLeadingTrivia(trivia));
                }
            }

            if (DefaultParamUtilities.TryAddNewModifier(_newModifierIndexes, count, _numNonDefaultParam, ref modifiers))
            {
                CurrentDeclaration = CurrentDeclaration.WithModifiers(modifiers);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Determines how many type parameters of the <see cref="OriginalDeclaration"/> should the <see cref="CurrentDeclaration"/> have.
        /// </summary>
        /// <param name="count">Number of type parameters to take.</param>
        public void WithTypeParameters(int count)
        {
            if (DefaultParamUtilities.TryUpdateTypeParameters(CurrentDeclaration.TypeParameterList, count, out TypeParameterListSyntax? updated))
            {
                CurrentDeclaration = CurrentDeclaration.WithTypeParameterList(updated);
            }

            CheckDirectCall(count);
            SyntaxTokenList modifiers = CurrentDeclaration.Modifiers;

            if (!modifiers.Any())
            {
                SyntaxTriviaList trivia = CurrentDeclaration.ReturnType.GetLeadingTrivia();

                if (trivia.Any())
                {
                    trivia             = trivia.RemoveAt(trivia.Count - 1);
                    CurrentDeclaration = CurrentDeclaration.WithReturnType(CurrentDeclaration.ReturnType.WithLeadingTrivia(trivia));
                }
            }

            if (DefaultParamUtilities.TryAddNewModifier(_newModifierIndexes, count, _numNonDefaultParam, ref modifiers))
            {
                CurrentDeclaration = CurrentDeclaration.WithModifiers(modifiers);
            }
        }
        public void SetData(DefaultParamDelegateData data, CancellationToken cancellationToken = default)
        {
            SemanticModel       = data.SemanticModel;
            OriginalDeclaration = data.Declaration;
            _newModifierIndexes = data.NewModifierIndexes;
            _numNonDefaultParam = data.TypeParameters.NumNonDefaultParam;

            DelegateDeclarationSyntax del = data.Declaration;

            if (del.TypeParameterList is null || !del.TypeParameterList.Parameters.Any())
            {
                _numOriginalTypeParameters = 0;
                CurrentDeclaration         = data.Declaration;
                return;
            }

            del = (DelegateDeclarationSyntax)DefaultParamUtilities.InitializeDeclaration(
                del,
                data.SemanticModel,
                data.ParentCompilation,
                del.TypeParameterList,
                cancellationToken,
                out TypeParameterListSyntax updatedParameters
                );

            _numOriginalConstraints    = del.ConstraintClauses.Count;
            _numOriginalTypeParameters = updatedParameters.Parameters.Count;

            CurrentDeclaration = del.WithTypeParameterList(updatedParameters);
        }
Beispiel #5
0
        /// <summary>
        /// Replaces <see cref="TypeParameterConstraintClauseSyntax"/>es of the <see cref="CurrentDeclaration"/> with the specified collection of <see cref="TypeParameterConstraintClauseSyntax"/>es.
        /// </summary>
        /// <param name="constraintClauses">Collection of <see cref="TypeParameterConstraintClauseSyntax"/> to apply to the <see cref="CurrentDeclaration"/>.</param>
        public void WithConstraintClauses(IEnumerable <TypeParameterConstraintClauseSyntax> constraintClauses)
        {
            SyntaxList <TypeParameterConstraintClauseSyntax> clauses = DefaultParamUtilities.ApplyConstraints(constraintClauses, _numOriginalConstraints);

            if (clauses.Any())
            {
                clauses = clauses.Replace(clauses.Last(), clauses.Last().WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
            }
            else if (!_symbol.IsAbstract)
            {
                CurrentDeclaration = CurrentDeclaration.WithParameterList(CurrentDeclaration.ParameterList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
            }

            CurrentDeclaration = CurrentDeclaration.WithConstraintClauses(clauses);
        }
        /// <summary>
        /// Replaces <see cref="TypeParameterConstraintClauseSyntax"/>es of the <see cref="CurrentDeclaration"/> with the specified collection of <see cref="TypeParameterConstraintClauseSyntax"/>es.
        /// </summary>
        /// <param name="constraintClauses">Collection of <see cref="TypeParameterConstraintClauseSyntax"/> to apply to the <see cref="CurrentDeclaration"/>.</param>
        public void WithConstraintClauses(IEnumerable <TypeParameterConstraintClauseSyntax> constraintClauses)
        {
            SyntaxList <TypeParameterConstraintClauseSyntax> clauses = DefaultParamUtilities.ApplyConstraints(constraintClauses, _numOriginalConstraints);

            if (clauses.Any())
            {
                clauses = clauses.Replace(clauses.Last(), clauses.Last().WithTrailingTrivia(null));
            }
            else if (CurrentDeclaration.ConstraintClauses.Any())
            {
                CurrentDeclaration = CurrentDeclaration.WithParameterList(CurrentDeclaration.ParameterList.WithoutTrailingTrivia());
            }

            CurrentDeclaration = CurrentDeclaration.WithConstraintClauses(clauses);
        }
        /// <summary>
        /// Determines whether the 'new' modifier is allowed to be applied to the target <see cref="ISymbol"/> according to the most specific <c>Durian.Configuration.DefaultParamConfigurationAttribute</c> or <c>Durian.Configuration.DefaultParamScopedConfigurationAttribute</c>.
        /// </summary>
        /// <param name="symbol"><see cref="ISymbol"/> to check.</param>
        /// <param name="compilation">Current <see cref="DefaultParamCompilationData"/>.</param>
        /// <param name="attributes">A collection of the target <see cref="ISymbol"/>'s attributes.</param>
        /// <param name="containingTypes"><see cref="INamedTypeSymbol"/>s that contain this <see cref="IMethodSymbol"/>.</param>
        public static bool AllowsNewModifier(
            ISymbol symbol,
            DefaultParamCompilationData compilation,
            IEnumerable <AttributeData>?attributes = null,
            INamedTypeSymbol[]?containingTypes     = null
            )
        {
            InitializeAttributes(ref attributes, symbol);
            InitializeContainingTypes(ref containingTypes, symbol);

            const string configPropertyName = MemberNames.Config_ApplyNewModifierWhenPossible;
            const string scopedPropertyName = MemberNames.Config_ApplyNewModifierWhenPossible;

            if (DefaultParamUtilities.TryGetConfigurationPropertyValue(attributes, compilation.DefaultParamConfigurationAttribute !, configPropertyName, out bool value))
            {
                return(value);
            }
Beispiel #8
0
        public void SetData(DefaultParamMethodData data, CancellationToken cancellationToken = default)
        {
            SemanticModel       = data.SemanticModel;
            OriginalDeclaration = data.Declaration;

            _symbol             = data.Symbol;
            _indentLevel        = DefaultParamUtilities.GetIndent(data.Declaration);
            _newModifierIndexes = data.NewModifierIndexes;
            _numNonDefaultParam = data.TypeParameters.NumNonDefaultParam;

            InitializeDeclaration(data, cancellationToken);

            if (data.CallInsteadOfCopying)
            {
                InitializeCallData(in data.TypeParameters);
            }
            else
            {
                _applyReturnSyntax = false;
                _callMethodSyntax  = null;
                _callArguments     = null;
                _callTypeArguments.Clear();
            }
        }