Exemple #1
0
        // indent is set by the containing VisitBlock
        public override SyntaxNode VisitTypeArgumentList(TypeArgumentListSyntax node)
        {
            var separatedSyntaxList = node.Arguments;

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

            m_CurrentIndent += k_IndentSize;
            node             = node
                               .WithLessThanToken(SyntaxFactory.Token(SyntaxKind.LessThanToken).WithTrailingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.LineFeed)))
                               .WithArguments(FormatList(separatedSyntaxList));
            var visited = base.VisitTypeArgumentList(node);

            m_CurrentIndent -= k_IndentSize;

            return(visited);
        }
        // Since TypeArgumentListSyntax and ArgumentListSyntax do not share a common base class with argument
        // access, it's easiest to just write the code out again.
        private static TypeArgumentListSyntax NormalizeTypeArgumentList(TypeArgumentListSyntax node)
        {
            int count = node.Arguments.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.LessThanToken.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.Arguments.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.Arguments[j].GetLeadingTrivia();
                SyntaxTriviaList trailing = node.Arguments[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.Arguments.GetSeparator(j).LeadingTrivia.Where(MultiLineCommentPredicate));
                argumentTriviasTrailing[j] = argumentTriviasTrailing[j].AddRange(leading);
            }
            if (count > 0)
            {
                // save trivia before close paren
                SyntaxTriviaList leading = node.GreaterThanToken.LeadingTrivia;
                argumentTriviasTrailing[count - 1] = argumentTriviasTrailing[count - 1].AddRange(leading);
            }

            bool multiline;
            int  indent;

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

            // strip all trivia
            node = node.WithLessThanToken(node.LessThanToken.WithTrailingTrivia(SyntaxTriviaList.Empty));
            node = node.WithGreaterThanToken(node.GreaterThanToken.WithLeadingTrivia(SyntaxTriviaList.Empty));
            for (int j = 0; j < count - 1; j++)
            {
                node = node.WithArguments(
                    node.Arguments.ReplaceSeparator(
                        node.Arguments.GetSeparator(j),
                        node.Arguments.GetSeparator(j).WithLeadingTrivia(SyntaxTriviaList.Empty).WithTrailingTrivia(SyntaxTriviaList.Empty)));
            }

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

            if (multiline)
            {
                node = node.WithLessThanToken(node.LessThanToken.WithTrailingTrivia(delimiterTrailing));
            }
            for (int j = 0; j < count - 1; j++)
            {
                node = node.WithArguments(
                    node.Arguments.ReplaceSeparator(
                        node.Arguments.GetSeparator(j),
                        node.Arguments.GetSeparator(j).WithTrailingTrivia(delimiterTrailing)));
            }

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

            return(node);
        }