Sarcasm.Unparsing.Formatter.GetUtokensBetweenCommentAndOwner C# (CSharp) Method

GetUtokensBetweenCommentAndOwner() public method

public GetUtokensBetweenCommentAndOwner ( UnparsableAst owner, Comment comment, int commentIndex, int commentCount, IEnumerable commentsBetweenThisAndOwner ) : InsertedUtokens
owner UnparsableAst
comment Comment
commentIndex int
commentCount int
commentsBetweenThisAndOwner IEnumerable
return InsertedUtokens
        public virtual InsertedUtokens GetUtokensBetweenCommentAndOwner(UnparsableAst owner, Comment comment, int commentIndex, int commentCount,
            IEnumerable<Comment> commentsBetweenThisAndOwner)
        {
            if (comment.LineIndexDistanceFromOwner > 0)
            {
                int newLinesCount;

                if (comment.Placement == CommentPlacement.OwnerLeft)
                {
                    newLinesCount = comment.LineIndexDistanceFromOwner - (comment.TextLines.Length - 1);

                    Comment nextComment = commentsBetweenThisAndOwner.FirstOrDefault();
                    if (nextComment != null)
                        newLinesCount -= nextComment.LineIndexDistanceFromOwner;
                }
                else
                {
                    newLinesCount = comment.LineIndexDistanceFromOwner;

                    Comment prevComment = commentsBetweenThisAndOwner.FirstOrDefault();
                    if (prevComment != null)
                        newLinesCount -= prevComment.LineIndexDistanceFromOwner + (prevComment.TextLines.Length - 1);
                }

                if (comment.Placement == CommentPlacement.OwnerLeft && comment.Kind == CommentKind.SingleLine)
                {
                    /*
                     * We have already yielded a NewLine in Unparser.UnparseComment (see comment there),
                     * thus we have to yield one less "formatting" NewLine here.
                     * */
                    newLinesCount--;
                }

                return new UtokenRepeat(UtokenWhitespace.NewLine(), newLinesCount);
            }
            else
                return UtokenWhitespace.Space();
        }

Usage Example

Esempio n. 1
0
        internal IEnumerable <UtokenBase> YieldBeforeComments(UnparsableAst owner, Comments comments, UnparseCommentDelegate unparseComment)
        {
            IList <Comment> beforeComments;
            IList <Comment> beforeCommentsForFormatter;
            Func <IEnumerable <Comment>, int, IEnumerable <Comment> > skipOrTake;

            if (direction == Unparser.Direction.LeftToRight)
            {
                beforeComments             = comments.Left;
                beforeCommentsForFormatter = comments.Left;
                skipOrTake = Enumerable.Skip;
            }
            else
            {
                beforeComments             = comments.Right.ReverseOptimized();
                beforeCommentsForFormatter = comments.Right;
                skipOrTake = Enumerable.Take;
            }

            foreach (var beforeComment in beforeComments.Select((beforeComment, commentIndex) => new { Value = beforeComment, Index = commentIndex }))
            {
                int index;
                int commentCountToSkipOrTake;

                if (direction == Unparser.Direction.LeftToRight)
                {
                    index = beforeComment.Index;
                    commentCountToSkipOrTake = index + 1;
                }
                else
                {
                    index = beforeComments.Count - 1 - beforeComment.Index;
                    commentCountToSkipOrTake = index;
                }

                foreach (UtokenBase utoken in unparseComment(owner, beforeComment.Value))
                {
                    yield return(utoken);
                }

                yield return(formatter.GetUtokensBetweenCommentAndOwner(owner, beforeComment.Value, index, beforeComments.Count, skipOrTake(beforeCommentsForFormatter, commentCountToSkipOrTake))
                             .SetKind(InsertedUtokens.Kind.Between)
                             .SetAffected(owner));
            }
        }