Esempio n. 1
0
        public static MethodDeclarationSyntax WithParameters(this MethodDeclarationSyntax method, IEnumerable <string> args = null)
        {
            ParameterListSyntax parameters = SyntaxFactory.ParameterList();

            if (args?.Any() ?? false)
            {
                parameters = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(args.Select(arg => SyntaxFactory.Parameter(SyntaxFactory.Identifier(arg))
                                                                                                 .WithType(Types.Type(typeof(int))))));
            }
            parameters.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))
            .WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken));

            return(method.WithParameterList(parameters));
        }
Esempio n. 2
0
        // indent is set by the containing VisitMethodDeclaration or VisitBlock+VisitParenthesizedLambda
        public override SyntaxNode VisitParameterList(ParameterListSyntax node)
        {
            var separatedSyntaxList = node.Parameters;

            if (!LineSpanNeedsWrapping(node, separatedSyntaxList))
            {
                return(base.VisitParameterList(node));
            }

            node = node
                   .WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken).WithTrailingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.LineFeed)))
                   .WithParameters(FormatList(separatedSyntaxList));
            var visited = base.VisitParameterList(node);

            return(visited);
        }
Esempio n. 3
0
        private static ParameterListSyntax NormalizeParameterList(ParameterListSyntax node)
        {
            int count = node.Parameters.Count;

            SyntaxTriviaList[] argumentTriviasLeading  = new SyntaxTriviaList[count];
            SyntaxTriviaList[] argumentTriviasTrailing = new SyntaxTriviaList[count];
            for (int j = 0; j < count; j++)
            {
                argumentTriviasLeading[j]  = SyntaxTriviaList.Empty;
                argumentTriviasTrailing[j] = SyntaxTriviaList.Empty;
            }

            if (count > 0)
            {
                // save trivia after open paren
                SyntaxTriviaList trailing = SyntaxTriviaList.Empty.AddRange(node.OpenParenToken.TrailingTrivia.Where(MultiLineCommentPredicate));
                argumentTriviasLeading[0] = argumentTriviasLeading[0].AddRange(trailing);
            }
            for (int j = 0; j < count - 1; j++)
            {
                // save trivia on trailing edge of separator to next argument
                SyntaxTriviaList trailing = SyntaxTriviaList.Empty.AddRange(node.Parameters.GetSeparator(j).TrailingTrivia.Where(MultiLineCommentPredicate));
                argumentTriviasLeading[j + 1] = argumentTriviasLeading[j + 1].AddRange(trailing);
            }
            for (int j = 0; j < count; j++)
            {
                // save leading and trailing trivia from argument
                SyntaxTriviaList leading  = node.Parameters[j].GetLeadingTrivia();
                SyntaxTriviaList trailing = node.Parameters[j].GetTrailingTrivia();
                argumentTriviasLeading[j]  = argumentTriviasLeading[j].AddRange(leading);
                argumentTriviasTrailing[j] = argumentTriviasTrailing[j].AddRange(trailing);
            }
            for (int j = 0; j < count - 1; j++)
            {
                // save trivia on leading edge of separator to previous argument
                SyntaxTriviaList leading = SyntaxTriviaList.Empty.AddRange(node.Parameters.GetSeparator(j).LeadingTrivia.Where(MultiLineCommentPredicate));
                argumentTriviasTrailing[j] = argumentTriviasTrailing[j].AddRange(leading);
            }
            if (count > 0)
            {
                // save trivia before close paren
                SyntaxTriviaList leading = node.CloseParenToken.LeadingTrivia;
                argumentTriviasTrailing[count - 1] = argumentTriviasTrailing[count - 1].AddRange(leading);
            }

            bool multiline;
            int  indent;

            DetectMultiLineArgumentList(node.Parameters, out multiline, out indent);

            // strip all trivia
            node = node.WithOpenParenToken(node.OpenParenToken.WithTrailingTrivia(SyntaxTriviaList.Empty));
            node = node.WithCloseParenToken(node.CloseParenToken.WithLeadingTrivia(SyntaxTriviaList.Empty));
            for (int j = 0; j < count - 1; j++)
            {
                node = node.WithParameters(
                    node.Parameters.ReplaceSeparator(
                        node.Parameters.GetSeparator(j),
                        node.Parameters.GetSeparator(j).WithLeadingTrivia(SyntaxTriviaList.Empty).WithTrailingTrivia(SyntaxTriviaList.Empty)));
            }

            // reformat
            SyntaxTriviaList delimiterTrailing = CreateDelimiterTrailer(multiline, indent);

            if (multiline)
            {
                node = node.WithOpenParenToken(node.OpenParenToken.WithTrailingTrivia(delimiterTrailing));
            }
            for (int j = 0; j < count - 1; j++)
            {
                node = node.WithParameters(
                    node.Parameters.ReplaceSeparator(
                        node.Parameters.GetSeparator(j),
                        node.Parameters.GetSeparator(j).WithTrailingTrivia(delimiterTrailing)));
            }

            // reassociate comment trivia to arguments
            for (int j = 0; j < count; j++)
            {
                node = node.WithParameters(
                    node.Parameters.Replace(
                        node.Parameters[j],
                        node.Parameters[j]
                        .WithoutTrivia()
                        .WithLeadingTrivia(argumentTriviasLeading[j])
                        .WithTrailingTrivia(argumentTriviasTrailing[j])));
            }

            return(node);
        }