Skip to content

Commit

Permalink
Initial C#11 support (#686)
Browse files Browse the repository at this point in the history
* Getting basics of new features to format

* Getting raw string literals formatting

* Adding some tests

* finishing up initial support for 11

* Support for utf8 string literals

* unsigned right shift operator

* checked operators

* Generic math

* don't remove ignore file

* self code review
  • Loading branch information
belav authored Jun 13, 2022
1 parent 5ffc51e commit 0d3825f
Show file tree
Hide file tree
Showing 27 changed files with 217 additions and 26 deletions.
11 changes: 8 additions & 3 deletions Scripts/UpdateCSharpierRepos.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ $repositories += "https://github.com/dotnet/runtime.git"
$tempLocation = "c:\temp\UpdateRepos"

#TODO just pull it they exist already
Remove-Item -Recurse -Force $tempLocation
if (Test-Path $tempLocation) {
Remove-Item -Recurse -Force $tempLocation
}

New-Item $tempLocation -Force -ItemType Directory
Set-Location $tempLocation
Expand All @@ -26,6 +28,7 @@ foreach ($repository in $repositories)
& git clone $repository
}

#TODO make sure to switch to main/master
$destination = "C:\projects\csharpier-repos\"

Get-ChildItem $tempLocation | Copy-Item -Destination $destination -Filter *.cs -Recurse -Force
Expand All @@ -35,7 +38,7 @@ foreach ($item in $items) {
if ($item.Name -eq ".git") {
Remove-Item -Force -Recurse $item.FullName
}
if ($item.Extension -ne ".cs") {
if ($item.Extension -ne ".cs" -and $item.Name -ne ".csharpierignore") {
Remove-Item $item.FullName
}
}
Expand All @@ -45,4 +48,6 @@ foreach ($item in $items) {
if ($item.Name -eq ".git") {
Remove-Item -Force -Recurse $item.FullName
}
}
}

# TODO commit and push
2 changes: 1 addition & 1 deletion Src/CSharpier.Cli/CSharpier.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
<PackageReference Include="System.IO.Abstractions" Version="13.2.29" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="13.2.29" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<PackageReference Include="UTF.Unknown" Version="2.3.0" />
<PackageReference Include="YamlDotNet" Version="11.1.1" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.FakeGenerators/CSharpier.FakeGenerators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Src/CSharpier.FakeGenerators/Ignored.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class Ignored
{ typeof(IndexerDeclarationSyntax), new[] { "semicolon" } },
{ typeof(SyntaxTrivia), new[] { "token" } },
{ typeof(SyntaxToken), new[] { "value", "valueText" } },
{ typeof(ParameterSyntax), new[] { "exclamationExclamationToken" } }
{ typeof(ParameterSyntax), new[] { "exclamationExclamationToken" } },
};

public static readonly HashSet<string> UnsupportedNodes =
Expand Down
7 changes: 7 additions & 0 deletions Src/CSharpier.FakeGenerators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
while (!directory.Name.Equals("Src"))
{
directory = directory.Parent;
if (directory == null)
{
throw new Exception(
"Could not find the directory Src above the directory "
+ Directory.GetCurrentDirectory()
);
}
}

var codeContext = new CodeContext(Path.Combine(directory.FullName, "CSharpier"));
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Generators/CSharpier.Generators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
<PackageReference Include="Scriban" Version="5.4.0" IncludeAssets="Build" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
<PackageReference Include="Scriban" Version="4.0.1" IncludeAssets="Build" />
</ItemGroup>

Expand Down
10 changes: 10 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/AttributeLists.cst
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,13 @@ class ClassName
AnotherAttribute
]
public class ClassName { }

[GenericAttribute<string>()]
public class ClassName { }

[Generic<
LongName______________________,
LongName______________________,
LongName______________________
>]
public class ClassName { }
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,7 @@ class TestClass
== someOtherLongThing________________________________________________,
secondParameter
);

var x = one >>> two;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface BasicInterface : BaseInterface { }
public interface BasicInterface
{
void SomeMethod();
static abstract void SomeOtherMethod();
}

interface CoContra<out T, in K> { }
10 changes: 10 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/ListPatterns.cst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var someValue = someArray is [1, 2, 3];

var someValue =
triviaToMove
is [
..,
{ RawKind: (int)SyntaxKind.SingleLineCommentTrivia },
{ RawKind: (int)SyntaxKind.SingleLineCommentTrivia },
{ RawKind: (int)SyntaxKind.EndOfLineTrivia }
];
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class ClassName
{
return a.Add(b);
}

