From 10883782605bda4be09c8c0e5f15583c78950f39 Mon Sep 17 00:00:00 2001 From: Martin Disch Date: Mon, 6 Jan 2025 16:33:09 +0100 Subject: [PATCH 1/2] Move => to beginning of line --- .../SyntaxNodePrinters/ArrowExpressionClause.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/ArrowExpressionClause.cs b/Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/ArrowExpressionClause.cs index cfc208761..c70c050d6 100644 --- a/Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/ArrowExpressionClause.cs +++ b/Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/ArrowExpressionClause.cs @@ -9,9 +9,11 @@ public static Doc Print(ArrowExpressionClauseSyntax node, PrintingContext contex { return Doc.Group( Doc.Indent( + Doc.IfBreak("", " "), + Doc.SoftLine, + Token.Print(node.ArrowToken, context), " ", - Token.PrintWithSuffix(node.ArrowToken, Doc.Line, context), - Node.Print(node.Expression, context) + Doc.Indent(Node.Print(node.Expression, context)) ) ); } From 9829e20c942793b8072c25e420906669e781f145 Mon Sep 17 00:00:00 2001 From: Martin Disch Date: Tue, 16 Sep 2025 11:26:10 +0200 Subject: [PATCH 2/2] Update snapshots --- .../TestFiles/cs/AssignmentExpressions.test | 28 +++--- .../TestFiles/cs/BinaryExpressions.test | 6 +- .../TestFiles/cs/CollectionExpressions.test | 44 +++++----- .../cs/ConversionOperatorDeclarations.test | 9 +- .../TestFiles/cs/Directives.test | 6 +- .../TestFiles/cs/ExpressionBodies.test | 53 ++++++++++++ .../TestFiles/cs/MethodDeclarations.test | 4 +- .../TestFiles/cs/ObnoxiousEdgeCases.test | 86 ++++++++++--------- .../TestFiles/cs/PropertyDeclarations.test | 4 +- 9 files changed, 147 insertions(+), 93 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ExpressionBodies.test diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AssignmentExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AssignmentExpressions.test index 562ba833f..f6512076b 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AssignmentExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AssignmentExpressions.test @@ -69,18 +69,18 @@ class ClassName ); } - public SomeExpressionBodyMethod() => - nonChainFormatting = anotherVariable1 = ( - someCondition________________________________ - || someOtherCondition___________________________ - ); - - public SomeExpressionBodyMethod() => - chainFormatting = - anotherVariable1 = - anotherVariable2 = - ( - someCondition________________________________ - || someOtherCondition___________________________ - ); + public SomeExpressionBodyMethod() + => nonChainFormatting = anotherVariable1 = ( + someCondition________________________________ + || someOtherCondition___________________________ + ); + + public SomeExpressionBodyMethod() + => chainFormatting = + anotherVariable1 = + anotherVariable2 = + ( + someCondition________________________________ + || someOtherCondition___________________________ + ); } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/BinaryExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/BinaryExpressions.test index 639b87065..41042a444 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/BinaryExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/BinaryExpressions.test @@ -1,8 +1,8 @@ class TestClass { - public string SomeProperty => - "someLongStringValue__________________________" - + "someOtherLongStringValue_______________________________"; + public string SomeProperty + => "someLongStringValue__________________________" + + "someOtherLongStringValue_______________________________"; void TestMethod() { diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test index 356cbe8ff..ed56169c1 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test @@ -107,28 +107,28 @@ class MyClass _items ??= []; } - public TheoryData ArgsData => - [ - [], - [ArgConstants.UseProgramMain], - [ArgConstants.GlobalInteractivity, ArgConstants.WebAssemblyInteractivity], - [ - ArgConstants.NoInteractivity, - ArgConstants.UseProgramMain, - ArgConstants.NoHttps, - ArgConstants.Empty, - ], - ]; - - public ReadOnlySpan Methods() => - [ - HttpMethod.None, - HttpMethod.Put, - HttpMethod.Post, - HttpMethod.Patch, - HttpMethod.None, - HttpMethod.Connect, - ]; + public TheoryData ArgsData + => [ + [], + [ArgConstants.UseProgramMain], + [ArgConstants.GlobalInteractivity, ArgConstants.WebAssemblyInteractivity], + [ + ArgConstants.NoInteractivity, + ArgConstants.UseProgramMain, + ArgConstants.NoHttps, + ArgConstants.Empty, + ], + ]; + + public ReadOnlySpan Methods() + => [ + HttpMethod.None, + HttpMethod.Put, + HttpMethod.Post, + HttpMethod.Patch, + HttpMethod.None, + HttpMethod.Connect, + ]; public List DaysOfWeek { get; } = [ diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ConversionOperatorDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ConversionOperatorDeclarations.test index 361866a89..25a427430 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ConversionOperatorDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ConversionOperatorDeclarations.test @@ -12,12 +12,11 @@ class Test return new Test(); } - public static implicit operator Test(Person p) => - new Test(p.First + " " + p.Last); + public static implicit operator Test(Person p) + => new Test(p.First + " " + p.Last); public static implicit operator Test(int x) => new Test(x); - public static implicit operator Test( - bool reallyLongNameThatWillMakeThisBreak________________ - ) => new Test(x); + public static implicit operator Test(bool reallyLongNameThatWillMakeThisBreak________________) + => new Test(x); } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives.test index a2368476e..c88ded331 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives.test @@ -18,10 +18,10 @@ public class ClassName { public bool SomeProperty => #if !DEBUG - someValue - && // trailing comment with endif should work properly + someValue + && // trailing comment with endif should work properly #endif - someOtherValue; + someOtherValue; void MethodWithOnlyDisabled() { diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ExpressionBodies.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ExpressionBodies.test new file mode 100644 index 000000000..0e0060d54 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ExpressionBodies.test @@ -0,0 +1,53 @@ +public class ClassName +{ + public string LambdaBod => this.Property; + + public string LongNameForcesALineBreakSoWeCanTestEvenLonger____________________ + => this.Property; + + public SomeExpressionBodyMethod() + => nonChainFormatting = anotherVariable1 = ( + someCondition________________________________ + || someOtherCondition___________________________ + ); + + public SomeExpressionBodyMethod() + => chainFormatting = + anotherVariable1 = + anotherVariable2 = + ( + someCondition________________________________ + || someOtherCondition___________________________ + ); + + public void MethodName() + { + CallMethod(() => + CallOtherMethod___________________________________________________________() + ); + } + + public void Complicated() + { + someLongAssObject.SomeLongAssFunction( + (someLongAssArg, someLongerAsserArg) => + someLongAssArg.SomeLongAssMethod(someLongerAsserArg.SomeLongAssProperty) + ); + } + + private bool ShouldNotBeDescribedKind(ITypeDefinition typeDefinition) + => typeDefinition.Kind switch + { + TypeKind.Class => (_configuration.Kinds & DescribeKind.Class) != DescribeKind.Class, + TypeKind.Interface => (_configuration.Kinds & DescribeKind.Interface) + != DescribeKind.Interface, + TypeKind.Struct => (_configuration.Kinds & DescribeKind.Struct) + != DescribeKind.Struct, + TypeKind.Delegate => (_configuration.Kinds & DescribeKind.Delegate) + != DescribeKind.Delegate, + TypeKind.Enum => (_configuration.Kinds & DescribeKind.Enum) != DescribeKind.Enum, + TypeKind.Void or TypeKind.NInt or TypeKind.NUInt or TypeKind.FunctionPointer => + throw new UnreachableException(), + _ => true, + }; +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MethodDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MethodDeclarations.test index d19b435f7..6159f5bfe 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MethodDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MethodDeclarations.test @@ -55,8 +55,8 @@ public class ClassName private Doc ShortMethod() => Method(); - private Doc ShortishMethod(SomeClass one) => - CallSomeOtherLongMethodSoThatItBreaks____________________(one); + private Doc ShortishMethod(SomeClass one) + => CallSomeOtherLongMethodSoThatItBreaks____________________(one); private Doc MethodWithParameters_______________________( SomeClass one, diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ObnoxiousEdgeCases.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ObnoxiousEdgeCases.test index 7c0cce35b..f213d6b79 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ObnoxiousEdgeCases.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ObnoxiousEdgeCases.test @@ -61,12 +61,12 @@ class ClassName [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TAccountClaimsPrincipalFactory >(this IRemoteAuthenticationBuilder builder) - where TAccountClaimsPrincipalFactory : AccountClaimsPrincipalFactory => - builder.AddAccountClaimsPrincipalFactory< - RemoteAuthenticationState, - RemoteUserAccount, - TAccountClaimsPrincipalFactory - >(); + where TAccountClaimsPrincipalFactory : AccountClaimsPrincipalFactory + => builder.AddAccountClaimsPrincipalFactory< + RemoteAuthenticationState, + RemoteUserAccount, + TAccountClaimsPrincipalFactory + >(); private static readonly Action ActionNotImplemented = new Action(_ => @@ -88,37 +88,35 @@ class ClassName } } - public void Condition( - Func, bool> condition - ) => - PathMapActions.Add(pm => - { - Expression< - Func - > expr = (src, dest, srcMember, destMember, ctxt) => - condition( - new ConditionParameters( - src, - dest, - srcMember, - destMember, - ctxt - ) - ); - pm.Condition = expr; - }); + public void Condition(Func, bool> condition) + => PathMapActions.Add(pm => + { + Expression< + Func + > expr = (src, dest, srcMember, destMember, ctxt) => + condition( + new ConditionParameters( + src, + dest, + srcMember, + destMember, + ctxt + ) + ); + pm.Condition = expr; + }); public override Type SourceType { - get => - _sourceType ??= - CustomMapExpression?.ReturnType - ?? CustomMapFunction?.ReturnType - ?? ( - _sourceMembers.Length > 0 - ? _sourceMembers[_sourceMembers.Length - 1].GetMemberType() - : Parameter.ParameterType - ); + get + => _sourceType ??= + CustomMapExpression?.ReturnType + ?? CustomMapFunction?.ReturnType + ?? ( + _sourceMembers.Length > 0 + ? _sourceMembers[_sourceMembers.Length - 1].GetMemberType() + : Parameter.ParameterType + ); protected set => _sourceType = value; } @@ -126,14 +124,18 @@ class ClassName this Expression expression, Expression then, Expression @else - ) => - expression.Type.IsValueType - ? ( - expression.Type.IsNullableType() - ? Condition(Property(expression, "HasValue"), ToType(@else, then.Type), then) - : @else - ) - : Condition(ReferenceEqual(expression, Null), then, ToType(@else, then.Type)); + ) + => expression.Type.IsValueType + ? ( + expression.Type.IsNullableType() + ? Condition( + Property(expression, "HasValue"), + ToType(@else, then.Type), + then + ) + : @else + ) + : Condition(ReferenceEqual(expression, Null), then, ToType(@else, then.Type)); private bool VisitChildForCompare(Pair tuple, ref int result) { diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test index 3f5de0e20..fabf84092 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test @@ -28,8 +28,8 @@ public class ClassName public string LambdaBod => this.Property; - public string LongNameForcesALineBreakSoWeCanTestEvenLonger____________________ => - this.Property; + public string LongNameForcesALineBreakSoWeCanTestEvenLonger____________________ + => this.Property; public string Property {