-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for c# 12 collection expressions
closes #964
- Loading branch information
Showing
3 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; | ||
|
||
Span<int> b = [ 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i' ]; | ||
|
||
string[] c = | ||
[ | ||
"________________________", | ||
"________________________", | ||
"________________________", | ||
"________________________" | ||
]; | ||
|
||
int[][] d = | ||
[ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9] | ||
]; |
38 changes: 38 additions & 0 deletions
38
Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CollectionExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters; | ||
|
||
internal static class CollectionExpression | ||
{ | ||
public static Doc Print(CollectionExpressionSyntax node, FormattingContext context) | ||
{ | ||
Doc separator = node.Parent | ||
is AssignmentExpressionSyntax | ||
or EqualsValueClauseSyntax { Parent: not PropertyDeclarationSyntax } | ||
? Doc.Line | ||
: Doc.Null; | ||
|
||
var alwaysBreak = | ||
node.Elements.FirstOrDefault() | ||
is ExpressionElementSyntax { Expression: CollectionExpressionSyntax }; | ||
|
||
var result = Doc.Concat( | ||
separator, | ||
Token.Print(node.OpenBracketToken, context), | ||
Doc.Indent( | ||
alwaysBreak ? Doc.HardLine : Doc.Line, | ||
SeparatedSyntaxList.Print( | ||
node.Elements, | ||
Node.Print, | ||
alwaysBreak ? Doc.HardLine : Doc.Line, | ||
context | ||
) | ||
), | ||
node.Elements.Any() | ||
? alwaysBreak | ||
? Doc.HardLine | ||
: Doc.Line | ||
: Doc.Null, | ||
Token.Print(node.CloseBracketToken, context) | ||
); | ||
return Doc.Group(result); | ||
} | ||
} |