Skip to content

Commit b294d20

Browse files
feat: add FirstOrDefault overload (#1498)
Overload for `FirstOrDefault(this SyntaxTriviaList source`, stops `SyntaxTriviaList` being boxed saving 0.9MB ### Before ![image](https://github.com/user-attachments/assets/3cf4f0c6-de1a-465d-844a-6f371d405bf7) ### After ![image](https://github.com/user-attachments/assets/d030e41d-22ab-4cab-a213-d3f00604057d) Co-authored-by: Bela VanderVoort <[email protected]>
1 parent 9ed8b7d commit b294d20

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static Doc PrintWithTrailingComma<T>(
3232
// sometimes there are trailing commas with calls to Print (some patterns do that)
3333
// and if you pass null to PrintWithTrailingComma it won't add a trailing comma if there isn't one
3434
private static Doc Print<T>(
35-
SeparatedSyntaxList<T> list,
35+
in SeparatedSyntaxList<T> list,
3636
Func<T, PrintingContext, Doc> printFunc,
3737
Doc afterSeparator,
3838
PrintingContext context,

Src/CSharpier/Utilities/ListExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,23 @@ public static bool Any(this in SyntaxTriviaList triviaList, Func<SyntaxTrivia, b
3333

3434
return false;
3535
}
36+
37+
public static SyntaxTrivia FirstOrDefault(
38+
this in SyntaxTriviaList source,
39+
Func<SyntaxTrivia, bool> predicate
40+
)
41+
{
42+
var first = new SyntaxTrivia();
43+
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
44+
foreach (var trivia in source)
45+
{
46+
if (predicate(trivia))
47+
{
48+
first = trivia;
49+
break;
50+
}
51+
}
52+
53+
return first;
54+
}
3655
}

0 commit comments

Comments
 (0)