Skip to content

Commit f7a9301

Browse files
nojafdawedawe
andauthored
Don't put = on the same line for when last parameter is single line tuple (#3042)
* Don't put `=` on the same line for when last parameter is single line tuple. * Update src/Fantomas.Core/CodePrinter.fs Co-authored-by: dawe <dawedawe@posteo.de> --------- Co-authored-by: dawe <dawedawe@posteo.de>
1 parent de8ac50 commit f7a9301

File tree

5 files changed

+92
-13
lines changed

5 files changed

+92
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
* Equals sign should only be on same line if last tuple is multiline. [#3040](https://github.com/fsprojects/fantomas/issues/3040)
7+
38
## 6.3.0-alpha-007 - 2024-01-27
49

510
### Changed

src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,8 @@ type RequestParser<'ctx, 'a> =
14901490
prohibited: ProhibitedRequestGetter list }
14911491

14921492
static member internal Create
1493-
(consumedFields, parse: 'ctx -> Request -> Async<Result<'a, Error list>>) : RequestParser<'ctx, 'a> =
1493+
(consumedFields, parse: 'ctx -> Request -> Async<Result<'a, Error list>>)
1494+
: RequestParser<'ctx, 'a> =
14941495
{ consumedFields = consumedFields
14951496
parse = parse
14961497
prohibited = [] }

src/Fantomas.Core.Tests/FunctionDefinitionTests.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,8 @@ let longFunctionWithLongTupleParameter
11061106
equal
11071107
"""
11081108
let longFunctionWithLongTupleParameter
1109-
(aVeryLongParam, aSecondVeryLongParam, aThirdVeryLongParam, aFourthVeryLongParam) =
1109+
(aVeryLongParam, aSecondVeryLongParam, aThirdVeryLongParam, aFourthVeryLongParam)
1110+
=
11101111
// ... the body of the method follows
11111112
()
11121113
"""

src/Fantomas.Core.Tests/LetBindingTests.fs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,3 +2284,31 @@ let bar
22842284
//
22852285
()
22862286
"""
2287+
2288+
[<Test>]
2289+
let ``equals sign should not be on same line when last tuple is single line, 3040`` () =
2290+
formatSourceString
2291+
"""
2292+
let processSnippetLine
2293+
(checkResults: FSharpCheckFileResults)
2294+
(semanticRanges: SemanticClassificationItem array)
2295+
(lines: string array)
2296+
(line: int, lineTokens: SnippetLine)
2297+
=
2298+
let lineStr = lines.[line]
2299+
()
2300+
"""
2301+
config
2302+
|> prepend newline
2303+
|> should
2304+
equal
2305+
"""
2306+
let processSnippetLine
2307+
(checkResults: FSharpCheckFileResults)
2308+
(semanticRanges: SemanticClassificationItem array)
2309+
(lines: string array)
2310+
(line: int, lineTokens: SnippetLine)
2311+
=
2312+
let lineStr = lines.[line]
2313+
()
2314+
"""

src/Fantomas.Core/CodePrinter.fs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,8 @@ let genBinding (b: BindingNode) (ctx: Context) : Context =
28392839
ifElse addSpaceBeforeParensInFunDef sepSpace sepNone
28402840
| _ -> sepSpace
28412841

2842+
/// Format everything in one line:
2843+
/// let fn p1 p2 : rt =
28422844
let short =
28432845
afterLetKeyword
28442846
+> sepSpace
@@ -2849,12 +2851,54 @@ let genBinding (b: BindingNode) (ctx: Context) : Context =
28492851
+> sepSpace
28502852
+> genSingleTextNode b.Equals
28512853

2854+
/// Format over multiple lines
2855+
/// AlternativeLongMemberDefinitions or AlignFunctionSignatureToIndentation can influence the equals and return type.
2856+
///
2857+
/// By default we go with:
2858+
/// let fn
2859+
/// p1
2860+
/// p2
2861+
/// : rt =
2862+
///
2863+
/// When `rt` is multiline, `=` is placed on the next line:
2864+
/// let fn
2865+
/// p1
2866+
/// p2
2867+
/// : rt
2868+
/// =
2869+
///
2870+
/// When the p2 is a multiline tuple, there are different rules formatting rules:
2871+
/// let fn
2872+
/// p1
2873+
/// (
2874+
/// p2_1,
2875+
/// p2_2,
2876+
/// p2_3
2877+
/// ) : rt =
2878+
///
2879+
/// AlignFunctionSignatureToIndentation will always place `rt` and `=` on their own lines:
2880+
/// let fn
2881+
/// p1
2882+
/// p2
2883+
/// : rt
2884+
/// =
2885+
///
2886+
/// This happens regardless whether p2 is a multiline tuple:
2887+
/// let fn
2888+
/// p1
2889+
/// (
2890+
/// p2_1,
2891+
/// p2_2,
2892+
/// p2_3
2893+
/// )
2894+
/// : rt
2895+
/// =
28522896
let long (ctx: Context) =
2853-
let endsWithTupleParameter =
2897+
let endsWithMultilineTupleParameter =
28542898
match List.tryLast b.Parameters with
2855-
| Some(Pattern.Paren parenNode) ->
2899+
| Some(Pattern.Paren parenNode as p) ->
28562900
match parenNode.Pattern with
2857-
| Pattern.Tuple _ -> true
2901+
| Pattern.Tuple _ -> futureNlnCheck (genLongParenPatParameter p) ctx
28582902
| _ -> false
28592903
| _ -> false
28602904

@@ -2875,21 +2919,21 @@ let genBinding (b: BindingNode) (ctx: Context) : Context =
28752919

28762920
beforeInline || beforeIdentifier || beforeAccessibility
28772921

2922+
let nlnOnSeparateLine = not endsWithMultilineTupleParameter || alternativeSyntax
2923+
28782924
(onlyIf hasTriviaAfterLeadingKeyword indent
28792925
+> afterLetKeyword
28802926
+> sepSpace
28812927
+> genFunctionName
28822928
+> indent
28832929
+> sepNln
28842930
+> genParameters
2885-
+> onlyIf (not endsWithTupleParameter || alternativeSyntax) sepNln
2886-
+> leadingExpressionIsMultiline
2887-
(genReturnType (not endsWithTupleParameter || alternativeSyntax))
2888-
(fun isMultiline ->
2889-
if (alternativeSyntax && Option.isSome b.ReturnType) || isMultiline then
2890-
sepNln +> genSingleTextNode b.Equals
2891-
else
2892-
sepSpace +> genSingleTextNode b.Equals)
2931+
+> onlyIf nlnOnSeparateLine sepNln
2932+
+> leadingExpressionIsMultiline (genReturnType nlnOnSeparateLine) (fun isMultiline ->
2933+
if (alternativeSyntax && Option.isSome b.ReturnType) || isMultiline then
2934+
sepNln +> genSingleTextNode b.Equals
2935+
else
2936+
sepSpace +> genSingleTextNode b.Equals)
28932937
+> unindent
28942938
+> onlyIf hasTriviaAfterLeadingKeyword unindent)
28952939
ctx

0 commit comments

Comments
 (0)