Exemplo n.º 1
0
        /// <summary>
        /// Iterates over every top-level comment and prints its comment range, contents, and replies.
        /// </summary>
        private static void PrintAllCommentInfo(NodeCollection comments)
        {
            CommentInfoPrinter commentVisitor = new CommentInfoPrinter();

            // Iterate over all top level comments. Unlike reply-type comments, top-level comments have no ancestor.
            foreach (Comment comment in comments.Where(c => ((Comment)c).Ancestor == null))
            {
                // First, visit the start of the comment range.
                CommentRangeStart commentRangeStart = (CommentRangeStart)comment.PreviousSibling.PreviousSibling.PreviousSibling;
                commentRangeStart.Accept(commentVisitor);

                // Then, visit the comment, and any replies that it may have.
                comment.Accept(commentVisitor);

                foreach (Comment reply in comment.Replies)
                {
                    reply.Accept(commentVisitor);
                }

                // Finally, visit the end of the comment range, and then print the visitor's text contents.
                CommentRangeEnd commentRangeEnd = (CommentRangeEnd)comment.PreviousSibling;
                commentRangeEnd.Accept(commentVisitor);

                Console.WriteLine(commentVisitor.GetText());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Use an iterator and a visitor to print info of every comment from within a document.
        /// </summary>
        private static void PrintAllCommentInfo(List <Comment> comments)
        {
            // Create an object that inherits from the DocumentVisitor class
            CommentInfoPrinter commentVisitor = new CommentInfoPrinter();

            // Get the enumerator from the document's comment collection and iterate over the comments
            using (IEnumerator <Comment> enumerator = comments.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Comment currentComment = enumerator.Current;

                    // Accept our DocumentVisitor it to print information about our comments
                    if (currentComment != null)
                    {
                        // Get CommentRangeStart from our current comment and construct its information
                        CommentRangeStart commentRangeStart = (CommentRangeStart)currentComment.PreviousSibling.PreviousSibling.PreviousSibling;
                        commentRangeStart.Accept(commentVisitor);

                        // Construct current comment information
                        currentComment.Accept(commentVisitor);

                        // Get CommentRangeEnd from our current comment and construct its information
                        CommentRangeEnd commentRangeEnd = (CommentRangeEnd)currentComment.PreviousSibling;
                        commentRangeEnd.Accept(commentVisitor);
                    }
                }

                // Output of all information received
                Console.WriteLine(commentVisitor.GetText());
            }
        }