From a3c8dd3e54e9e158374668e83fbb6452e3f8f8a7 Mon Sep 17 00:00:00 2001 From: Retheesh Date: Mon, 6 Sep 2021 06:44:44 +0200 Subject: [PATCH] WIP for issue #1442 --- src/Fantomas.Tests/ClassTests.fs | 33 +++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 50 +++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/src/Fantomas.Tests/ClassTests.fs b/src/Fantomas.Tests/ClassTests.fs index 664c310e83..2acae4af14 100644 --- a/src/Fantomas.Tests/ClassTests.fs +++ b/src/Fantomas.Tests/ClassTests.fs @@ -896,3 +896,36 @@ type MaybeBuilder() = ) : 'U option = Option.bind binder value """ + +[] +let ``avoid vanity alignment when calling base constructor`` () = + + let actual = formatSourceString + false + """ +type public DerivedExceptionWithLongNaaaaaaaaameException (message: string, + code: int, + originalRequest: string, + originalResponse: string) = + inherit BaseExceptionWithLongNaaaameException(message, code, originalRequest, originalResponse)""" + { config with + MaxLineLength = 80 } + actual |> prepend newline + |> should + equal + """ +type public DerivedExceptionWithLongNaaaaaaaaameException + ( + message: string, + code: int, + originalRequest: string, + originalResponse: string + ) = + inherit BaseExceptionWithLongNaaaameException + ( + message, + code, + originalRequest, + originalResponse + ) +""" \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index db2d45d824..5e10ecab4c 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -913,12 +913,18 @@ and genAnonRecordFieldName astContext (AnonRecordFieldName (s, e)) = !-s +> sepEq +> expr -and genTuple astContext es = +and genTuple astContext es baseCtorCall = let genShortExpr astContext e = addParenForTupleWhen (genExpr astContext) e + let sep = + if baseCtorCall then + (sepComma +> sepNln) + else + sepComma + let shortExpression = - col sepComma es (genShortExpr astContext) + col sep es (genShortExpr astContext) let longExpression = let containsLambdaOrMatchExpr = @@ -936,7 +942,7 @@ and genTuple astContext es = (sepNln +> sepComma) else (sepComma +> sepNln) - + let lastIndex = List.length es - 1 let genExpr astContext idx e = @@ -966,11 +972,11 @@ and genNamedArgumentExpr (astContext: ASTContext) operatorExpr e1 e2 = +> genExpr astContext e2 +> unindent - expressionFitsOnRestOfLine short long + expressionFitsOnRestOfLine short long and genExpr astContext synExpr ctx = let kw tokenName f = tokN synExpr.Range tokenName f - + let expr = match synExpr with | ElmishReactWithoutChildren (identifier, isArray, children, childrenRange) when @@ -1232,11 +1238,19 @@ and genExpr astContext synExpr ctx = +> genType astContext false t +> sepSpace +> genExpr astContext e - | Tuple (es, _) -> genTuple astContext es + | Tuple (es, _) -> + let rec listContainsInherit list = match list with + | (")")::_ -> false + | (" inherit BaseExceptionWithLongNaaaameException")::p -> true + | _::tail -> listContainsInherit tail + | [] -> false + + let baseCtorCall = listContainsInherit ctx.WriterModel.Lines + genTuple astContext es baseCtorCall | StructTuple es -> !- "struct " +> sepOpenT - +> genTuple astContext es + +> genTuple astContext es false +> sepCloseT | ArrayOrList (isArray, [], _) -> ifElse @@ -1611,9 +1625,19 @@ and genExpr astContext synExpr ctx = +> atCurrentColumn (genExpr astContext e) +> sepCloseTFor rpr pr | _ -> - sepOpenTFor lpr - +> genExpr astContext e - +> sepCloseTFor rpr pr + //sepOpenTFor lpr + //+> genExpr astContext e + //+> sepCloseTFor rpr pr + indent +> sepNln +> indent + +> ( + sepOpenTFor lpr + +> sepNln + +> genExpr astContext e + +> unindent + +> sepNln + +> unindent + +> sepCloseTFor rpr pr + ) | CompApp (s, e) -> !-s +> sepSpace @@ -2526,6 +2550,9 @@ and genExpr astContext synExpr ctx = expr ctx +and genExprInherit astContext synExpr ctx node = + genExpr astContext synExpr ctx + and genInfixOperator operatorText (operatorExpr: SynExpr) = (!-operatorText |> genTriviaFor SynExpr_Ident operatorExpr.Range) @@ -4567,6 +4594,7 @@ and genMemberDefn astContext node = let genCtor = let shortExpr = optPre sepSpace sepSpace ao genAccess + //+> sepNln +> ((sepOpenT +> col sepComma (simplePats ps) (genSimplePat astContext) +> sepCloseT) @@ -4594,7 +4622,7 @@ and genMemberDefn astContext node = +> col sepComma (simplePats ps) (genSimplePat astContext) +> sepCloseT - let longPats = + let longPats = sepOpenT +> indent +> sepNln