Skip to content

Commit

Permalink
Optimizations related to directives (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Dec 3, 2023
1 parent eb5e546 commit e213afa
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/Core/Extensions/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@ public static bool SpanContainsDirectives(this SyntaxNode node)
throw new ArgumentNullException(nameof(node));

return node.ContainsDirectives
&& !node.GetLeadingTrivia().Any(f => f.IsDirective)
&& !node.GetTrailingTrivia().Any(f => f.IsDirective);
&& !node.GetLeadingTrivia().ContainsDirective()
&& !node.GetTrailingTrivia().ContainsDirective();
}

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

return node.ContainsDirectives
&& !node.GetTrailingTrivia().Any(f => f.IsDirective);
&& !node.GetTrailingTrivia().ContainsDirective();
}

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

return node.ContainsDirectives
&& !node.GetLeadingTrivia().Any(f => f.IsDirective);
&& !node.GetLeadingTrivia().ContainsDirective();
}

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

return node.ContainsDirectives
&& node.DescendantTrivia(span).Any(f => f.IsDirective);
if (node.ContainsDirectives)
{
foreach (SyntaxTrivia trivia in node.DescendantTrivia(span))
{
if (trivia.IsDirective)
return true;
}
}

return false;
}

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

return -1;
}

internal static bool ContainsDirective(this SyntaxTriviaList triviaList)
{
foreach (SyntaxTrivia trivia in triviaList)
{
if (trivia.IsDirective)
return true;
}

return false;
}
#endregion SyntaxTriviaList
}

0 comments on commit e213afa

Please sign in to comment.