private void ThrowIfInvalid(TextSpan span)
 {
     if (!FullSpan.Contains(span))
     {
         throw new ArgumentException("", nameof(span));
     }
 }
 private void ThrowIfInvalid(SyntaxTrivia trivia)
 {
     if (!FullSpan.Contains(trivia.FullSpan))
     {
         throw new ArgumentException("", nameof(trivia));
     }
 }
 private void ThrowIfInvalid(SyntaxToken token)
 {
     if (!FullSpan.Contains(token.FullSpan))
     {
         throw new ArgumentException("", nameof(token));
     }
 }
 private void ThrowIfInvalid(SyntaxNode node)
 {
     if (!FullSpan.Contains(node.FullSpan))
     {
         throw new ArgumentException("", nameof(node));
     }
 }
예제 #5
0
        /// <summary>
        /// Returns child node or token that contains given position.
        /// </summary>
        public override SyntaxNodeOrToken ChildThatContainsPosition(int position)
        {
            //PERF: it is very important to keep this method fast.

            if (!FullSpan.Contains(position))
            {
                throw new ArgumentOutOfRangeException("position");
            }

            SyntaxNodeOrToken childNodeOrToken = ChildSyntaxList.ChildThatContainsPosition(this, position);
            Debug.Assert(childNodeOrToken.FullSpan.Contains(position), "ChildThatContainsPosition's return value does not contain the requested position.");
            return childNodeOrToken;
        }
예제 #6
0
        public SyntaxToken FindToken(int position, bool descendIntoTrivia = false)
        {
            if (FullSpan.End == position)
            {
                var compilationUnit = this as CompilationUnitSyntax;
                if (compilationUnit != null)
                {
                    return(compilationUnit.EndOfFileToken);
                }
            }

            if (!FullSpan.Contains(position))
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }

            var child = (from nodeOrToken in ChildNodesAndTokens()
                         where nodeOrToken.FullSpan.Contains(position)
                         select nodeOrToken).First();

            if (child.IsNode)
            {
                return(child.AsNode().FindToken(position, descendIntoTrivia));
            }

            var token = child.AsToken();

            if (descendIntoTrivia)
            {
                var triviaStructure = (from t in token.LeadingTrivia.Concat(token.TrailingTrivia)
                                       where t.Structure != null && t.Structure.FullSpan.Contains(position)
                                       select t.Structure).FirstOrDefault();

                if (triviaStructure != null)
                {
                    return(triviaStructure.FindToken(position, true));
                }
            }

            return(token);
        }
예제 #7
0
        /// <summary>
        /// Returns child node or token that contains given position.
        /// </summary>
        public override SyntaxNodeOrToken ChildThatContainsPosition(int position)
        {
            //PERF: it is very important to keep this method fast.
            //      if there are ways to make it faster through some use of green nodes and such -
            //      it would be a welcome change.
            var childList = this.ChildNodesAndTokens();

            int left  = 0;
            int right = childList.Count - 1;

            while (left <= right)
            {
                int middle             = left + ((right - left) / 2);
                SyntaxNodeOrToken node = ChildSyntaxList.ItemInternal(childList.Node, middle);

                if (position < node.Position)
                {
                    right = middle - 1;
                }
                else
                {
                    if (position >= node.EndPosition)
                    {
                        left = middle + 1;
                        continue;
                    }

                    return(node);
                }
            }

            // we could check up front that index is within FullSpan,
            // but we want to optimize for the common case where position is valid.
            Debug.Assert(!FullSpan.Contains(position), "Position is valid. How could we not find a child?");
            throw new ArgumentOutOfRangeException("position");
        }