static Complex operator checked -(Complex value);

static BigInteger IUnaryNegationOperators<BigInteger, BigInteger>.operator checked -(
BigInteger value
) => -value;
}
75 changes: 69 additions & 6 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/StringLiterals.cst
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ three

private string stayOnLine1 =
@"one
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
two
";

private string stayOnLine2 =
$@"one
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
two
";

private string stayOnLine3 =
Expand All @@ -61,7 +61,7 @@ four";
two
three
four
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
five
"
);

Expand All @@ -70,7 +70,7 @@ jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksd
two
three
four
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
five
"
);

Expand All @@ -79,15 +79,70 @@ jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksd
two
three
four",
"two"
five
);

CallSomeLongMethod(
$@"one
two
three
four",
"two"
five
);

var multiLineRaw = """
This is a long message.
It has several lines.
Some are indented
more than others.
Some should start at the first column.
Some have "quoted text" in them.
""";

var multiLineRawInterpolated = $"""
This is a long message.
It has several lines.
Some are indented
more than others.
Some should start at the first column.
Some have "quoted text" in them.
""";

var shortRaw = """Short Raw String""";
var shortRawInterpolated = $"""Short Raw String""";
var longRaw =
"""Long Raw String """;
var longRawInterpolated =
$"""Long Raw String """;

CallSomeLongMethod(
"""
one
two
three
four
five
"""
);

CallSomeLongMethod(
"""
one
two
three
four
""",
five
);

CallSomeLongMethod(
one,
"""
two
three
four
""",
five
);

var expressionsInInterpolatedVerbatimStringDontBreak =
Expand All @@ -98,5 +153,13 @@ four",
// some comment
CallMethod()
} ";

var butKeepExistingBreaks =
$" {
// some comment
CallMethod()
} ";

ReadOnlySpan<byte> utf8StringLiteral = "hello"u8;
}
}
2 changes: 1 addition & 1 deletion Src/CSharpier/CSharpier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0-1.final" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CSharpier;

