From a746604c9f6a38c63edfbae41fb69df9d9f95ed7 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Tue, 10 Jan 2023 18:18:17 -0600 Subject: [PATCH] Put generic constraints on new line (#786) closes #527 --- .../TestFiles/ClassDeclarations.cst | 6 +++-- .../TestFiles/ClassDeclarations_Tabs.cst | 6 +++-- .../TestFiles/DelegateDeclarations.cst | 3 ++- .../TypeParameterConstraintClauses.cst | 24 ++++++++++++------- .../SyntaxPrinter/ConstraintClauses.cs | 3 +-- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations.cst index cb1401330..2980e6eae 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations.cst @@ -14,7 +14,8 @@ public class ThisIsSomeLongNameAndItShouldFormatWell1 AndYetAnotherLongClassName, AndStillOneMore { } -public class SimpleGeneric where T : new() { } +public class SimpleGeneric + where T : new() { } public class LongTypeConstraints where T : SomeLongNameThatJustKeepsGoing, @@ -32,7 +33,8 @@ public class LongerClassNameWithLotsOfGenerics< TThirdLongName__________________ > : SomeBaseClass { } -public class SimpleGeneric : BaseClass where T : new() { } +public class SimpleGeneric : BaseClass + where T : new() { } public class ThisIsSomeLongNameAndItShouldFormatWell2 : AnotherLongClassName, diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_Tabs.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_Tabs.cst index 7fa066428..7b2c65197 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_Tabs.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_Tabs.cst @@ -14,7 +14,8 @@ public class ThisIsSomeLongNameAndItShouldFormatWell1 AndYetAnotherLongClassName, AndStillOneMore { } -public class SimpleGeneric where T : new() { } +public class SimpleGeneric + where T : new() { } public class LongTypeConstraints where T : SomeLongNameThatJustKeepsGoing, @@ -32,7 +33,8 @@ public class LongerClassNameWithLotsOfGenerics< TThirdLongName__________________ > : SomeBaseClass { } -public class SimpleGeneric : BaseClass where T : new() { } +public class SimpleGeneric : BaseClass + where T : new() { } public class ThisIsSomeLongNameAndItShouldFormatWell2 : AnotherLongClassName, diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/DelegateDeclarations.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/DelegateDeclarations.cst index 333276b1a..87948bd2f 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/DelegateDeclarations.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/DelegateDeclarations.cst @@ -2,5 +2,6 @@ class ClassName { delegate void Delegate(); - delegate void Delegate<[System.Obsolete()] out T>() where T : struct; + delegate void Delegate<[System.Obsolete()] out T>() + where T : struct; } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/TypeParameterConstraintClauses.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/TypeParameterConstraintClauses.cst index 233fc0b83..cbc19f140 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/TypeParameterConstraintClauses.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/TypeParameterConstraintClauses.cst @@ -1,10 +1,14 @@ -delegate void Delegate() where T : struct; +delegate void Delegate() + where T : struct; -class ClassName where T : class +class ClassName + where T : class { - void MethodName() where T : class + void MethodName() + where T : class { - void LocalFunction() where T : class + void LocalFunction() + where T : class { return; } @@ -31,7 +35,8 @@ class ClassName where T : class public static ReturnType MethodName( string longParameter_______________________, string longParameter_______________________ - ) where T : class + ) + where T : class { return; } @@ -39,7 +44,8 @@ class ClassName where T : class public static ReturnType MethodName( string longParameter_______________________, string longParameter_______________________ - ) where T : class { } + ) + where T : class { } public static ReturnType MethodName( string longParameter_______________________, @@ -59,9 +65,11 @@ class ClassName where T : class where U : class { } } -interface InterfaceName where T : class { } +interface InterfaceName + where T : class { } -struct Struct where T : class { } +struct Struct + where T : class { } class ClassName where N : new() diff --git a/Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs b/Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs index 6413e02df..84dd6c938 100644 --- a/Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs +++ b/Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs @@ -13,12 +13,11 @@ FormattingContext context { return Doc.Null; } - var prefix = constraintClausesList.Count >= 2 ? Doc.HardLine : Doc.Line; var body = Doc.Join( Doc.HardLine, constraintClausesList.Select(o => TypeParameterConstraintClause.Print(o, context)) ); - return Doc.Group(Doc.Indent(prefix, body)); + return Doc.Group(Doc.Indent(Doc.HardLine, body)); } }