From 261ecf5e26f667819730e627242f485883b342bd Mon Sep 17 00:00:00 2001 From: nojaf Date: Sun, 14 Jan 2024 12:06:20 +0100 Subject: [PATCH] Only indentSepNlnUnindent when pat is tuple inside paren. --- src/Fantomas.Core.Tests/ConstructorTests.fs | 5 +- src/Fantomas.Core.Tests/LetBindingTests.fs | 85 ++++++++++++++++++- .../TypeAnnotationTests.fs | 14 ++- src/Fantomas.Core/CodePrinter.fs | 34 +++++--- 4 files changed, 112 insertions(+), 26 deletions(-) diff --git a/src/Fantomas.Core.Tests/ConstructorTests.fs b/src/Fantomas.Core.Tests/ConstructorTests.fs index 893f785fd5..8480e7db3f 100644 --- a/src/Fantomas.Core.Tests/ConstructorTests.fs +++ b/src/Fantomas.Core.Tests/ConstructorTests.fs @@ -160,9 +160,8 @@ type StateMachine = new ( - // also meh but with an int - x: int - ) as secondCtor + // also meh but with an int + x: int) as secondCtor = StateMachine() """ diff --git a/src/Fantomas.Core.Tests/LetBindingTests.fs b/src/Fantomas.Core.Tests/LetBindingTests.fs index 196bcb4e5d..ad81c7987f 100644 --- a/src/Fantomas.Core.Tests/LetBindingTests.fs +++ b/src/Fantomas.Core.Tests/LetBindingTests.fs @@ -2141,6 +2141,88 @@ let bar |> should equal """ +let foo + ( + a: RatherLongTypeName, + b: RatherLongTypeName, + c: RatherLongTypeName, + d: RatherLongTypeName, + e: RatherLongTypeName, + f: RatherLongTypeName, + g: RatherLongTypeName, + h: RatherLongTypeName, + i: RatherLongTypeName, + j: RatherLongTypeName, + k: RatherLongTypeName, + l: RatherLongTypeName, + m: RatherLongTypeName, + n: RatherLongTypeName, + o: RatherLongTypeName, + p: RatherLongTypeName, + q: RatherLongTypeName, + r: RatherLongTypeName, + s: RatherLongTypeName + ) + ( + t: RatherLongTypeName, + u: RatherLongTypeName, + v: RatherLongTypeName, + w: RatherLongTypeName, + x: RatherLongTypeName, + y: RatherLongTypeName, + z: RatherLongTypeName + ) = + // + () + +let bar + ( + a: RatherLongTypeName, + b: RatherLongTypeName, + c: RatherLongTypeName, + d: RatherLongTypeName, + e: RatherLongTypeName, + f: RatherLongTypeName, + g: RatherLongTypeName, + h: RatherLongTypeName, + i: RatherLongTypeName, + j: RatherLongTypeName, + k: RatherLongTypeName, + l: RatherLongTypeName, + m: RatherLongTypeName, + n: RatherLongTypeName, + o: RatherLongTypeName, + p: RatherLongTypeName, + q: RatherLongTypeName, + r: RatherLongTypeName, + s: RatherLongTypeName + ) = + // + () +""" + +[] +let ``binding with multiple long paren tuple parameters, AlignFunctionSignatureToIndentation`` () = + formatSourceString + """ +let foo + (a: RatherLongTypeName, b: RatherLongTypeName, c: RatherLongTypeName, d: RatherLongTypeName, e: RatherLongTypeName, f: RatherLongTypeName, g: RatherLongTypeName, h: RatherLongTypeName, i: RatherLongTypeName, j: RatherLongTypeName, k: RatherLongTypeName, l: RatherLongTypeName, m: RatherLongTypeName, n: RatherLongTypeName, o: RatherLongTypeName, p: RatherLongTypeName, q: RatherLongTypeName, r: RatherLongTypeName, s: RatherLongTypeName) + (t: RatherLongTypeName, u: RatherLongTypeName, v: RatherLongTypeName, w: RatherLongTypeName, x: RatherLongTypeName, y: RatherLongTypeName, z: RatherLongTypeName) = + // + () + +let bar + (a: RatherLongTypeName, b: RatherLongTypeName, c: RatherLongTypeName, d: RatherLongTypeName, e: RatherLongTypeName, f: RatherLongTypeName, g: RatherLongTypeName, h: RatherLongTypeName, i: RatherLongTypeName, j: RatherLongTypeName, k: RatherLongTypeName, l: RatherLongTypeName, m: RatherLongTypeName, n: RatherLongTypeName, o: RatherLongTypeName, p: RatherLongTypeName, q: RatherLongTypeName, r: RatherLongTypeName, s: RatherLongTypeName) + = + // + () +""" + { config with + AlignFunctionSignatureToIndentation = true } + |> prepend newline + |> should + equal + """ let foo ( a: RatherLongTypeName, @@ -2197,7 +2279,8 @@ let bar q: RatherLongTypeName, r: RatherLongTypeName, s: RatherLongTypeName - ) = + ) + = // () """ diff --git a/src/Fantomas.Core.Tests/TypeAnnotationTests.fs b/src/Fantomas.Core.Tests/TypeAnnotationTests.fs index d24c12d665..2d3c446eb9 100644 --- a/src/Fantomas.Core.Tests/TypeAnnotationTests.fs +++ b/src/Fantomas.Core.Tests/TypeAnnotationTests.fs @@ -48,14 +48,12 @@ type Meh equal """ type Meh - ( - input: - LongTupleItemTypeOneThing * - LongTupleItemTypeThingTwo * - LongTupleItemTypeThree * - LongThingFour * - LongThingFiveYow - ) = class end + (input: + LongTupleItemTypeOneThing * + LongTupleItemTypeThingTwo * + LongTupleItemTypeThree * + LongThingFour * + LongThingFiveYow) = class end """ [] diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 0199067bd7..5099ef4fa1 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -2850,13 +2850,15 @@ let genBinding (b: BindingNode) (ctx: Context) : Context = +> genSingleTextNode b.Equals let long (ctx: Context) = - let genParameters, hasSingleTupledArg = - match b.Parameters with - | [ Pattern.Paren parenNode as pat ] -> + let endsWithTupleParameter = + match List.tryLast b.Parameters with + | Some(Pattern.Paren parenNode) -> match parenNode.Pattern with - | Pattern.Tuple _ -> genLongParenPatParameter pat, true - | _ -> col sepNln b.Parameters genLongParenPatParameter, false - | _ -> col sepNln b.Parameters genLongParenPatParameter, false + | Pattern.Tuple _ -> true + | _ -> false + | _ -> false + + let genParameters = col sepNln b.Parameters genLongParenPatParameter let hasTriviaAfterLeadingKeyword = let beforeInline = @@ -2880,9 +2882,9 @@ let genBinding (b: BindingNode) (ctx: Context) : Context = +> indent +> sepNln +> genParameters - +> onlyIf (not hasSingleTupledArg || alternativeSyntax) sepNln + +> onlyIf (not endsWithTupleParameter || alternativeSyntax) sepNln +> leadingExpressionIsMultiline - (genReturnType (not hasSingleTupledArg || alternativeSyntax)) + (genReturnType (not endsWithTupleParameter || alternativeSyntax)) (fun isMultiline -> if (alternativeSyntax && Option.isSome b.ReturnType) || isMultiline then sepNln +> genSingleTextNode b.Equals @@ -3255,15 +3257,19 @@ let sepNlnBetweenTypeAndMembers (node: ITypeDefn) (ctx: Context) : Context = ctx /// Format a long parentheses parameter pattern in a binding or constructor. +/// Alternate formatting will applied when a paren tuple does not fit on the remainder of the line. let genLongParenPatParameter (pat: Pattern) = match pat with | Pattern.Paren patParen -> - genSingleTextNode patParen.OpeningParen - +> expressionFitsOnRestOfLine - (genPat patParen.Pattern) - (indentSepNlnUnindent (genPat patParen.Pattern) +> sepNln) - +> genSingleTextNode patParen.ClosingParen - |> genNode patParen + match patParen.Pattern with + | Pattern.Tuple _ -> + genSingleTextNode patParen.OpeningParen + +> expressionFitsOnRestOfLine + (genPat patParen.Pattern) + (indentSepNlnUnindent (genPat patParen.Pattern) +> sepNln) + +> genSingleTextNode patParen.ClosingParen + |> genNode patParen + | _ -> genPat pat | _ -> genPat pat let genImplicitConstructor (node: ImplicitConstructorNode) =