From 107b4b57b055fdc3be5bbfcf1dfd1dac54a774cf Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Fri, 25 Oct 2024 18:03:45 -0400 Subject: [PATCH 1/9] Add support for generics constraint, many bug fixes --- Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs index b84663c..13c2df2 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs @@ -126,6 +126,7 @@ public override Cs VisitCompilationUnit(Cs.CompilationUnit compilationUnit, Prin return methodDeclaration; } + public override J? VisitAnnotatedStatement(Cs.AnnotatedStatement annotatedStatement, PrintOutputCapture p) { BeforeSyntax(annotatedStatement, CsSpace.Location.ANNOTATED_STATEMENT_PREFIX, p); @@ -659,7 +660,7 @@ public override J VisitBlock(J.Block block, PrintOutputCapture p) VisitSpace(block.End, Space.Location.BLOCK_END, p); } - else if (!block.Markers.OfType().Any() || block.Statements.Any()) + else if (block.Markers.FirstOrDefault(m => m is OmitBraces) == null) { p.Append('{'); VisitStatements(block.Padding.Statements, JRightPadded.Location.BLOCK_STATEMENT, p); From 6ce394fb919a38b435de1a7a1d3778bb7fb234f5 Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Tue, 29 Oct 2024 11:14:59 -0400 Subject: [PATCH 2/9] Add support for using statement --- Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs index 13c2df2..ddbed2a 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs @@ -660,7 +660,7 @@ public override J VisitBlock(J.Block block, PrintOutputCapture p) VisitSpace(block.End, Space.Location.BLOCK_END, p); } - else if (block.Markers.FirstOrDefault(m => m is OmitBraces) == null) + else if (!block.Markers.OfType().Any() || block.Statements.Any()) { p.Append('{'); VisitStatements(block.Padding.Statements, JRightPadded.Location.BLOCK_STATEMENT, p); From 3056c0356bba07918e859fbda771484ec6c1d46b Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Thu, 31 Oct 2024 22:51:07 -0400 Subject: [PATCH 3/9] support for method invocation ref & out modifiers (ex. TryParse("one", out int one)) support for tuple deconstruction (including nested kinds) var (x,y) = (1,2) & (var x, var y) = (1,2) & (var x, (var a, var b)) = (1,(2,3)) implicit array initializers new []{1,2,3} entirely new lst model for dealing with using statements (similar to java's try-with-resources) new Rewrite.Analyzers project that takes uses native .net code generation for all enums such asCsSpace, CsRightPadded, CsContainer, CsLeftPadded . code is contributed at compile time rather then design time, so no autogenerated files need to be checked in nor generator rerun explicitly - it's called automatically as part of build. It also generates a ToString() overload for all LST types to produce output from printer major improvement to c# project parser, nearly tripling its parsing speed by adding some parallel processing when using local printer, tests can now run in parallel vastly speeding up their execution (entire test suite now executes in under 5 seconds) --- .gitignore | 1 + Rewrite/Rewrite.Sources.sln | 7 + Rewrite/Rewrite.Sources.v3.ncrunchsolution | 8 + Rewrite/Rewrite.sln | 7 + .../src/Rewrite.Analyzers/DebugAnalyzer.sln | 21 + .../src/Rewrite.Analyzers/EnumGenerator.cs | 150 ++++ Rewrite/src/Rewrite.Analyzers/Extensions.cs | 36 + Rewrite/src/Rewrite.Analyzers/LstToString.cs | 67 ++ .../Properties/launchSettings.json | 9 + .../Rewrite.Analyzers.csproj | 29 + Rewrite/src/Rewrite.CSharp/CSharpParser.cs | 30 +- Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs | 119 ++- Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs | 144 +++- .../Parser/CSharpParserVisitor.cs | 244 ++++-- .../src/Rewrite.CSharp/Rewrite.CSharp.csproj | 5 + .../{NamedArgument.g.cs => Argument.g.cs} | 41 +- .../src/Rewrite.CSharp/Tree/Constructor.g.cs | 87 +++ .../Tree/ConstructorInitializer.g.cs | 124 ++++ Rewrite/src/Rewrite.CSharp/Tree/Cs.g.cs | 8 + .../src/Rewrite.CSharp/Tree/CsContainer.cs | 17 - .../src/Rewrite.CSharp/Tree/CsLeftPadded.cs | 15 - .../src/Rewrite.CSharp/Tree/CsRightPadded.cs | 42 -- Rewrite/src/Rewrite.CSharp/Tree/CsSpace.cs | 89 --- .../Tree/DeclarationExpression.g.cs | 93 +++ .../Tree/DiscardVariableDesignation.g.cs | 86 +++ Rewrite/src/Rewrite.CSharp/Tree/Extensions.cs | 49 +- Rewrite/src/Rewrite.CSharp/Tree/Keyword.g.cs | 80 ++ .../ParenthesizedVariableDesignation.g.cs | 124 ++++ .../Tree/SingleVariableDesignation.g.cs | 86 +++ .../Rewrite.CSharp/Tree/TupleExpression.g.cs | 123 +++ Rewrite/src/Rewrite.CSharp/Tree/Unary.g.cs | 135 ++++ .../Rewrite.CSharp/Tree/UsingStatement.g.cs | 8 +- .../Tree/VariableDesignation.g.cs | 29 + .../Rewrite.CSharp/Tree/_CommonOverrides.cs | 108 --- Rewrite/src/Rewrite.Core/AsyncHelper.cs | 28 + Rewrite/src/Rewrite.Java/Rewrite.Java.csproj | 6 +- Rewrite/src/Rewrite.Java/Tree/Extensions.cs | 2 +- .../src/Rewrite.Java/Tree/_CommonOverrides.cs | 307 -------- .../CSharp/CSharpReceiver.g.cs | 207 +++++- .../CSharp/CSharpSender.g.cs | 104 ++- Rewrite/src/Rewrite.Test.CSharp/Assertions.cs | 4 +- .../src/Rewrite.Test/LocalPrinterFixture.cs | 7 +- .../src/Rewrite.Test/SourceSpecExtensions.cs | 6 + .../Properties/AssemblyInfo.cs | 3 + .../Rewrite.CSharp.Tests.v3.ncrunchproject | 27 + .../tests/Rewrite.CSharp.Tests/TestSetup.cs | 27 + .../Tree/ArrayAccessTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/ArrayTests.cs | 7 +- .../Tree/ArrayTypeTests.cs | 1 - .../Tree/AssignmentTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/AsyncTests.cs | 2 +- .../Tree/AttributeTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/BinaryTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/BlockTests.cs | 1 - .../Tree/ClassDeclarationTests.cs | 3 - .../Tree/CollectionExpressionTests.cs | 1 - .../Tree/CompilationUnitTests.cs | 1 - .../Tree/ConstructorTests.cs | 44 ++ .../Rewrite.CSharp.Tests/Tree/EmptyTests.cs | 1 - .../Tree/ExternAliasTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/FieldTests.cs | 1 - .../Tree/ForEachLoopTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/ForLoopTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/GlobalTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/IfTests.cs | 1 - .../Tree/InstanceOfTests.cs | 1 - .../Tree/InterfaceDeclarationTests.cs | 1 - .../Tree/InterpolatedStringTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/LabelTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/LambdaTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/LiteralTests.cs | 1 - .../Tree/MemberAccessTests.cs | 1 - .../Tree/MethodDeclarationTests.cs | 1 - .../Tree/MethodInvocationTests.cs | 54 +- .../Tree/NamespaceDeclarationTests.cs | 1 - .../Tree/NewClassTests.cs | 1 - .../Tree/NullSafeExpressionTests.cs | 3 - .../Tree/NullableTypeTests.cs | 1 - .../Tree/ParenthesesTests.cs | 1 - .../Tree/PropertyDeclarationTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/ReturnTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/SizeOfTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/StructTests.cs | 1 - .../Tree/SwitchStatementTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/TernaryTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/ThrowTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/TryTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/TupleTests.cs | 49 ++ .../Tree/TypeCastTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/TypeOfTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/UnaryTests.cs | 15 +- .../Tree/UsingDirectiveTests.cs | 1 - .../Rewrite.CSharp.Tests/Tree/UsingTests.cs | 1 - .../Tree/VariableDeclarationsTests.cs | 1 - .../Tree/WhileLoopTests.cs | 1 - build.gradle.kts | 3 - .../csharp/remote/CSharpReceiver.java | 198 ++++- .../csharp/remote/CSharpSender.java | 104 ++- .../openrewrite/csharp/CSharpIsoVisitor.java | 49 +- .../org/openrewrite/csharp/CSharpPrinter.java | 102 ++- .../org/openrewrite/csharp/CSharpVisitor.java | 135 +++- .../java/org/openrewrite/csharp/tree/Cs.java | 698 +++++++++++++++++- .../openrewrite/csharp/tree/CsContainer.java | 8 +- .../openrewrite/csharp/tree/CsLeftPadded.java | 2 +- .../csharp/tree/CsRightPadded.java | 10 +- .../org/openrewrite/csharp/tree/CsSpace.java | 39 +- 106 files changed, 3688 insertions(+), 827 deletions(-) create mode 100644 Rewrite/Rewrite.Sources.v3.ncrunchsolution create mode 100644 Rewrite/src/Rewrite.Analyzers/DebugAnalyzer.sln create mode 100644 Rewrite/src/Rewrite.Analyzers/EnumGenerator.cs create mode 100644 Rewrite/src/Rewrite.Analyzers/Extensions.cs create mode 100644 Rewrite/src/Rewrite.Analyzers/LstToString.cs create mode 100644 Rewrite/src/Rewrite.Analyzers/Properties/launchSettings.json create mode 100644 Rewrite/src/Rewrite.Analyzers/Rewrite.Analyzers.csproj rename Rewrite/src/Rewrite.CSharp/Tree/{NamedArgument.g.cs => Argument.g.cs} (68%) create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/Constructor.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/ConstructorInitializer.g.cs delete mode 100644 Rewrite/src/Rewrite.CSharp/Tree/CsContainer.cs delete mode 100644 Rewrite/src/Rewrite.CSharp/Tree/CsLeftPadded.cs delete mode 100644 Rewrite/src/Rewrite.CSharp/Tree/CsRightPadded.cs delete mode 100644 Rewrite/src/Rewrite.CSharp/Tree/CsSpace.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/DeclarationExpression.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/DiscardVariableDesignation.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/Keyword.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/ParenthesizedVariableDesignation.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/SingleVariableDesignation.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/TupleExpression.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/Unary.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/VariableDesignation.g.cs delete mode 100644 Rewrite/src/Rewrite.CSharp/Tree/_CommonOverrides.cs create mode 100644 Rewrite/src/Rewrite.Core/AsyncHelper.cs delete mode 100644 Rewrite/src/Rewrite.Java/Tree/_CommonOverrides.cs create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/Properties/AssemblyInfo.cs create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.v3.ncrunchproject create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/Tree/ConstructorTests.cs create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs diff --git a/.gitignore b/.gitignore index 2708713..2a7bfee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. +**/.NCrunch*/** **/fixtures/** **/GeneratedCertificates/** diff --git a/Rewrite/Rewrite.Sources.sln b/Rewrite/Rewrite.Sources.sln index fbd8697..06af622 100644 --- a/Rewrite/Rewrite.Sources.sln +++ b/Rewrite/Rewrite.Sources.sln @@ -47,6 +47,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Test.Engine.Remote" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Server", "..\..\..\moderneinc\rewrite-remote\Rewrite.Remote\src\Rewrite.Server\Rewrite.Server.csproj", "{E0721BA1-2772-4486-BDF3-FC9301723BB9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Analyzers", "src\Rewrite.Analyzers\Rewrite.Analyzers.csproj", "{9818146E-C516-4D3C-80CE-A84741E7772B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -115,6 +117,10 @@ Global {E0721BA1-2772-4486-BDF3-FC9301723BB9}.Debug|Any CPU.Build.0 = Debug|Any CPU {E0721BA1-2772-4486-BDF3-FC9301723BB9}.Release|Any CPU.ActiveCfg = Release|Any CPU {E0721BA1-2772-4486-BDF3-FC9301723BB9}.Release|Any CPU.Build.0 = Release|Any CPU + {9818146E-C516-4D3C-80CE-A84741E7772B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9818146E-C516-4D3C-80CE-A84741E7772B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9818146E-C516-4D3C-80CE-A84741E7772B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9818146E-C516-4D3C-80CE-A84741E7772B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {25EEACCB-9BAE-4361-A980-9E9CDA37602E} = {7B268266-B9CD-4D85-ABC5-3B2C4611FD74} @@ -133,5 +139,6 @@ Global {5C70ACE0-BE37-4D67-A67F-EE69767994D2} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} {C04C7CA4-69C3-444F-A654-6C722A0FC3BE} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} {E0721BA1-2772-4486-BDF3-FC9301723BB9} = {45B6292D-DC7E-46A7-A86E-7455BBEAED70} + {9818146E-C516-4D3C-80CE-A84741E7772B} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} EndGlobalSection EndGlobal diff --git a/Rewrite/Rewrite.Sources.v3.ncrunchsolution b/Rewrite/Rewrite.Sources.v3.ncrunchsolution new file mode 100644 index 0000000..1ef1d71 --- /dev/null +++ b/Rewrite/Rewrite.Sources.v3.ncrunchsolution @@ -0,0 +1,8 @@ + + + False + True + True + True + + \ No newline at end of file diff --git a/Rewrite/Rewrite.sln b/Rewrite/Rewrite.sln index 8afcb17..427a91f 100644 --- a/Rewrite/Rewrite.sln +++ b/Rewrite/Rewrite.sln @@ -37,6 +37,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_solution", "_solution", "{ Directory.Build.props.user = Directory.Build.props.user EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Analyzers", "src\Rewrite.Analyzers\Rewrite.Analyzers.csproj", "{8623AABC-D198-4D56-B26B-14A503B226E1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -91,6 +93,10 @@ Global {6EC419CB-E60B-4F06-A710-B02EA6B9AAAC}.Debug|Any CPU.Build.0 = Debug|Any CPU {6EC419CB-E60B-4F06-A710-B02EA6B9AAAC}.Release|Any CPU.ActiveCfg = Release|Any CPU {6EC419CB-E60B-4F06-A710-B02EA6B9AAAC}.Release|Any CPU.Build.0 = Release|Any CPU + {8623AABC-D198-4D56-B26B-14A503B226E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8623AABC-D198-4D56-B26B-14A503B226E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8623AABC-D198-4D56-B26B-14A503B226E1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8623AABC-D198-4D56-B26B-14A503B226E1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {25EEACCB-9BAE-4361-A980-9E9CDA37602E} = {7B268266-B9CD-4D85-ABC5-3B2C4611FD74} @@ -105,5 +111,6 @@ Global {D3F48947-C702-4D1A-B6EE-96B89D13953D} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} {D38BB305-3276-4AE1-87B1-C19C7A9F3840} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} {F7F4F27F-2CAE-422B-B6CE-25A88C3603A1} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} + {8623AABC-D198-4D56-B26B-14A503B226E1} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5} EndGlobalSection EndGlobal diff --git a/Rewrite/src/Rewrite.Analyzers/DebugAnalyzer.sln b/Rewrite/src/Rewrite.Analyzers/DebugAnalyzer.sln new file mode 100644 index 0000000..cde3071 --- /dev/null +++ b/Rewrite/src/Rewrite.Analyzers/DebugAnalyzer.sln @@ -0,0 +1,21 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Analyzers", "Rewrite.Analyzers.csproj", "{A9BDECE3-D39F-48AE-A94E-B546887F5EC2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.CSharp", "..\Rewrite.CSharp\Rewrite.CSharp.csproj", "{E18B9AF4-A15B-4A68-AD48-6F4795DAFA38}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A9BDECE3-D39F-48AE-A94E-B546887F5EC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9BDECE3-D39F-48AE-A94E-B546887F5EC2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9BDECE3-D39F-48AE-A94E-B546887F5EC2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9BDECE3-D39F-48AE-A94E-B546887F5EC2}.Release|Any CPU.Build.0 = Release|Any CPU + {E18B9AF4-A15B-4A68-AD48-6F4795DAFA38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E18B9AF4-A15B-4A68-AD48-6F4795DAFA38}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E18B9AF4-A15B-4A68-AD48-6F4795DAFA38}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Rewrite/src/Rewrite.Analyzers/EnumGenerator.cs b/Rewrite/src/Rewrite.Analyzers/EnumGenerator.cs new file mode 100644 index 0000000..bff192c --- /dev/null +++ b/Rewrite/src/Rewrite.Analyzers/EnumGenerator.cs @@ -0,0 +1,150 @@ +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace Rewrite.Analyzers; + +[Generator] +public class EnumGenerator : ISourceGenerator +{ + public void Initialize(GeneratorInitializationContext context) + { + context.RegisterForSyntaxNotifications(() => new SyntaxScanner()); + } + + public void Execute(GeneratorExecutionContext context) + { + var scanner = (SyntaxScanner)context.SyntaxReceiver!; + + scanner.CsRightPaddedEnumValues.UnionWith(scanner.CsContainerEnumValues); + scanner.CsSpaceEnumValues.UnionWith(scanner.CsContainerEnumValues); + scanner.CsSpaceEnumValues.UnionWith(scanner.CsRightPaddedEnumValues); + scanner.CsSpaceEnumValues.UnionWith(scanner.CsLeftPaddedEnumValues); + // scanner.CsLeftPaddedEnumValues.UnionWith(scanner.CsContainerEnumValues); + var csContainerSrc = $$""" + namespace Rewrite.RewriteCSharp.Tree; + + public interface CsContainer + { + public record Location(CsSpace.Location BeforeLocation, CsRightPadded.Location ElementLocation) + { + {{ scanner.CsContainerEnumValues.Render(enumValue => $$""" + public static readonly Location {{enumValue}} = new(CsSpace.Location.{{enumValue}}, CsRightPadded.Location.{{enumValue}}); + """, "\n").Ident(2) }} + } + } + """; + var csSpaceSrc = $$""" + namespace Rewrite.RewriteCSharp.Tree; + + public interface CsSpace + { + public record Location + { + {{ scanner.CsSpaceEnumValues.Render(enumValue => $$""" + public static readonly Location {{enumValue}} = new(); + """, "\n").Ident(2) }} + } + } + """; + + var csRightPaddedSrc = $$""" + namespace Rewrite.RewriteCSharp.Tree; + + public interface CsRightPadded + { + public record Location(CsSpace.Location AfterLocation) + { + {{ scanner.CsRightPaddedEnumValues.Render(enumValue => $$""" + public static readonly Location {{enumValue}} = new (CsSpace.Location.{{enumValue}}); + """, "\n").Ident(2) }} + } + } + """; + + var csLeftPaddedSrc = $$""" + namespace Rewrite.RewriteCSharp.Tree; + + public interface CsLeftPadded + { + public record Location(CsSpace.Location BeforeLocation) + { + {{ scanner.CsLeftPaddedEnumValues.Render(enumValue => $$""" + public static readonly Location {{enumValue}} = new (CsSpace.Location.{{enumValue}}); + """, "\n").Ident(2) }} + } + } + """; + if(scanner.CsContainerEnumValues.Count > 0) + context.AddSource("CsContainer.gs", SourceText.From(csContainerSrc, Encoding.UTF8)); + if(scanner.CsRightPaddedEnumValues.Count > 0) + context.AddSource("CsRightPadded.gs", SourceText.From(csRightPaddedSrc, Encoding.UTF8)); + if(scanner.CsLeftPaddedEnumValues.Count > 0) + context.AddSource("CsLeftPadded.gs", SourceText.From(csLeftPaddedSrc, Encoding.UTF8)); + if(scanner.CsSpaceEnumValues.Count > 0) + context.AddSource("CsSpace.gs", SourceText.From(csSpaceSrc, Encoding.UTF8)); + } + public class SyntaxScanner : ISyntaxReceiver + { + public HashSet CsContainerEnumValues { get; } = new(); + public HashSet CsRightPaddedEnumValues { get; } = new(); + public HashSet CsLeftPaddedEnumValues { get; } = new(); + public HashSet CsSpaceEnumValues { get; } = new(); + public string? AssemblyFileVersion { get; set; } + + public void OnVisitSyntaxNode(SyntaxNode syntaxNode) + { + if (syntaxNode is MemberAccessExpressionSyntax + { + Expression: MemberAccessExpressionSyntax + { + Expression: IdentifierNameSyntax { Identifier.Text: "CsContainer" }, + Name: IdentifierNameSyntax { Identifier.Text: "Location" }, + } + } csContainerLocation) + { + + CsContainerEnumValues.Add(csContainerLocation.Name.Identifier.Text); + } + + if (syntaxNode is MemberAccessExpressionSyntax + { + Expression: MemberAccessExpressionSyntax + { + Expression: IdentifierNameSyntax { Identifier.Text: "CsRightPadded" }, + Name: IdentifierNameSyntax { Identifier.Text: "Location" }, + } + } csRightPaddedLocation) + { + + CsRightPaddedEnumValues.Add(csRightPaddedLocation.Name.Identifier.Text); + } + if (syntaxNode is MemberAccessExpressionSyntax + { + Expression: MemberAccessExpressionSyntax + { + Expression: IdentifierNameSyntax { Identifier.Text: "CsLeftPadded" }, + Name: IdentifierNameSyntax { Identifier.Text: "Location" }, + } + } csLeftPaddedLocation) + { + + CsLeftPaddedEnumValues.Add(csLeftPaddedLocation.Name.Identifier.Text); + } + if (syntaxNode is MemberAccessExpressionSyntax + { + Expression: MemberAccessExpressionSyntax + { + Expression: IdentifierNameSyntax { Identifier.Text: "CsSpace" }, + Name: IdentifierNameSyntax { Identifier.Text: "Location" }, + } + } csSpaceLocation) + { + + CsSpaceEnumValues.Add(csSpaceLocation.Name.Identifier.Text); + } + + } + } +} diff --git a/Rewrite/src/Rewrite.Analyzers/Extensions.cs b/Rewrite/src/Rewrite.Analyzers/Extensions.cs new file mode 100644 index 0000000..8e43bd6 --- /dev/null +++ b/Rewrite/src/Rewrite.Analyzers/Extensions.cs @@ -0,0 +1,36 @@ +using Microsoft.CodeAnalysis; + +namespace Rewrite.Analyzers; + +public static class Extensions +{ + public static bool InheritsFrom(this INamedTypeSymbol symbol, string type) + { + var current = symbol.BaseType; + while (current != null) + { + if (current.Name == type) + return true; + current = current.BaseType; + } + return false; + } + public static string Ident(this object source, int identLevels) + { + var lines = source.ToString().TrimStart(' ').Split('\n'); + var ident = new string(' ', identLevels * 4); + return string.Join("\n", lines.Select((x, i) => $"""{ (i > 0 ? ident : "") }{x}""")); + } + + public static string Render(this IEnumerable source, Func template, string separator = "", string openToken = "", string closeToken = "", bool renderEmpty = true) + { + if (!renderEmpty && source.Count() == 0) + return ""; + return $"{openToken}{string.Join(separator, source.Select(template))}{closeToken}"; + } + + public static string Render(this IEnumerable source, Func template, string separator = "") + { + return string.Join(separator, source.Select(template)); + } +} diff --git a/Rewrite/src/Rewrite.Analyzers/LstToString.cs b/Rewrite/src/Rewrite.Analyzers/LstToString.cs new file mode 100644 index 0000000..b372867 --- /dev/null +++ b/Rewrite/src/Rewrite.Analyzers/LstToString.cs @@ -0,0 +1,67 @@ +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace Rewrite.Analyzers; + +[Generator] +public class LstToGenerator : ISourceGenerator +{ + public void Initialize(GeneratorInitializationContext context) + { + context.RegisterForSyntaxNotifications(() => new SyntaxScanner()); + } + + public void Execute(GeneratorExecutionContext context) + { + var scanner = (SyntaxScanner)context.SyntaxReceiver!; + string GetSrc(List classes, string parent) + { + var ns = parent == "J" ? "RewriteJava" : "RewriteCSharp"; + return $$""" + using Rewrite.Core; + + using Rewrite.RewriteJava.Tree; + namespace Rewrite.{{ns}}.Tree + { + public partial interface {{parent}} + { + {{ classes.Render(c => $$""" + partial class {{c.Identifier}}{{c.TypeParameterList?.Parameters.Render(p => p.Identifier.Text, ",", "<", ">", renderEmpty: false)}} + { + public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); + } + """, "\n").Ident(2) }} + } + } + """; + } + if(scanner.CsClasses.Count > 0) + context.AddSource("CsLstToString.gs", SourceText.From(GetSrc(scanner.CsClasses.Values.ToList(), "Cs"), Encoding.UTF8)); + if(scanner.JClasses.Count > 0) + context.AddSource("JLstToString.gs", SourceText.From(GetSrc(scanner.JClasses.Values.ToList(), "J"), Encoding.UTF8)); + + } + public class SyntaxScanner : ISyntaxReceiver + { + public Dictionary CsClasses { get; } = new(); + public Dictionary JClasses { get; } = new(); + public void OnVisitSyntaxNode(SyntaxNode syntaxNode) + { + if (syntaxNode is ClassDeclarationSyntax + { + Parent: InterfaceDeclarationSyntax + { + Identifier.Text: "Cs" or "J" , + } parent + } classDeclaration) + { + if (parent.Identifier.Text == "J") + JClasses[classDeclaration.Identifier.Text] = classDeclaration; + else + CsClasses[classDeclaration.Identifier.Text] = classDeclaration; + } + } + } +} diff --git a/Rewrite/src/Rewrite.Analyzers/Properties/launchSettings.json b/Rewrite/src/Rewrite.Analyzers/Properties/launchSettings.json new file mode 100644 index 0000000..471f01c --- /dev/null +++ b/Rewrite/src/Rewrite.Analyzers/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "Generators": { + "commandName": "DebugRoslynComponent", + "targetProject": "../Rewrite.CSharp/Rewrite.CSharp.csproj" + } + } +} diff --git a/Rewrite/src/Rewrite.Analyzers/Rewrite.Analyzers.csproj b/Rewrite/src/Rewrite.Analyzers/Rewrite.Analyzers.csproj new file mode 100644 index 0000000..87b9216 --- /dev/null +++ b/Rewrite/src/Rewrite.Analyzers/Rewrite.Analyzers.csproj @@ -0,0 +1,29 @@ + + + netstandard2.0 + true + false + NU5128 + $(GetTargetPathDependsOn);GetDependencyTargetPaths + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + diff --git a/Rewrite/src/Rewrite.CSharp/CSharpParser.cs b/Rewrite/src/Rewrite.CSharp/CSharpParser.cs index 2bb006a..1dd6cd1 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpParser.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpParser.cs @@ -63,7 +63,7 @@ public IEnumerable ParseInputs(IEnumerable source var encoding = Encoding.GetEncoding((this as Core.Parser).GetCharset(ctx)); var sourceFiles = new List(); - foreach (var source in sources) + Parallel.ForEach(sources,source => { SourceFile cs; try @@ -88,7 +88,33 @@ public IEnumerable ParseInputs(IEnumerable source } sourceFiles.Add(cs!); - } + }); + // foreach (var source in sources) + // { + // SourceFile cs; + // try + // { + // using var stream = source.GetSource(ctx); + // var sourceText = SourceText.From(stream, encoding); + // var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, path: source.GetRelativePath(relativeTo)); + // var root = syntaxTree.GetRoot(); + // var compilation = CSharpCompilation.Create("Dummy") + // .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) + // .AddReferences(references) + // .AddSyntaxTrees(syntaxTree); + // var semanticModel = compilation.GetSemanticModel(syntaxTree); + // + // var visitor = new CSharpParserVisitor(this, semanticModel); + // cs = visitor.Visit(root) as Cs.CompilationUnit ?? throw new InvalidOperationException("Visitor.Visit returned null instead of Compilation Unit"); + // } + // catch (Exception t) + // { + // // ctx.OnError.accept(t); + // cs = ParseError.Build(this, source, relativeTo, ctx, t); + // } + // + // sourceFiles.Add(cs!); + // } return sourceFiles; } diff --git a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs index ddbed2a..3b48692 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs @@ -55,16 +55,49 @@ public CSharpPrinter() } } - public override J? VisitNamedArgument(Cs.NamedArgument namedArgument, PrintOutputCapture p) + public override J? VisitTupleExpression(Cs.TupleExpression tupleExpression, PrintOutputCapture p) { - BeforeSyntax(namedArgument, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); - var padding = namedArgument.Padding; + BeforeSyntax(tupleExpression, CsSpace.Location.TUPLE_EXPRESSION_PREFIX, p); + VisitContainer("(", tupleExpression.Padding.Arguments, CsContainer.Location.TUPLE_EXPRESSION_ARGUMENTS, ",", ")", p); + AfterSyntax(tupleExpression, p); + return tupleExpression; + } + + public override J? VisitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation node, PrintOutputCapture p) + { + BeforeSyntax(node, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); + VisitContainer("(", node.Padding.Variables, CsContainer.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, ",", ")", p); + AfterSyntax(node, p); + return node; + } + + public override J? VisitArgument(Cs.Argument argument, PrintOutputCapture p) + { + BeforeSyntax(argument, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); + var padding = argument.Padding; + + if (argument.NameColumn != null) + { + VisitRightPadded(padding.NameColumn, CsRightPadded.Location.NAMED_ARGUMENT_NAME_COLUMN, p); + p.Append(':'); + } + + if (argument.RefKindKeyword != null) + { + VisitKeyword(argument.RefKindKeyword, p); + } + + Visit(argument.Expression, p); + AfterSyntax(argument, p); + return argument; + } - VisitRightPadded(padding.NameColumn, CsRightPadded.Location.NAMED_ARGUMENT_NAME_COLUMN, p); - p.Append(':'); - Visit(namedArgument.Expression, p); - AfterSyntax(namedArgument, p); - return namedArgument; + public override J? VisitKeyword(Cs.Keyword keyword, PrintOutputCapture p) + { + BeforeSyntax(keyword, CsSpace.Location.KEYWORD_PREFIX, p); + p.Append(keyword.Kind.ToString().ToLowerInvariant()); + AfterSyntax(keyword, p); + return keyword; } public override J? VisitUsingStatement(Cs.UsingStatement usingStatement, PrintOutputCapture p) @@ -73,7 +106,7 @@ public CSharpPrinter() p.Append("using"); if (usingStatement.AwaitKeyword != null) { - VisitSpace(usingStatement.AwaitKeyword, CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD, p); + VisitKeyword(usingStatement.AwaitKeyword, p); } VisitContainer("(", usingStatement.Padding.Expression, CsContainer.Location.USING_STATEMENT_EXPRESSION, "", ")", p); @@ -82,6 +115,22 @@ public CSharpPrinter() return usingStatement; } + public override J? VisitUnary(Cs.Unary unary, PrintOutputCapture p) + { + BeforeSyntax(unary, Space.Location.UNARY_PREFIX, p); + switch (unary.Operator) + { + default: + case Cs.Unary.Type.SuppressNullableWarning: + Visit(unary.Expression, p); + p.Append("!"); + break; + } + + AfterSyntax(unary, p); + return unary; + } + public override Cs VisitCompilationUnit(Cs.CompilationUnit compilationUnit, PrintOutputCapture p) { BeforeSyntax(compilationUnit, Space.Location.COMPILATION_UNIT_PREFIX, p); @@ -304,6 +353,7 @@ public override Cs VisitFileScopeNamespaceDeclaration(Cs.FileScopeNamespaceDecla return interpolatedString; } + public override J? VisitInterpolation(Cs.Interpolation interpolation, PrintOutputCapture p) { BeforeSyntax(interpolation, CsSpace.Location.INTERPOLATION_PREFIX, p); @@ -327,6 +377,7 @@ public override Cs VisitFileScopeNamespaceDeclaration(Cs.FileScopeNamespaceDecla return interpolation; } + public override J? VisitNullSafeExpression(Cs.NullSafeExpression nullSafeExpression, PrintOutputCapture p) { BeforeSyntax(nullSafeExpression, CsSpace.Location.NULL_SAFE_EXPRESSION_PREFIX, p); @@ -403,6 +454,44 @@ public override Cs VisitFileScopeNamespaceDeclaration(Cs.FileScopeNamespaceDecla return usingDirective; } + public override J? VisitConstructor(Cs.Constructor constructor, PrintOutputCapture p) + { + var method = constructor.ConstructorCore; + BeforeSyntax(method, Space.Location.METHOD_DECLARATION_PREFIX, p); + VisitSpace(Space.EMPTY, Space.Location.ANNOTATIONS, p); + Visit(method.LeadingAnnotations, p); + foreach (var modifier in method.Modifiers) + { + _delegate.VisitModifier(modifier, p); + } + + Visit(method.Annotations.Name.Annotations, p); + Visit(method.Name, p); + + + + if (method.Markers.FirstOrDefault(m => m is CompactConstructor) == null) + { + VisitContainer("(", method.Padding.Parameters, CsContainer.Location.METHOD_DECLARATION_PARAMETERS, ",", ")", p); + } + + Visit(constructor.Initializer, p); + + Visit(method.Body, p); + AfterSyntax(constructor, p); + return constructor; + } + + public override J? VisitConstructorInitializer(Cs.ConstructorInitializer node, PrintOutputCapture p) + { + BeforeSyntax(node, Space.Location.METHOD_DECLARATION_PREFIX, p); + p.Append(":"); + VisitKeyword(node.Keyword, p); + VisitContainer("(", node.Padding.Arguments, CsContainer.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, ",", ")", p); + AfterSyntax(node, p); + return node; + } + public override Cs VisitLambda(Cs.Lambda lambda, PrintOutputCapture p) { if (lambda.Modifiers.Any()) // only put space in front if current node actually has anything to contribute @@ -588,6 +677,18 @@ private class CSharpJavaPrinter(CSharpPrinter _parent) : JavaPrinter p) + { + BeforeSyntax(newArray, Space.Location.NEW_ARRAY_PREFIX, p); + p.Append("new"); + + Visit(newArray.TypeExpression, p); + // VisitArrayDimension() + Visit(newArray.Dimensions, p); + VisitContainer("{", newArray.Padding.Initializer, JContainer.Location.NEW_ARRAY_INITIALIZER, ",", "}", p); + AfterSyntax(newArray, p); + return newArray; + } public override J VisitClassDeclaration(J.ClassDeclaration classDecl, PrintOutputCapture p) { diff --git a/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs b/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs index 7a0d557..94cb5e2 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs @@ -36,19 +36,20 @@ public override bool IsAcceptable(SourceFile sourceFile, P p) return compilationUnit; } - public virtual J? VisitNamedArgument(Cs.NamedArgument namedArgument, P p) + public virtual J? VisitArgument(Cs.Argument argument, P p) { - namedArgument = namedArgument.WithPrefix(VisitSpace(namedArgument.Prefix, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p)!); - var tempExpression = (Expression) VisitExpression(namedArgument, p); - if (tempExpression is not Cs.NamedArgument) + argument = argument.WithPrefix(VisitSpace(argument.Prefix, CsSpace.Location.ARGUMENT_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(argument, p); + if (tempExpression is not Cs.Argument) { return tempExpression; } - namedArgument = (Cs.NamedArgument) tempExpression; - namedArgument = namedArgument.WithMarkers(VisitMarkers(namedArgument.Markers, p)); - namedArgument = namedArgument.Padding.WithNameColumn(namedArgument.Padding.NameColumn == null ? null : VisitRightPadded(namedArgument.Padding.NameColumn, CsRightPadded.Location.NAMED_ARGUMENT_NAME_COLUMN, p)); - namedArgument = namedArgument.WithExpression(VisitAndCast(namedArgument.Expression, p)!); - return namedArgument; + argument = (Cs.Argument) tempExpression; + argument = argument.WithMarkers(VisitMarkers(argument.Markers, p)); + argument = argument.Padding.WithNameColumn(argument.Padding.NameColumn == null ? null : VisitRightPadded(argument.Padding.NameColumn, CsRightPadded.Location.ARGUMENT_NAME_COLUMN, p)); + argument = argument.WithRefKindKeyword(VisitAndCast(argument.RefKindKeyword, p)); + argument = argument.WithExpression(VisitAndCast(argument.Expression, p)!); + return argument; } public virtual J? VisitAnnotatedStatement(Cs.AnnotatedStatement annotatedStatement, P p) @@ -314,6 +315,13 @@ public override bool IsAcceptable(SourceFile sourceFile, P p) return propertyDeclaration; } + public virtual J? VisitKeyword(Cs.Keyword keyword, P p) + { + keyword = keyword.WithPrefix(VisitSpace(keyword.Prefix, CsSpace.Location.KEYWORD_PREFIX, p)!); + keyword = keyword.WithMarkers(VisitMarkers(keyword.Markers, p)); + return keyword; + } + public virtual J? VisitLambda(Cs.Lambda lambda, P p) { lambda = lambda.WithPrefix(VisitSpace(lambda.Prefix, CsSpace.Location.LAMBDA_PREFIX, p)!); @@ -375,7 +383,7 @@ public override bool IsAcceptable(SourceFile sourceFile, P p) } usingStatement = (Cs.UsingStatement) tempStatement; usingStatement = usingStatement.WithMarkers(VisitMarkers(usingStatement.Markers, p)); - usingStatement = usingStatement.WithAwaitKeyword(usingStatement.AwaitKeyword == null ? null : VisitSpace(usingStatement.AwaitKeyword, CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD, p)); + usingStatement = usingStatement.WithAwaitKeyword(VisitAndCast(usingStatement.AwaitKeyword, p)); usingStatement = usingStatement.Padding.WithExpression(VisitContainer(usingStatement.Padding.Expression, CsContainer.Location.USING_STATEMENT_EXPRESSION, p)!); usingStatement = usingStatement.WithStatement(VisitAndCast(usingStatement.Statement, p)!); return usingStatement; @@ -434,6 +442,122 @@ public override bool IsAcceptable(SourceFile sourceFile, P p) return defaultConstraint; } + public virtual J? VisitDeclarationExpression(Cs.DeclarationExpression declarationExpression, P p) + { + declarationExpression = declarationExpression.WithPrefix(VisitSpace(declarationExpression.Prefix, CsSpace.Location.DECLARATION_EXPRESSION_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(declarationExpression, p); + if (tempExpression is not Cs.DeclarationExpression) + { + return tempExpression; + } + declarationExpression = (Cs.DeclarationExpression) tempExpression; + declarationExpression = declarationExpression.WithMarkers(VisitMarkers(declarationExpression.Markers, p)); + declarationExpression = declarationExpression.WithTypeExpression(VisitAndCast(declarationExpression.TypeExpression, p)); + declarationExpression = declarationExpression.WithVariables(VisitAndCast(declarationExpression.Variables, p)!); + return declarationExpression; + } + + public virtual J? VisitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, P p) + { + singleVariableDesignation = singleVariableDesignation.WithPrefix(VisitSpace(singleVariableDesignation.Prefix, CsSpace.Location.SINGLE_VARIABLE_DESIGNATION_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(singleVariableDesignation, p); + if (tempExpression is not Cs.SingleVariableDesignation) + { + return tempExpression; + } + singleVariableDesignation = (Cs.SingleVariableDesignation) tempExpression; + singleVariableDesignation = singleVariableDesignation.WithMarkers(VisitMarkers(singleVariableDesignation.Markers, p)); + singleVariableDesignation = singleVariableDesignation.WithName(VisitAndCast(singleVariableDesignation.Name, p)!); + return singleVariableDesignation; + } + + public virtual J? VisitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, P p) + { + parenthesizedVariableDesignation = parenthesizedVariableDesignation.WithPrefix(VisitSpace(parenthesizedVariableDesignation.Prefix, CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(parenthesizedVariableDesignation, p); + if (tempExpression is not Cs.ParenthesizedVariableDesignation) + { + return tempExpression; + } + parenthesizedVariableDesignation = (Cs.ParenthesizedVariableDesignation) tempExpression; + parenthesizedVariableDesignation = parenthesizedVariableDesignation.WithMarkers(VisitMarkers(parenthesizedVariableDesignation.Markers, p)); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.Padding.WithVariables(VisitContainer(parenthesizedVariableDesignation.Padding.Variables, CsContainer.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, p)!); + return parenthesizedVariableDesignation; + } + + public virtual J? VisitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, P p) + { + discardVariableDesignation = discardVariableDesignation.WithPrefix(VisitSpace(discardVariableDesignation.Prefix, CsSpace.Location.DISCARD_VARIABLE_DESIGNATION_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(discardVariableDesignation, p); + if (tempExpression is not Cs.DiscardVariableDesignation) + { + return tempExpression; + } + discardVariableDesignation = (Cs.DiscardVariableDesignation) tempExpression; + discardVariableDesignation = discardVariableDesignation.WithMarkers(VisitMarkers(discardVariableDesignation.Markers, p)); + discardVariableDesignation = discardVariableDesignation.WithDiscard(VisitAndCast(discardVariableDesignation.Discard, p)!); + return discardVariableDesignation; + } + + public virtual J? VisitTupleExpression(Cs.TupleExpression tupleExpression, P p) + { + tupleExpression = tupleExpression.WithPrefix(VisitSpace(tupleExpression.Prefix, CsSpace.Location.TUPLE_EXPRESSION_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(tupleExpression, p); + if (tempExpression is not Cs.TupleExpression) + { + return tempExpression; + } + tupleExpression = (Cs.TupleExpression) tempExpression; + tupleExpression = tupleExpression.WithMarkers(VisitMarkers(tupleExpression.Markers, p)); + tupleExpression = tupleExpression.Padding.WithArguments(VisitContainer(tupleExpression.Padding.Arguments, CsContainer.Location.TUPLE_EXPRESSION_ARGUMENTS, p)!); + return tupleExpression; + } + + public virtual J? VisitConstructor(Cs.Constructor constructor, P p) + { + constructor = constructor.WithPrefix(VisitSpace(constructor.Prefix, CsSpace.Location.CONSTRUCTOR_PREFIX, p)!); + var tempStatement = (Statement) VisitStatement(constructor, p); + if (tempStatement is not Cs.Constructor) + { + return tempStatement; + } + constructor = (Cs.Constructor) tempStatement; + constructor = constructor.WithMarkers(VisitMarkers(constructor.Markers, p)); + constructor = constructor.WithInitializer(VisitAndCast(constructor.Initializer, p)); + constructor = constructor.WithConstructorCore(VisitAndCast(constructor.ConstructorCore, p)!); + return constructor; + } + + public virtual J? VisitUnary(Cs.Unary unary, P p) + { + unary = unary.WithPrefix(VisitSpace(unary.Prefix, CsSpace.Location.UNARY_PREFIX, p)!); + var tempStatement = (Statement) VisitStatement(unary, p); + if (tempStatement is not Cs.Unary) + { + return tempStatement; + } + unary = (Cs.Unary) tempStatement; + var tempExpression = (Expression) VisitExpression(unary, p); + if (tempExpression is not Cs.Unary) + { + return tempExpression; + } + unary = (Cs.Unary) tempExpression; + unary = unary.WithMarkers(VisitMarkers(unary.Markers, p)); + unary = unary.Padding.WithOperator(VisitLeftPadded(unary.Padding.Operator, CsLeftPadded.Location.UNARY_OPERATOR, p)!); + unary = unary.WithExpression(VisitAndCast(unary.Expression, p)!); + return unary; + } + + public virtual J? VisitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, P p) + { + constructorInitializer = constructorInitializer.WithPrefix(VisitSpace(constructorInitializer.Prefix, CsSpace.Location.CONSTRUCTOR_INITIALIZER_PREFIX, p)!); + constructorInitializer = constructorInitializer.WithMarkers(VisitMarkers(constructorInitializer.Markers, p)); + constructorInitializer = constructorInitializer.WithKeyword(VisitAndCast(constructorInitializer.Keyword, p)!); + constructorInitializer = constructorInitializer.Padding.WithArguments(VisitContainer(constructorInitializer.Padding.Arguments, CsContainer.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, p)!); + return constructorInitializer; + } + protected virtual JContainer? VisitContainer(JContainer? container, CsContainer.Location loc, P p) where J2 : J { if (container == null) { diff --git a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs index cf3abd2..9ff0f00 100644 --- a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs +++ b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs @@ -596,6 +596,11 @@ private JRightPadded MapArgument(ArgumentSyntax argument) public override J? VisitMethodDeclaration(MethodDeclarationSyntax node) { + // DeclarationExpressionSyntax f; + // SingleVariableDesignationSyntax s; + // ArgumentSyntax s; + // J.VariableDeclarations; + J.Block body; if (node.Body != null) { @@ -707,23 +712,26 @@ private JRightPadded MapArgument(ArgumentSyntax argument) public override J? VisitArgument(ArgumentSyntax node) { - if (node.NameColon == null) - { - return Convert(node.Expression); - } - else - { - // - return new Cs.NamedArgument( + // if (node.NameColon == null && node.RefKindKeyword.IsKind(SyntaxKind.None)) + // { + // return Convert(node.Expression); + // } + // else + // { + var nameColumn = node.NameColon != null + ? new JRightPadded( + MapIdentifier(node.NameColon.Name.Identifier, null), + Format(Trailing(node.NameColon.Name)), Markers.EMPTY) + : null; + return new Cs.Argument( Core.Tree.RandomId(), - Format(Leading(node.NameColon)), + Format(Leading(node)), Markers.EMPTY, - new JRightPadded( - MapIdentifier(node.NameColon.Name.Identifier, null), - Format(Trailing(node.NameColon.Name)), Markers.EMPTY), + nameColumn, + VisitKeyword(node.RefKindKeyword), Convert(node.Expression)! ); - } + // } } public override J.Block VisitBlock(BlockSyntax node) @@ -1362,8 +1370,11 @@ private J.Identifier MapIdentifier(SyntaxToken identifier, JavaType? type) public override J? VisitTupleExpression(TupleExpressionSyntax node) { - // This was added in C# 7.0 - return base.VisitTupleExpression(node); + return new Cs.TupleExpression( + Core.Tree.RandomId(), + Format(Leading(node.OpenParenToken)), + Markers.EMPTY, + ToJContainer(node.Arguments, node.OpenParenToken)); } public override J? VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node) @@ -1431,40 +1442,60 @@ private J.Identifier MapIdentifier(SyntaxToken identifier, JavaType? type) public override J? VisitPostfixUnaryExpression(PostfixUnaryExpressionSyntax node) { - // TODO remove `if` once we support all - if (!node.OperatorToken.IsKind(SyntaxKind.PlusPlusToken) && - !node.OperatorToken.IsKind(SyntaxKind.MinusMinusToken)) - return DefaultVisit(node); - - return new J.Unary( - Core.Tree.RandomId(), - Format(Leading(node)), - Markers.EMPTY, - MapPostfixUnaryOperator(node.OperatorToken), - Convert(node.Operand)!, - MapType(node) - ); + var operatorEnum = MapPostfixUnaryOperator(node.OperatorToken); + if (operatorEnum is J.Unary.Type jOperator) + { + return new J.Unary( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + new JLeftPadded( + Format(Leading(node.OperatorToken)), + jOperator, + Markers.EMPTY + ), + Convert(node.Operand)!, + MapType(node)); + } + else + { + return new Cs.Unary( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + JLeftPadded.Create( + (Cs.Unary.Type)operatorEnum, + Format(Leading(node.OperatorToken)), + Markers.EMPTY + ), + Convert(node.Operand)!, + MapType(node)); + } } - private JLeftPadded MapPostfixUnaryOperator(SyntaxToken operatorToken) + private Enum MapPostfixUnaryOperator(SyntaxToken operatorToken) { - return operatorToken.Kind() switch + Enum type = operatorToken.Kind() switch { - SyntaxKind.PlusPlusToken => new JLeftPadded( - Format(Leading(operatorToken)), - J.Unary.Type.PostIncrement, - Markers.EMPTY - ), - SyntaxKind.MinusMinusToken => new JLeftPadded( - Format(Leading(operatorToken)), - J.Unary.Type.PostDecrement, - Markers.EMPTY - ), - // TODO implement all - _ => throw new NotImplementedException(operatorToken.ToString()) + SyntaxKind.PlusPlusToken => J.Unary.Type.PostIncrement, + SyntaxKind.MinusMinusToken => J.Unary.Type.PostDecrement, + SyntaxKind.ExclamationToken => Cs.Unary.Type.SuppressNullableWarning, + _ => throw new InvalidOperationException($"Unsupported token type {operatorToken} in PostfixUnaryExpression") }; + return type; } + // private Cs.Unary.Type MapPostfixUnaryOperatorToJ(SyntaxToken operatorToken) + // { + // J.Unary.Type? type = operatorToken.Kind() switch + // { + // SyntaxKind.PlusPlusToken => J.Unary.Type.PostIncrement, + // SyntaxKind.MinusMinusToken => J.Unary.Type.PostDecrement, + // _ => null + // }; + // return type; + // } + public override J? VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { var name = Convert(node.Name)!; @@ -1854,7 +1885,13 @@ private J.ArrayAccess MapArrayAccess(ElementAccessExpressionSyntax node, int ind public override J? VisitDeclarationExpression(DeclarationExpressionSyntax node) { - return base.VisitDeclarationExpression(node); + var result = new Cs.DeclarationExpression( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + Convert(node.Type), + Convert(node.Designation)!); + return result; } public override J? VisitAnonymousMethodExpression(AnonymousMethodExpressionSyntax node) @@ -2216,8 +2253,33 @@ private JRightPadded MapAnonymousObjectMember(AnonymousObjectMemberDe public override J? VisitImplicitArrayCreationExpression(ImplicitArrayCreationExpressionSyntax node) { - // This was added in C# 3.0 - return base.VisitImplicitArrayCreationExpression(node); + return new J.NewArray( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + null, + [ new J.ArrayDimension( + Core.Tree.RandomId(), + Format(Leading(node.OpenBracketToken)), + Markers.EMPTY, + JRightPadded.Create(new Cs.ArrayRankSpecifier( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + JContainer.Create(node.Commas.Select(c => JRightPadded.Create(new J.Empty(Core.Tree.RandomId(), Format(Leading(c)), Markers.EMPTY), Format(Trailing(c)))).ToList())))) ], + // node.Commas.Select(c => new J.ArrayDimension( + // Core.Tree.RandomId(), + // Format(Leading(c)), + // Markers.EMPTY, + // JRightPadded.Create(new J.Empty(Core.Tree.RandomId(), Space.EMPTY, Markers.EMPTY)))).ToList(), + new JContainer( + Format(Leading(node.Initializer.OpenBraceToken)), + node.Initializer.Expressions.Select(e => + new JRightPadded(Convert(e)!, Format(Trailing(e)), Markers.EMPTY)) + .ToList(), + Markers.EMPTY), + MapType(node) + ); } public override J? VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node) @@ -2661,17 +2723,30 @@ private IList> MapArrayDimensions(BracketedArgumentListSyntax public override J? VisitSingleVariableDesignation(SingleVariableDesignationSyntax node) { - return MapIdentifier(node.Identifier, MapType(node)); + return new Cs.SingleVariableDesignation( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + MapIdentifier(node.Identifier, MapType(node))); } public override J? VisitDiscardDesignation(DiscardDesignationSyntax node) { - return MapIdentifier(node.UnderscoreToken, MapType(node)); + return new Cs.DiscardVariableDesignation( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + MapIdentifier(node.UnderscoreToken, MapType(node))); } public override J? VisitParenthesizedVariableDesignation(ParenthesizedVariableDesignationSyntax node) { - return base.VisitParenthesizedVariableDesignation(node); + return new Cs.ParenthesizedVariableDesignation( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + ToJContainer(node.Variables, node.OpenParenToken), + MapType(node)); } public override J? VisitExpressionStatement(ExpressionStatementSyntax node) @@ -2891,7 +2966,7 @@ private IList> MapArrayDimensions(BracketedArgumentListSyntax Core.Tree.RandomId(), Format(Leading(node)), Markers.EMPTY, - node.AwaitKeyword.IsKeyword() ? Format(Trailing(node.AwaitKeyword)) : null, + VisitKeyword(node.AwaitKeyword), JContainer.Create(new List> { expression @@ -2900,6 +2975,25 @@ private IList> MapArrayDimensions(BracketedArgumentListSyntax return usingStatement; } + public Cs.Keyword? VisitKeyword(SyntaxToken token) + { + if (token.IsKind(SyntaxKind.None)) + return null; + var kind = token.Kind() switch + { + SyntaxKind.AwaitKeyword => Cs.KeywordKind.Await, + SyntaxKind.RefKeyword => Cs.KeywordKind.Ref, + SyntaxKind.OutKeyword => Cs.KeywordKind.Out, + _ => throw new NotSupportedException($"Keyword is {token} supported.") + }; + return new Cs.Keyword( + Core.Tree.RandomId(), + Format(Leading(token)), + Markers.EMPTY, + kind); + + } + public override J? VisitFixedStatement(FixedStatementSyntax node) { return base.VisitFixedStatement(node); @@ -3279,11 +3373,7 @@ private JRightPadded MapSwitchCaseLabel(SwitchLabelSyntax sls) public override J? VisitConstructorDeclaration(ConstructorDeclarationSyntax node) { - if (node.Initializer != null) - { - // TODO support constructor delegation - return base.VisitConstructorDeclaration(node); - } + var attributeLists = MapAttributes(node.AttributeLists); @@ -3306,20 +3396,39 @@ private JRightPadded MapSwitchCaseLabel(SwitchLabelSyntax sls) MapType(node) as JavaType.Method ); + + var constructor = new Cs.Constructor( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + Convert(node.Initializer), + methodDeclaration); + return attributeLists != null ? new Cs.AnnotatedStatement( Core.Tree.RandomId(), Format(Leading(node)), Markers.EMPTY, attributeLists, - methodDeclaration + constructor ) - : methodDeclaration; + : constructor; } public override J? VisitConstructorInitializer(ConstructorInitializerSyntax node) { - return base.VisitConstructorInitializer(node); + return new Cs.ConstructorInitializer( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + new Cs.Keyword( + Core.Tree.RandomId(), + Format(Leading(node.ThisOrBaseKeyword)), + Markers.EMPTY, + node.ThisOrBaseKeyword.IsKind(SyntaxKind.BaseKeyword) ? Cs.KeywordKind.Base : Cs.KeywordKind.This + ), + ToJContainer(node.ArgumentList.Arguments, node.ArgumentList.OpenParenToken) + ); } public override J? VisitDestructorDeclaration(DestructorDeclarationSyntax node) @@ -3629,7 +3738,7 @@ private JRightPadded MapSwitchCaseLabel(SwitchLabelSyntax sls) #if DEBUG_VISITOR [DebuggerStepThrough] #endif - T? Convert(SyntaxNode? node) where T : class, J + internal T? Convert(SyntaxNode? node) where T : class, J { if (node == null) return default; @@ -3783,7 +3892,7 @@ private JRightPadded MapStatement(StatementSyntax statementSyntax) #if DEBUG_VISITOR [DebuggerStepThrough] #endif - private SyntaxTriviaList Leading(SyntaxList list) where T : SyntaxNode + internal SyntaxTriviaList Leading(SyntaxList list) where T : SyntaxNode { return list.Count == 0 ? SyntaxTriviaList.Empty : Leading(list.First()); } @@ -3791,7 +3900,7 @@ private SyntaxTriviaList Leading(SyntaxList list) where T : SyntaxNode #if DEBUG_VISITOR [DebuggerStepThrough] #endif - private SyntaxTriviaList Leading(SyntaxNode node) + internal SyntaxTriviaList Leading(SyntaxNode node) { var firstToken = node.GetFirstToken(); return Leading(firstToken); @@ -3800,7 +3909,7 @@ private SyntaxTriviaList Leading(SyntaxNode node) #if DEBUG_VISITOR [DebuggerStepThrough] #endif - private SyntaxTriviaList Leading(SyntaxToken token) + internal SyntaxTriviaList Leading(SyntaxToken token) { var previousToken = token.GetPreviousToken(); var leading = token.LeadingTrivia; @@ -3847,21 +3956,21 @@ private SyntaxTriviaList OnlyUnseenTrivia(SyntaxTriviaList trivia1, SyntaxTrivia #if DEBUG_VISITOR [DebuggerStepThrough] #endif - private SyntaxTriviaList Trailing(SyntaxNode node) + internal SyntaxTriviaList Trailing(SyntaxNode node) { return Trailing(node.GetLastToken()); } #if DEBUG_VISITOR [DebuggerStepThrough] #endif - private SyntaxTriviaList Trailing(SyntaxToken token) + internal SyntaxTriviaList Trailing(SyntaxToken token) { return Leading(token.GetNextToken()); } #if DEBUG_VISITOR [DebuggerStepThrough] #endif - private static Space Format(SyntaxTriviaList trivia) + internal static Space Format(SyntaxTriviaList trivia) { // FIXME optimize return Space.Format(trivia.ToString()); @@ -3873,4 +3982,11 @@ private List> ToRightPadded(SeparatedSyntaxList JRightPadded.Create(Convert(x)!, Format(Trailing(x)))).ToList(); } + + private JContainer ToJContainer(SeparatedSyntaxList syntaxList, SyntaxToken openingToken) + where TFrom : SyntaxNode + where TTo : class, J + { + return JContainer.Create(ToRightPadded(syntaxList), Format(Leading(openingToken))); + } } diff --git a/Rewrite/src/Rewrite.CSharp/Rewrite.CSharp.csproj b/Rewrite/src/Rewrite.CSharp/Rewrite.CSharp.csproj index f7ba94e..d23d12b 100644 --- a/Rewrite/src/Rewrite.CSharp/Rewrite.CSharp.csproj +++ b/Rewrite/src/Rewrite.CSharp/Rewrite.CSharp.csproj @@ -22,5 +22,10 @@ + + + diff --git a/Rewrite/src/Rewrite.CSharp/Tree/NamedArgument.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/Argument.g.cs similarity index 68% rename from Rewrite/src/Rewrite.CSharp/Tree/NamedArgument.g.cs rename to Rewrite/src/Rewrite.CSharp/Tree/Argument.g.cs index 3be5c27..5224c75 100644 --- a/Rewrite/src/Rewrite.CSharp/Tree/NamedArgument.g.cs +++ b/Rewrite/src/Rewrite.CSharp/Tree/Argument.g.cs @@ -26,13 +26,14 @@ public partial interface Cs : J #if DEBUG_VISITOR [DebuggerStepThrough] #endif - public partial class NamedArgument( + public partial class Argument( Guid id, Space prefix, Markers markers, JRightPadded? nameColumn, + Keyword? refKindKeyword, Expression expression - ) : Cs, Expression, MutableTree + ) : Cs, Expression, MutableTree { [NonSerialized] private WeakReference? _padding; @@ -61,53 +62,59 @@ public PaddingHelper Padding public J? AcceptCSharp

(CSharpVisitor

v, P p) { - return v.VisitNamedArgument(this, p); + return v.VisitArgument(this, p); } public JavaType? Type => Extensions.GetJavaType(this); - public NamedArgument WithType(JavaType newType) + public Argument WithType(JavaType newType) { return Extensions.WithJavaType(this, newType); } public Guid Id => id; - public NamedArgument WithId(Guid newId) + public Argument WithId(Guid newId) { - return newId == id ? this : new NamedArgument(newId, prefix, markers, _nameColumn, expression); + return newId == id ? this : new Argument(newId, prefix, markers, _nameColumn, refKindKeyword, expression); } public Space Prefix => prefix; - public NamedArgument WithPrefix(Space newPrefix) + public Argument WithPrefix(Space newPrefix) { - return newPrefix == prefix ? this : new NamedArgument(id, newPrefix, markers, _nameColumn, expression); + return newPrefix == prefix ? this : new Argument(id, newPrefix, markers, _nameColumn, refKindKeyword, expression); } public Markers Markers => markers; - public NamedArgument WithMarkers(Markers newMarkers) + public Argument WithMarkers(Markers newMarkers) { - return ReferenceEquals(newMarkers, markers) ? this : new NamedArgument(id, prefix, newMarkers, _nameColumn, expression); + return ReferenceEquals(newMarkers, markers) ? this : new Argument(id, prefix, newMarkers, _nameColumn, refKindKeyword, expression); } private readonly JRightPadded? _nameColumn = nameColumn; public J.Identifier? NameColumn => _nameColumn?.Element; - public NamedArgument WithNameColumn(J.Identifier? newNameColumn) + public Argument WithNameColumn(J.Identifier? newNameColumn) { return Padding.WithNameColumn(JRightPadded.WithElement(_nameColumn, newNameColumn)); } + public Cs.Keyword? RefKindKeyword => refKindKeyword; + + public Argument WithRefKindKeyword(Cs.Keyword? newRefKindKeyword) + { + return ReferenceEquals(newRefKindKeyword, refKindKeyword) ? this : new Argument(id, prefix, markers, _nameColumn, newRefKindKeyword, expression); + } public Expression Expression => expression; - public NamedArgument WithExpression(Expression newExpression) + public Argument WithExpression(Expression newExpression) { - return ReferenceEquals(newExpression, expression) ? this : new NamedArgument(id, prefix, markers, _nameColumn, newExpression); + return ReferenceEquals(newExpression, expression) ? this : new Argument(id, prefix, markers, _nameColumn, refKindKeyword, newExpression); } - public sealed record PaddingHelper(Cs.NamedArgument T) + public sealed record PaddingHelper(Cs.Argument T) { public JRightPadded? NameColumn => T._nameColumn; - public Cs.NamedArgument WithNameColumn(JRightPadded? newNameColumn) + public Cs.Argument WithNameColumn(JRightPadded? newNameColumn) { - return T._nameColumn == newNameColumn ? T : new Cs.NamedArgument(T.Id, T.Prefix, T.Markers, newNameColumn, T.Expression); + return T._nameColumn == newNameColumn ? T : new Cs.Argument(T.Id, T.Prefix, T.Markers, newNameColumn, T.RefKindKeyword, T.Expression); } } @@ -117,7 +124,7 @@ public Cs.NamedArgument WithNameColumn(JRightPadded? newNameColumn #endif public bool Equals(Rewrite.Core.Tree? other) { - return other is NamedArgument && other.Id == Id; + return other is Argument && other.Id == Id; } #if DEBUG_VISITOR [DebuggerStepThrough] diff --git a/Rewrite/src/Rewrite.CSharp/Tree/Constructor.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/Constructor.g.cs new file mode 100644 index 0000000..3eda848 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/Constructor.g.cs @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class Constructor( + Guid id, + Space prefix, + Markers markers, + ConstructorInitializer? initializer, + J.MethodDeclaration constructorCore + ) : Cs, Statement, MutableTree + { + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitConstructor(this, p); + } + + public Guid Id => id; + + public Constructor WithId(Guid newId) + { + return newId == id ? this : new Constructor(newId, prefix, markers, initializer, constructorCore); + } + public Space Prefix => prefix; + + public Constructor WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new Constructor(id, newPrefix, markers, initializer, constructorCore); + } + public Markers Markers => markers; + + public Constructor WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new Constructor(id, prefix, newMarkers, initializer, constructorCore); + } + public Cs.ConstructorInitializer? Initializer => initializer; + + public Constructor WithInitializer(Cs.ConstructorInitializer? newInitializer) + { + return ReferenceEquals(newInitializer, initializer) ? this : new Constructor(id, prefix, markers, newInitializer, constructorCore); + } + public J.MethodDeclaration ConstructorCore => constructorCore; + + public Constructor WithConstructorCore(J.MethodDeclaration newConstructorCore) + { + return ReferenceEquals(newConstructorCore, constructorCore) ? this : new Constructor(id, prefix, markers, initializer, newConstructorCore); + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is Constructor && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/ConstructorInitializer.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/ConstructorInitializer.g.cs new file mode 100644 index 0000000..0eafe11 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/ConstructorInitializer.g.cs @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class ConstructorInitializer( + Guid id, + Space prefix, + Markers markers, + Keyword keyword, + JContainer arguments + ) : Cs, MutableTree + { + [NonSerialized] private WeakReference? _padding; + + public PaddingHelper Padding + { + get + { + PaddingHelper? p; + if (_padding == null) + { + p = new PaddingHelper(this); + _padding = new WeakReference(p); + } + else + { + _padding.TryGetTarget(out p); + if (p == null || p.T != this) + { + p = new PaddingHelper(this); + _padding.SetTarget(p); + } + } + return p; + } + } + + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitConstructorInitializer(this, p); + } + + public Guid Id => id; + + public ConstructorInitializer WithId(Guid newId) + { + return newId == id ? this : new ConstructorInitializer(newId, prefix, markers, keyword, _arguments); + } + public Space Prefix => prefix; + + public ConstructorInitializer WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new ConstructorInitializer(id, newPrefix, markers, keyword, _arguments); + } + public Markers Markers => markers; + + public ConstructorInitializer WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new ConstructorInitializer(id, prefix, newMarkers, keyword, _arguments); + } + public Cs.Keyword Keyword => keyword; + + public ConstructorInitializer WithKeyword(Cs.Keyword newKeyword) + { + return ReferenceEquals(newKeyword, keyword) ? this : new ConstructorInitializer(id, prefix, markers, newKeyword, _arguments); + } + private readonly JContainer _arguments = arguments; + public IList Arguments => _arguments.GetElements(); + + public ConstructorInitializer WithArguments(IList newArguments) + { + return Padding.WithArguments(JContainer.WithElements(_arguments, newArguments)); + } + public sealed record PaddingHelper(Cs.ConstructorInitializer T) + { + public JContainer Arguments => T._arguments; + + public Cs.ConstructorInitializer WithArguments(JContainer newArguments) + { + return T._arguments == newArguments ? T : new Cs.ConstructorInitializer(T.Id, T.Prefix, T.Markers, T.Keyword, newArguments); + } + + } + + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is ConstructorInitializer && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/Cs.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/Cs.g.cs index 301c1bd..a644494 100644 --- a/Rewrite/src/Rewrite.CSharp/Tree/Cs.g.cs +++ b/Rewrite/src/Rewrite.CSharp/Tree/Cs.g.cs @@ -45,4 +45,12 @@ bool Core.Tree.IsAcceptable(ITreeVisitor v, P p) return v.DefaultValue(this, p); } + public enum KeywordKind + { + Ref, + Out, + Await, + Base, + This, + } } diff --git a/Rewrite/src/Rewrite.CSharp/Tree/CsContainer.cs b/Rewrite/src/Rewrite.CSharp/Tree/CsContainer.cs deleted file mode 100644 index 4e6030a..0000000 --- a/Rewrite/src/Rewrite.CSharp/Tree/CsContainer.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Rewrite.RewriteCSharp.Tree; - -public interface CsContainer -{ - public record Location(CsSpace.Location BeforeLocation, CsRightPadded.Location ElementLocation) - { - public static readonly Location USING_STATEMENT_EXPRESSION = new(CsSpace.Location.USING_STATEMENT_EXPRESSION, CsRightPadded.Location.USING_STATEMENT_EXPRESSION); - public static readonly Location METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES = new(CsSpace.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, CsRightPadded.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES); - public static readonly Location ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS = new(CsSpace.Location.ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS, CsRightPadded.Location.ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS = new(CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS, CsRightPadded.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS); - public static readonly Location ALLOWS_CONSTRAINT_EXPRESSIONS = new(CsSpace.Location.ALLOWS_CONSTRAINT_EXPRESSIONS, CsRightPadded.Location.ALLOWS_CONSTRAINT_EXPRESSIONS); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS = new(CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS, CsRightPadded.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS); - public static readonly Location CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES = new(CsSpace.Location.CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, CsRightPadded.Location.CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES); - public static readonly Location ARRAY_RANK_SPECIFIER_SIZES = new(CsSpace.Location.ARRAY_RANK_SPECIFIER_SIZES, CsRightPadded.Location.ARRAY_RANK_SPECIFIER_SIZE); - public static readonly Location PROPERTY_DECLARATION_ACCESSORS = new(CsSpace.Location.PROPERTY_DECLARATION_ACCESSORS, CsRightPadded.Location.PROPERTY_DECLARATION_ACCESSORS); - } -} diff --git a/Rewrite/src/Rewrite.CSharp/Tree/CsLeftPadded.cs b/Rewrite/src/Rewrite.CSharp/Tree/CsLeftPadded.cs deleted file mode 100644 index c73544d..0000000 --- a/Rewrite/src/Rewrite.CSharp/Tree/CsLeftPadded.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Rewrite.RewriteCSharp.Tree; - -public interface CsLeftPadded -{ - public record Location(CsSpace.Location BeforeLocation) - { - public static readonly Location ASSIGNMENT_OPERATION_OPERATOR = new(CsSpace.Location.ASSIGNMENT_OPERATION_OPERATOR); - public static readonly Location BINARY_OPERATOR = new(CsSpace.Location.BINARY_OPERATOR); - public static readonly Location EXTERN_ALIAS_IDENTIFIER = new(CsSpace.Location.EXTERN_ALIAS_IDENTIFIER); - public static readonly Location PROPERTY_DECLARATION_EXPRESSION_BODY = new (CsSpace.Location.PROPERTY_DECLARATION_EXPRESSION_BODY); - public static readonly Location PROPERTY_DECLARATION_INITIALIZER = new (CsSpace.Location.PROPERTY_DECLARATION_INITIALIZER); - public static readonly Location USING_DIRECTIVE_STATIC = new(CsSpace.Location.USING_DIRECTIVE_STATIC); - public static readonly Location USING_DIRECTIVE_UNSAFE = new(CsSpace.Location.USING_DIRECTIVE_UNSAFE); - } -} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/CsRightPadded.cs b/Rewrite/src/Rewrite.CSharp/Tree/CsRightPadded.cs deleted file mode 100644 index 36d70d6..0000000 --- a/Rewrite/src/Rewrite.CSharp/Tree/CsRightPadded.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace Rewrite.RewriteCSharp.Tree; - -public interface CsRightPadded -{ - public record Location(CsSpace.Location AfterLocation) - { - public static readonly Location USING_STATEMENT_AWAIT_KEYWORD = new (CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD); - public static readonly Location USING_STATEMENT_EXPRESSION = new (CsSpace.Location.USING_STATEMENT_EXPRESSION); - public static readonly Location METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES = new (CsSpace.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES); - public static readonly Location ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS = new (CsSpace.Location.ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS = new(CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS); - public static readonly Location ALLOWS_CONSTRAINT_EXPRESSIONS = new(CsSpace.Location.ALLOWS_CONSTRAINT_EXPRESSIONS); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS = new(CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER = new(CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER); - public static readonly Location CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES = new(CsSpace.Location.CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES); - public static readonly Location NAMED_ARGUMENT_NAME_COLUMN = new(CsSpace.Location.NAMED_ARGUMENT_NAME_COLUMN_SUFFIX); - public static readonly Location ARRAY_RANK_SPECIFIER_SIZE = new(CsSpace.Location.ARRAY_RANK_SPECIFIER_SIZE_SUFFIX); - public static readonly Location ATTRIBUTE_LIST_ATTRIBUTES = new(CsSpace.Location.ATTRIBUTE_LIST_ATTRIBUTE_SUFFIX); - public static readonly Location ATTRIBUTE_LIST_TARGET = new(CsSpace.Location.ATTRIBUTE_LIST_TARGET_SUFFIX); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_EXTERNS = new(CsSpace.Location.BLOCK_SCOPE_NAMESPACE_DECLARATION_EXTERNS_SUFFIX); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_MEMBERS = new(CsSpace.Location.BLOCK_SCOPE_NAMESPACE_DECLARATION_MEMBERS); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_NAME = new(CsSpace.Location.BLOCK_SCOPE_NAMESPACE_DECLARATION_NAME); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_USINGS = new(CsSpace.Location.BLOCK_SCOPE_NAMESPACE_DECLARATION_USINGS); - public static readonly Location COLLECTION_EXPRESSION_ELEMENTS = new(CsSpace.Location.COLLECTION_EXPRESSION_ELEMENTS); - public static readonly Location COMPILATION_UNIT_EXTERNS = new(CsSpace.Location.COMPILATION_UNIT_EXTERNS_SUFFIX); - public static readonly Location COMPILATION_UNIT_MEMBERS = new(CsSpace.Location.COMPILATION_UNIT_MEMBERS); - public static readonly Location COMPILATION_UNIT_USINGS = new(CsSpace.Location.COMPILATION_UNIT_USINGS); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_EXTERNS = new(CsSpace.Location.FILE_SCOPE_NAMESPACE_DECLARATION_EXTERNS_SUFFIX); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_MEMBERS = new(CsSpace.Location.FILE_SCOPE_NAMESPACE_DECLARATION_MEMBERS); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_NAME = new(CsSpace.Location.FILE_SCOPE_NAMESPACE_DECLARATION_NAME); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_USINGS = new(CsSpace.Location.FILE_SCOPE_NAMESPACE_DECLARATION_USINGS); - public static readonly Location INTERPOLATED_STRING_PARTS = new(CsSpace.Location.INTERPOLATED_STRING_PARTS_SUFFIX); - public static readonly Location INTERPOLATION_ALIGNMENT = new(CsSpace.Location.INTERPOLATION_ALIGNMENT_SUFFIX); - public static readonly Location INTERPOLATION_EXPRESSION = new(CsSpace.Location.INTERPOLATION_EXPRESSION_SUFFIX); - public static readonly Location INTERPOLATION_FORMAT = new(CsSpace.Location.INTERPOLATION_FORMAT_SUFFIX); - public static readonly Location NULL_SAFE_EXPRESSION_EXPRESSION = new(CsSpace.Location.NULL_SAFE_EXPRESSION_EXPRESSION_SUFFIX); - public static readonly Location PROPERTY_DECLARATION_ACCESSORS = new(CsSpace.Location.PROPERTY_DECLARATION_ACCESSORS_PREFIX); - public static readonly Location PROPERTY_DECLARATION_INTERFACE_SPECIFIER = new(CsSpace.Location.PROPERTY_DECLARATION_INTERFACE_SPECIFIER_PREFIX); - public static readonly Location USING_DIRECTIVE_ALIAS = new(CsSpace.Location.USING_DIRECTIVE_ALIAS); - public static readonly Location USING_DIRECTIVE_GLOBAL = new(CsSpace.Location.USING_DIRECTIVE_GLOBAL_SUFFIX); - } -} diff --git a/Rewrite/src/Rewrite.CSharp/Tree/CsSpace.cs b/Rewrite/src/Rewrite.CSharp/Tree/CsSpace.cs deleted file mode 100644 index 1102478..0000000 --- a/Rewrite/src/Rewrite.CSharp/Tree/CsSpace.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Rewrite.RewriteJava.Tree; - -namespace Rewrite.RewriteCSharp.Tree; - -public interface CsSpace -{ - [SuppressMessage("ReSharper", "InconsistentNaming")] - public record Location - { - public static readonly Location USING_STATEMENT_AWAIT_KEYWORD = new(); - public static readonly Location USING_STATEMENT_EXPRESSION = new(); - public static readonly Location USING_STATEMENT_RESOURCE = new(); - public static readonly Location USING_STATEMENT_PREFIX = new(); - public static readonly Location TYPE_PARAMETERS_CONSTRAINT_PREFIX = new(); - public static readonly Location TYPE_PARAMETERS_CONSTRAINT_CLAUSE_PREFIX = new(); - public static readonly Location METHOD_DECLARATION_PREFIX = new(); - public static readonly Location METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES = new(); - public static readonly Location REF_STRUCT_CONSTRAINT_PREFIX = new(); - public static readonly Location ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS = new(); - public static readonly Location ALLOWS_CONSTRAINT_CLAUSE_PREFIX = new(); - public static readonly Location DEFAULT_CONSTRAINT_PREFIX = new(); - public static readonly Location CONSTRUCTOR_CONSTRAINT_PREFIX = new(); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS = new(); - public static readonly Location CLASS_OR_STRUCT_CONSTRAINT_PREFIX = new(); - public static readonly Location ALLOWS_CONSTRAINT_EXPRESSIONS = new(); - public static readonly Location ALLOWS_CONSTRAINT_PREFIX = new(); - public static readonly Location TYPE_CONSTRAINT_PREFIX = new(); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS = new(); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER = new(); - public static readonly Location TYPE_PARAMETER_CONSTRAINT_CLAUSE_PREFIX = new(); - public static readonly Location CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES = new(); - public static readonly Location CLASS_DECLARATION_PREFIX = new(); - public static readonly Location NAMED_ARGUMENT_NAME_COLUMN_SUFFIX = new(); - public static readonly Location NAMED_ARGUMENT_PREFIX = new(); - public static readonly Location ANNOTATED_STATEMENT_PREFIX = new(); - public static readonly Location ARRAY_RANK_SPECIFIER_PREFIX = new(); - public static readonly Location ARRAY_RANK_SPECIFIER_SIZES = new(); - public static readonly Location ARRAY_RANK_SPECIFIER_SIZE_SUFFIX = new(); - public static readonly Location ASSIGNMENT_OPERATION_OPERATOR = new(); - public static readonly Location ASSIGNMENT_OPERATION_PREFIX = new(); - public static readonly Location ATTRIBUTE_LIST_ATTRIBUTE_SUFFIX = new(); - public static readonly Location ATTRIBUTE_LIST_PREFIX = new(); - public static readonly Location ATTRIBUTE_LIST_TARGET_SUFFIX = new(); - public static readonly Location AWAIT_EXPRESSION_PREFIX = new(); - public static readonly Location BINARY_OPERATOR = new(); - public static readonly Location BINARY_PREFIX = new(); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_END = new(); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_EXTERNS_SUFFIX = new(); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_MEMBERS = new(); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_NAME = new(); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_PREFIX = new(); - public static readonly Location BLOCK_SCOPE_NAMESPACE_DECLARATION_USINGS = new(); - public static readonly Location COLLECTION_EXPRESSION_ELEMENTS = new(); - public static readonly Location COLLECTION_EXPRESSION_PREFIX = new(); - public static readonly Location COMPILATION_UNIT_EXTERNS_SUFFIX = new(); - public static readonly Location COMPILATION_UNIT_MEMBERS = new(); - public static readonly Location COMPILATION_UNIT_USINGS = new(); - public static readonly Location EXPRESSION_STATEMENT_PREFIX = new(); - public static readonly Location EXTERN_ALIAS_IDENTIFIER = new(); - public static readonly Location EXTERN_ALIAS_PREFIX = new(); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_EXTERNS_SUFFIX = new(); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_MEMBERS = new(); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_NAME = new(); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_PREFIX = new(); - public static readonly Location FILE_SCOPE_NAMESPACE_DECLARATION_USINGS = new(); - public static readonly Location INTERPOLATED_STRING_PARTS_SUFFIX = new(); - public static readonly Location INTERPOLATED_STRING_PREFIX = new(); - public static readonly Location INTERPOLATION_ALIGNMENT_SUFFIX = new(); - public static readonly Location INTERPOLATION_EXPRESSION_SUFFIX = new(); - public static readonly Location INTERPOLATION_FORMAT_SUFFIX = new(); - public static readonly Location INTERPOLATION_PREFIX = new(); - public static readonly Location NULL_SAFE_EXPRESSION_EXPRESSION_SUFFIX = new(); - public static readonly Location NULL_SAFE_EXPRESSION_PREFIX = new(); - public static readonly Location PROPERTY_DECLARATION_ACCESSORS = new(); - public static readonly Location PROPERTY_DECLARATION_ACCESSORS_PREFIX = new(); - public static readonly Location PROPERTY_DECLARATION_EXPRESSION_BODY = new(); - public static readonly Location PROPERTY_DECLARATION_INITIALIZER = new(); - public static readonly Location PROPERTY_DECLARATION_INTERFACE_SPECIFIER_PREFIX = new(); - public static readonly Location PROPERTY_DECLARATION_PREFIX = new(); - public static readonly Location STATEMENT_EXPRESSION_PREFIX = new(); - public static readonly Location USING_DIRECTIVE_ALIAS = new(); - public static readonly Location USING_DIRECTIVE_GLOBAL_SUFFIX = new(); - public static readonly Location USING_DIRECTIVE_PREFIX = new(); - public static readonly Location USING_DIRECTIVE_STATIC = new(); - public static readonly Location USING_DIRECTIVE_UNSAFE = new(); - public static readonly Location LAMBDA_PREFIX = new(); - } -} diff --git a/Rewrite/src/Rewrite.CSharp/Tree/DeclarationExpression.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/DeclarationExpression.g.cs new file mode 100644 index 0000000..505351d --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/DeclarationExpression.g.cs @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class DeclarationExpression( + Guid id, + Space prefix, + Markers markers, + TypeTree? typeExpression, + VariableDesignation variables + ) : Cs, Expression, TypedTree, MutableTree + { + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitDeclarationExpression(this, p); + } + + public JavaType? Type => Extensions.GetJavaType(this); + + public DeclarationExpression WithType(JavaType newType) + { + return Extensions.WithJavaType(this, newType); + } + public Guid Id => id; + + public DeclarationExpression WithId(Guid newId) + { + return newId == id ? this : new DeclarationExpression(newId, prefix, markers, typeExpression, variables); + } + public Space Prefix => prefix; + + public DeclarationExpression WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new DeclarationExpression(id, newPrefix, markers, typeExpression, variables); + } + public Markers Markers => markers; + + public DeclarationExpression WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new DeclarationExpression(id, prefix, newMarkers, typeExpression, variables); + } + public TypeTree? TypeExpression => typeExpression; + + public DeclarationExpression WithTypeExpression(TypeTree? newTypeExpression) + { + return ReferenceEquals(newTypeExpression, typeExpression) ? this : new DeclarationExpression(id, prefix, markers, newTypeExpression, variables); + } + public Cs.VariableDesignation Variables => variables; + + public DeclarationExpression WithVariables(Cs.VariableDesignation newVariables) + { + return ReferenceEquals(newVariables, variables) ? this : new DeclarationExpression(id, prefix, markers, typeExpression, newVariables); + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is DeclarationExpression && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/DiscardVariableDesignation.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/DiscardVariableDesignation.g.cs new file mode 100644 index 0000000..2dacf0c --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/DiscardVariableDesignation.g.cs @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class DiscardVariableDesignation( + Guid id, + Space prefix, + Markers markers, + J.Identifier discard + ) : Cs.VariableDesignation, Cs, MutableTree + { + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitDiscardVariableDesignation(this, p); + } + + public JavaType? Type => Extensions.GetJavaType(this); + + public DiscardVariableDesignation WithType(JavaType newType) + { + return Extensions.WithJavaType(this, newType); + } + public Guid Id => id; + + public DiscardVariableDesignation WithId(Guid newId) + { + return newId == id ? this : new DiscardVariableDesignation(newId, prefix, markers, discard); + } + public Space Prefix => prefix; + + public DiscardVariableDesignation WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new DiscardVariableDesignation(id, newPrefix, markers, discard); + } + public Markers Markers => markers; + + public DiscardVariableDesignation WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new DiscardVariableDesignation(id, prefix, newMarkers, discard); + } + public J.Identifier Discard => discard; + + public DiscardVariableDesignation WithDiscard(J.Identifier newDiscard) + { + return ReferenceEquals(newDiscard, discard) ? this : new DiscardVariableDesignation(id, prefix, markers, newDiscard); + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is DiscardVariableDesignation && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/Extensions.cs b/Rewrite/src/Rewrite.CSharp/Tree/Extensions.cs index 3c11d69..3dc0f59 100644 --- a/Rewrite/src/Rewrite.CSharp/Tree/Extensions.cs +++ b/Rewrite/src/Rewrite.CSharp/Tree/Extensions.cs @@ -5,6 +5,21 @@ namespace Rewrite.RewriteCSharp.Tree; internal static class Extensions { + + + + public static JavaType? GetJavaType(Cs.DeclarationExpression expr) + { + // todo: not implemented properly + return null; + } + public static Cs.DeclarationExpression WithJavaType(Cs.DeclarationExpression expr, JavaType newType) + { + // todo: not implemented properly + return expr; + } + + public static JavaType? GetJavaType(Cs.Lambda expr) { return expr.LambdaExpression.Type; @@ -74,13 +89,41 @@ public static Cs.Interpolation WithJavaType(Cs.Interpolation expr, JavaType newT return expr.WithExpression(expr.Expression.WithType(newType)); } - public static JavaType? GetJavaType(Cs.NamedArgument expr) + public static JavaType? GetJavaType(Cs.Argument expr) { - return expr.Type; + return expr.Expression.Type; } - public static Cs.NamedArgument WithJavaType(Cs.NamedArgument expr, JavaType newType) + public static Cs.Argument WithJavaType(Cs.Argument expr, JavaType newType) { return expr.WithExpression(expr.Expression.WithType(newType)); } + + public static JavaType? GetJavaType(Cs.SingleVariableDesignation expr) + { + return expr.Name.Type; + } + public static Cs.SingleVariableDesignation WithJavaType(Cs.SingleVariableDesignation expr, JavaType newType) + { + return expr.WithName(expr.Name.WithType(newType)); + } + + public static JavaType? GetJavaType(Cs.DiscardVariableDesignation expr) + { + return expr.Discard.Type; + } + public static Cs.DiscardVariableDesignation WithJavaType(Cs.DiscardVariableDesignation expr, JavaType newType) + { + return expr.WithDiscard(expr.Discard.WithType(newType)); + } + + public static JavaType? GetJavaType(Cs.TupleExpression expr) + { + return null; + } + + public static Cs.TupleExpression WithJavaType(Cs.TupleExpression expr, JavaType newType) + { + return expr; + } } diff --git a/Rewrite/src/Rewrite.CSharp/Tree/Keyword.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/Keyword.g.cs new file mode 100644 index 0000000..15211c6 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/Keyword.g.cs @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class Keyword( + Guid id, + Space prefix, + Markers markers, + KeywordKind kind + ) : Cs, MutableTree + { + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitKeyword(this, p); + } + + public Guid Id => id; + + public Keyword WithId(Guid newId) + { + return newId == id ? this : new Keyword(newId, prefix, markers, kind); + } + public Space Prefix => prefix; + + public Keyword WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new Keyword(id, newPrefix, markers, kind); + } + public Markers Markers => markers; + + public Keyword WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new Keyword(id, prefix, newMarkers, kind); + } + public Cs.KeywordKind Kind => kind; + + public Keyword WithKind(Cs.KeywordKind newKind) + { + return newKind == kind ? this : new Keyword(id, prefix, markers, newKind); + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is Keyword && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/ParenthesizedVariableDesignation.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/ParenthesizedVariableDesignation.g.cs new file mode 100644 index 0000000..f824d50 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/ParenthesizedVariableDesignation.g.cs @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class ParenthesizedVariableDesignation( + Guid id, + Space prefix, + Markers markers, + JContainer variables, + JavaType? type + ) : Cs.VariableDesignation, Cs, MutableTree + { + [NonSerialized] private WeakReference? _padding; + + public PaddingHelper Padding + { + get + { + PaddingHelper? p; + if (_padding == null) + { + p = new PaddingHelper(this); + _padding = new WeakReference(p); + } + else + { + _padding.TryGetTarget(out p); + if (p == null || p.T != this) + { + p = new PaddingHelper(this); + _padding.SetTarget(p); + } + } + return p; + } + } + + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitParenthesizedVariableDesignation(this, p); + } + + public Guid Id => id; + + public ParenthesizedVariableDesignation WithId(Guid newId) + { + return newId == id ? this : new ParenthesizedVariableDesignation(newId, prefix, markers, _variables, type); + } + public Space Prefix => prefix; + + public ParenthesizedVariableDesignation WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new ParenthesizedVariableDesignation(id, newPrefix, markers, _variables, type); + } + public Markers Markers => markers; + + public ParenthesizedVariableDesignation WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new ParenthesizedVariableDesignation(id, prefix, newMarkers, _variables, type); + } + private readonly JContainer _variables = variables; + public IList Variables => _variables.GetElements(); + + public ParenthesizedVariableDesignation WithVariables(IList newVariables) + { + return Padding.WithVariables(JContainer.WithElements(_variables, newVariables)); + } + public JavaType? Type => type; + + public ParenthesizedVariableDesignation WithType(JavaType? newType) + { + return newType == type ? this : new ParenthesizedVariableDesignation(id, prefix, markers, _variables, newType); + } + public sealed record PaddingHelper(Cs.ParenthesizedVariableDesignation T) + { + public JContainer Variables => T._variables; + + public Cs.ParenthesizedVariableDesignation WithVariables(JContainer newVariables) + { + return T._variables == newVariables ? T : new Cs.ParenthesizedVariableDesignation(T.Id, T.Prefix, T.Markers, newVariables, T.Type); + } + + } + + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is ParenthesizedVariableDesignation && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/SingleVariableDesignation.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/SingleVariableDesignation.g.cs new file mode 100644 index 0000000..6cf98db --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/SingleVariableDesignation.g.cs @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class SingleVariableDesignation( + Guid id, + Space prefix, + Markers markers, + J.Identifier name + ) : Cs.VariableDesignation, Cs, MutableTree + { + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitSingleVariableDesignation(this, p); + } + + public JavaType? Type => Extensions.GetJavaType(this); + + public SingleVariableDesignation WithType(JavaType newType) + { + return Extensions.WithJavaType(this, newType); + } + public Guid Id => id; + + public SingleVariableDesignation WithId(Guid newId) + { + return newId == id ? this : new SingleVariableDesignation(newId, prefix, markers, name); + } + public Space Prefix => prefix; + + public SingleVariableDesignation WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new SingleVariableDesignation(id, newPrefix, markers, name); + } + public Markers Markers => markers; + + public SingleVariableDesignation WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new SingleVariableDesignation(id, prefix, newMarkers, name); + } + public J.Identifier Name => name; + + public SingleVariableDesignation WithName(J.Identifier newName) + { + return ReferenceEquals(newName, name) ? this : new SingleVariableDesignation(id, prefix, markers, newName); + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is SingleVariableDesignation && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/TupleExpression.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/TupleExpression.g.cs new file mode 100644 index 0000000..dade061 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/TupleExpression.g.cs @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class TupleExpression( + Guid id, + Space prefix, + Markers markers, + JContainer arguments + ) : Cs, Expression, MutableTree + { + [NonSerialized] private WeakReference? _padding; + + public PaddingHelper Padding + { + get + { + PaddingHelper? p; + if (_padding == null) + { + p = new PaddingHelper(this); + _padding = new WeakReference(p); + } + else + { + _padding.TryGetTarget(out p); + if (p == null || p.T != this) + { + p = new PaddingHelper(this); + _padding.SetTarget(p); + } + } + return p; + } + } + + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitTupleExpression(this, p); + } + + public JavaType? Type => Extensions.GetJavaType(this); + + public TupleExpression WithType(JavaType newType) + { + return Extensions.WithJavaType(this, newType); + } + public Guid Id => id; + + public TupleExpression WithId(Guid newId) + { + return newId == id ? this : new TupleExpression(newId, prefix, markers, _arguments); + } + public Space Prefix => prefix; + + public TupleExpression WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new TupleExpression(id, newPrefix, markers, _arguments); + } + public Markers Markers => markers; + + public TupleExpression WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new TupleExpression(id, prefix, newMarkers, _arguments); + } + private readonly JContainer _arguments = arguments; + public IList Arguments => _arguments.GetElements(); + + public TupleExpression WithArguments(IList newArguments) + { + return Padding.WithArguments(JContainer.WithElements(_arguments, newArguments)); + } + public sealed record PaddingHelper(Cs.TupleExpression T) + { + public JContainer Arguments => T._arguments; + + public Cs.TupleExpression WithArguments(JContainer newArguments) + { + return T._arguments == newArguments ? T : new Cs.TupleExpression(T.Id, T.Prefix, T.Markers, newArguments); + } + + } + + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is TupleExpression && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/Unary.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/Unary.g.cs new file mode 100644 index 0000000..11dcc7c --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/Unary.g.cs @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class Unary( + Guid id, + Space prefix, + Markers markers, + JLeftPadded @operator, + Expression expression, + JavaType? javaType + ) : Cs, Statement, Expression, TypedTree, MutableTree + { + [NonSerialized] private WeakReference? _padding; + + public PaddingHelper Padding + { + get + { + PaddingHelper? p; + if (_padding == null) + { + p = new PaddingHelper(this); + _padding = new WeakReference(p); + } + else + { + _padding.TryGetTarget(out p); + if (p == null || p.T != this) + { + p = new PaddingHelper(this); + _padding.SetTarget(p); + } + } + return p; + } + } + + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitUnary(this, p); + } + + public Guid Id => id; + + public Unary WithId(Guid newId) + { + return newId == id ? this : new Unary(newId, prefix, markers, _operator, expression, javaType); + } + public Space Prefix => prefix; + + public Unary WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new Unary(id, newPrefix, markers, _operator, expression, javaType); + } + public Markers Markers => markers; + + public Unary WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new Unary(id, prefix, newMarkers, _operator, expression, javaType); + } + private readonly JLeftPadded _operator = @operator; + public Type Operator => _operator.Element; + + public Unary WithOperator(Type newOperator) + { + return Padding.WithOperator(_operator.WithElement(newOperator)); + } + public Expression Expression => expression; + + public Unary WithExpression(Expression newExpression) + { + return ReferenceEquals(newExpression, expression) ? this : new Unary(id, prefix, markers, _operator, newExpression, javaType); + } + public JavaType? JavaType => javaType; + + public Unary WithJavaType(JavaType? newJavaType) + { + return newJavaType == javaType ? this : new Unary(id, prefix, markers, _operator, expression, newJavaType); + } + public enum Type + { + SuppressNullableWarning, + } + public sealed record PaddingHelper(Cs.Unary T) + { + public JLeftPadded Operator => T._operator; + + public Cs.Unary WithOperator(JLeftPadded newOperator) + { + return T._operator == newOperator ? T : new Cs.Unary(T.Id, T.Prefix, T.Markers, newOperator, T.Expression, T.JavaType); + } + + } + + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is Unary && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/UsingStatement.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/UsingStatement.g.cs index 64603c0..d2ad476 100644 --- a/Rewrite/src/Rewrite.CSharp/Tree/UsingStatement.g.cs +++ b/Rewrite/src/Rewrite.CSharp/Tree/UsingStatement.g.cs @@ -30,7 +30,7 @@ public partial class UsingStatement( Guid id, Space prefix, Markers markers, - Space? awaitKeyword, + Keyword? awaitKeyword, JContainer expression, Statement statement ) : Cs, Statement, MutableTree @@ -83,11 +83,11 @@ public UsingStatement WithMarkers(Markers newMarkers) { return ReferenceEquals(newMarkers, markers) ? this : new UsingStatement(id, prefix, newMarkers, awaitKeyword, _expression, statement); } - public Space? AwaitKeyword => awaitKeyword; + public Cs.Keyword? AwaitKeyword => awaitKeyword; - public UsingStatement WithAwaitKeyword(Space? newAwaitKeyword) + public UsingStatement WithAwaitKeyword(Cs.Keyword? newAwaitKeyword) { - return newAwaitKeyword == awaitKeyword ? this : new UsingStatement(id, prefix, markers, newAwaitKeyword, _expression, statement); + return ReferenceEquals(newAwaitKeyword, awaitKeyword) ? this : new UsingStatement(id, prefix, markers, newAwaitKeyword, _expression, statement); } private readonly JContainer _expression = expression; public IList Expression => _expression.GetElements(); diff --git a/Rewrite/src/Rewrite.CSharp/Tree/VariableDesignation.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/VariableDesignation.g.cs new file mode 100644 index 0000000..30961da --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/VariableDesignation.g.cs @@ -0,0 +1,29 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + public partial interface VariableDesignation : Expression, Cs + { + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/_CommonOverrides.cs b/Rewrite/src/Rewrite.CSharp/Tree/_CommonOverrides.cs deleted file mode 100644 index e93990e..0000000 --- a/Rewrite/src/Rewrite.CSharp/Tree/_CommonOverrides.cs +++ /dev/null @@ -1,108 +0,0 @@ -using Rewrite.Core; -using Rewrite.RewriteCSharp.Tree; -using Rewrite.RewriteJava.Tree; - -namespace Rewrite.RewriteCSharp.Tree -{ - public partial interface Cs - { - partial class UsingStatement - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - partial class AnnotatedStatement - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ArrayRankSpecifier - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class AssignmentOperation - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class AttributeList - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class AwaitExpression - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Binary - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class BlockScopeNamespaceDeclaration - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class CollectionExpression - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class CompilationUnit - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ExpressionStatement - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ExternAlias - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class FileScopeNamespaceDeclaration - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class InterpolatedString - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Interpolation - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class NamedArgument - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class NullSafeExpression - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class PropertyDeclaration - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class StatementExpression - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class UsingDirective - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - } -} diff --git a/Rewrite/src/Rewrite.Core/AsyncHelper.cs b/Rewrite/src/Rewrite.Core/AsyncHelper.cs new file mode 100644 index 0000000..fdf07a7 --- /dev/null +++ b/Rewrite/src/Rewrite.Core/AsyncHelper.cs @@ -0,0 +1,28 @@ +namespace Rewrite.Remote; + +public static class AsyncHelper +{ + private static readonly TaskFactory _myTaskFactory = new + TaskFactory(CancellationToken.None, + TaskCreationOptions.None, + TaskContinuationOptions.None, + TaskScheduler.Default); + + public static TResult RunSync(Func> func) + { + return AsyncHelper._myTaskFactory + .StartNew>(func) + .Unwrap() + .GetAwaiter() + .GetResult(); + } + + public static void RunSync(Func func) + { + AsyncHelper._myTaskFactory + .StartNew(func) + .Unwrap() + .GetAwaiter() + .GetResult(); + } +} diff --git a/Rewrite/src/Rewrite.Java/Rewrite.Java.csproj b/Rewrite/src/Rewrite.Java/Rewrite.Java.csproj index a0a9cbf..e8c9303 100644 --- a/Rewrite/src/Rewrite.Java/Rewrite.Java.csproj +++ b/Rewrite/src/Rewrite.Java/Rewrite.Java.csproj @@ -20,5 +20,9 @@ - + + + diff --git a/Rewrite/src/Rewrite.Java/Tree/Extensions.cs b/Rewrite/src/Rewrite.Java/Tree/Extensions.cs index 5d42715..1dc955a 100644 --- a/Rewrite/src/Rewrite.Java/Tree/Extensions.cs +++ b/Rewrite/src/Rewrite.Java/Tree/Extensions.cs @@ -339,7 +339,7 @@ public static J.MethodInvocation WithMethodType(this J.MethodInvocation method, public static J.MethodInvocation WithName(J.MethodInvocation method, J.Identifier name) { - if (method.Name == name) return method; + if (ReferenceEquals(method.Name, name)) return method; // FIXME add type attribution logic return new J.MethodInvocation(method.Id, method.Prefix, method.Markers, method.Padding.Select, method.Padding.TypeParameters, name, method.Padding.Arguments, method.MethodType); diff --git a/Rewrite/src/Rewrite.Java/Tree/_CommonOverrides.cs b/Rewrite/src/Rewrite.Java/Tree/_CommonOverrides.cs deleted file mode 100644 index 7f3a383..0000000 --- a/Rewrite/src/Rewrite.Java/Tree/_CommonOverrides.cs +++ /dev/null @@ -1,307 +0,0 @@ -using Rewrite.Core; - -namespace Rewrite.RewriteJava.Tree -{ - public partial interface J - { - partial class AnnotatedType - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Annotation - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ArrayAccess - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ArrayDimension - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ArrayType - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Assert - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Assignment - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class AssignmentOperation - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Binary - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Block - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Break - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Case - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ClassDeclaration - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class CompilationUnit - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Continue - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ControlParentheses - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class DoWhileLoop - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Empty - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class EnumValue - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class EnumValueSet - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class FieldAccess - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ForEachLoop - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ForLoop - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Identifier - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class If - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Import - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class InstanceOf - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class IntersectionType - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Label - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Lambda - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Literal - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class MemberReference - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class MethodDeclaration - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class MethodInvocation - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Modifier - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class MultiCatch - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class NewArray - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class NewClass - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class NullableType - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Package - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ParameterizedType - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Parentheses - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class ParenthesizedTypeTree - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Primitive - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Return - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Switch - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class SwitchExpression - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Synchronized - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Ternary - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Throw - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Try - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class TypeCast - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class TypeParameter - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class TypeParameters - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Unary - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Unknown - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class VariableDeclarations - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class WhileLoop - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Wildcard - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - - partial class Yield - { - public override string? ToString() => Core.Tree.ToString(this) ?? base.ToString(); - } - } -} diff --git a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs index 77097c1..aef2e1c 100644 --- a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs +++ b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs @@ -64,14 +64,15 @@ public override J VisitCompilationUnit(Cs.CompilationUnit compilationUnit, Recei return compilationUnit; } - public override J VisitNamedArgument(Cs.NamedArgument namedArgument, ReceiverContext ctx) + public override J VisitArgument(Cs.Argument argument, ReceiverContext ctx) { - namedArgument = namedArgument.WithId(ctx.ReceiveValue(namedArgument.Id)!); - namedArgument = namedArgument.WithPrefix(ctx.ReceiveNode(namedArgument.Prefix, ReceiveSpace)!); - namedArgument = namedArgument.WithMarkers(ctx.ReceiveNode(namedArgument.Markers, ctx.ReceiveMarkers)!); - namedArgument = namedArgument.Padding.WithNameColumn(ctx.ReceiveNode(namedArgument.Padding.NameColumn, ReceiveRightPadded)); - namedArgument = namedArgument.WithExpression(ctx.ReceiveNode(namedArgument.Expression, ctx.ReceiveTree)!); - return namedArgument; + argument = argument.WithId(ctx.ReceiveValue(argument.Id)!); + argument = argument.WithPrefix(ctx.ReceiveNode(argument.Prefix, ReceiveSpace)!); + argument = argument.WithMarkers(ctx.ReceiveNode(argument.Markers, ctx.ReceiveMarkers)!); + argument = argument.Padding.WithNameColumn(ctx.ReceiveNode(argument.Padding.NameColumn, ReceiveRightPadded)); + argument = argument.WithRefKindKeyword(ctx.ReceiveNode(argument.RefKindKeyword, ctx.ReceiveTree)); + argument = argument.WithExpression(ctx.ReceiveNode(argument.Expression, ctx.ReceiveTree)!); + return argument; } public override J VisitAnnotatedStatement(Cs.AnnotatedStatement annotatedStatement, ReceiverContext ctx) @@ -258,6 +259,15 @@ public override J VisitPropertyDeclaration(Cs.PropertyDeclaration propertyDeclar return propertyDeclaration; } + public override J VisitKeyword(Cs.Keyword keyword, ReceiverContext ctx) + { + keyword = keyword.WithId(ctx.ReceiveValue(keyword.Id)!); + keyword = keyword.WithPrefix(ctx.ReceiveNode(keyword.Prefix, ReceiveSpace)!); + keyword = keyword.WithMarkers(ctx.ReceiveNode(keyword.Markers, ctx.ReceiveMarkers)!); + keyword = keyword.WithKind(ctx.ReceiveValue(keyword.Kind)!); + return keyword; + } + public override J VisitLambda(Cs.Lambda lambda, ReceiverContext ctx) { lambda = lambda.WithId(ctx.ReceiveValue(lambda.Id)!); @@ -293,7 +303,7 @@ public override J VisitUsingStatement(Cs.UsingStatement usingStatement, Receiver usingStatement = usingStatement.WithId(ctx.ReceiveValue(usingStatement.Id)!); usingStatement = usingStatement.WithPrefix(ctx.ReceiveNode(usingStatement.Prefix, ReceiveSpace)!); usingStatement = usingStatement.WithMarkers(ctx.ReceiveNode(usingStatement.Markers, ctx.ReceiveMarkers)!); - usingStatement = usingStatement.WithAwaitKeyword(ctx.ReceiveNode(usingStatement.AwaitKeyword, ReceiveSpace)); + usingStatement = usingStatement.WithAwaitKeyword(ctx.ReceiveNode(usingStatement.AwaitKeyword, ctx.ReceiveTree)); usingStatement = usingStatement.Padding.WithExpression(ctx.ReceiveNode(usingStatement.Padding.Expression, ReceiveContainer)!); usingStatement = usingStatement.WithStatement(ctx.ReceiveNode(usingStatement.Statement, ctx.ReceiveTree)!); return usingStatement; @@ -360,6 +370,84 @@ public override J VisitDefaultConstraint(Cs.DefaultConstraint defaultConstraint, return defaultConstraint; } + public override J VisitDeclarationExpression(Cs.DeclarationExpression declarationExpression, ReceiverContext ctx) + { + declarationExpression = declarationExpression.WithId(ctx.ReceiveValue(declarationExpression.Id)!); + declarationExpression = declarationExpression.WithPrefix(ctx.ReceiveNode(declarationExpression.Prefix, ReceiveSpace)!); + declarationExpression = declarationExpression.WithMarkers(ctx.ReceiveNode(declarationExpression.Markers, ctx.ReceiveMarkers)!); + declarationExpression = declarationExpression.WithTypeExpression(ctx.ReceiveNode(declarationExpression.TypeExpression, ctx.ReceiveTree)); + declarationExpression = declarationExpression.WithVariables(ctx.ReceiveNode(declarationExpression.Variables, ctx.ReceiveTree)!); + return declarationExpression; + } + + public override J VisitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, ReceiverContext ctx) + { + singleVariableDesignation = singleVariableDesignation.WithId(ctx.ReceiveValue(singleVariableDesignation.Id)!); + singleVariableDesignation = singleVariableDesignation.WithPrefix(ctx.ReceiveNode(singleVariableDesignation.Prefix, ReceiveSpace)!); + singleVariableDesignation = singleVariableDesignation.WithMarkers(ctx.ReceiveNode(singleVariableDesignation.Markers, ctx.ReceiveMarkers)!); + singleVariableDesignation = singleVariableDesignation.WithName(ctx.ReceiveNode(singleVariableDesignation.Name, ctx.ReceiveTree)!); + return singleVariableDesignation; + } + + public override J VisitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, ReceiverContext ctx) + { + parenthesizedVariableDesignation = parenthesizedVariableDesignation.WithId(ctx.ReceiveValue(parenthesizedVariableDesignation.Id)!); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.WithPrefix(ctx.ReceiveNode(parenthesizedVariableDesignation.Prefix, ReceiveSpace)!); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.WithMarkers(ctx.ReceiveNode(parenthesizedVariableDesignation.Markers, ctx.ReceiveMarkers)!); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.Padding.WithVariables(ctx.ReceiveNode(parenthesizedVariableDesignation.Padding.Variables, ReceiveContainer)!); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.WithType(ctx.ReceiveValue(parenthesizedVariableDesignation.Type)); + return parenthesizedVariableDesignation; + } + + public override J VisitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, ReceiverContext ctx) + { + discardVariableDesignation = discardVariableDesignation.WithId(ctx.ReceiveValue(discardVariableDesignation.Id)!); + discardVariableDesignation = discardVariableDesignation.WithPrefix(ctx.ReceiveNode(discardVariableDesignation.Prefix, ReceiveSpace)!); + discardVariableDesignation = discardVariableDesignation.WithMarkers(ctx.ReceiveNode(discardVariableDesignation.Markers, ctx.ReceiveMarkers)!); + discardVariableDesignation = discardVariableDesignation.WithDiscard(ctx.ReceiveNode(discardVariableDesignation.Discard, ctx.ReceiveTree)!); + return discardVariableDesignation; + } + + public override J VisitTupleExpression(Cs.TupleExpression tupleExpression, ReceiverContext ctx) + { + tupleExpression = tupleExpression.WithId(ctx.ReceiveValue(tupleExpression.Id)!); + tupleExpression = tupleExpression.WithPrefix(ctx.ReceiveNode(tupleExpression.Prefix, ReceiveSpace)!); + tupleExpression = tupleExpression.WithMarkers(ctx.ReceiveNode(tupleExpression.Markers, ctx.ReceiveMarkers)!); + tupleExpression = tupleExpression.Padding.WithArguments(ctx.ReceiveNode(tupleExpression.Padding.Arguments, ReceiveContainer)!); + return tupleExpression; + } + + public override J VisitConstructor(Cs.Constructor constructor, ReceiverContext ctx) + { + constructor = constructor.WithId(ctx.ReceiveValue(constructor.Id)!); + constructor = constructor.WithPrefix(ctx.ReceiveNode(constructor.Prefix, ReceiveSpace)!); + constructor = constructor.WithMarkers(ctx.ReceiveNode(constructor.Markers, ctx.ReceiveMarkers)!); + constructor = constructor.WithInitializer(ctx.ReceiveNode(constructor.Initializer, ctx.ReceiveTree)); + constructor = constructor.WithConstructorCore(ctx.ReceiveNode(constructor.ConstructorCore, ctx.ReceiveTree)!); + return constructor; + } + + public override J VisitUnary(Cs.Unary unary, ReceiverContext ctx) + { + unary = unary.WithId(ctx.ReceiveValue(unary.Id)!); + unary = unary.WithPrefix(ctx.ReceiveNode(unary.Prefix, ReceiveSpace)!); + unary = unary.WithMarkers(ctx.ReceiveNode(unary.Markers, ctx.ReceiveMarkers)!); + unary = unary.Padding.WithOperator(ctx.ReceiveNode(unary.Padding.Operator, ReceiveLeftPadded)!); + unary = unary.WithExpression(ctx.ReceiveNode(unary.Expression, ctx.ReceiveTree)!); + unary = unary.WithJavaType(ctx.ReceiveValue(unary.JavaType)); + return unary; + } + + public override J VisitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, ReceiverContext ctx) + { + constructorInitializer = constructorInitializer.WithId(ctx.ReceiveValue(constructorInitializer.Id)!); + constructorInitializer = constructorInitializer.WithPrefix(ctx.ReceiveNode(constructorInitializer.Prefix, ReceiveSpace)!); + constructorInitializer = constructorInitializer.WithMarkers(ctx.ReceiveNode(constructorInitializer.Markers, ctx.ReceiveMarkers)!); + constructorInitializer = constructorInitializer.WithKeyword(ctx.ReceiveNode(constructorInitializer.Keyword, ctx.ReceiveTree)!); + constructorInitializer = constructorInitializer.Padding.WithArguments(ctx.ReceiveNode(constructorInitializer.Padding.Arguments, ReceiveContainer)!); + return constructorInitializer; + } + public override J VisitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.WithId(ctx.ReceiveValue(annotatedType.Id)!); @@ -1110,13 +1198,14 @@ public Rewrite.Core.Tree Create(string type, ReceiverContext ctx) where T : R ); } - if (type is "Rewrite.RewriteCSharp.Tree.Cs.NamedArgument" or "org.openrewrite.csharp.tree.Cs$NamedArgument") + if (type is "Rewrite.RewriteCSharp.Tree.Cs.Argument" or "org.openrewrite.csharp.tree.Cs$Argument") { - return new Cs.NamedArgument( + return new Cs.Argument( ctx.ReceiveValue(default(Guid))!, ctx.ReceiveNode(default(Space), ReceiveSpace)!, ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, ctx.ReceiveNode(default(JRightPadded), ReceiveRightPadded), + ctx.ReceiveNode(default(Cs.Keyword), ctx.ReceiveTree), ctx.ReceiveNode(default(Expression), ctx.ReceiveTree)! ); } @@ -1322,6 +1411,16 @@ public Rewrite.Core.Tree Create(string type, ReceiverContext ctx) where T : R ); } + if (type is "Rewrite.RewriteCSharp.Tree.Cs.Keyword" or "org.openrewrite.csharp.tree.Cs$Keyword") + { + return new Cs.Keyword( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveValue(default(Cs.KeywordKind))! + ); + } + if (type is "Rewrite.RewriteCSharp.Tree.Cs.Lambda" or "org.openrewrite.csharp.tree.Cs$Lambda") { return new Cs.Lambda( @@ -1361,7 +1460,7 @@ public Rewrite.Core.Tree Create(string type, ReceiverContext ctx) where T : R ctx.ReceiveValue(default(Guid))!, ctx.ReceiveNode(default(Space), ReceiveSpace)!, ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, - ctx.ReceiveNode(default(Space), ReceiveSpace), + ctx.ReceiveNode(default(Cs.Keyword), ctx.ReceiveTree), ctx.ReceiveNode(default(JContainer), ReceiveContainer)!, ctx.ReceiveNode(default(Statement), ctx.ReceiveTree)! ); @@ -1435,6 +1534,92 @@ public Rewrite.Core.Tree Create(string type, ReceiverContext ctx) where T : R ); } + if (type is "Rewrite.RewriteCSharp.Tree.Cs.DeclarationExpression" or "org.openrewrite.csharp.tree.Cs$DeclarationExpression") + { + return new Cs.DeclarationExpression( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(TypeTree), ctx.ReceiveTree), + ctx.ReceiveNode(default(Cs.VariableDesignation), ctx.ReceiveTree)! + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.SingleVariableDesignation" or "org.openrewrite.csharp.tree.Cs$SingleVariableDesignation") + { + return new Cs.SingleVariableDesignation( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(J.Identifier), ctx.ReceiveTree)! + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.ParenthesizedVariableDesignation" or "org.openrewrite.csharp.tree.Cs$ParenthesizedVariableDesignation") + { + return new Cs.ParenthesizedVariableDesignation( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(JContainer), ReceiveContainer)!, + ctx.ReceiveValue(default(JavaType)) + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.DiscardVariableDesignation" or "org.openrewrite.csharp.tree.Cs$DiscardVariableDesignation") + { + return new Cs.DiscardVariableDesignation( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(J.Identifier), ctx.ReceiveTree)! + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.TupleExpression" or "org.openrewrite.csharp.tree.Cs$TupleExpression") + { + return new Cs.TupleExpression( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(JContainer), ReceiveContainer)! + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.Constructor" or "org.openrewrite.csharp.tree.Cs$Constructor") + { + return new Cs.Constructor( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(Cs.ConstructorInitializer), ctx.ReceiveTree), + ctx.ReceiveNode(default(J.MethodDeclaration), ctx.ReceiveTree)! + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.Unary" or "org.openrewrite.csharp.tree.Cs$Unary") + { + return new Cs.Unary( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(JLeftPadded), ReceiveLeftPadded)!, + ctx.ReceiveNode(default(Expression), ctx.ReceiveTree)!, + ctx.ReceiveValue(default(JavaType)) + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.ConstructorInitializer" or "org.openrewrite.csharp.tree.Cs$ConstructorInitializer") + { + return new Cs.ConstructorInitializer( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(Cs.Keyword), ctx.ReceiveTree)!, + ctx.ReceiveNode(default(JContainer), ReceiveContainer)! + ); + } + if (type is "Rewrite.RewriteCSharp.Tree.J.AnnotatedType" or "org.openrewrite.java.tree.J$AnnotatedType") { return new J.AnnotatedType( diff --git a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs index 08c7853..c3d7018 100644 --- a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs +++ b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs @@ -55,14 +55,15 @@ public override J VisitCompilationUnit(Cs.CompilationUnit compilationUnit, Sende return compilationUnit; } - public override J VisitNamedArgument(Cs.NamedArgument namedArgument, SenderContext ctx) + public override J VisitArgument(Cs.Argument argument, SenderContext ctx) { - ctx.SendValue(namedArgument, v => v.Id); - ctx.SendNode(namedArgument, v => v.Prefix, SendSpace); - ctx.SendNode(namedArgument, v => v.Markers, ctx.SendMarkers); - ctx.SendNode(namedArgument, v => v.Padding.NameColumn, SendRightPadded); - ctx.SendNode(namedArgument, v => v.Expression, ctx.SendTree); - return namedArgument; + ctx.SendValue(argument, v => v.Id); + ctx.SendNode(argument, v => v.Prefix, SendSpace); + ctx.SendNode(argument, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(argument, v => v.Padding.NameColumn, SendRightPadded); + ctx.SendNode(argument, v => v.RefKindKeyword, ctx.SendTree); + ctx.SendNode(argument, v => v.Expression, ctx.SendTree); + return argument; } public override J VisitAnnotatedStatement(Cs.AnnotatedStatement annotatedStatement, SenderContext ctx) @@ -249,6 +250,15 @@ public override J VisitPropertyDeclaration(Cs.PropertyDeclaration propertyDeclar return propertyDeclaration; } + public override J VisitKeyword(Cs.Keyword keyword, SenderContext ctx) + { + ctx.SendValue(keyword, v => v.Id); + ctx.SendNode(keyword, v => v.Prefix, SendSpace); + ctx.SendNode(keyword, v => v.Markers, ctx.SendMarkers); + ctx.SendValue(keyword, v => v.Kind); + return keyword; + } + public override J VisitLambda(Cs.Lambda lambda, SenderContext ctx) { ctx.SendValue(lambda, v => v.Id); @@ -284,7 +294,7 @@ public override J VisitUsingStatement(Cs.UsingStatement usingStatement, SenderCo ctx.SendValue(usingStatement, v => v.Id); ctx.SendNode(usingStatement, v => v.Prefix, SendSpace); ctx.SendNode(usingStatement, v => v.Markers, ctx.SendMarkers); - ctx.SendNode(usingStatement, v => v.AwaitKeyword, SendSpace); + ctx.SendNode(usingStatement, v => v.AwaitKeyword, ctx.SendTree); ctx.SendNode(usingStatement, v => v.Padding.Expression, SendContainer); ctx.SendNode(usingStatement, v => v.Statement, ctx.SendTree); return usingStatement; @@ -351,6 +361,84 @@ public override J VisitDefaultConstraint(Cs.DefaultConstraint defaultConstraint, return defaultConstraint; } + public override J VisitDeclarationExpression(Cs.DeclarationExpression declarationExpression, SenderContext ctx) + { + ctx.SendValue(declarationExpression, v => v.Id); + ctx.SendNode(declarationExpression, v => v.Prefix, SendSpace); + ctx.SendNode(declarationExpression, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(declarationExpression, v => v.TypeExpression, ctx.SendTree); + ctx.SendNode(declarationExpression, v => v.Variables, ctx.SendTree); + return declarationExpression; + } + + public override J VisitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, SenderContext ctx) + { + ctx.SendValue(singleVariableDesignation, v => v.Id); + ctx.SendNode(singleVariableDesignation, v => v.Prefix, SendSpace); + ctx.SendNode(singleVariableDesignation, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(singleVariableDesignation, v => v.Name, ctx.SendTree); + return singleVariableDesignation; + } + + public override J VisitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, SenderContext ctx) + { + ctx.SendValue(parenthesizedVariableDesignation, v => v.Id); + ctx.SendNode(parenthesizedVariableDesignation, v => v.Prefix, SendSpace); + ctx.SendNode(parenthesizedVariableDesignation, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(parenthesizedVariableDesignation, v => v.Padding.Variables, SendContainer); + ctx.SendTypedValue(parenthesizedVariableDesignation, v => v.Type); + return parenthesizedVariableDesignation; + } + + public override J VisitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, SenderContext ctx) + { + ctx.SendValue(discardVariableDesignation, v => v.Id); + ctx.SendNode(discardVariableDesignation, v => v.Prefix, SendSpace); + ctx.SendNode(discardVariableDesignation, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(discardVariableDesignation, v => v.Discard, ctx.SendTree); + return discardVariableDesignation; + } + + public override J VisitTupleExpression(Cs.TupleExpression tupleExpression, SenderContext ctx) + { + ctx.SendValue(tupleExpression, v => v.Id); + ctx.SendNode(tupleExpression, v => v.Prefix, SendSpace); + ctx.SendNode(tupleExpression, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(tupleExpression, v => v.Padding.Arguments, SendContainer); + return tupleExpression; + } + + public override J VisitConstructor(Cs.Constructor constructor, SenderContext ctx) + { + ctx.SendValue(constructor, v => v.Id); + ctx.SendNode(constructor, v => v.Prefix, SendSpace); + ctx.SendNode(constructor, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(constructor, v => v.Initializer, ctx.SendTree); + ctx.SendNode(constructor, v => v.ConstructorCore, ctx.SendTree); + return constructor; + } + + public override J VisitUnary(Cs.Unary unary, SenderContext ctx) + { + ctx.SendValue(unary, v => v.Id); + ctx.SendNode(unary, v => v.Prefix, SendSpace); + ctx.SendNode(unary, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(unary, v => v.Padding.Operator, SendLeftPadded); + ctx.SendNode(unary, v => v.Expression, ctx.SendTree); + ctx.SendTypedValue(unary, v => v.JavaType); + return unary; + } + + public override J VisitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, SenderContext ctx) + { + ctx.SendValue(constructorInitializer, v => v.Id); + ctx.SendNode(constructorInitializer, v => v.Prefix, SendSpace); + ctx.SendNode(constructorInitializer, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(constructorInitializer, v => v.Keyword, ctx.SendTree); + ctx.SendNode(constructorInitializer, v => v.Padding.Arguments, SendContainer); + return constructorInitializer; + } + public override J VisitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.SendValue(annotatedType, v => v.Id); diff --git a/Rewrite/src/Rewrite.Test.CSharp/Assertions.cs b/Rewrite/src/Rewrite.Test.CSharp/Assertions.cs index f3889e8..c1097ee 100644 --- a/Rewrite/src/Rewrite.Test.CSharp/Assertions.cs +++ b/Rewrite/src/Rewrite.Test.CSharp/Assertions.cs @@ -19,9 +19,9 @@ public static ValidateSource Noop() return (sourceFile, _) => sourceFile; } - public static SourceSpecs CSharp([LanguageInjection("C#")] string before) + public static SourceSpec CSharp([LanguageInjection("C#")] string before) { - return CSharp(before, s => { }); + return CSharp(before, s => { }).Cast>().First(); } public static SourceSpecs CSharp([LanguageInjection("C#")] string before, Action> spec) diff --git a/Rewrite/src/Rewrite.Test/LocalPrinterFixture.cs b/Rewrite/src/Rewrite.Test/LocalPrinterFixture.cs index 77e6dcc..5b3bcf6 100644 --- a/Rewrite/src/Rewrite.Test/LocalPrinterFixture.cs +++ b/Rewrite/src/Rewrite.Test/LocalPrinterFixture.cs @@ -3,11 +3,16 @@ namespace Rewrite.CSharp.Tests; -public class LocalPrinterFixture +public class LocalPrinterFixture : IDisposable { public LocalPrinterFixture() { ITestExecutionContext.SetCurrent(new LocalTestExecutionContext()); IPrinterFactory.Set(new LocalPrinterFactory()); } + + public void Dispose() + { + // TODO release managed resources here + } } diff --git a/Rewrite/src/Rewrite.Test/SourceSpecExtensions.cs b/Rewrite/src/Rewrite.Test/SourceSpecExtensions.cs index 66a482e..80b50a0 100644 --- a/Rewrite/src/Rewrite.Test/SourceSpecExtensions.cs +++ b/Rewrite/src/Rewrite.Test/SourceSpecExtensions.cs @@ -12,6 +12,12 @@ public static T Parse(this SourceSpec sourceSpec) where T : SourceFile } + public static T Parse(this SourceSpec sourceSpec) where T : SourceFile + { + var sourceText = sourceSpec.Before ?? ""; + return (T)sourceSpec.Parser.Build().Parse(sourceText); + } + public static IEnumerable Parse(this SourceSpecs sourceSpec) where T : SourceFile { foreach (var spec in sourceSpec) diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Properties/AssemblyInfo.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7bb647d --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +#if REMOTE_PRINTER +[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)] +#endif diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.v3.ncrunchproject b/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.v3.ncrunchproject new file mode 100644 index 0000000..b1461fe --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.v3.ncrunchproject @@ -0,0 +1,27 @@ + + + + + Rewrite.CSharp.Tests.Solutions.SolutionTests + + + Rewrite.CSharp.Tests.PlayTests + + + Rewrite.CSharp.Tests.RoslynTestCases.ParseTests.ParseAndPrint + + + Rewrite.CSharp.Tests.FindClassDeclarationTests.ClassWithoutBody2 + + + Rewrite.CSharp.Tests.FindClassDeclarationTests + + + Rewrite.CSharp.Tests.FindClassTests + + + Rewrite.CSharp.Tests.RecipeOptionsTest + + + + \ No newline at end of file diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs b/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs new file mode 100644 index 0000000..062a62a --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs @@ -0,0 +1,27 @@ +// +// +// using Rewrite.Test.Engine.Remote; +// using Xunit.Abstractions; +// using Xunit.Sdk; +// +// [assembly: Xunit.TestFramework("Rewrite.CSharp.Tests.TestSetup", "Rewrite.CSharp.Tests")] +// +// namespace Rewrite.CSharp.Tests; +// +// public sealed class TestSetup : XunitTestFramework, IDisposable +// { +// #if REMOTE_PRINTER +// private readonly IDisposable _fixture = new RemotingFixture(); +// #else +// private readonly IDisposable _fixture = new LocalPrinterFixture(); +// #endif +// public TestSetup(IMessageSink messageSink) +// :base(messageSink) +// { +// } +// +// public new void Dispose() +// { +// _fixture.Dispose(); +// } +// } diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayAccessTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayAccessTests.cs index 154f051..0bdf349 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayAccessTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayAccessTests.cs @@ -6,7 +6,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ArrayAccessTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTests.cs index 1792945..f172ab6 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTests.cs @@ -5,7 +5,7 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] + public class ArrayTests : RewriteTest { [Fact] @@ -24,15 +24,14 @@ public class Foo } [Fact] - public void MultiDimensionalSpace() + public void ImplicitArray() { RewriteRun( CSharp( """ public class Foo { - object arr = /*0*/ new /*1*/ int /*2*/ [ /*3*/ 1 /*4*/ , /*5*/ 1 /*6*/ ] /*7*/ [ /*8*/ ] /*9*/ - { /*10*/ { /*11*/ { /*12*/ 1 /*13*/ , /*14*/ } /*15*/ } /*16*/ } /*17*/ ; /*18*/ + object arr = new [] { 1 }; } """ ) diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTypeTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTypeTests.cs index 9bfb191..3a39c97 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTypeTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ArrayTypeTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ArrayTypeTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AssignmentTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AssignmentTests.cs index 016e710..2ffe39a 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AssignmentTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AssignmentTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class AssignmentTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AsyncTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AsyncTests.cs index 49173bd..05c4142 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AsyncTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AsyncTests.cs @@ -5,7 +5,7 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] + public class AsyncTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AttributeTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AttributeTests.cs index 20e2086..8896032 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AttributeTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/AttributeTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class AttributeTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BinaryTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BinaryTests.cs index 4f09f16..b4255f4 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BinaryTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BinaryTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class BinaryTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BlockTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BlockTests.cs index 484f618..5da38ff 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BlockTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BlockTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class BlockTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ClassDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ClassDeclarationTests.cs index a46682f..87d58c2 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ClassDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ClassDeclarationTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ClassDeclarationTests : RewriteTest { [Fact] @@ -138,7 +137,6 @@ class Foo where T : struct; } [Fact] - [KnownBug] public void TypeParameterWithNewConstraint() { RewriteRun( @@ -151,7 +149,6 @@ public void TypeParameterWithNewConstraint() } [Fact] - [KnownBug] public void TypeParameterWithMultipleConstraints() { RewriteRun( diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CollectionExpressionTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CollectionExpressionTests.cs index 61b6ca9..10a8b8f 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CollectionExpressionTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CollectionExpressionTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class CollectionExpressionTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CompilationUnitTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CompilationUnitTests.cs index f7924bf..0519e27 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CompilationUnitTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/CompilationUnitTests.cs @@ -6,7 +6,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class CompilationUnitTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ConstructorTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ConstructorTests.cs new file mode 100644 index 0000000..fda7770 --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ConstructorTests.cs @@ -0,0 +1,44 @@ +using Rewrite.Test.CSharp; +using Rewrite.RewriteCSharp.Tree; +using Rewrite.Test; + +namespace Rewrite.CSharp.Tests.Tree; + +using static Assertions; + +public class ConstructorTests : RewriteTest +{ + [Fact] + public void VanillaConstructor() + { + var src = CSharp(""" + public class A + { + public A() + {} + }) + """); + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); + } + + [Fact] + public void ConstructorWithThisCall() + { + var src = CSharp(""" + public class A + { + public A() : this(1) + {} + + public A(int a) + { + } + }) + """); + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); + } +} + + diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EmptyTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EmptyTests.cs index fcdcc58..0ceaf85 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EmptyTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EmptyTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class EmptyTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ExternAliasTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ExternAliasTests.cs index e3ee9c9..f8dce8a 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ExternAliasTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ExternAliasTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ExternAliasTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/FieldTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/FieldTests.cs index 03c3c57..0734d3b 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/FieldTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/FieldTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class FieldTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForEachLoopTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForEachLoopTests.cs index 6291b82..5db8866 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForEachLoopTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForEachLoopTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ForEachLoopTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForLoopTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForLoopTests.cs index b2fa356..b5dea20 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForLoopTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ForLoopTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ForLoopTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/GlobalTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/GlobalTests.cs index 4116fe2..bef6088 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/GlobalTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/GlobalTests.cs @@ -1,6 +1,5 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class GlobalTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/IfTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/IfTests.cs index a3a4ea8..d026181 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/IfTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/IfTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class IfTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InstanceOfTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InstanceOfTests.cs index ccae01b..b0f4033 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InstanceOfTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InstanceOfTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class InstanceOfTests : RewriteTest { diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterfaceDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterfaceDeclarationTests.cs index fa960e8..3de40f6 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterfaceDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterfaceDeclarationTests.cs @@ -7,7 +7,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class InterfaceDeclarationTests : RewriteTest { diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterpolatedStringTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterpolatedStringTests.cs index 7b8f579..db92705 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterpolatedStringTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/InterpolatedStringTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class InterpolatedStringTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LabelTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LabelTests.cs index 103bf5c..2343061 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LabelTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LabelTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class LabelTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LambdaTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LambdaTests.cs index c617885..225cbd0 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LambdaTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LambdaTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class LambdaTests : RewriteTest { diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LiteralTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LiteralTests.cs index 6cf668e..c627d24 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LiteralTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/LiteralTests.cs @@ -7,7 +7,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class LiteralTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MemberAccessTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MemberAccessTests.cs index 0477441..11724b7 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MemberAccessTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MemberAccessTests.cs @@ -6,7 +6,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class MemberAccessTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs index f600c34..c4df359 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs @@ -7,7 +7,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class MethodDeclarationTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodInvocationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodInvocationTests.cs index ccebd64..0a1d87c 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodInvocationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodInvocationTests.cs @@ -6,7 +6,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class MethodInvocationTests : RewriteTest { [Fact] @@ -42,16 +41,59 @@ public class T [Fact] public void InvocationWithNamedParameters() { - RewriteRun( - CSharp( - @" + var src = CSharp( + @" public class T { - string s = this.Equals(obj: null); + void M() + { + string s = this.Equals(obj: null, 1); + } } " - ) ); + + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); + } + + [Fact] + public void InvocationWithOutParameter() + { + var src = CSharp( + """ + public class T + { + void M() + { + int.TryParse("1", out var one); + } + } + """ + ); + + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); + } + + [Fact] + public void InvocationWithRefParameter() + { + var src = CSharp( + """ + public class T + { + void M() + { + int.TryParse("1", ref one); + } + } + """ + ); + + var lst = src.Parse(); + var statement = lst.Descendents().OfType().First(); + lst.ToString().ShouldBeSameAs(src.Before); } [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NamespaceDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NamespaceDeclarationTests.cs index 98b4ab0..26a1e72 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NamespaceDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NamespaceDeclarationTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class NamespaceDeclarationTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NewClassTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NewClassTests.cs index cff7336..dc3f037 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NewClassTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NewClassTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class NewClassTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullSafeExpressionTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullSafeExpressionTests.cs index 7e6b395..ec8ac6d 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullSafeExpressionTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullSafeExpressionTests.cs @@ -7,7 +7,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class NullSafeExpressionTests : RewriteTest { [Fact] @@ -84,8 +83,6 @@ public object M() } } """); - var lst = src.Parse().First(); - } [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullableTypeTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullableTypeTests.cs index 74c655e..4086451 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullableTypeTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullableTypeTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class NullableTypeTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ParenthesesTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ParenthesesTests.cs index 1b33bc8..9ed1fbe 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ParenthesesTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ParenthesesTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ParenthesesTests : RewriteTest { diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/PropertyDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/PropertyDeclarationTests.cs index 4f77848..89802d3 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/PropertyDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/PropertyDeclarationTests.cs @@ -8,7 +8,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class PropertyDeclarationTests : RewriteTest { diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ReturnTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ReturnTests.cs index 1958ac4..9c19134 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ReturnTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ReturnTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ReturnTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SizeOfTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SizeOfTests.cs index 0008dd9..eba478e 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SizeOfTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SizeOfTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class SizeOfTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/StructTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/StructTests.cs index 936e1ab..27ede0d 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/StructTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/StructTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class StructTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SwitchStatementTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SwitchStatementTests.cs index b6d783b..1465455 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SwitchStatementTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/SwitchStatementTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class SwitchStatementTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TernaryTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TernaryTests.cs index a72fa9d..d8644d9 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TernaryTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TernaryTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class TernaryTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ThrowTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ThrowTests.cs index f5632fe..fc0e81e 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ThrowTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/ThrowTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class ThrowTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TryTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TryTests.cs index 919c995..daa6ff1 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TryTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TryTests.cs @@ -7,7 +7,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class TryTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs new file mode 100644 index 0000000..9c1deff --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs @@ -0,0 +1,49 @@ +using FluentAssertions; +using Rewrite.Test.CSharp; +using Rewrite.RewriteJava.Tree; +using Rewrite.Test; + +namespace Rewrite.CSharp.Tests.Tree; + +using static Assertions; + +public class TupleTests : RewriteTest +{ + [Fact] + public void TupleDeconstruction() + { + var src = CSharp( + """ + public class T + { + void M() + { + var (x, y) = (1,2); + } + } + """ + ); + + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); + } + [Fact] + public void TupleExpression() + { + var src = CSharp( + """ + public class T + { + void M() + { + (var x, var y) = (1,2); + } + } + """ + ); + + var lst = src.Parse(); + var statement = lst.Descendents().OfType().First().Body!.Statements.First(); + lst.ToString().ShouldBeSameAs(src.Before); + } +} diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeCastTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeCastTests.cs index 0b42184..3b2831b 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeCastTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeCastTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class TypeCastTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeOfTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeOfTests.cs index f84ac7c..8d548b2 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeOfTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TypeOfTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class TypeOfTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UnaryTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UnaryTests.cs index 775bf74..e98319a 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UnaryTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UnaryTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class UnaryTests : RewriteTest { [Fact] @@ -45,4 +44,18 @@ class Test { ) ); } + + [Fact] + void NullWarningSuppress() + { + RewriteRun( + CSharp( + """ + class Test { + string a = ""!; + } + """ + ) + ); + } } diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingDirectiveTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingDirectiveTests.cs index d494ea6..932a763 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingDirectiveTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingDirectiveTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class UsingDirectiveTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingTests.cs index 690fbc5..a3ec1b0 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/UsingTests.cs @@ -7,7 +7,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class UsingTests(ITestOutputHelper output) : RewriteTest { private readonly ITestOutputHelper _output = output; diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/VariableDeclarationsTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/VariableDeclarationsTests.cs index 737c6d1..82b0948 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/VariableDeclarationsTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/VariableDeclarationsTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class VariableDeclarationsTests : RewriteTest { [Fact] diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/WhileLoopTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/WhileLoopTests.cs index 8e6d354..82147c0 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/WhileLoopTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/WhileLoopTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests.Tree; using static Assertions; -[Collection(Collections.PrinterAccess)] public class WhileLoopTests : RewriteTest { [Fact] diff --git a/build.gradle.kts b/build.gradle.kts index e840e7c..90cfa41 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,4 @@ plugins { allprojects { group = "org.openrewrite" description = "OpenRewrite C# language module." - repositories { - mavenCentral() - } } diff --git a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java index 2bd6038..b253c00 100644 --- a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java +++ b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java @@ -92,13 +92,14 @@ public Cs.CompilationUnit visitCompilationUnit(Cs.CompilationUnit compilationUni } @Override - public Cs.NamedArgument visitNamedArgument(Cs.NamedArgument namedArgument, ReceiverContext ctx) { - namedArgument = namedArgument.withId(ctx.receiveNonNullValue(namedArgument.getId(), UUID.class)); - namedArgument = namedArgument.withPrefix(ctx.receiveNonNullNode(namedArgument.getPrefix(), CSharpReceiver::receiveSpace)); - namedArgument = namedArgument.withMarkers(ctx.receiveNonNullNode(namedArgument.getMarkers(), ctx::receiveMarkers)); - namedArgument = namedArgument.getPadding().withNameColumn(ctx.receiveNode(namedArgument.getPadding().getNameColumn(), CSharpReceiver::receiveRightPaddedTree)); - namedArgument = namedArgument.withExpression(ctx.receiveNonNullNode(namedArgument.getExpression(), ctx::receiveTree)); - return namedArgument; + public Cs.Argument visitArgument(Cs.Argument argument, ReceiverContext ctx) { + argument = argument.withId(ctx.receiveNonNullValue(argument.getId(), UUID.class)); + argument = argument.withPrefix(ctx.receiveNonNullNode(argument.getPrefix(), CSharpReceiver::receiveSpace)); + argument = argument.withMarkers(ctx.receiveNonNullNode(argument.getMarkers(), ctx::receiveMarkers)); + argument = argument.getPadding().withNameColumn(ctx.receiveNode(argument.getPadding().getNameColumn(), CSharpReceiver::receiveRightPaddedTree)); + argument = argument.withRefKindKeyword(ctx.receiveNode(argument.getRefKindKeyword(), ctx::receiveTree)); + argument = argument.withExpression(ctx.receiveNonNullNode(argument.getExpression(), ctx::receiveTree)); + return argument; } @Override @@ -285,6 +286,15 @@ public Cs.PropertyDeclaration visitPropertyDeclaration(Cs.PropertyDeclaration pr return propertyDeclaration; } + @Override + public Cs.Keyword visitKeyword(Cs.Keyword keyword, ReceiverContext ctx) { + keyword = keyword.withId(ctx.receiveNonNullValue(keyword.getId(), UUID.class)); + keyword = keyword.withPrefix(ctx.receiveNonNullNode(keyword.getPrefix(), CSharpReceiver::receiveSpace)); + keyword = keyword.withMarkers(ctx.receiveNonNullNode(keyword.getMarkers(), ctx::receiveMarkers)); + keyword = keyword.withKind(ctx.receiveNonNullValue(keyword.getKind(), Cs.KeywordKind.class)); + return keyword; + } + @Override public Cs.Lambda visitLambda(Cs.Lambda lambda, ReceiverContext ctx) { lambda = lambda.withId(ctx.receiveNonNullValue(lambda.getId(), UUID.class)); @@ -320,7 +330,7 @@ public Cs.UsingStatement visitUsingStatement(Cs.UsingStatement usingStatement, R usingStatement = usingStatement.withId(ctx.receiveNonNullValue(usingStatement.getId(), UUID.class)); usingStatement = usingStatement.withPrefix(ctx.receiveNonNullNode(usingStatement.getPrefix(), CSharpReceiver::receiveSpace)); usingStatement = usingStatement.withMarkers(ctx.receiveNonNullNode(usingStatement.getMarkers(), ctx::receiveMarkers)); - usingStatement = usingStatement.withAwaitKeyword(ctx.receiveNode(usingStatement.getAwaitKeyword(), CSharpReceiver::receiveSpace)); + usingStatement = usingStatement.withAwaitKeyword(ctx.receiveNode(usingStatement.getAwaitKeyword(), ctx::receiveTree)); usingStatement = usingStatement.getPadding().withExpression(ctx.receiveNonNullNode(usingStatement.getPadding().getExpression(), CSharpReceiver::receiveContainer)); usingStatement = usingStatement.withStatement(ctx.receiveNonNullNode(usingStatement.getStatement(), ctx::receiveTree)); return usingStatement; @@ -387,6 +397,84 @@ public Cs.DefaultConstraint visitDefaultConstraint(Cs.DefaultConstraint defaultC return defaultConstraint; } + @Override + public Cs.DeclarationExpression visitDeclarationExpression(Cs.DeclarationExpression declarationExpression, ReceiverContext ctx) { + declarationExpression = declarationExpression.withId(ctx.receiveNonNullValue(declarationExpression.getId(), UUID.class)); + declarationExpression = declarationExpression.withPrefix(ctx.receiveNonNullNode(declarationExpression.getPrefix(), CSharpReceiver::receiveSpace)); + declarationExpression = declarationExpression.withMarkers(ctx.receiveNonNullNode(declarationExpression.getMarkers(), ctx::receiveMarkers)); + declarationExpression = declarationExpression.withTypeExpression(ctx.receiveNode(declarationExpression.getTypeExpression(), ctx::receiveTree)); + declarationExpression = declarationExpression.withVariables(ctx.receiveNonNullNode(declarationExpression.getVariables(), ctx::receiveTree)); + return declarationExpression; + } + + @Override + public Cs.SingleVariableDesignation visitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, ReceiverContext ctx) { + singleVariableDesignation = singleVariableDesignation.withId(ctx.receiveNonNullValue(singleVariableDesignation.getId(), UUID.class)); + singleVariableDesignation = singleVariableDesignation.withPrefix(ctx.receiveNonNullNode(singleVariableDesignation.getPrefix(), CSharpReceiver::receiveSpace)); + singleVariableDesignation = singleVariableDesignation.withMarkers(ctx.receiveNonNullNode(singleVariableDesignation.getMarkers(), ctx::receiveMarkers)); + singleVariableDesignation = singleVariableDesignation.withName(ctx.receiveNonNullNode(singleVariableDesignation.getName(), ctx::receiveTree)); + return singleVariableDesignation; + } + + @Override + public Cs.ParenthesizedVariableDesignation visitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, ReceiverContext ctx) { + parenthesizedVariableDesignation = parenthesizedVariableDesignation.withId(ctx.receiveNonNullValue(parenthesizedVariableDesignation.getId(), UUID.class)); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.withPrefix(ctx.receiveNonNullNode(parenthesizedVariableDesignation.getPrefix(), CSharpReceiver::receiveSpace)); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.withMarkers(ctx.receiveNonNullNode(parenthesizedVariableDesignation.getMarkers(), ctx::receiveMarkers)); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.getPadding().withVariables(ctx.receiveNonNullNode(parenthesizedVariableDesignation.getPadding().getVariables(), CSharpReceiver::receiveContainer)); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.withType(ctx.receiveValue(parenthesizedVariableDesignation.getType(), JavaType.class)); + return parenthesizedVariableDesignation; + } + + @Override + public Cs.DiscardVariableDesignation visitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, ReceiverContext ctx) { + discardVariableDesignation = discardVariableDesignation.withId(ctx.receiveNonNullValue(discardVariableDesignation.getId(), UUID.class)); + discardVariableDesignation = discardVariableDesignation.withPrefix(ctx.receiveNonNullNode(discardVariableDesignation.getPrefix(), CSharpReceiver::receiveSpace)); + discardVariableDesignation = discardVariableDesignation.withMarkers(ctx.receiveNonNullNode(discardVariableDesignation.getMarkers(), ctx::receiveMarkers)); + discardVariableDesignation = discardVariableDesignation.withDiscard(ctx.receiveNonNullNode(discardVariableDesignation.getDiscard(), ctx::receiveTree)); + return discardVariableDesignation; + } + + @Override + public Cs.TupleExpression visitTupleExpression(Cs.TupleExpression tupleExpression, ReceiverContext ctx) { + tupleExpression = tupleExpression.withId(ctx.receiveNonNullValue(tupleExpression.getId(), UUID.class)); + tupleExpression = tupleExpression.withPrefix(ctx.receiveNonNullNode(tupleExpression.getPrefix(), CSharpReceiver::receiveSpace)); + tupleExpression = tupleExpression.withMarkers(ctx.receiveNonNullNode(tupleExpression.getMarkers(), ctx::receiveMarkers)); + tupleExpression = tupleExpression.getPadding().withArguments(ctx.receiveNonNullNode(tupleExpression.getPadding().getArguments(), CSharpReceiver::receiveContainer)); + return tupleExpression; + } + + @Override + public Cs.Constructor visitConstructor(Cs.Constructor constructor, ReceiverContext ctx) { + constructor = constructor.withId(ctx.receiveNonNullValue(constructor.getId(), UUID.class)); + constructor = constructor.withPrefix(ctx.receiveNonNullNode(constructor.getPrefix(), CSharpReceiver::receiveSpace)); + constructor = constructor.withMarkers(ctx.receiveNonNullNode(constructor.getMarkers(), ctx::receiveMarkers)); + constructor = constructor.withInitializer(ctx.receiveNode(constructor.getInitializer(), ctx::receiveTree)); + constructor = constructor.withConstructorCore(ctx.receiveNonNullNode(constructor.getConstructorCore(), ctx::receiveTree)); + return constructor; + } + + @Override + public Cs.Unary visitUnary(Cs.Unary unary, ReceiverContext ctx) { + unary = unary.withId(ctx.receiveNonNullValue(unary.getId(), UUID.class)); + unary = unary.withPrefix(ctx.receiveNonNullNode(unary.getPrefix(), CSharpReceiver::receiveSpace)); + unary = unary.withMarkers(ctx.receiveNonNullNode(unary.getMarkers(), ctx::receiveMarkers)); + unary = unary.getPadding().withOperator(ctx.receiveNonNullNode(unary.getPadding().getOperator(), leftPaddedValueReceiver(org.openrewrite.csharp.tree.Cs.Unary.Type.class))); + unary = unary.withExpression(ctx.receiveNonNullNode(unary.getExpression(), ctx::receiveTree)); + unary = unary.withType(ctx.receiveValue(unary.getType(), JavaType.class)); + return unary; + } + + @Override + public Cs.ConstructorInitializer visitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, ReceiverContext ctx) { + constructorInitializer = constructorInitializer.withId(ctx.receiveNonNullValue(constructorInitializer.getId(), UUID.class)); + constructorInitializer = constructorInitializer.withPrefix(ctx.receiveNonNullNode(constructorInitializer.getPrefix(), CSharpReceiver::receiveSpace)); + constructorInitializer = constructorInitializer.withMarkers(ctx.receiveNonNullNode(constructorInitializer.getMarkers(), ctx::receiveMarkers)); + constructorInitializer = constructorInitializer.withKeyword(ctx.receiveNonNullNode(constructorInitializer.getKeyword(), ctx::receiveTree)); + constructorInitializer = constructorInitializer.getPadding().withArguments(ctx.receiveNonNullNode(constructorInitializer.getPadding().getArguments(), CSharpReceiver::receiveContainer)); + return constructorInitializer; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class)); @@ -1105,12 +1193,13 @@ public T create(Class type, ReceiverContext ctx) { ); } - if (type == Cs.NamedArgument.class) { - return (T) new Cs.NamedArgument( + if (type == Cs.Argument.class) { + return (T) new Cs.Argument( ctx.receiveNonNullValue(null, UUID.class), ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), ctx.receiveNode(null, CSharpReceiver::receiveRightPaddedTree), + ctx.receiveNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, ctx::receiveTree) ); } @@ -1299,6 +1388,15 @@ public T create(Class type, ReceiverContext ctx) { ); } + if (type == Cs.Keyword.class) { + return (T) new Cs.Keyword( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullValue(null, Cs.KeywordKind.class) + ); + } + if (type == Cs.Lambda.class) { return (T) new Cs.Lambda( ctx.receiveNonNullValue(null, UUID.class), @@ -1334,7 +1432,7 @@ public T create(Class type, ReceiverContext ctx) { ctx.receiveNonNullValue(null, UUID.class), ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, CSharpReceiver::receiveContainer), ctx.receiveNonNullNode(null, ctx::receiveTree) ); @@ -1401,6 +1499,84 @@ public T create(Class type, ReceiverContext ctx) { ); } + if (type == Cs.DeclarationExpression.class) { + return (T) new Cs.DeclarationExpression( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, ctx::receiveTree) + ); + } + + if (type == Cs.SingleVariableDesignation.class) { + return (T) new Cs.SingleVariableDesignation( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, ctx::receiveTree) + ); + } + + if (type == Cs.ParenthesizedVariableDesignation.class) { + return (T) new Cs.ParenthesizedVariableDesignation( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveContainer), + ctx.receiveValue(null, JavaType.class) + ); + } + + if (type == Cs.DiscardVariableDesignation.class) { + return (T) new Cs.DiscardVariableDesignation( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, ctx::receiveTree) + ); + } + + if (type == Cs.TupleExpression.class) { + return (T) new Cs.TupleExpression( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveContainer) + ); + } + + if (type == Cs.Constructor.class) { + return (T) new Cs.Constructor( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, ctx::receiveTree) + ); + } + + if (type == Cs.Unary.class) { + return (T) new Cs.Unary( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(org.openrewrite.csharp.tree.Cs.Unary.Type.class)), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveValue(null, JavaType.class) + ); + } + + if (type == Cs.ConstructorInitializer.class) { + return (T) new Cs.ConstructorInitializer( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveContainer) + ); + } + if (type == J.AnnotatedType.class) { return (T) new J.AnnotatedType( ctx.receiveNonNullValue(null, UUID.class), diff --git a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java index 4ef4036..3f6cf81 100644 --- a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java +++ b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java @@ -75,13 +75,14 @@ public Cs.CompilationUnit visitCompilationUnit(Cs.CompilationUnit compilationUni } @Override - public Cs.NamedArgument visitNamedArgument(Cs.NamedArgument namedArgument, SenderContext ctx) { - ctx.sendValue(namedArgument, Cs.NamedArgument::getId); - ctx.sendNode(namedArgument, Cs.NamedArgument::getPrefix, CSharpSender::sendSpace); - ctx.sendNode(namedArgument, Cs.NamedArgument::getMarkers, ctx::sendMarkers); - ctx.sendNode(namedArgument, e -> e.getPadding().getNameColumn(), CSharpSender::sendRightPadded); - ctx.sendNode(namedArgument, Cs.NamedArgument::getExpression, ctx::sendTree); - return namedArgument; + public Cs.Argument visitArgument(Cs.Argument argument, SenderContext ctx) { + ctx.sendValue(argument, Cs.Argument::getId); + ctx.sendNode(argument, Cs.Argument::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(argument, Cs.Argument::getMarkers, ctx::sendMarkers); + ctx.sendNode(argument, e -> e.getPadding().getNameColumn(), CSharpSender::sendRightPadded); + ctx.sendNode(argument, Cs.Argument::getRefKindKeyword, ctx::sendTree); + ctx.sendNode(argument, Cs.Argument::getExpression, ctx::sendTree); + return argument; } @Override @@ -268,6 +269,15 @@ public Cs.PropertyDeclaration visitPropertyDeclaration(Cs.PropertyDeclaration pr return propertyDeclaration; } + @Override + public Cs.Keyword visitKeyword(Cs.Keyword keyword, SenderContext ctx) { + ctx.sendValue(keyword, Cs.Keyword::getId); + ctx.sendNode(keyword, Cs.Keyword::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(keyword, Cs.Keyword::getMarkers, ctx::sendMarkers); + ctx.sendValue(keyword, Cs.Keyword::getKind); + return keyword; + } + @Override public Cs.Lambda visitLambda(Cs.Lambda lambda, SenderContext ctx) { ctx.sendValue(lambda, Cs.Lambda::getId); @@ -303,7 +313,7 @@ public Cs.UsingStatement visitUsingStatement(Cs.UsingStatement usingStatement, S ctx.sendValue(usingStatement, Cs.UsingStatement::getId); ctx.sendNode(usingStatement, Cs.UsingStatement::getPrefix, CSharpSender::sendSpace); ctx.sendNode(usingStatement, Cs.UsingStatement::getMarkers, ctx::sendMarkers); - ctx.sendNode(usingStatement, Cs.UsingStatement::getAwaitKeyword, CSharpSender::sendSpace); + ctx.sendNode(usingStatement, Cs.UsingStatement::getAwaitKeyword, ctx::sendTree); ctx.sendNode(usingStatement, e -> e.getPadding().getExpression(), CSharpSender::sendContainer); ctx.sendNode(usingStatement, Cs.UsingStatement::getStatement, ctx::sendTree); return usingStatement; @@ -370,6 +380,84 @@ public Cs.DefaultConstraint visitDefaultConstraint(Cs.DefaultConstraint defaultC return defaultConstraint; } + @Override + public Cs.DeclarationExpression visitDeclarationExpression(Cs.DeclarationExpression declarationExpression, SenderContext ctx) { + ctx.sendValue(declarationExpression, Cs.DeclarationExpression::getId); + ctx.sendNode(declarationExpression, Cs.DeclarationExpression::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(declarationExpression, Cs.DeclarationExpression::getMarkers, ctx::sendMarkers); + ctx.sendNode(declarationExpression, Cs.DeclarationExpression::getTypeExpression, ctx::sendTree); + ctx.sendNode(declarationExpression, Cs.DeclarationExpression::getVariables, ctx::sendTree); + return declarationExpression; + } + + @Override + public Cs.SingleVariableDesignation visitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, SenderContext ctx) { + ctx.sendValue(singleVariableDesignation, Cs.SingleVariableDesignation::getId); + ctx.sendNode(singleVariableDesignation, Cs.SingleVariableDesignation::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(singleVariableDesignation, Cs.SingleVariableDesignation::getMarkers, ctx::sendMarkers); + ctx.sendNode(singleVariableDesignation, Cs.SingleVariableDesignation::getName, ctx::sendTree); + return singleVariableDesignation; + } + + @Override + public Cs.ParenthesizedVariableDesignation visitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, SenderContext ctx) { + ctx.sendValue(parenthesizedVariableDesignation, Cs.ParenthesizedVariableDesignation::getId); + ctx.sendNode(parenthesizedVariableDesignation, Cs.ParenthesizedVariableDesignation::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(parenthesizedVariableDesignation, Cs.ParenthesizedVariableDesignation::getMarkers, ctx::sendMarkers); + ctx.sendNode(parenthesizedVariableDesignation, e -> e.getPadding().getVariables(), CSharpSender::sendContainer); + ctx.sendTypedValue(parenthesizedVariableDesignation, Cs.ParenthesizedVariableDesignation::getType); + return parenthesizedVariableDesignation; + } + + @Override + public Cs.DiscardVariableDesignation visitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, SenderContext ctx) { + ctx.sendValue(discardVariableDesignation, Cs.DiscardVariableDesignation::getId); + ctx.sendNode(discardVariableDesignation, Cs.DiscardVariableDesignation::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(discardVariableDesignation, Cs.DiscardVariableDesignation::getMarkers, ctx::sendMarkers); + ctx.sendNode(discardVariableDesignation, Cs.DiscardVariableDesignation::getDiscard, ctx::sendTree); + return discardVariableDesignation; + } + + @Override + public Cs.TupleExpression visitTupleExpression(Cs.TupleExpression tupleExpression, SenderContext ctx) { + ctx.sendValue(tupleExpression, Cs.TupleExpression::getId); + ctx.sendNode(tupleExpression, Cs.TupleExpression::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(tupleExpression, Cs.TupleExpression::getMarkers, ctx::sendMarkers); + ctx.sendNode(tupleExpression, e -> e.getPadding().getArguments(), CSharpSender::sendContainer); + return tupleExpression; + } + + @Override + public Cs.Constructor visitConstructor(Cs.Constructor constructor, SenderContext ctx) { + ctx.sendValue(constructor, Cs.Constructor::getId); + ctx.sendNode(constructor, Cs.Constructor::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(constructor, Cs.Constructor::getMarkers, ctx::sendMarkers); + ctx.sendNode(constructor, Cs.Constructor::getInitializer, ctx::sendTree); + ctx.sendNode(constructor, Cs.Constructor::getConstructorCore, ctx::sendTree); + return constructor; + } + + @Override + public Cs.Unary visitUnary(Cs.Unary unary, SenderContext ctx) { + ctx.sendValue(unary, Cs.Unary::getId); + ctx.sendNode(unary, Cs.Unary::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(unary, Cs.Unary::getMarkers, ctx::sendMarkers); + ctx.sendNode(unary, e -> e.getPadding().getOperator(), CSharpSender::sendLeftPadded); + ctx.sendNode(unary, Cs.Unary::getExpression, ctx::sendTree); + ctx.sendTypedValue(unary, Cs.Unary::getType); + return unary; + } + + @Override + public Cs.ConstructorInitializer visitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, SenderContext ctx) { + ctx.sendValue(constructorInitializer, Cs.ConstructorInitializer::getId); + ctx.sendNode(constructorInitializer, Cs.ConstructorInitializer::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(constructorInitializer, Cs.ConstructorInitializer::getMarkers, ctx::sendMarkers); + ctx.sendNode(constructorInitializer, Cs.ConstructorInitializer::getKeyword, ctx::sendTree); + ctx.sendNode(constructorInitializer, e -> e.getPadding().getArguments(), CSharpSender::sendContainer); + return constructorInitializer; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.sendValue(annotatedType, J.AnnotatedType::getId); diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java index 6fe19b6..0553d8d 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java @@ -41,8 +41,8 @@ public Cs.CompilationUnit visitCompilationUnit(Cs.CompilationUnit compilationUni } @Override - public Cs.NamedArgument visitNamedArgument(Cs.NamedArgument namedArgument, P p) { - return (Cs.NamedArgument) super.visitNamedArgument(namedArgument, p); + public Cs.Argument visitArgument(Cs.Argument argument, P p) { + return (Cs.Argument) super.visitArgument(argument, p); } @Override @@ -130,6 +130,11 @@ public Cs.PropertyDeclaration visitPropertyDeclaration(Cs.PropertyDeclaration pr return (Cs.PropertyDeclaration) super.visitPropertyDeclaration(propertyDeclaration, p); } + @Override + public Cs.Keyword visitKeyword(Cs.Keyword keyword, P p) { + return (Cs.Keyword) super.visitKeyword(keyword, p); + } + @Override public Cs.Lambda visitLambda(Cs.Lambda lambda, P p) { return (Cs.Lambda) super.visitLambda(lambda, p); @@ -185,6 +190,46 @@ public Cs.DefaultConstraint visitDefaultConstraint(Cs.DefaultConstraint defaultC return (Cs.DefaultConstraint) super.visitDefaultConstraint(defaultConstraint, p); } + @Override + public Cs.DeclarationExpression visitDeclarationExpression(Cs.DeclarationExpression declarationExpression, P p) { + return (Cs.DeclarationExpression) super.visitDeclarationExpression(declarationExpression, p); + } + + @Override + public Cs.SingleVariableDesignation visitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, P p) { + return (Cs.SingleVariableDesignation) super.visitSingleVariableDesignation(singleVariableDesignation, p); + } + + @Override + public Cs.ParenthesizedVariableDesignation visitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, P p) { + return (Cs.ParenthesizedVariableDesignation) super.visitParenthesizedVariableDesignation(parenthesizedVariableDesignation, p); + } + + @Override + public Cs.DiscardVariableDesignation visitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, P p) { + return (Cs.DiscardVariableDesignation) super.visitDiscardVariableDesignation(discardVariableDesignation, p); + } + + @Override + public Cs.TupleExpression visitTupleExpression(Cs.TupleExpression tupleExpression, P p) { + return (Cs.TupleExpression) super.visitTupleExpression(tupleExpression, p); + } + + @Override + public Cs.Constructor visitConstructor(Cs.Constructor constructor, P p) { + return (Cs.Constructor) super.visitConstructor(constructor, p); + } + + @Override + public Cs.Unary visitUnary(Cs.Unary unary, P p) { + return (Cs.Unary) super.visitUnary(unary, p); + } + + @Override + public Cs.ConstructorInitializer visitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, P p) { + return (Cs.ConstructorInitializer) super.visitConstructorInitializer(constructorInitializer, p); + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, P p) { return (J.AnnotatedType) super.visitAnnotatedType(annotatedType, p); diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java index 86c293b..b6ea6a7 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java @@ -47,15 +47,30 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { } @Override - public J visitNamedArgument(Cs.NamedArgument namedArgument, PrintOutputCapture

p) { - beforeSyntax(namedArgument, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); - Cs.NamedArgument.Padding padding = namedArgument.getPadding(); + public J visitArgument(Cs.Argument argument, PrintOutputCapture

p) { + beforeSyntax(argument, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); + Cs.Argument.Padding padding = argument.getPadding(); - visitRightPadded(padding.getNameColumn(), CsRightPadded.Location.NAMED_ARGUMENT_NAME_COLUMN, p); - p.append(':'); - visit(namedArgument.getExpression(), p); - afterSyntax(namedArgument, p); - return namedArgument; + if (argument.getNameColumn() != null) { + visitRightPadded(padding.getNameColumn(), CsRightPadded.Location.NAMED_ARGUMENT_NAME_COLUMN, p); + p.append(':'); + } + if (argument.getRefKindKeyword() != null) + { + visitKeyword(argument.getRefKindKeyword(), p); + } + visit(argument.getExpression(), p); + afterSyntax(argument, p); + return argument; + } + + @Override + public Cs visitKeyword(Cs.Keyword keyword, PrintOutputCapture

p) + { + beforeSyntax(keyword, CsSpace.Location.KEYWORD_PREFIX, p); + p.append(keyword.getKind().toString().toLowerCase()); + afterSyntax(keyword, p); + return keyword; } @Override @@ -65,7 +80,7 @@ public J visitUsingStatement(Cs.UsingStatement usingStatement, PrintOutputCaptur p.append("using"); if (usingStatement.getAwaitKeyword() != null) { - visitSpace(usingStatement.getAwaitKeyword(), CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD, p); + visitKeyword(usingStatement.getAwaitKeyword(), p); } visitContainer("(", usingStatement.getPadding().getExpression(), CsContainer.Location.USING_STATEMENT_EXPRESSION, "", ")", p); @@ -73,6 +88,24 @@ public J visitUsingStatement(Cs.UsingStatement usingStatement, PrintOutputCaptur afterSyntax(usingStatement, p); return usingStatement; } + + @Override + public Cs visitUnary(Cs.Unary unary, PrintOutputCapture

p) + { + beforeSyntax(unary, Space.Location.UNARY_PREFIX, p); + switch (unary.getOperator()) + { + default: + case SuppressNullableWarning: + visit(unary.getExpression(), p); + p.append("!"); + break; + } + + afterSyntax(unary, p); + return unary; + } + @Override public Cs visitCompilationUnit(Cs.CompilationUnit compilationUnit, PrintOutputCapture

p) { beforeSyntax(compilationUnit, Space.Location.COMPILATION_UNIT_PREFIX, p); @@ -286,6 +319,44 @@ public J visitNullSafeExpression(Cs.NullSafeExpression nullSafeExpression, Print return nullSafeExpression; } + @Override + public Cs visitConstructor(Cs.Constructor constructor, PrintOutputCapture

p) + { + J.MethodDeclaration method = constructor.getConstructorCore(); + beforeSyntax(method, Space.Location.METHOD_DECLARATION_PREFIX, p); + visitSpace(Space.EMPTY, Space.Location.ANNOTATIONS, p); + visit(method.getLeadingAnnotations(), p); + for (J.Modifier modifier : method.getModifiers()) + { + delegate.visitModifier(modifier, p); + } + + visit(method.getAnnotations().getName().getAnnotations(), p); + visit(method.getName(), p); + + + if (!method.getMarkers().findFirst(CompactConstructor.class).isPresent()) { + visitContainer("(", method.getPadding().getParameters(), CsContainer.Location.METHOD_DECLARATION_PARAMETERS, ",", ")", p); + } + + visit(constructor.getInitializer(), p); + + visit(method.getBody(), p); + afterSyntax(constructor, p); + return constructor; + } + + @Override + public Cs visitConstructorInitializer(Cs.ConstructorInitializer node, PrintOutputCapture

p) + { + beforeSyntax(node, Space.Location.METHOD_DECLARATION_PREFIX, p); + p.append(":"); + visitKeyword(node.getKeyword(), p); + visitContainer("(", node.getPadding().getArguments(), CsContainer.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, ",", ")", p); + afterSyntax(node, p); + return node; + } + @Override public J visitPropertyDeclaration(Cs.PropertyDeclaration propertyDeclaration, PrintOutputCapture

p) { @@ -475,6 +546,19 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { } } + @Override + public J visitNewArray(J.NewArray newArray, PrintOutputCapture

p) + { + beforeSyntax(newArray, Space.Location.NEW_ARRAY_PREFIX, p); + p.append("new"); + + visit(newArray.getTypeExpression(), p); + visit(newArray.getDimensions(), p); + visitContainer("{", newArray.getPadding().getInitializer(), JContainer.Location.NEW_ARRAY_INITIALIZER, ",", "}", p); + afterSyntax(newArray, p); + return newArray; + } + @Override public J visitClassDeclaration(J.ClassDeclaration classDecl, PrintOutputCapture

p) { J csParent = CSharpPrinter.this.getCursor().getValue(); diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java index a1f743b..623e439 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java @@ -51,18 +51,19 @@ public J visitCompilationUnit(Cs.CompilationUnit compilationUnit, P p) { return compilationUnit; } - public J visitNamedArgument(Cs.NamedArgument namedArgument, P p) { - namedArgument = namedArgument.withPrefix(visitSpace(namedArgument.getPrefix(), CsSpace.Location.NAMED_ARGUMENT_PREFIX, p)); - Expression tempExpression = (Expression) visitExpression(namedArgument, p); - if (!(tempExpression instanceof Cs.NamedArgument)) + public J visitArgument(Cs.Argument argument, P p) { + argument = argument.withPrefix(visitSpace(argument.getPrefix(), CsSpace.Location.ARGUMENT_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(argument, p); + if (!(tempExpression instanceof Cs.Argument)) { return tempExpression; } - namedArgument = (Cs.NamedArgument) tempExpression; - namedArgument = namedArgument.withMarkers(visitMarkers(namedArgument.getMarkers(), p)); - namedArgument = namedArgument.getPadding().withNameColumn(visitRightPadded(namedArgument.getPadding().getNameColumn(), CsRightPadded.Location.NAMED_ARGUMENT_NAME_COLUMN, p)); - namedArgument = namedArgument.withExpression(visitAndCast(namedArgument.getExpression(), p)); - return namedArgument; + argument = (Cs.Argument) tempExpression; + argument = argument.withMarkers(visitMarkers(argument.getMarkers(), p)); + argument = argument.getPadding().withNameColumn(visitRightPadded(argument.getPadding().getNameColumn(), CsRightPadded.Location.ARGUMENT_NAME_COLUMN, p)); + argument = argument.withRefKindKeyword(visitAndCast(argument.getRefKindKeyword(), p)); + argument = argument.withExpression(visitAndCast(argument.getExpression(), p)); + return argument; } public J visitAnnotatedStatement(Cs.AnnotatedStatement annotatedStatement, P p) { @@ -311,6 +312,12 @@ public J visitPropertyDeclaration(Cs.PropertyDeclaration propertyDeclaration, P return propertyDeclaration; } + public J visitKeyword(Cs.Keyword keyword, P p) { + keyword = keyword.withPrefix(visitSpace(keyword.getPrefix(), CsSpace.Location.KEYWORD_PREFIX, p)); + keyword = keyword.withMarkers(visitMarkers(keyword.getMarkers(), p)); + return keyword; + } + public J visitLambda(Cs.Lambda lambda, P p) { lambda = lambda.withPrefix(visitSpace(lambda.getPrefix(), CsSpace.Location.LAMBDA_PREFIX, p)); Statement tempStatement = (Statement) visitStatement(lambda, p); @@ -368,7 +375,7 @@ public J visitUsingStatement(Cs.UsingStatement usingStatement, P p) { } usingStatement = (Cs.UsingStatement) tempStatement; usingStatement = usingStatement.withMarkers(visitMarkers(usingStatement.getMarkers(), p)); - usingStatement = usingStatement.withAwaitKeyword(visitSpace(usingStatement.getAwaitKeyword(), CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD, p)); + usingStatement = usingStatement.withAwaitKeyword(visitAndCast(usingStatement.getAwaitKeyword(), p)); usingStatement = usingStatement.getPadding().withExpression(visitContainer(usingStatement.getPadding().getExpression(), CsContainer.Location.USING_STATEMENT_EXPRESSION, p)); usingStatement = usingStatement.withStatement(visitAndCast(usingStatement.getStatement(), p)); return usingStatement; @@ -420,6 +427,114 @@ public J visitDefaultConstraint(Cs.DefaultConstraint defaultConstraint, P p) { return defaultConstraint; } + public J visitDeclarationExpression(Cs.DeclarationExpression declarationExpression, P p) { + declarationExpression = declarationExpression.withPrefix(visitSpace(declarationExpression.getPrefix(), CsSpace.Location.DECLARATION_EXPRESSION_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(declarationExpression, p); + if (!(tempExpression instanceof Cs.DeclarationExpression)) + { + return tempExpression; + } + declarationExpression = (Cs.DeclarationExpression) tempExpression; + declarationExpression = declarationExpression.withMarkers(visitMarkers(declarationExpression.getMarkers(), p)); + declarationExpression = declarationExpression.withTypeExpression(visitAndCast(declarationExpression.getTypeExpression(), p)); + declarationExpression = declarationExpression.withVariables(visitAndCast(declarationExpression.getVariables(), p)); + return declarationExpression; + } + + public J visitSingleVariableDesignation(Cs.SingleVariableDesignation singleVariableDesignation, P p) { + singleVariableDesignation = singleVariableDesignation.withPrefix(visitSpace(singleVariableDesignation.getPrefix(), CsSpace.Location.SINGLE_VARIABLE_DESIGNATION_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(singleVariableDesignation, p); + if (!(tempExpression instanceof Cs.SingleVariableDesignation)) + { + return tempExpression; + } + singleVariableDesignation = (Cs.SingleVariableDesignation) tempExpression; + singleVariableDesignation = singleVariableDesignation.withMarkers(visitMarkers(singleVariableDesignation.getMarkers(), p)); + singleVariableDesignation = singleVariableDesignation.withName(visitAndCast(singleVariableDesignation.getName(), p)); + return singleVariableDesignation; + } + + public J visitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation parenthesizedVariableDesignation, P p) { + parenthesizedVariableDesignation = parenthesizedVariableDesignation.withPrefix(visitSpace(parenthesizedVariableDesignation.getPrefix(), CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(parenthesizedVariableDesignation, p); + if (!(tempExpression instanceof Cs.ParenthesizedVariableDesignation)) + { + return tempExpression; + } + parenthesizedVariableDesignation = (Cs.ParenthesizedVariableDesignation) tempExpression; + parenthesizedVariableDesignation = parenthesizedVariableDesignation.withMarkers(visitMarkers(parenthesizedVariableDesignation.getMarkers(), p)); + parenthesizedVariableDesignation = parenthesizedVariableDesignation.getPadding().withVariables(visitContainer(parenthesizedVariableDesignation.getPadding().getVariables(), CsContainer.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, p)); + return parenthesizedVariableDesignation; + } + + public J visitDiscardVariableDesignation(Cs.DiscardVariableDesignation discardVariableDesignation, P p) { + discardVariableDesignation = discardVariableDesignation.withPrefix(visitSpace(discardVariableDesignation.getPrefix(), CsSpace.Location.DISCARD_VARIABLE_DESIGNATION_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(discardVariableDesignation, p); + if (!(tempExpression instanceof Cs.DiscardVariableDesignation)) + { + return tempExpression; + } + discardVariableDesignation = (Cs.DiscardVariableDesignation) tempExpression; + discardVariableDesignation = discardVariableDesignation.withMarkers(visitMarkers(discardVariableDesignation.getMarkers(), p)); + discardVariableDesignation = discardVariableDesignation.withDiscard(visitAndCast(discardVariableDesignation.getDiscard(), p)); + return discardVariableDesignation; + } + + public J visitTupleExpression(Cs.TupleExpression tupleExpression, P p) { + tupleExpression = tupleExpression.withPrefix(visitSpace(tupleExpression.getPrefix(), CsSpace.Location.TUPLE_EXPRESSION_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(tupleExpression, p); + if (!(tempExpression instanceof Cs.TupleExpression)) + { + return tempExpression; + } + tupleExpression = (Cs.TupleExpression) tempExpression; + tupleExpression = tupleExpression.withMarkers(visitMarkers(tupleExpression.getMarkers(), p)); + tupleExpression = tupleExpression.getPadding().withArguments(visitContainer(tupleExpression.getPadding().getArguments(), CsContainer.Location.TUPLE_EXPRESSION_ARGUMENTS, p)); + return tupleExpression; + } + + public J visitConstructor(Cs.Constructor constructor, P p) { + constructor = constructor.withPrefix(visitSpace(constructor.getPrefix(), CsSpace.Location.CONSTRUCTOR_PREFIX, p)); + Statement tempStatement = (Statement) visitStatement(constructor, p); + if (!(tempStatement instanceof Cs.Constructor)) + { + return tempStatement; + } + constructor = (Cs.Constructor) tempStatement; + constructor = constructor.withMarkers(visitMarkers(constructor.getMarkers(), p)); + constructor = constructor.withInitializer(visitAndCast(constructor.getInitializer(), p)); + constructor = constructor.withConstructorCore(visitAndCast(constructor.getConstructorCore(), p)); + return constructor; + } + + public J visitUnary(Cs.Unary unary, P p) { + unary = unary.withPrefix(visitSpace(unary.getPrefix(), CsSpace.Location.UNARY_PREFIX, p)); + Statement tempStatement = (Statement) visitStatement(unary, p); + if (!(tempStatement instanceof Cs.Unary)) + { + return tempStatement; + } + unary = (Cs.Unary) tempStatement; + Expression tempExpression = (Expression) visitExpression(unary, p); + if (!(tempExpression instanceof Cs.Unary)) + { + return tempExpression; + } + unary = (Cs.Unary) tempExpression; + unary = unary.withMarkers(visitMarkers(unary.getMarkers(), p)); + unary = unary.getPadding().withOperator(visitLeftPadded(unary.getPadding().getOperator(), CsLeftPadded.Location.UNARY_OPERATOR, p)); + unary = unary.withExpression(visitAndCast(unary.getExpression(), p)); + return unary; + } + + public J visitConstructorInitializer(Cs.ConstructorInitializer constructorInitializer, P p) { + constructorInitializer = constructorInitializer.withPrefix(visitSpace(constructorInitializer.getPrefix(), CsSpace.Location.CONSTRUCTOR_INITIALIZER_PREFIX, p)); + constructorInitializer = constructorInitializer.withMarkers(visitMarkers(constructorInitializer.getMarkers(), p)); + constructorInitializer = constructorInitializer.withKeyword(visitAndCast(constructorInitializer.getKeyword(), p)); + constructorInitializer = constructorInitializer.getPadding().withArguments(visitContainer(constructorInitializer.getPadding().getArguments(), CsContainer.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, p)); + return constructorInitializer; + } + public JContainer visitContainer(@Nullable JContainer container, CsContainer.Location loc, P p) { if (container == null) { diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java index 84928f5..07aea6e 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java @@ -24,6 +24,7 @@ import org.openrewrite.csharp.CSharpVisitor; import org.openrewrite.java.JavaPrinter; import org.openrewrite.java.JavaTypeVisitor; +import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.internal.TypesInUse; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -310,27 +311,35 @@ public Cs.CompilationUnit withUsings(List> usings) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) - final class NamedArgument implements Cs, Expression { + final class Argument implements Cs, Expression { @Nullable @NonFinal - transient WeakReference padding; + transient WeakReference padding; @With + @Getter @EqualsAndHashCode.Include UUID id; @With + @Getter Space prefix; @With + @Getter Markers markers; @Nullable JRightPadded nameColumn; + @Nullable + @Getter + @With + Keyword refKindKeyword; + public J.@Nullable Identifier getNameColumn() { return nameColumn == null ? null : nameColumn.getElement(); } - public NamedArgument withNameColumn(J.@Nullable Identifier nameColumn) { + public Argument withNameColumn(J.@Nullable Identifier nameColumn) { return getPadding().withNameColumn(JRightPadded.withElement(this.nameColumn, nameColumn)); } @@ -344,13 +353,13 @@ public NamedArgument withNameColumn(J.@Nullable Identifier nameColumn) { @SuppressWarnings("unchecked") @Override - public NamedArgument withType(@Nullable JavaType type) { - return expression.getType() == type ? this : new NamedArgument(id, prefix, markers, nameColumn, expression.withType(type)); + public Argument withType(@Nullable JavaType type) { + return expression.getType() == type ? this : new Argument(id, prefix, markers, nameColumn, refKindKeyword, expression.withType(type)); } @Override public

J acceptCSharp(CSharpVisitor

v, P p) { - return v.visitNamedArgument(this, p); + return v.visitArgument(this, p); } public Padding getPadding() { @@ -381,14 +390,14 @@ public String toString() { @RequiredArgsConstructor public static class Padding { - private final NamedArgument t; + private final Argument t; public @Nullable JRightPadded getNameColumn() { return t.nameColumn; } - public NamedArgument withNameColumn(@Nullable JRightPadded target) { - return t.nameColumn == target ? t : new NamedArgument(t.id, t.prefix, t.markers, target, t.expression); + public Argument withNameColumn(@Nullable JRightPadded target) { + return t.nameColumn == target ? t : new Argument(t.id, t.prefix, t.markers, target, t.refKindKeyword, t.expression); } } @@ -1881,7 +1890,38 @@ public PropertyDeclaration withInitializer(@Nullable JLeftPadded ini } } + enum KeywordKind + { + Ref, + Out, + Await, + Base, + This + } + + @Getter + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + final class Keyword implements Cs + { + @With + @Getter + @EqualsAndHashCode.Include + UUID id; + + @With + @Getter + Space prefix; + @With + @Getter + Markers markers; + + @With + @Getter + KeywordKind kind; + } @Getter @@ -2121,7 +2161,7 @@ final class UsingStatement implements Cs, Statement { @With @Nullable @Getter - Space awaitKeyword; + Keyword awaitKeyword; JContainer expression; @@ -2548,4 +2588,642 @@ public

J acceptCSharp(CSharpVisitor

v, P p) { return v.visitDefaultConstraint(this, p); } } + + /** + * A declaration expression node represents a local variable declaration in an expression context. + * This is used in two primary scenarios in C#: + *

    + *
  • Out variable declarations: {@code Method(out int x)}
  • + *
  • Deconstruction declarations: {@code (int x, string y) = GetPoint()}
  • + *
+ * Example 1: Out variable declaration: + *
+     * if(int.TryParse(s, out int result)) {
+     *     // use result
+     * }
+     * 
+ * + * Example 2: Deconstruction declaration: + *
+     * (int x, string y) = point;
+     * (int count, (string name, int age)) = GetPersonDetails();
+     * 
+ */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PUBLIC) + public final class DeclarationExpression implements Cs, Expression, TypedTree { + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + + @With + @Getter + @Nullable + TypeTree typeExpression; + + @With + @Getter + VariableDesignation variables; + + + @Override + public @Nullable JavaType getType() { + return variables.getType(); + } + + @Override + public DeclarationExpression withType(@Nullable JavaType type) { + return withType(type == null ? null : this.variables.withType(type)); + } + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitDeclarationExpression(this, p); + } + + @Override + @Transient + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + } + + /** + * Interface for variable designators in declaration expressions. + * This can be either a single variable name or a parenthesized list of designators for deconstruction. + * @see SingleVariableDesignation + * @see ParenthesizedVariableDesignation + */ + interface VariableDesignation extends Expression, Cs { + } + + /** + * Represents a single variable declaration within a declaration expression. + * Used both for simple out variable declarations and as elements within deconstruction declarations. + * + * Example in out variable: + *

+     * int.TryParse(s, out int x)  // 'int x' is the SingleVariable
+     * 
+ * + * Example in deconstruction: + *
+     * (int x, string y) = point;  // both 'int x' and 'string y' are SingleVariables
+     * 
+ */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PUBLIC) + public final class SingleVariableDesignation implements VariableDesignation, Cs { + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + J.Identifier name; + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitSingleVariableDesignation(this, p); + } + + @Override + public @Nullable JavaType getType() { + return name.getType(); + } + + @Override + public SingleVariableDesignation withType(@Nullable JavaType type) { + return this.getType() == type ? this : new Cs.SingleVariableDesignation( + id, + prefix, + markers, + name.withType(type)); + } + + @Override + @Transient + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + } + + /** + * Represents a parenthesized list of variable declarations used in deconstruction patterns. + * Can contain both single variables and nested deconstruction patterns. + * + * Example of simple deconstruction: + *

+     * (int x, string y) = point;
+     * 
+ * + * Example of nested deconstruction: + *
+     * (int count, (string name, int age)) = GetPersonDetails();
+     *                 // ^^^^^^^^^^^^^^^^^^ nested ParenthesizedVariable
+     * //    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ outer ParenthesizedVariable
+     * 
+ */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PRIVATE) + @RequiredArgsConstructor + public final class ParenthesizedVariableDesignation implements VariableDesignation, Cs { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JContainer variables; + + public List getVariables() { + return variables.getElements(); + } + + public ParenthesizedVariableDesignation withVariables(List variables) { + return getPadding().withVariables(JContainer.withElements(this.variables, variables)); + } + + @With + @Getter + @Nullable + JavaType type; + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitParenthesizedVariableDesignation(this, p); + } + + @Override + @Transient + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public class Padding { + private final ParenthesizedVariableDesignation t; + + public JContainer getVariables() { + return t.variables; + } + + public ParenthesizedVariableDesignation withVariables(JContainer variables) { + return t.variables == variables ? t : new ParenthesizedVariableDesignation(t.id, t.prefix, t.markers, variables, t.type); + } + } + } + + /** + * Represents a discard designation in pattern matching expressions, indicated by an underscore (_). + * For example in pattern matching: + * ```csharp + * if (obj is _) // discard pattern + * ``` + * Or in deconstruction: + * ```csharp + * var (x, _, z) = tuple; // discards second element + * ``` + */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PUBLIC) + final class DiscardVariableDesignation implements VariableDesignation, Cs { + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + J.Identifier discard; + + @Override + public @Nullable JavaType getType() { + return discard.getType(); + } + + @Override + public J2 withType(@Nullable JavaType type) { + return (J2) withDiscard(discard.withType(type)); + } + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitDiscardVariableDesignation(this, p); + } + + @Override + @Transient + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + } + + /** + * Represents a tuple expression in C#. + * Can be used in tuple construction, deconstruction and tuple literals. + * + * Examples: + * ```csharp + * // Tuple construction + * var point = (1, 2); + * + * // Named tuple elements + * var person = (name: "John", age: 25); + * + * // Nested tuples + * var nested = (1, (2, 3)); + * + * // Tuple type with multiple elements + * (string name, int age) person = ("John", 25); + * ``` + */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class TupleExpression implements Cs, Expression { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JContainer arguments; + + public List getArguments() { + return arguments.getElements(); + } + + public TupleExpression withArguments(List arguments) { + return getPadding().withArguments(JContainer.withElements(this.arguments, arguments)); + } + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitTupleExpression(this, p); + } + + @Override + public @Nullable JavaType getType() { + return null; + } + + @Override + public TupleExpression withType(@Nullable JavaType type) { + return this; + } + + @Override + @Transient + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final TupleExpression t; + + public JContainer getArguments() { + return t.arguments; + } + + public TupleExpression withArguments(JContainer arguments) { + return t.arguments == arguments ? t : new TupleExpression(t.id, t.prefix, t.markers, arguments); + } + } + } + + /** + * Represents a C# constructor declaration which may include an optional constructor initializer. + *

+ * For example: + *

+     *   // Constructor with no initializer
+     *   public MyClass() {
+     *   }
+     *
+     *   // Constructor with base class initializer
+     *   public MyClass(int x) : base(x) {
+     *   }
+     *
+     *   // Constructor with this initializer
+     *   public MyClass(string s) : this(int.Parse(s)) {
+     *   }
+     * 
+ */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PUBLIC) + public class Constructor implements Cs, Statement { + @Getter + @With + @EqualsAndHashCode.Include + UUID id; + + @Getter + @With + Space prefix; + + @Getter + @With + Markers markers; + + @Getter + @With + @Nullable + ConstructorInitializer initializer; + + @Getter + @With + J.MethodDeclaration constructorCore; + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitConstructor(this, p); + } + + @Override + @Transient + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); + } + } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class Unary implements Cs, Statement, Expression, TypedTree { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JLeftPadded operator; + + public Type getOperator() { + return operator.getElement(); + } + + public Cs.Unary withOperator(Type operator) { + return getPadding().withOperator(this.operator.withElement(operator)); + } + + @With + @Getter + Expression expression; + + @With + @Nullable + @Getter + JavaType type; + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitUnary(this, p); + } + + @Override + @Transient + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); + } + + @Override + @Transient + public List getSideEffects() { + return getOperator().isModifying() ? singletonList(this) : expression.getSideEffects(); + } + + public enum Type { + + /** + * Represent x! syntax + */ + SuppressNullableWarning; + + public boolean isModifying() { + switch (this) { + default: + return false; + } + } + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @Override + public String toString() { + return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>()); + } + + @RequiredArgsConstructor + public static class Padding { + private final Cs.Unary t; + + public JLeftPadded getOperator() { + return t.operator; + } + + public Cs.Unary withOperator(JLeftPadded operator) { + return t.operator == operator ? t : new Cs.Unary(t.id, t.prefix, t.markers, operator, t.expression, t.type); + } + } + } + + + /** + * Represents a constructor initializer which is a call to another constructor, either in the same class (this) + * or in the base class (base). + * + * Examples: + * ```csharp + * class Person { + * // Constructor with 'this' initializer + * public Person(string name) : this(name, 0) { } + * + * // Constructor with 'base' initializer + * public Person(string name, int age) : base(name) { } + * } + * ``` + */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ConstructorInitializer implements Cs { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + Cs.Keyword keyword; + + JContainer arguments; + + public List getArguments() { + return arguments.getElements(); + } + + public ConstructorInitializer withArguments(List arguments) { + return getPadding().withArguments(JContainer.withElements(this.arguments, arguments)); + } + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitConstructorInitializer(this, p); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ConstructorInitializer t; + + public JContainer getArguments() { + return t.arguments; + } + + public ConstructorInitializer withArguments(JContainer arguments) { + return t.arguments == arguments ? t : new ConstructorInitializer(t.id, t.prefix, t.markers, t.keyword, arguments); + } + } + } } diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java index 5f20b1d..4c30f8b 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java @@ -24,7 +24,13 @@ public enum Location { ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS(CsSpace.Location.ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS, CsRightPadded.Location.ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS), TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS(CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS, CsRightPadded.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS), METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES(CsSpace.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, CsRightPadded.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES), - USING_STATEMENT_EXPRESSION(CsSpace.Location.USING_STATEMENT_EXPRESSION, CsRightPadded.Location.USING_STATEMENT_EXPRESSION); + USING_STATEMENT_EXPRESSION(CsSpace.Location.USING_STATEMENT_EXPRESSION, CsRightPadded.Location.USING_STATEMENT_EXPRESSION), + DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_VARIABLES(CsSpace.Location.DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_VARIABLES, CsRightPadded.Location.DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_VARIABLES), + PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES(CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, CsRightPadded.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES), + DECLARATION_EXPRESSION_VARIABLES(CsSpace.Location.DECLARATION_EXPRESSION_VARIABLES, CsRightPadded.Location.DECLARATION_EXPRESSION_VARIABLES), + TUPLE_EXPRESSION_ARGUMENTS(CsSpace.Location.TUPLE_EXPRESSION_ARGUMENTS, CsRightPadded.Location.TUPLE_EXPRESSION_ARGUMENTS), + METHOD_DECLARATION_PARAMETERS(CsSpace.Location.METHOD_DECLARATION_PARAMETERS, CsRightPadded.Location.METHOD_DECLARATION_PARAMETERS), + CONSTRUCTOR_INITIALIZER_ARGUMENTS(CsSpace.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, CsRightPadded.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS); private final CsSpace.Location beforeLocation; private final CsRightPadded.Location elementLocation; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsLeftPadded.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsLeftPadded.java index c3e2e82..f7a6938 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsLeftPadded.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsLeftPadded.java @@ -24,7 +24,7 @@ public enum Location { PROPERTY_DECLARATION_INITIALIZER(CsSpace.Location.PROPERTY_DECLARATION_INITIALIZER), USING_DIRECTIVE_STATIC(CsSpace.Location.USING_DIRECTIVE_STATIC), USING_DIRECTIVE_UNSAFE(CsSpace.Location.USING_DIRECTIVE_UNSAFE), - ; + UNARY_OPERATOR(CsSpace.Location.UNARY_OPERATOR); private CsSpace.Location beforeLocation; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java index 4820942..acfa51c 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java @@ -49,7 +49,15 @@ enum Location { METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES(CsSpace.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES), USING_STATEMENT_RESOURCE(CsSpace.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES), USING_STATEMENT_EXPRESSION(CsSpace.Location.USING_STATEMENT_EXPRESSION), - USING_STATEMENT_AWAIT_KEYWORD(CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD); + USING_STATEMENT_AWAIT_KEYWORD(CsSpace.Location.USING_STATEMENT_AWAIT_KEYWORD), + ARGUMENT_NAME_COLUMN(CsSpace.Location.ARGUMENT_NAME_COLUMN), + DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_VARIABLES(CsSpace.Location.DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_VARIABLES), + PARENTHESIZED_VARIABLE_VARIABLES(CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES), + DECLARATION_EXPRESSION_VARIABLES(CsSpace.Location.DECLARATION_EXPRESSION_VARIABLES), + PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES(CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES), + TUPLE_EXPRESSION_ARGUMENTS(CsSpace.Location.TUPLE_EXPRESSION_ARGUMENTS), + METHOD_DECLARATION_PARAMETERS(CsSpace.Location.METHOD_DECLARATION_PARAMETERS), + CONSTRUCTOR_INITIALIZER_ARGUMENTS(CsSpace.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS); private final CsSpace.Location afterLocation; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java index 84564e5..dadbd86 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java @@ -73,6 +73,43 @@ public enum Location { CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS, TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER, - ALLOWS_CONSTRAINT_PREFIX, ALLOWS_CONSTRAINT_CLAUSE_PREFIX, REF_STRUCT_CONSTRAINT_PREFIX, CLASS_OR_STRUCT_CONSTRAINT_PREFIX, CONSTRUCTOR_CONSTRAINT_PREFIX, DEFAULT_CONSTRAINT_PREFIX, ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS, TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS, METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, METHOD_DECLARATION_PREFIX, TYPE_PARAMETERS_CONSTRAINT_CLAUSE_PREFIX, TYPE_PARAMETERS_CONSTRAINT_PREFIX, USING_STATEMENT_PREFIX, USING_STATEMENT_RESOURCES, USING_STATEMENT_EXPRESSION, USING_STATEMENT_AWAIT_KEYWORD, LAMBDA_PREFIX + ALLOWS_CONSTRAINT_PREFIX, + ALLOWS_CONSTRAINT_CLAUSE_PREFIX, + REF_STRUCT_CONSTRAINT_PREFIX, + CLASS_OR_STRUCT_CONSTRAINT_PREFIX, + CONSTRUCTOR_CONSTRAINT_PREFIX, + DEFAULT_CONSTRAINT_PREFIX, + ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS, + TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS, + METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, + METHOD_DECLARATION_PREFIX, + TYPE_PARAMETERS_CONSTRAINT_CLAUSE_PREFIX, + TYPE_PARAMETERS_CONSTRAINT_PREFIX, + USING_STATEMENT_PREFIX, + USING_STATEMENT_RESOURCES, + USING_STATEMENT_EXPRESSION, + USING_STATEMENT_AWAIT_KEYWORD, + ARGUMENT_NAME_COLUMN, + KEYWORD_PREFIX, + ARGUMENT_PREFIX, + DECLARATION_EXPRESSION_SINGLE_VARIABLE_PREFIX, + DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_PREFIX, + DECLARATION_EXPRESSION_PARENTHESIZED_VARIABLE_VARIABLES, + DECLARATION_EXPRESSION_PREFIX, + SINGLE_VARIABLE_PREFIX, + PARENTHESIZED_VARIABLE_PREFIX, + PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, + DECLARATION_EXPRESSION_VARIABLES, + LAMBDA_PREFIX, + DECLARATION_EXPRESSION_DESIGNATION_VARIABLES, + SINGLE_VARIABLE_DESIGNATION_PREFIX, + PARENTHESIZED_VARIABLE_DESIGNATION_PREFIX, + DISCARD_VARIABLE_DESIGNATION_PREFIX, + TUPLE_EXPRESSION_ARGUMENTS, + TUPLE_EXPRESSION_PREFIX, + METHOD_DECLARATION_PARAMETERS, + CONSTRUCTOR_INITIALIZER_ARGUMENTS, CONSTRUCTOR_PREFIX, UNARY_PREFIX, UNARY_OPERATOR, CONSTRUCTOR_INITIALIZER_PREFIX; + + } } From b4639e6fb59e841dcc5ee00a465a6a745e0a5bd4 Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Fri, 1 Nov 2024 00:37:25 -0400 Subject: [PATCH 4/9] Add enum support --- .../Parser/CSharpParserVisitor.cs | 97 ++++++++++++++++++- .../Tree/EnumDeclarationTests.cs | 42 ++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/Tree/EnumDeclarationTests.cs diff --git a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs index 9ff0f00..94ac68f 100644 --- a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs +++ b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs @@ -1,3 +1,4 @@ +using System.Collections; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; @@ -130,9 +131,103 @@ public override Cs VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDeclar public override J? VisitEnumDeclaration(EnumDeclarationSyntax node) { - return base.VisitEnumDeclaration(node); + var attributeLists = MapAttributes(node.AttributeLists); + var javaType = MapType(node); + var hasBaseClass = node.BaseList is { Types.Count: > 0 }; + + var isEmptyBody = node.OpenBraceToken.IsKind(SyntaxKind.None); + + var classDeclaration = new J.ClassDeclaration( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + [], + MapModifiers(node.Modifiers), + new J.ClassDeclaration.Kind( + Core.Tree.RandomId(), + Format(Leading(node.EnumKeyword)), + Markers.EMPTY, + [], + J.ClassDeclaration.Kind.Type.Enum + ), + new J.Identifier( + Core.Tree.RandomId(), + Format(Leading(node.Identifier)), + Markers.EMPTY, + [], + node.Identifier.Text, + javaType, + null + ), + null, + null, + hasBaseClass + ? new JLeftPadded(Format(Leading(node.BaseList!)), + (Visit(node.BaseList!.Types[0]) as TypeTree)!, Markers.EMPTY) + : null, + null, + null, + new J.Block( + Core.Tree.RandomId(), + Format(Leading(isEmptyBody ? node.SemicolonToken : node.OpenBraceToken)), + isEmptyBody ? Markers.Create(new OmitBraces()) : Markers.EMPTY, + JRightPadded.Create(false), + [JRightPadded.Create(MapEnumMembers(node.Members))], + Format(Leading(node.CloseBraceToken)) + ), + javaType as JavaType.FullyQualified + ); + + if(attributeLists != null) + { + return new Cs.AnnotatedStatement( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + attributeLists, + classDeclaration); + } + + + + return classDeclaration; + } + + J.EnumValueSet MapEnumMembers(SeparatedSyntaxList members) + { + return new J.EnumValueSet( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + members.Select(x => JRightPadded.Create(new J.EnumValue( + Core.Tree.RandomId(), + Space.EMPTY, + Markers.EMPTY, + [], // todo: figure out how to make c# attributes map here + ToJIdentifier(x.Identifier), + null + ), Format(Trailing(x)))).ToList(), + false + ); } + J.Identifier ToJIdentifier(SyntaxToken token, JavaType? type = null) + { + if (type == null && token.Parent != null) + { + type = MapType(token.Parent); + } + + return new J.Identifier( + Core.Tree.RandomId(), + Format(Leading(token)), + Markers.EMPTY, + [], + token.Text, + type, + null + ); + } public override J? VisitRecordDeclaration(RecordDeclarationSyntax node) { return VisitTypeDeclaration(node, J.ClassDeclaration.Kind.Type.Record); diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EnumDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EnumDeclarationTests.cs new file mode 100644 index 0000000..a96fd4b --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/EnumDeclarationTests.cs @@ -0,0 +1,42 @@ +using Rewrite.Test.CSharp; +using Rewrite.Test; + +namespace Rewrite.CSharp.Tests.Tree; + +using static Assertions; + +public class EnumDeclarationTests : RewriteTest +{ + [Fact] + public void BasicEnum() + { + RewriteRun( + CSharp( + """ + public enum A + { + a, + b + } + """ + ) + ); + } + + [Fact] + public void EnumWithBaseType() + { + RewriteRun( + CSharp( + """ + [Test] + public enum A : int + { + a, + b + } + """ + ) + ); + } +} From 4ff3ff184a81d1f4dcad3b35413a3c6bc39f4f74 Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Fri, 1 Nov 2024 00:52:02 -0400 Subject: [PATCH 5/9] Add base expression support --- .../Parser/CSharpParserVisitor.cs | 21 ++------------ .../Tree/BaseExpressionTests.cs | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/Tree/BaseExpressionTests.cs diff --git a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs index 94ac68f..2264f08 100644 --- a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs +++ b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs @@ -204,30 +204,13 @@ J.EnumValueSet MapEnumMembers(SeparatedSyntaxList m Space.EMPTY, Markers.EMPTY, [], // todo: figure out how to make c# attributes map here - ToJIdentifier(x.Identifier), + MapIdentifier(x.Identifier, MapType(x.Parent!)), null ), Format(Trailing(x)))).ToList(), false ); } - J.Identifier ToJIdentifier(SyntaxToken token, JavaType? type = null) - { - if (type == null && token.Parent != null) - { - type = MapType(token.Parent); - } - - return new J.Identifier( - Core.Tree.RandomId(), - Format(Leading(token)), - Markers.EMPTY, - [], - token.Text, - type, - null - ); - } public override J? VisitRecordDeclaration(RecordDeclarationSyntax node) { return VisitTypeDeclaration(node, J.ClassDeclaration.Kind.Type.Record); @@ -1042,7 +1025,7 @@ private J.AssignmentOperation.Type MapAssignmentOperator(SyntaxToken op) public override J? VisitBaseExpression(BaseExpressionSyntax node) { - return base.VisitBaseExpression(node); + return MapIdentifier(node.Token, null); } public override J? VisitBaseList(BaseListSyntax node) diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BaseExpressionTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BaseExpressionTests.cs new file mode 100644 index 0000000..5ad6323 --- /dev/null +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/BaseExpressionTests.cs @@ -0,0 +1,28 @@ +using Rewrite.Test.CSharp; +using Rewrite.Test; + +namespace Rewrite.CSharp.Tests.Tree; + +using static Assertions; + + +public class BaseExpressionTests : RewriteTest +{ + [Fact] + private void BaseExpresion() + { + RewriteRun( + CSharp( + """ + class T + { + void M() + { + base.ToString(); + } + } + """ + ) + ); + } +} From 11727eb534d3604b75ad8ad31cca2b2b2c436eec Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Mon, 4 Nov 2024 14:01:40 -0500 Subject: [PATCH 6/9] Add tuple support --- Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs | 21 +++ Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs | 23 +++ .../Parser/CSharpParserVisitor.cs | 37 +++-- .../src/Rewrite.CSharp/Tree/TupleElement.g.cs | 87 +++++++++++ .../src/Rewrite.CSharp/Tree/TupleType.g.cs | 124 ++++++++++++++++ .../CSharp/CSharpReceiver.g.cs | 42 ++++++ .../CSharp/CSharpSender.g.cs | 20 +++ .../RemotingFixture.cs | 14 +- .../tests/Rewrite.CSharp.Tests/TestSetup.cs | 54 +++---- .../Tree/MethodDeclarationTests.cs | 2 +- .../Rewrite.CSharp.Tests/Tree/TupleTests.cs | 39 ++++- .../csharp/remote/CSharpReceiver.java | 40 +++++ .../csharp/remote/CSharpSender.java | 20 +++ .../openrewrite/csharp/CSharpIsoVisitor.java | 10 ++ .../org/openrewrite/csharp/CSharpPrinter.java | 40 +++++ .../org/openrewrite/csharp/CSharpVisitor.java | 21 +++ .../java/org/openrewrite/csharp/tree/Cs.java | 137 ++++++++++++++++++ .../openrewrite/csharp/tree/CsContainer.java | 3 +- .../csharp/tree/CsRightPadded.java | 3 +- .../org/openrewrite/csharp/tree/CsSpace.java | 2 +- 20 files changed, 685 insertions(+), 54 deletions(-) create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/TupleElement.g.cs create mode 100644 Rewrite/src/Rewrite.CSharp/Tree/TupleType.g.cs diff --git a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs index 3b48692..bfa9177 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs @@ -63,6 +63,26 @@ public CSharpPrinter() return tupleExpression; } + public override J? VisitTupleType(Cs.TupleType node, PrintOutputCapture p) + { + BeforeSyntax(node, CsSpace.Location.TUPLE_TYPE_PREFIX, p); + VisitContainer("(", node.Padding.Elements, CsContainer.Location.TUPLE_TYPE_ELEMENTS, ",", ")", p); + AfterSyntax(node, p); + return node; + } + + public override J? VisitTupleElement(Cs.TupleElement node, PrintOutputCapture p) + { + BeforeSyntax(node, CsSpace.Location.TUPLE_ELEMENT_PREFIX, p); + Visit(node.Type, p); + if (node.Name != null) + { + Visit(node.Name, p); + } + AfterSyntax(node, p); + return node; + } + public override J? VisitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation node, PrintOutputCapture p) { BeforeSyntax(node, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); @@ -677,6 +697,7 @@ private class CSharpJavaPrinter(CSharpPrinter _parent) : JavaPrinter p) { BeforeSyntax(newArray, Space.Location.NEW_ARRAY_PREFIX, p); diff --git a/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs b/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs index 94cb5e2..385324c 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs @@ -558,6 +558,29 @@ public override bool IsAcceptable(SourceFile sourceFile, P p) return constructorInitializer; } + public virtual J? VisitTupleType(Cs.TupleType tupleType, P p) + { + tupleType = tupleType.WithPrefix(VisitSpace(tupleType.Prefix, CsSpace.Location.TUPLE_TYPE_PREFIX, p)!); + var tempExpression = (Expression) VisitExpression(tupleType, p); + if (tempExpression is not Cs.TupleType) + { + return tempExpression; + } + tupleType = (Cs.TupleType) tempExpression; + tupleType = tupleType.WithMarkers(VisitMarkers(tupleType.Markers, p)); + tupleType = tupleType.Padding.WithElements(VisitContainer(tupleType.Padding.Elements, CsContainer.Location.TUPLE_TYPE_ELEMENTS, p)!); + return tupleType; + } + + public virtual J? VisitTupleElement(Cs.TupleElement tupleElement, P p) + { + tupleElement = tupleElement.WithPrefix(VisitSpace(tupleElement.Prefix, CsSpace.Location.TUPLE_ELEMENT_PREFIX, p)!); + tupleElement = tupleElement.WithMarkers(VisitMarkers(tupleElement.Markers, p)); + tupleElement = tupleElement.WithType(VisitAndCast(tupleElement.Type, p)!); + tupleElement = tupleElement.WithName(VisitAndCast(tupleElement.Name, p)); + return tupleElement; + } + protected virtual JContainer? VisitContainer(JContainer? container, CsContainer.Location loc, P p) where J2 : J { if (container == null) { diff --git a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs index 2264f08..dd7c494 100644 --- a/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs +++ b/Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs @@ -1324,6 +1324,11 @@ private J.Identifier MapIdentifier(SyntaxToken identifier, JavaType? type) } public override J? VisitPredefinedType(PredefinedTypeSyntax node) + { + return MapTypeTree(node); + } + + public TypeTree MapTypeTree(SyntaxNode node) { var type = MapType(node); if (type is JavaType.Primitive) @@ -1410,14 +1415,25 @@ private J.Identifier MapIdentifier(SyntaxToken identifier, JavaType? type) public override J? VisitTupleType(TupleTypeSyntax node) { - // This was added in C# 7.0 - return base.VisitTupleType(node); + return new Cs.TupleType( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + JContainer.Create(node.Elements.Select(x => JRightPadded.Create(Convert(x)!, Format(Trailing(x)))).ToList()), + MapType(node) + ); } - public override J? VisitTupleElement(TupleElementSyntax node) + public override Cs VisitTupleElement(TupleElementSyntax node) { - // This was added in C# 7.0 - return base.VisitTupleElement(node); + var type = MapType(node.Type); + return new Cs.TupleElement( + Core.Tree.RandomId(), + Format(Leading(node)), + Markers.EMPTY, + MapTypeTree(node.Type), + MapIdentifier(node.Identifier, type)); + } public override J? VisitOmittedTypeArgument(OmittedTypeArgumentSyntax node) @@ -1563,16 +1579,6 @@ private Enum MapPostfixUnaryOperator(SyntaxToken operatorToken) return type; } - // private Cs.Unary.Type MapPostfixUnaryOperatorToJ(SyntaxToken operatorToken) - // { - // J.Unary.Type? type = operatorToken.Kind() switch - // { - // SyntaxKind.PlusPlusToken => J.Unary.Type.PostIncrement, - // SyntaxKind.MinusMinusToken => J.Unary.Type.PostDecrement, - // _ => null - // }; - // return type; - // } public override J? VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { @@ -2523,6 +2529,7 @@ private JRightPadded MapAnonymousObjectMember(AnonymousObjectMemberDe public override J? VisitIsPatternExpression(IsPatternExpressionSyntax node) { + // todo: handle ConstantPatternSyntax (such as "something is true") if (node.Pattern is DeclarationPatternSyntax dp) { return new J.InstanceOf( diff --git a/Rewrite/src/Rewrite.CSharp/Tree/TupleElement.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/TupleElement.g.cs new file mode 100644 index 0000000..ec54493 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/TupleElement.g.cs @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class TupleElement( + Guid id, + Space prefix, + Markers markers, + TypeTree type, + J.Identifier? name + ) : Cs, MutableTree + { + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitTupleElement(this, p); + } + + public Guid Id => id; + + public TupleElement WithId(Guid newId) + { + return newId == id ? this : new TupleElement(newId, prefix, markers, type, name); + } + public Space Prefix => prefix; + + public TupleElement WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new TupleElement(id, newPrefix, markers, type, name); + } + public Markers Markers => markers; + + public TupleElement WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new TupleElement(id, prefix, newMarkers, type, name); + } + public TypeTree Type => type; + + public TupleElement WithType(TypeTree newType) + { + return ReferenceEquals(newType, type) ? this : new TupleElement(id, prefix, markers, newType, name); + } + public J.Identifier? Name => name; + + public TupleElement WithName(J.Identifier? newName) + { + return ReferenceEquals(newName, name) ? this : new TupleElement(id, prefix, markers, type, newName); + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is TupleElement && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.CSharp/Tree/TupleType.g.cs b/Rewrite/src/Rewrite.CSharp/Tree/TupleType.g.cs new file mode 100644 index 0000000..b6d1832 --- /dev/null +++ b/Rewrite/src/Rewrite.CSharp/Tree/TupleType.g.cs @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +#nullable enable +#pragma warning disable CS0108 +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using Rewrite.Core; +using Rewrite.Core.Marker; +using FileAttributes = Rewrite.Core.FileAttributes; +using Rewrite.RewriteJava.Tree; + +namespace Rewrite.RewriteCSharp.Tree; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")] +[SuppressMessage("ReSharper", "InvertIf")] +[SuppressMessage("ReSharper", "RedundantExtendsListEntry")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial interface Cs : J +{ + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public partial class TupleType( + Guid id, + Space prefix, + Markers markers, + JContainer elements, + JavaType? type + ) : Cs, TypeTree, Expression, MutableTree + { + [NonSerialized] private WeakReference? _padding; + + public PaddingHelper Padding + { + get + { + PaddingHelper? p; + if (_padding == null) + { + p = new PaddingHelper(this); + _padding = new WeakReference(p); + } + else + { + _padding.TryGetTarget(out p); + if (p == null || p.T != this) + { + p = new PaddingHelper(this); + _padding.SetTarget(p); + } + } + return p; + } + } + + public J? AcceptCSharp

(CSharpVisitor

v, P p) + { + return v.VisitTupleType(this, p); + } + + public Guid Id => id; + + public TupleType WithId(Guid newId) + { + return newId == id ? this : new TupleType(newId, prefix, markers, _elements, type); + } + public Space Prefix => prefix; + + public TupleType WithPrefix(Space newPrefix) + { + return newPrefix == prefix ? this : new TupleType(id, newPrefix, markers, _elements, type); + } + public Markers Markers => markers; + + public TupleType WithMarkers(Markers newMarkers) + { + return ReferenceEquals(newMarkers, markers) ? this : new TupleType(id, prefix, newMarkers, _elements, type); + } + private readonly JContainer _elements = elements; + public IList Elements => _elements.GetElements(); + + public TupleType WithElements(IList newElements) + { + return Padding.WithElements(JContainer.WithElements(_elements, newElements)); + } + public JavaType? Type => type; + + public TupleType WithType(JavaType? newType) + { + return newType == type ? this : new TupleType(id, prefix, markers, _elements, newType); + } + public sealed record PaddingHelper(Cs.TupleType T) + { + public JContainer Elements => T._elements; + + public Cs.TupleType WithElements(JContainer newElements) + { + return T._elements == newElements ? T : new Cs.TupleType(T.Id, T.Prefix, T.Markers, newElements, T.Type); + } + + } + + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public bool Equals(Rewrite.Core.Tree? other) + { + return other is TupleType && other.Id == Id; + } + #if DEBUG_VISITOR + [DebuggerStepThrough] + #endif + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs index aef2e1c..e1db1d5 100644 --- a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs +++ b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpReceiver.g.cs @@ -448,6 +448,26 @@ public override J VisitConstructorInitializer(Cs.ConstructorInitializer construc return constructorInitializer; } + public override J VisitTupleType(Cs.TupleType tupleType, ReceiverContext ctx) + { + tupleType = tupleType.WithId(ctx.ReceiveValue(tupleType.Id)!); + tupleType = tupleType.WithPrefix(ctx.ReceiveNode(tupleType.Prefix, ReceiveSpace)!); + tupleType = tupleType.WithMarkers(ctx.ReceiveNode(tupleType.Markers, ctx.ReceiveMarkers)!); + tupleType = tupleType.Padding.WithElements(ctx.ReceiveNode(tupleType.Padding.Elements, ReceiveContainer)!); + tupleType = tupleType.WithType(ctx.ReceiveValue(tupleType.Type)); + return tupleType; + } + + public override J VisitTupleElement(Cs.TupleElement tupleElement, ReceiverContext ctx) + { + tupleElement = tupleElement.WithId(ctx.ReceiveValue(tupleElement.Id)!); + tupleElement = tupleElement.WithPrefix(ctx.ReceiveNode(tupleElement.Prefix, ReceiveSpace)!); + tupleElement = tupleElement.WithMarkers(ctx.ReceiveNode(tupleElement.Markers, ctx.ReceiveMarkers)!); + tupleElement = tupleElement.WithType(ctx.ReceiveNode(tupleElement.Type, ctx.ReceiveTree)!); + tupleElement = tupleElement.WithName(ctx.ReceiveNode(tupleElement.Name, ctx.ReceiveTree)); + return tupleElement; + } + public override J VisitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.WithId(ctx.ReceiveValue(annotatedType.Id)!); @@ -1620,6 +1640,28 @@ public Rewrite.Core.Tree Create(string type, ReceiverContext ctx) where T : R ); } + if (type is "Rewrite.RewriteCSharp.Tree.Cs.TupleType" or "org.openrewrite.csharp.tree.Cs$TupleType") + { + return new Cs.TupleType( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(JContainer), ReceiveContainer)!, + ctx.ReceiveValue(default(JavaType)) + ); + } + + if (type is "Rewrite.RewriteCSharp.Tree.Cs.TupleElement" or "org.openrewrite.csharp.tree.Cs$TupleElement") + { + return new Cs.TupleElement( + ctx.ReceiveValue(default(Guid))!, + ctx.ReceiveNode(default(Space), ReceiveSpace)!, + ctx.ReceiveNode(default(Markers), ctx.ReceiveMarkers)!, + ctx.ReceiveNode(default(TypeTree), ctx.ReceiveTree)!, + ctx.ReceiveNode(default(J.Identifier), ctx.ReceiveTree) + ); + } + if (type is "Rewrite.RewriteCSharp.Tree.J.AnnotatedType" or "org.openrewrite.java.tree.J$AnnotatedType") { return new J.AnnotatedType( diff --git a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs index c3d7018..ab90295 100644 --- a/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs +++ b/Rewrite/src/Rewrite.Remote.Codec/CSharp/CSharpSender.g.cs @@ -439,6 +439,26 @@ public override J VisitConstructorInitializer(Cs.ConstructorInitializer construc return constructorInitializer; } + public override J VisitTupleType(Cs.TupleType tupleType, SenderContext ctx) + { + ctx.SendValue(tupleType, v => v.Id); + ctx.SendNode(tupleType, v => v.Prefix, SendSpace); + ctx.SendNode(tupleType, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(tupleType, v => v.Padding.Elements, SendContainer); + ctx.SendTypedValue(tupleType, v => v.Type); + return tupleType; + } + + public override J VisitTupleElement(Cs.TupleElement tupleElement, SenderContext ctx) + { + ctx.SendValue(tupleElement, v => v.Id); + ctx.SendNode(tupleElement, v => v.Prefix, SendSpace); + ctx.SendNode(tupleElement, v => v.Markers, ctx.SendMarkers); + ctx.SendNode(tupleElement, v => v.Type, ctx.SendTree); + ctx.SendNode(tupleElement, v => v.Name, ctx.SendTree); + return tupleElement; + } + public override J VisitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.SendValue(annotatedType, v => v.Id); diff --git a/Rewrite/src/Rewrite.Test.Engine.Remote/RemotingFixture.cs b/Rewrite/src/Rewrite.Test.Engine.Remote/RemotingFixture.cs index 48104ba..5a62da3 100644 --- a/Rewrite/src/Rewrite.Test.Engine.Remote/RemotingFixture.cs +++ b/Rewrite/src/Rewrite.Test.Engine.Remote/RemotingFixture.cs @@ -11,7 +11,7 @@ namespace Rewrite.Test.Engine.Remote; -public class RemotingFixture : IAsyncLifetime +public class RemotingFixture : IDisposable { private string? _rewriteRemoteDir; private Socket _socket = null!; @@ -84,7 +84,7 @@ public void ExtractEmbeddedResources(string outputDirectory) - public async Task InitializeAsync() + public RemotingFixture() { // register recipes (try to do this dynamically) IRemotingContext.RegisterRecipeFactory(Recipe.Noop().GetType().FullName!, _ => Recipe.Noop()); @@ -94,7 +94,7 @@ public async Task InitializeAsync() try { - await _socket.ConnectAsync(IPAddress.Loopback, 65432); + _socket.Connect(IPAddress.Loopback, 65432); remotingContext.Connect(_socket); remotingContext.SetCurrent(); ITestExecutionContext.SetCurrent(new RemotingTestExecutionContext(remotingContext)); @@ -108,14 +108,14 @@ public async Task InitializeAsync() { try { - await _socket.ConnectAsync(IPAddress.Loopback, 65432); + _socket.Connect(IPAddress.Loopback, 65432); remotingContext.Connect(_socket); remotingContext.SetCurrent(); break; } catch (SocketException) { - await Task.Delay(1000); + AsyncHelper.RunSync(() => Task.Delay(1000)); } } } @@ -124,7 +124,7 @@ public async Task InitializeAsync() IPrinterFactory.Set(new RemotePrinterFactory(remotingContext.Client!)); } - public async Task DisposeAsync() + public void Dispose() { _socket.Close(); @@ -149,7 +149,7 @@ public async Task DisposeAsync() } catch (Exception) { - await Task.Delay(1000); + AsyncHelper.RunSync(() => Task.Delay(1000)); } } } diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs b/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs index 062a62a..b3c4b66 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/TestSetup.cs @@ -1,27 +1,27 @@ -// -// -// using Rewrite.Test.Engine.Remote; -// using Xunit.Abstractions; -// using Xunit.Sdk; -// -// [assembly: Xunit.TestFramework("Rewrite.CSharp.Tests.TestSetup", "Rewrite.CSharp.Tests")] -// -// namespace Rewrite.CSharp.Tests; -// -// public sealed class TestSetup : XunitTestFramework, IDisposable -// { -// #if REMOTE_PRINTER -// private readonly IDisposable _fixture = new RemotingFixture(); -// #else -// private readonly IDisposable _fixture = new LocalPrinterFixture(); -// #endif -// public TestSetup(IMessageSink messageSink) -// :base(messageSink) -// { -// } -// -// public new void Dispose() -// { -// _fixture.Dispose(); -// } -// } + + +using Rewrite.Test.Engine.Remote; +using Xunit.Abstractions; +using Xunit.Sdk; + +[assembly: Xunit.TestFramework("Rewrite.CSharp.Tests.TestSetup", "Rewrite.CSharp.Tests")] + +namespace Rewrite.CSharp.Tests; + +public sealed class TestSetup : XunitTestFramework, IDisposable +{ +#if REMOTE_PRINTER + private readonly IDisposable _fixture = new RemotingFixture(); +#else + private readonly IDisposable _fixture = new LocalPrinterFixture(); +#endif + public TestSetup(IMessageSink messageSink) + :base(messageSink) + { + } + + public new void Dispose() + { + _fixture.Dispose(); + } +} diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs index c4df359..3baa2dc 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs @@ -37,7 +37,7 @@ public class Foo public void ConstructorDelegation() { RewriteRun( - spec => spec.TypeValidation = new TypeValidation(Unknowns: false), + // spec => spec.TypeValidation = new TypeValidation(Unknowns: false), CSharp( """ public class Foo diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs index 9c1deff..7269a90 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/TupleTests.cs @@ -43,7 +43,44 @@ void M() ); var lst = src.Parse(); - var statement = lst.Descendents().OfType().First().Body!.Statements.First(); + lst.ToString().ShouldBeSameAs(src.Before); + } + + [Fact] + public void NamedTupleType() + { + var src = CSharp( + """ + public class T + { + void M() + { + (int x, int y) myTuple; + } + } + """ + ); + + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); + } + + [Fact] + public void BasicTupleType() + { + var src = CSharp( + """ + public class T + { + void M() + { + (int, int) myTuple; + } + } + """ + ); + + var lst = src.Parse(); lst.ToString().ShouldBeSameAs(src.Before); } } diff --git a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java index b253c00..266a70d 100644 --- a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java +++ b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java @@ -475,6 +475,26 @@ public Cs.ConstructorInitializer visitConstructorInitializer(Cs.ConstructorIniti return constructorInitializer; } + @Override + public Cs.TupleType visitTupleType(Cs.TupleType tupleType, ReceiverContext ctx) { + tupleType = tupleType.withId(ctx.receiveNonNullValue(tupleType.getId(), UUID.class)); + tupleType = tupleType.withPrefix(ctx.receiveNonNullNode(tupleType.getPrefix(), CSharpReceiver::receiveSpace)); + tupleType = tupleType.withMarkers(ctx.receiveNonNullNode(tupleType.getMarkers(), ctx::receiveMarkers)); + tupleType = tupleType.getPadding().withElements(ctx.receiveNonNullNode(tupleType.getPadding().getElements(), CSharpReceiver::receiveContainer)); + tupleType = tupleType.withType(ctx.receiveValue(tupleType.getType(), JavaType.class)); + return tupleType; + } + + @Override + public Cs.TupleElement visitTupleElement(Cs.TupleElement tupleElement, ReceiverContext ctx) { + tupleElement = tupleElement.withId(ctx.receiveNonNullValue(tupleElement.getId(), UUID.class)); + tupleElement = tupleElement.withPrefix(ctx.receiveNonNullNode(tupleElement.getPrefix(), CSharpReceiver::receiveSpace)); + tupleElement = tupleElement.withMarkers(ctx.receiveNonNullNode(tupleElement.getMarkers(), ctx::receiveMarkers)); + tupleElement = tupleElement.withType(ctx.receiveNonNullNode(tupleElement.getType(), ctx::receiveTree)); + tupleElement = tupleElement.withName(ctx.receiveNode(tupleElement.getName(), ctx::receiveTree)); + return tupleElement; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class)); @@ -1577,6 +1597,26 @@ public T create(Class type, ReceiverContext ctx) { ); } + if (type == Cs.TupleType.class) { + return (T) new Cs.TupleType( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveContainer), + ctx.receiveValue(null, JavaType.class) + ); + } + + if (type == Cs.TupleElement.class) { + return (T) new Cs.TupleElement( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, CSharpReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNode(null, ctx::receiveTree) + ); + } + if (type == J.AnnotatedType.class) { return (T) new J.AnnotatedType( ctx.receiveNonNullValue(null, UUID.class), diff --git a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java index 3f6cf81..9ce7c0e 100644 --- a/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java +++ b/rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java @@ -458,6 +458,26 @@ public Cs.ConstructorInitializer visitConstructorInitializer(Cs.ConstructorIniti return constructorInitializer; } + @Override + public Cs.TupleType visitTupleType(Cs.TupleType tupleType, SenderContext ctx) { + ctx.sendValue(tupleType, Cs.TupleType::getId); + ctx.sendNode(tupleType, Cs.TupleType::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(tupleType, Cs.TupleType::getMarkers, ctx::sendMarkers); + ctx.sendNode(tupleType, e -> e.getPadding().getElements(), CSharpSender::sendContainer); + ctx.sendTypedValue(tupleType, Cs.TupleType::getType); + return tupleType; + } + + @Override + public Cs.TupleElement visitTupleElement(Cs.TupleElement tupleElement, SenderContext ctx) { + ctx.sendValue(tupleElement, Cs.TupleElement::getId); + ctx.sendNode(tupleElement, Cs.TupleElement::getPrefix, CSharpSender::sendSpace); + ctx.sendNode(tupleElement, Cs.TupleElement::getMarkers, ctx::sendMarkers); + ctx.sendNode(tupleElement, Cs.TupleElement::getType, ctx::sendTree); + ctx.sendNode(tupleElement, Cs.TupleElement::getName, ctx::sendTree); + return tupleElement; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.sendValue(annotatedType, J.AnnotatedType::getId); diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java index 0553d8d..9bccceb 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java @@ -230,6 +230,16 @@ public Cs.ConstructorInitializer visitConstructorInitializer(Cs.ConstructorIniti return (Cs.ConstructorInitializer) super.visitConstructorInitializer(constructorInitializer, p); } + @Override + public Cs.TupleType visitTupleType(Cs.TupleType tupleType, P p) { + return (Cs.TupleType) super.visitTupleType(tupleType, p); + } + + @Override + public Cs.TupleElement visitTupleElement(Cs.TupleElement tupleElement, P p) { + return (Cs.TupleElement) super.visitTupleElement(tupleElement, p); + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, P p) { return (J.AnnotatedType) super.visitAnnotatedType(annotatedType, p); diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java index b6ea6a7..cfd4a59 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java @@ -46,6 +46,46 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { } } + @Override + public Cs visitTupleExpression(Cs.TupleExpression tupleExpression, PrintOutputCapture

p) + { + beforeSyntax(tupleExpression, CsSpace.Location.TUPLE_EXPRESSION_PREFIX, p); + visitContainer("(", tupleExpression.getPadding().getArguments(), CsContainer.Location.TUPLE_EXPRESSION_ARGUMENTS, ",", ")", p); + afterSyntax(tupleExpression, p); + return tupleExpression; + } + + @Override + public Cs visitTupleType(Cs.TupleType node, PrintOutputCapture

p) + { + beforeSyntax(node, CsSpace.Location.TUPLE_TYPE_PREFIX, p); + visitContainer("(", node.getPadding().getElements(), CsContainer.Location.TUPLE_TYPE_ELEMENTS, ",", ")", p); + afterSyntax(node, p); + return node; + } + + @Override + public Cs visitTupleElement(Cs.TupleElement node, PrintOutputCapture

p) + { + beforeSyntax(node, CsSpace.Location.TUPLE_ELEMENT_PREFIX, p); + visit(node.getType(), p); + if (node.getName() != null) + { + visit(node.getName(), p); + } + afterSyntax(node, p); + return node; + } + + @Override + public Cs visitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation node, PrintOutputCapture

p) + { + beforeSyntax(node, CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, p); + visitContainer("(", node.getPadding().getVariables(), CsContainer.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES, ",", ")", p); + afterSyntax(node, p); + return node; + } + @Override public J visitArgument(Cs.Argument argument, PrintOutputCapture

p) { beforeSyntax(argument, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p); diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java index 623e439..03a011e 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java @@ -535,6 +535,27 @@ public J visitConstructorInitializer(Cs.ConstructorInitializer constructorInitia return constructorInitializer; } + public J visitTupleType(Cs.TupleType tupleType, P p) { + tupleType = tupleType.withPrefix(visitSpace(tupleType.getPrefix(), CsSpace.Location.TUPLE_TYPE_PREFIX, p)); + Expression tempExpression = (Expression) visitExpression(tupleType, p); + if (!(tempExpression instanceof Cs.TupleType)) + { + return tempExpression; + } + tupleType = (Cs.TupleType) tempExpression; + tupleType = tupleType.withMarkers(visitMarkers(tupleType.getMarkers(), p)); + tupleType = tupleType.getPadding().withElements(visitContainer(tupleType.getPadding().getElements(), CsContainer.Location.TUPLE_TYPE_ELEMENTS, p)); + return tupleType; + } + + public J visitTupleElement(Cs.TupleElement tupleElement, P p) { + tupleElement = tupleElement.withPrefix(visitSpace(tupleElement.getPrefix(), CsSpace.Location.TUPLE_ELEMENT_PREFIX, p)); + tupleElement = tupleElement.withMarkers(visitMarkers(tupleElement.getMarkers(), p)); + tupleElement = tupleElement.withType(visitAndCast(tupleElement.getType(), p)); + tupleElement = tupleElement.withName(visitAndCast(tupleElement.getName(), p)); + return tupleElement; + } + public JContainer visitContainer(@Nullable JContainer container, CsContainer.Location loc, P p) { if (container == null) { diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java index 07aea6e..f287aef 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java @@ -3226,4 +3226,141 @@ public ConstructorInitializer withArguments(JContainer arguments) { } } } + + /** + * Represents a C# tuple type specification, which allows grouping multiple types into a single type. + * Can be used in method returns, variable declarations, etc. + *

+ * For example: + *

+     *   // Simple tuple type
+     *   (int, string) coordinates;
+     *
+     *   // Tuple type with named elements
+     *   (int x, string label) namedTuple;
+     *
+     *   // Nested tuple types
+     *   (int, (string, bool)) complexTuple;
+     *
+     *   // As method return type
+     *   public (string name, int age) GetPersonDetails() { }
+     *
+     *   // As parameter type
+     *   public void ProcessData((int id, string value) data) { }
+     * 
+ */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + public class TupleType implements Cs, TypeTree, Expression { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @Getter + @EqualsAndHashCode.Include + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JContainer elements; + + @With + @Nullable + @Getter + JavaType type; + + public List getElements() { + return elements.getElements(); + } + + public TupleType withElements(List elements) { + return getPadding().withElements(JContainer.withElements(this.elements, elements)); + } + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitTupleType(this, p); + } + + @Override + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final TupleType t; + + public JContainer getElements() { + return t.elements; + } + + public TupleType withElements(JContainer elements) { + return t.elements == elements ? t : new TupleType(t.id, t.prefix, t.markers, elements, t.type); + } + } + } + + /** + * Represents a single element within a tuple type, which may include an optional + * identifier for named tuple elements. + */ + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PUBLIC) + public class TupleElement implements Cs { + + @With + @Getter + @EqualsAndHashCode.Include + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + TypeTree type; + + @With + @Getter + J.@Nullable Identifier name; + + + @Override + public

J acceptCSharp(CSharpVisitor

v, P p) { + return v.visitTupleElement(this, p); + } + + } + } diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java index 4c30f8b..ceeaed9 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsContainer.java @@ -30,7 +30,8 @@ public enum Location { DECLARATION_EXPRESSION_VARIABLES(CsSpace.Location.DECLARATION_EXPRESSION_VARIABLES, CsRightPadded.Location.DECLARATION_EXPRESSION_VARIABLES), TUPLE_EXPRESSION_ARGUMENTS(CsSpace.Location.TUPLE_EXPRESSION_ARGUMENTS, CsRightPadded.Location.TUPLE_EXPRESSION_ARGUMENTS), METHOD_DECLARATION_PARAMETERS(CsSpace.Location.METHOD_DECLARATION_PARAMETERS, CsRightPadded.Location.METHOD_DECLARATION_PARAMETERS), - CONSTRUCTOR_INITIALIZER_ARGUMENTS(CsSpace.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, CsRightPadded.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS); + CONSTRUCTOR_INITIALIZER_ARGUMENTS(CsSpace.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS, CsRightPadded.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS), + TUPLE_TYPE_ELEMENTS(CsSpace.Location.TUPLE_TYPE_ELEMENTS, CsRightPadded.Location.TUPLE_TYPE_ELEMENTS); private final CsSpace.Location beforeLocation; private final CsRightPadded.Location elementLocation; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java index acfa51c..36938bc 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsRightPadded.java @@ -57,7 +57,8 @@ enum Location { PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES(CsSpace.Location.PARENTHESIZED_VARIABLE_DESIGNATION_VARIABLES), TUPLE_EXPRESSION_ARGUMENTS(CsSpace.Location.TUPLE_EXPRESSION_ARGUMENTS), METHOD_DECLARATION_PARAMETERS(CsSpace.Location.METHOD_DECLARATION_PARAMETERS), - CONSTRUCTOR_INITIALIZER_ARGUMENTS(CsSpace.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS); + CONSTRUCTOR_INITIALIZER_ARGUMENTS(CsSpace.Location.CONSTRUCTOR_INITIALIZER_ARGUMENTS), + TUPLE_TYPE_ELEMENTS(CsSpace.Location.TUPLE_TYPE_ELEMENTS); private final CsSpace.Location afterLocation; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java index dadbd86..4ea458a 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/CsSpace.java @@ -108,7 +108,7 @@ public enum Location { TUPLE_EXPRESSION_ARGUMENTS, TUPLE_EXPRESSION_PREFIX, METHOD_DECLARATION_PARAMETERS, - CONSTRUCTOR_INITIALIZER_ARGUMENTS, CONSTRUCTOR_PREFIX, UNARY_PREFIX, UNARY_OPERATOR, CONSTRUCTOR_INITIALIZER_PREFIX; + CONSTRUCTOR_INITIALIZER_ARGUMENTS, CONSTRUCTOR_PREFIX, UNARY_PREFIX, UNARY_OPERATOR, CONSTRUCTOR_INITIALIZER_PREFIX, TUPLE_ELEMENT_PREFIX, TUPLE_TYPE_PREFIX, TUPLE_TYPE_ELEMENTS; } From 11a1bf890029da0788b6e4dd082de3e721da5f0b Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Mon, 4 Nov 2024 15:15:46 -0500 Subject: [PATCH 7/9] Fixup transitive dependencies --- .../Rewrite.Test.Engine.Remote.csproj | 5 +++-- .../tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.csproj | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Rewrite/src/Rewrite.Test.Engine.Remote/Rewrite.Test.Engine.Remote.csproj b/Rewrite/src/Rewrite.Test.Engine.Remote/Rewrite.Test.Engine.Remote.csproj index f51c329..370fa90 100644 --- a/Rewrite/src/Rewrite.Test.Engine.Remote/Rewrite.Test.Engine.Remote.csproj +++ b/Rewrite/src/Rewrite.Test.Engine.Remote/Rewrite.Test.Engine.Remote.csproj @@ -17,13 +17,14 @@ + - + - + diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.csproj b/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.csproj index 24b2b76..189c750 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.csproj +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Rewrite.CSharp.Tests.csproj @@ -7,6 +7,7 @@ true + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -22,6 +23,7 @@ + From 9224451e49c4086ddc34ee178935a701c4058ef7 Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Mon, 4 Nov 2024 16:13:43 -0500 Subject: [PATCH 8/9] Remove usage of fixture collections --- .../CollectionDefinitions.cs | 40 +++++++++---------- .../FindClassDeclarationTests.cs | 1 - .../Rewrite.CSharp.Tests/FindClassTests.cs | 1 - .../tests/Rewrite.CSharp.Tests/PlayTests.cs | 2 +- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs b/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs index a0490dd..a4c85ee 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs @@ -1,20 +1,20 @@ -using Rewrite.Test.CSharp; -using Rewrite.Test.Engine.Remote; -using Rewrite.Test; - -namespace Rewrite.CSharp.Tests; - -[CollectionDefinition("LocalPrinter")] -public class LocalPrinterCollection : ICollectionFixture; - -[CollectionDefinition("RemotePrinter")] -public class RemotePrinterCollection : ICollectionFixture; - -public static class Collections -{ - #if REMOTE_PRINTER - public const string PrinterAccess = "RemotePrinter"; - #else - public const string PrinterAccess = "LocalPrinter"; - #endif -} +// using Rewrite.Test.CSharp; +// using Rewrite.Test.Engine.Remote; +// using Rewrite.Test; +// +// namespace Rewrite.CSharp.Tests; +// +// [CollectionDefinition("LocalPrinter")] +// public class LocalPrinterCollection : ICollectionFixture; +// +// [CollectionDefinition("RemotePrinter")] +// public class RemotePrinterCollection : ICollectionFixture; +// +// public static class Collections +// { +// #if REMOTE_PRINTER +// public const string PrinterAccess = "RemotePrinter"; +// #else +// public const string PrinterAccess = "LocalPrinter"; +// #endif +// } diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/FindClassDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/FindClassDeclarationTests.cs index d07a26d..df37c0b 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/FindClassDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/FindClassDeclarationTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests; using static Assertions; -[Collection(Collections.PrinterAccess)] public class FindClassDeclarationTests : RewriteTest { protected override void Defaults(RecipeSpec spec) diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/FindClassTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/FindClassTests.cs index 98ab13e..1c98c29 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/FindClassTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/FindClassTests.cs @@ -5,7 +5,6 @@ namespace Rewrite.CSharp.Tests; using static Assertions; -[Collection(Collections.PrinterAccess)] public class FindClassTests : RewriteTest { protected override void Defaults(RecipeSpec spec) diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/PlayTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/PlayTests.cs index 6e63753..00486b1 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/PlayTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/PlayTests.cs @@ -1,6 +1,5 @@ namespace Rewrite.CSharp.Tests; -[Collection(Collections.PrinterAccess)] [Exploratory] public class PlayTests(ITestOutputHelper _output) : RewriteTest { @@ -31,6 +30,7 @@ public void Delta(string before, string after) } [Fact] + [Exploratory] public void MyTest() { var root = new CSharpParser.Builder().Build().Parse( From 7990ff1ebc9e7f42e0009470f27ac908feaafb33 Mon Sep 17 00:00:00 2001 From: Andrew Stakhov Date: Mon, 4 Nov 2024 16:42:36 -0500 Subject: [PATCH 9/9] Fix failing test --- Rewrite/src/Rewrite.CSharp/CSharpParser.cs | 27 ------------------- .../CollectionDefinitions.cs | 20 -------------- .../Tree/MethodDeclarationTests.cs | 25 +++++++++-------- 3 files changed, 12 insertions(+), 60 deletions(-) delete mode 100644 Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs diff --git a/Rewrite/src/Rewrite.CSharp/CSharpParser.cs b/Rewrite/src/Rewrite.CSharp/CSharpParser.cs index 1dd6cd1..ab91a52 100644 --- a/Rewrite/src/Rewrite.CSharp/CSharpParser.cs +++ b/Rewrite/src/Rewrite.CSharp/CSharpParser.cs @@ -89,33 +89,6 @@ public IEnumerable ParseInputs(IEnumerable source sourceFiles.Add(cs!); }); - // foreach (var source in sources) - // { - // SourceFile cs; - // try - // { - // using var stream = source.GetSource(ctx); - // var sourceText = SourceText.From(stream, encoding); - // var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, path: source.GetRelativePath(relativeTo)); - // var root = syntaxTree.GetRoot(); - // var compilation = CSharpCompilation.Create("Dummy") - // .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) - // .AddReferences(references) - // .AddSyntaxTrees(syntaxTree); - // var semanticModel = compilation.GetSemanticModel(syntaxTree); - // - // var visitor = new CSharpParserVisitor(this, semanticModel); - // cs = visitor.Visit(root) as Cs.CompilationUnit ?? throw new InvalidOperationException("Visitor.Visit returned null instead of Compilation Unit"); - // } - // catch (Exception t) - // { - // // ctx.OnError.accept(t); - // cs = ParseError.Build(this, source, relativeTo, ctx, t); - // } - // - // sourceFiles.Add(cs!); - // } - return sourceFiles; } } diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs b/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs deleted file mode 100644 index a4c85ee..0000000 --- a/Rewrite/tests/Rewrite.CSharp.Tests/CollectionDefinitions.cs +++ /dev/null @@ -1,20 +0,0 @@ -// using Rewrite.Test.CSharp; -// using Rewrite.Test.Engine.Remote; -// using Rewrite.Test; -// -// namespace Rewrite.CSharp.Tests; -// -// [CollectionDefinition("LocalPrinter")] -// public class LocalPrinterCollection : ICollectionFixture; -// -// [CollectionDefinition("RemotePrinter")] -// public class RemotePrinterCollection : ICollectionFixture; -// -// public static class Collections -// { -// #if REMOTE_PRINTER -// public const string PrinterAccess = "RemotePrinter"; -// #else -// public const string PrinterAccess = "LocalPrinter"; -// #endif -// } diff --git a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs index 3baa2dc..204bdde 100644 --- a/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs +++ b/Rewrite/tests/Rewrite.CSharp.Tests/Tree/MethodDeclarationTests.cs @@ -36,22 +36,21 @@ public class Foo [Fact] public void ConstructorDelegation() { - RewriteRun( - // spec => spec.TypeValidation = new TypeValidation(Unknowns: false), - CSharp( - """ - public class Foo + var src = CSharp( + """ + public class Foo + { + Foo() : this(1) { - Foo() : this(1) - { - } - Foo(int i) - { - } } - """ - ) + Foo(int i) + { + } + } + """ ); + var lst = src.Parse(); + lst.ToString().ShouldBeSameAs(src.Before); } [Fact]