Add a GetDirectives method for CSharpSyntaxNode

Topics: APIs
May 22, 2014 at 3:10 PM
Please add a method to enumerate over all directives under a node's sub-tree. There are currently GetFirstDirective and GetLastDirective methods, but if I want to enumerate over all directives, I have to do something like this, which may not be the most efficient way to do it:
public static IEnumerable<DirectiveTriviaSyntax> GetDirectives(this CSharpSyntaxNode node)
    if (node.ContainsDirectives == false)
        yield break;

    var directive = node.GetFirstDirective();
    while (directive != null && node.FullSpan.Contains(directive.Span))
        yield return directive;
        directive = directive.GetNextDirective();
Thank you