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 c3ebc71
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
35 changes: 35 additions & 0 deletions src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,38 @@ 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
)
"""
45 changes: 37 additions & 8 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -913,12 +913,17 @@ 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 shortExpression =
col sepComma es (genShortExpr astContext)
let sep =
if baseCtorCall then
(sepComma +> sepNln)
else
sepComma

let shortExpression = col sep es (genShortExpr astContext)

let longExpression =
let containsLambdaOrMatchExpr =
Expand Down Expand Up @@ -1232,11 +1237,22 @@ 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 +1627,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 +2552,8 @@ 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 +4595,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

0 comments on commit c3ebc71

Please sign in to comment.