Skip to content

Commit

Permalink
Only indentSepNlnUnindent when pat is tuple inside paren.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jan 14, 2024
1 parent c0c2a56 commit 261ecf5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/Fantomas.Core.Tests/ConstructorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
"""
85 changes: 84 additions & 1 deletion src/Fantomas.Core.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
) =
//
()
"""

[<Test>]
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,
Expand Down Expand Up @@ -2197,7 +2279,8 @@ let bar
q: RatherLongTypeName,
r: RatherLongTypeName,
s: RatherLongTypeName
) =
)
=
//
()
"""
14 changes: 6 additions & 8 deletions src/Fantomas.Core.Tests/TypeAnnotationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

[<Test>]
Expand Down
34 changes: 20 additions & 14 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down Expand Up @@ -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) =
Expand Down

0 comments on commit 261ecf5

Please sign in to comment.