private IEnumerable <SyntaxTrivia> DescendantTriviaIntoTrivia(TextSpan span, Func <SyntaxNode, bool> descendIntoChildren) { using (var stack = new TwoEnumeratorListStack(this, descendIntoChildren)) { while (stack.IsNotEmpty) { switch (stack.PeekNext()) { case TwoEnumeratorListStack.Which.Node: SyntaxNodeOrToken value; if (stack.TryGetNextInSpan(ref span, out value)) { if (value.IsNode) { var nodeValue = value.AsNode(); stack.PushChildren(nodeValue, descendIntoChildren); } else if (value.IsToken) { var token = value.AsToken(); if (token.HasTrailingTrivia) { stack.PushTrailingTrivia(ref token); } if (token.HasLeadingTrivia) { stack.PushLeadingTrivia(ref token); } } } break; case TwoEnumeratorListStack.Which.Trivia: // yield structure nodes and enumerate their children SyntaxTrivia trivia; if (stack.TryGetNext(out trivia)) { // PERF: Push before yield return so that "trivia" is 'dead' after the yield // and therefore doesn't need to be stored in the iterator state machine. This // saves a field. if (trivia.HasStructure) { var structureNode = trivia.GetStructure(); stack.PushChildren(structureNode, descendIntoChildren); } if (IsInSpan(ref span, trivia.FullSpan)) { yield return(trivia); } } break; } } } }
private IEnumerable<SyntaxTrivia> DescendantTriviaIntoTrivia(TextSpan span, Func<SyntaxNode, bool> descendIntoChildren) { using (var stack = new TwoEnumeratorListStack(this, descendIntoChildren)) { while (stack.IsNotEmpty) { switch (stack.PeekNext()) { case TwoEnumeratorListStack.Which.Node: SyntaxNodeOrToken value; if (stack.TryGetNextInSpan(ref span, out value)) { if (value.IsNode) { var nodeValue = value.AsNode(); stack.PushChildren(nodeValue, descendIntoChildren); } else if (value.IsToken) { var token = value.AsToken(); if (token.HasTrailingTrivia) { stack.PushTrailingTrivia(ref token); } if (token.HasLeadingTrivia) { stack.PushLeadingTrivia(ref token); } } } break; case TwoEnumeratorListStack.Which.Trivia: // yield structure nodes and enumerate their children SyntaxTrivia trivia; if (stack.TryGetNext(out trivia)) { // PERF: Push before yield returnsca so that "trivia" is 'dead' after the yield // and therefore doesn't need to be stored in the iterator state machine. This // saves a field. if (trivia.HasStructure) { var structureNode = trivia.GetStructure(); stack.PushChildren(structureNode, descendIntoChildren); } if (IsInSpan(ref span, trivia.FullSpan)) { yield return trivia; } } break; } } } }