Skip to content

Commit

Permalink
WIP for issue fsprojects#1442
Browse files Browse the repository at this point in the history
  • Loading branch information
su8898 authored and knocte committed Sep 9, 2021
1 parent a80f888 commit a3c8dd3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
33 changes: 33 additions & 0 deletions src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,36 @@ type MaybeBuilder() =
) : 'U option =
Option.bind binder value
"""

[<Test>]
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
)
"""
50 changes: 39 additions & 11 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -936,7 +942,7 @@ and genTuple astContext es =
(sepNln +> sepComma)
else
(sepComma +> sepNln)

let lastIndex = List.length es - 1

let genExpr astContext idx e =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -4594,7 +4622,7 @@ and genMemberDefn astContext node =
+> col sepComma (simplePats ps) (genSimplePat astContext)
+> sepCloseT

let longPats =
let longPats =
sepOpenT
+> indent
+> sepNln
Expand Down

0 comments on commit a3c8dd3

Please sign in to comment.