public class CodeFormatter
{
public static LanguageVersion LanguageVersion = LanguageVersion.CSharp10;
public static LanguageVersion LanguageVersion = LanguageVersion.Preview;

public static string Format(string code, CodeFormatterOptions? options = null)
{
Expand Down
8 changes: 8 additions & 0 deletions Src/CSharpier/SyntaxNodeComparer.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,8 @@ private CompareResult CompareConversionOperatorDeclarationSyntax(ConversionOpera
if (result.IsInvalid) return result;
originalStack.Push((originalNode.Body, originalNode));
formattedStack.Push((formattedNode.Body, formattedNode));
result = this.Compare(originalNode.CheckedKeyword, formattedNode.CheckedKeyword, originalNode, formattedNode);
if (result.IsInvalid) return result;
originalStack.Push((originalNode.ExplicitInterfaceSpecifier, originalNode));
formattedStack.Push((formattedNode.ExplicitInterfaceSpecifier, formattedNode));
originalStack.Push((originalNode.ExpressionBody, originalNode));
Expand All @@ -1172,6 +1174,8 @@ private CompareResult CompareConversionOperatorDeclarationSyntax(ConversionOpera
private CompareResult CompareConversionOperatorMemberCrefSyntax(ConversionOperatorMemberCrefSyntax originalNode, ConversionOperatorMemberCrefSyntax formattedNode)
{
CompareResult result;
result = this.Compare(originalNode.CheckedKeyword, formattedNode.CheckedKeyword, originalNode, formattedNode);
if (result.IsInvalid) return result;
result = this.Compare(originalNode.ImplicitOrExplicitKeyword, formattedNode.ImplicitOrExplicitKeyword, originalNode, formattedNode);
if (result.IsInvalid) return result;
if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode);
Expand Down Expand Up @@ -2630,6 +2634,8 @@ private CompareResult CompareOperatorDeclarationSyntax(OperatorDeclarationSyntax
if (result.IsInvalid) return result;
originalStack.Push((originalNode.Body, originalNode));
formattedStack.Push((formattedNode.Body, formattedNode));
result = this.Compare(originalNode.CheckedKeyword, formattedNode.CheckedKeyword, originalNode, formattedNode);
if (result.IsInvalid) return result;
originalStack.Push((originalNode.ExplicitInterfaceSpecifier, originalNode));
formattedStack.Push((formattedNode.ExplicitInterfaceSpecifier, formattedNode));
originalStack.Push((originalNode.ExpressionBody, originalNode));
Expand All @@ -2652,6 +2658,8 @@ private CompareResult CompareOperatorDeclarationSyntax(OperatorDeclarationSyntax
private CompareResult CompareOperatorMemberCrefSyntax(OperatorMemberCrefSyntax originalNode, OperatorMemberCrefSyntax formattedNode)
{
CompareResult result;
result = this.Compare(originalNode.CheckedKeyword, formattedNode.CheckedKeyword, originalNode, formattedNode);
if (result.IsInvalid) return result;
if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode);
result = this.Compare(originalNode.OperatorKeyword, formattedNode.OperatorKeyword, originalNode, formattedNode);
if (result.IsInvalid) return result;
Expand Down
24 changes: 24 additions & 0 deletions Src/CSharpier/SyntaxNodeJsonWriter.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,12 @@ public static void WriteConversionOperatorDeclarationSyntax(StringBuilder builde
WriteBlockSyntax(bodyBuilder, syntaxNode.Body);
properties.Add($"\"body\":{bodyBuilder.ToString()}");
}
if (syntaxNode.CheckedKeyword != default(SyntaxToken))
{
var checkedKeywordBuilder = new StringBuilder();
WriteSyntaxToken(checkedKeywordBuilder, syntaxNode.CheckedKeyword);
properties.Add($"\"checkedKeyword\":{checkedKeywordBuilder.ToString()}");
}
if (syntaxNode.ExplicitInterfaceSpecifier != default(ExplicitInterfaceSpecifierSyntax))
{
var explicitInterfaceSpecifierBuilder = new StringBuilder();
Expand Down Expand Up @@ -1921,6 +1927,12 @@ public static void WriteConversionOperatorMemberCrefSyntax(StringBuilder builder
var properties = new List<string>();
properties.Add($"\"nodeType\":\"{GetNodeType(syntaxNode.GetType())}\"");
properties.Add($"\"kind\":\"{syntaxNode.Kind().ToString()}\"");
if (syntaxNode.CheckedKeyword != default(SyntaxToken))
{
var checkedKeywordBuilder = new StringBuilder();
WriteSyntaxToken(checkedKeywordBuilder, syntaxNode.CheckedKeyword);
properties.Add($"\"checkedKeyword\":{checkedKeywordBuilder.ToString()}");
}
properties.Add(WriteBoolean("hasLeadingTrivia", syntaxNode.HasLeadingTrivia));
properties.Add(WriteBoolean("hasTrailingTrivia", syntaxNode.HasTrailingTrivia));
if (syntaxNode.ImplicitOrExplicitKeyword != default(SyntaxToken))
Expand Down Expand Up @@ -5714,6 +5726,12 @@ public static void WriteOperatorDeclarationSyntax(StringBuilder builder, Operato
WriteBlockSyntax(bodyBuilder, syntaxNode.Body);
properties.Add($"\"body\":{bodyBuilder.ToString()}");
}
if (syntaxNode.CheckedKeyword != default(SyntaxToken))
{
var checkedKeywordBuilder = new StringBuilder();
WriteSyntaxToken(checkedKeywordBuilder, syntaxNode.CheckedKeyword);
properties.Add($"\"checkedKeyword\":{checkedKeywordBuilder.ToString()}");
}
if (syntaxNode.ExplicitInterfaceSpecifier != default(ExplicitInterfaceSpecifierSyntax))
{
var explicitInterfaceSpecifierBuilder = new StringBuilder();
Expand Down Expand Up @@ -5776,6 +5794,12 @@ public static void WriteOperatorMemberCrefSyntax(StringBuilder builder, Operator
var properties = new List<string>();
properties.Add($"\"nodeType\":\"{GetNodeType(syntaxNode.GetType())}\"");
properties.Add($"\"kind\":\"{syntaxNode.Kind().ToString()}\"");
if (syntaxNode.CheckedKeyword != default(SyntaxToken))
{
var checkedKeywordBuilder = new StringBuilder();
WriteSyntaxToken(checkedKeywordBuilder, syntaxNode.CheckedKeyword);
properties.Add($"\"checkedKeyword\":{checkedKeywordBuilder.ToString()}");
}
properties.Add(WriteBoolean("hasLeadingTrivia", syntaxNode.HasLeadingTrivia));
properties.Add(WriteBoolean("hasTrailingTrivia", syntaxNode.HasTrailingTrivia));
properties.Add(WriteBoolean("isMissing", syntaxNode.IsMissing));
Expand Down
Loading

0 comments on commit 0d3825f

Please sign in to comment.