Skip to content

Commit

Permalink
Support for c# 12 collection expressions
Browse files Browse the repository at this point in the history
closes #964
  • Loading branch information
belav committed Sep 29, 2023
1 parent 8c6a3b1 commit 0883144
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageVersion Include="Ignore" Version="0.1.48" />
<PackageVersion Include="ini-parser-netstandard" Version="2.5.2" />
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.6.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.7.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CSharp" Version="4.5.0" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageVersion Include="NUnit" Version="3.12.0" />
Expand All @@ -29,8 +29,8 @@
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="13.2.29" />
<PackageVersion Include="System.IO.Hashing" Version="7.0.0-preview.7.22375.6" />
<PackageVersion Include="System.Text.Encoding.CodePages" Version="7.0.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.3" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="YamlDotNet" Version="11.1.1" />
<PackageVersion Include="YamlDotNet" Version="13.4.0" />
</ItemGroup>
</Project>
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]
];
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);
}
}

0 comments on commit 0883144

Please sign in to comment.