Exemplo n.º 1
0
        private bool ContainsOneBlankLine(SyntaxTriviaList syntaxTriviaList)
        {
            int endOfLineCount         = syntaxTriviaList.Count(syntaxTrivia => syntaxTrivia.IsKind(SyntaxKind.EndOfLineTrivia));
            int singleCommentLineCount = syntaxTriviaList.Count(syntaxTrivia => syntaxTrivia.IsKind(SyntaxKind.SingleLineCommentTrivia));
            int multiCommentLineCount  = syntaxTriviaList.Count(syntaxTrivia => syntaxTrivia.IsKind(SyntaxKind.MultiLineCommentTrivia));

            // Counting only the "blank lines", so lines with comments do not count
            return(endOfLineCount - singleCommentLineCount - multiCommentLineCount == 1);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The delegate that receives control when nodes, tokens and trivia are visited.
        /// </summary>
        /// <remarks>
        /// This method performs most of the rendering work and for the most part simply displays the
        /// <paramref name="treeElement"/> using its <c>ToString()</c> method with the text indented to reflect the position of
        /// the child nodes and tokens. For example, to code
        /// <code>
        /// Console.WriteLine(new SyntaxTreeRenderer().Render("static void Main() {Console.WriteLine(\"Hello\");}"));
        /// </code>
        /// is rendered as
        /// <code>
        /// <![CDATA[
        /// >Node CompilationUnit |static void Main() {Console.WriteLine("Hello");}| [0..46)
        ///     >Node MethodDeclaration |static void Main() {Console.WriteLine("Hello");}| [0..46)
        ///         Token StaticKeyword |static| [0..6) 1 trailing trivia  Parent.Kind=MethodDeclaration
        ///         Trivia WhitespaceTrivia | | [6..7)  for Token StaticKeyword
        ///         >Node PredefinedType |void| [7..11)
        ///             Token VoidKeyword |void| [7..11) 1 trailing trivia  Parent.Kind=PredefinedType
        ///             Trivia WhitespaceTrivia | | [11..12)  for Token VoidKeyword
        ///         <Node PredefinedType |void| [7..11)
        ///         Token IdentifierToken |Main| [12..16)  Parent.Kind=MethodDeclaration
        ///         >Node ParameterList |()| [16..18)
        ///             Token OpenParenToken |(| [16..17)  Parent.Kind=ParameterList
        ///             Token CloseParenToken |)| [17..18) 1 trailing trivia  Parent.Kind=ParameterList
        ///             Trivia WhitespaceTrivia | | [18..19)  for Token CloseParenToken
        ///         <Node ParameterList |()| [16..18)
        ///         >Node Block |{Console.WriteLine("Hello");}| [19..46)
        ///             Token OpenBraceToken |{| [19..20)  Parent.Kind=Block
        ///             >Node ExpressionStatement |Console.WriteLine("Hello");| [20..45)
        ///                 >Node InvocationExpression |Console.WriteLine("Hello")| [20..44)
        ///                     >Node MemberAccessExpression |Console.WriteLine| [20..35)
        ///                         >Node IdentifierName |Console| [20..27)
        ///                             Token IdentifierToken |Console| [20..27)  Parent.Kind=IdentifierName
        ///                         <Node IdentifierName |Console| [20..27)
        ///                         Token DotToken |.| [27..28)  Parent.Kind=MemberAccessExpression
        ///                         >Node IdentifierName |WriteLine| [28..35)
        ///                             Token IdentifierToken |WriteLine| [28..35)  Parent.Kind=IdentifierName
        ///                         <Node IdentifierName |WriteLine| [28..35)
        ///                     <Node MemberAccessExpression |Console.WriteLine| [20..35)
        ///                     >Node ArgumentList |("Hello")| [35..44)
        ///                         Token OpenParenToken |(| [35..36)  Parent.Kind=ArgumentList
        ///                         >Node Argument |"Hello"| [36..43)
        ///                             >Node StringLiteralExpression |"Hello"| [36..43)
        ///                                 Token StringLiteralToken |"Hello"| [36..43)  Parent.Kind=StringLiteralExpression
        ///                             <Node StringLiteralExpression |"Hello"| [36..43)
        ///                         <Node Argument |"Hello"| [36..43)
        ///                         Token CloseParenToken |)| [43..44)  Parent.Kind=ArgumentList
        ///                     <Node ArgumentList |("Hello")| [35..44)
        ///                 <Node InvocationExpression |Console.WriteLine("Hello")| [20..44)
        ///                 Token SemicolonToken |;| [44..45)  Parent.Kind=ExpressionStatement
        ///             <Node ExpressionStatement |Console.WriteLine("Hello");| [20..45)
        ///             Token CloseBraceToken |}| [45..46)  Parent.Kind=Block
        ///         <Node Block |{Console.WriteLine("Hello");}| [19..46)
        ///     <Node MethodDeclaration |static void Main() {Console.WriteLine("Hello");}| [0..46)
        ///     Token EndOfFileToken || [46..46)  Parent.Kind=CompilationUnit
        /// <Node CompilationUnit |static void Main() {Console.WriteLine("Hello");}| [0..46)
        /// ]]>
        /// </code>
        /// </remarks>
        /// <param name="treeElement">A <see cref="SyntaxTreeElement"/> instance that contains the wrapped syntax tree element
        /// currently processed.</param>
        private void RenderElementCallback(SyntaxTreeElement treeElement)
        {
            Writer.Write("{0}{2}{1}",
                         Indent,
                         treeElement,
                         treeElement.IsNode ? '\u25BA'.ToString() : string.Empty);
            if (treeElement.IsNode)
            {
                Indent = new string(' ', 4 * (++NestingLevel));
            }
            else if (treeElement.IsToken)
            {
                SyntaxToken   token      = treeElement.Token;
                StringBuilder triviaInfo = new StringBuilder();

                if (token.HasLeadingTrivia)
                {
                    SyntaxTriviaList triviaList = token.LeadingTrivia;
                    triviaInfo.AppendFormat(" {0} leading trivia", triviaList.Count());
                    if (triviaList.Any(t => t.HasStructure))
                    {
                        triviaInfo.AppendFormat(" with {0} structured and the first is \"{1}\"",
                                                triviaList.Count(t => t.HasStructure),
                                                triviaList.First(t => t.HasStructure).ToString());
                    }
                }

                if (token.HasTrailingTrivia)
                {
                    SyntaxTriviaList triviaList = token.TrailingTrivia;
                    triviaInfo.AppendFormat(" {0} trailing trivia", triviaList.Count());
                    if (triviaList.Any(t => t.HasStructure))
                    {
                        triviaInfo.AppendFormat(" with {0} structured and the first is \"{1}\"",
                                                triviaList.Count(t => t.HasStructure),
                                                triviaList.First(t => t.HasStructure).ToString());
                    }
                }

                Writer.Write(triviaInfo.ToString());

                if (token.Parent != null)
                {
                    Writer.Write(string.Format("  Parent.Kind={0}", token.Parent.Kind()));
                }
            }
            else if (treeElement.IsTrivia)
            {
                Writer.Write(string.Format("  for Token {0}", treeElement.Trivia.Token.Kind()));
            }

            Writer.WriteLine();
        }
            protected SyntaxTriviaList CleanUpListWithNoWhitespaces(SyntaxTriviaList syntaxTrivias, bool itsForCloseBrace = false)
            {
                syntaxTrivias = ProcessSpecialTrivias(syntaxTrivias, itsForCloseBrace);

                var specialTriviasCount =
                    syntaxTrivias
                    .Count(t =>
                           !t.IsKind(SyntaxKind.EndOfLineTrivia) && !t.IsKind(SyntaxKind.WhitespaceTrivia)
                           );

                if (specialTriviasCount > 0)
                {
                    return(CleanUpList(syntaxTrivias));
                }

                return(CleanUpList(syntaxTrivias, 0));
            }
        protected SyntaxTriviaList ProcessSpecialTrivias(SyntaxTriviaList syntaxTrivias, bool itsForCloseBrace)
        {
            if (CheckShortSyntax(syntaxTrivias, itsForCloseBrace))
            {
                return(syntaxTrivias);
            }
            var specialTriviasCount = syntaxTrivias.Count(t => !t.IsKind(SyntaxKind.EndOfLineTrivia) && !t.IsKind(SyntaxKind.WhitespaceTrivia));

            var outputTriviasList  = new List <SyntaxTrivia>();
            var specialTiviasCount = 0;
            var bAddedBlankLine    = false;

            for (var i = 0; i < syntaxTrivias.Count; i++)
            {
                var countOfChars = 0;

                if (specialTiviasCount == specialTriviasCount)
                {
                    if (itsForCloseBrace)
                    {
                        if (CheckOption((int)CleanupTypes.Remove_BLs_after_Open_Bracket_and_Before_Close_Brackets))
                        {
                            i += RemoveBlankDuplication(syntaxTrivias, SyntaxKind.EndOfLineTrivia, i) + 1;

                            if (RemoveBlankDuplication(syntaxTrivias, SyntaxKind.WhitespaceTrivia, i) != -1)
                            {
                                outputTriviasList.Add(syntaxTrivias[i]);
                            }

                            i = syntaxTrivias.Count;
                        }
                        else
                        {
                            outputTriviasList.Add(syntaxTrivias[i]);
                        }

                        continue;
                    }
                }

                if
                (
                    (
                        syntaxTrivias[i].IsKind(SyntaxKind.EndOfLineTrivia) ||
                        syntaxTrivias[i].IsKind(SyntaxKind.WhitespaceTrivia) ||
                        syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia) ||
                        syntaxTrivias[i].IsKind(SyntaxKind.MultiLineCommentTrivia)
                    ) == false
                )
                {
                    outputTriviasList.Add(syntaxTrivias[i]);
                    specialTiviasCount++;
                    continue;
                }

                if (syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia) || syntaxTrivias[i].IsKind(SyntaxKind.MultiLineCommentTrivia))
                {
                    syntaxTrivias = Insert_Space_Before_Comment_Text(syntaxTrivias, syntaxTrivias[i]);

                    outputTriviasList.Add(syntaxTrivias[i]);
                    i++;
                    if (i < syntaxTrivias.Count && syntaxTrivias[i].IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        outputTriviasList.Add(syntaxTrivias[i]);
                    }

                    specialTiviasCount++;
                    continue;
                }

                if (CheckOption((int)CleanupTypes.Remove_DBL_Inside_Comments) == false)
                {
                    outputTriviasList.Add(syntaxTrivias[i]);

                    continue;
                }

                if ((countOfChars = RemoveBlankDuplication(syntaxTrivias, SyntaxKind.EndOfLineTrivia, i)) != -1)
                {
                    outputTriviasList.Add(syntaxTrivias[i]);
                    i += countOfChars + 1;
                    bAddedBlankLine = true;
                }

                if ((countOfChars = RemoveBlankDuplication(syntaxTrivias, SyntaxKind.WhitespaceTrivia, i)) != -1)
                {
                    outputTriviasList.Add(syntaxTrivias[i]);
                    i += countOfChars;
                }
                else if (bAddedBlankLine)
                {
                    i--;
                }

                bAddedBlankLine = false;
            }

            return(outputTriviasList.ToSyntaxTriviaList());
        }
            protected SyntaxTriviaList ProcessSpecialTrivias(SyntaxTriviaList syntaxTrivias, bool itsForCloseBrace)
            {
                if (CheckShortSyntax(syntaxTrivias, itsForCloseBrace))
                {
                    return(syntaxTrivias);
                }
                var specialTriviasCount = syntaxTrivias.Count(t => !t.IsKind(SyntaxKind.EndOfLineTrivia) && !t.IsKind(SyntaxKind.WhitespaceTrivia));

                var outputTriviasList  = new List <SyntaxTrivia>();
                var specialTiviasCount = 0;
                var bAddedBlankLine    = false;

                for (var i = 0; i < syntaxTrivias.Count; i++)
                {
                    var countOfChars = 0;

                    if (specialTiviasCount == specialTriviasCount)
                    {
                        if (itsForCloseBrace)
                        {
                            i += RemoveBlankDuplication(syntaxTrivias, SyntaxKind.EndOfLineTrivia, i) + 1;

                            if (RemoveBlankDuplication(syntaxTrivias, SyntaxKind.WhitespaceTrivia, i) != -1)
                            {
                                outputTriviasList.Add(syntaxTrivias[i]);
                            }
                            i = syntaxTrivias.Count;
                            continue;
                        }
                    }
                    if
                    (
                        (
                            syntaxTrivias[i].IsKind(SyntaxKind.EndOfLineTrivia) ||
                            syntaxTrivias[i].IsKind(SyntaxKind.WhitespaceTrivia) ||
                            syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia) ||
                            syntaxTrivias[i].IsKind(SyntaxKind.MultiLineCommentTrivia)
                        ) == false
                    )
                    {
                        outputTriviasList.Add(syntaxTrivias[i]);
                        specialTiviasCount++;
                        continue;
                    }

                    if (syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia) || syntaxTrivias[i].IsKind(SyntaxKind.MultiLineCommentTrivia))
                    {
                        if (syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia))
                        {
                            var commentText = syntaxTrivias[i].ToFullString().Trim();
                            if (commentText.Length > 2 && commentText[2] != ' ')
                            {
                                commentText = $"{commentText.Substring(0, 2)} {commentText.Substring(2)}";
                            }
                            syntaxTrivias = syntaxTrivias.Replace(syntaxTrivias[i], SyntaxFactory.Comment(commentText));
                        }
                        outputTriviasList.Add(syntaxTrivias[i]);
                        i++;
                        if (i < syntaxTrivias.Count && syntaxTrivias[i].IsKind(SyntaxKind.EndOfLineTrivia))
                        {
                            outputTriviasList.Add(syntaxTrivias[i]);
                        }
                        specialTiviasCount++;
                        continue;
                    }

                    if ((countOfChars = RemoveBlankDuplication(syntaxTrivias, SyntaxKind.EndOfLineTrivia, i)) != -1)
                    {
                        outputTriviasList.Add(syntaxTrivias[i]);
                        i += countOfChars + 1;
                        bAddedBlankLine = true;
                    }
                    if ((countOfChars = RemoveBlankDuplication(syntaxTrivias, SyntaxKind.WhitespaceTrivia, i)) != -1)
                    {
                        outputTriviasList.Add(syntaxTrivias[i]);
                        i += countOfChars;
                    }
                    else if (bAddedBlankLine)
                    {
                        i--;
                    }
                    bAddedBlankLine = false;
                }
                return(outputTriviasList.ToSyntaxTriviaList());
            }