Skip to content

Commit e213afa

Browse files
authored
Optimizations related to directives (#1318)
1 parent eb5e546 commit e213afa

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/Core/Extensions/SyntaxExtensions.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ public static bool SpanContainsDirectives(this SyntaxNode node)
670670
throw new ArgumentNullException(nameof(node));
671671

672672
return node.ContainsDirectives
673-
&& !node.GetLeadingTrivia().Any(f => f.IsDirective)
674-
&& !node.GetTrailingTrivia().Any(f => f.IsDirective);
673+
&& !node.GetLeadingTrivia().ContainsDirective()
674+
&& !node.GetTrailingTrivia().ContainsDirective();
675675
}
676676

677677
internal static bool SpanOrLeadingTriviaContainsDirectives(this SyntaxNode node)
@@ -680,7 +680,7 @@ internal static bool SpanOrLeadingTriviaContainsDirectives(this SyntaxNode node)
680680
throw new ArgumentNullException(nameof(node));
681681

682682
return node.ContainsDirectives
683-
&& !node.GetTrailingTrivia().Any(f => f.IsDirective);
683+
&& !node.GetTrailingTrivia().ContainsDirective();
684684
}
685685

686686
internal static bool SpanOrTrailingTriviaContainsDirectives(this SyntaxNode node)
@@ -689,7 +689,7 @@ internal static bool SpanOrTrailingTriviaContainsDirectives(this SyntaxNode node
689689
throw new ArgumentNullException(nameof(node));
690690

691691
return node.ContainsDirectives
692-
&& !node.GetLeadingTrivia().Any(f => f.IsDirective);
692+
&& !node.GetLeadingTrivia().ContainsDirective();
693693
}
694694

695695
/// <summary>
@@ -702,8 +702,16 @@ public static bool ContainsDirectives(this SyntaxNode node, TextSpan span)
702702
if (node is null)
703703
throw new ArgumentNullException(nameof(node));
704704

705-
return node.ContainsDirectives
706-
&& node.DescendantTrivia(span).Any(f => f.IsDirective);
705+
if (node.ContainsDirectives)
706+
{
707+
foreach (SyntaxTrivia trivia in node.DescendantTrivia(span))
708+
{
709+
if (trivia.IsDirective)
710+
return true;
711+
}
712+
}
713+
714+
return false;
707715
}
708716

709717
/// <summary>
@@ -1434,5 +1442,16 @@ public static int IndexOf(this SyntaxTriviaList triviaList, Func<SyntaxTrivia, b
14341442

14351443
return -1;
14361444
}
1445+
1446+
internal static bool ContainsDirective(this SyntaxTriviaList triviaList)
1447+
{
1448+
foreach (SyntaxTrivia trivia in triviaList)
1449+
{
1450+
if (trivia.IsDirective)
1451+
return true;
1452+
}
1453+
1454+
return false;
1455+
}
14371456
#endregion SyntaxTriviaList
14381457
}

0 commit comments

Comments
 (0)