From a1b141eaf4b386a9fb6afe6f39f31cd819a2567c Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 25 Aug 2023 09:59:39 +0200 Subject: [PATCH] Ensure the arguments in a multiline application are always indented further than the function expr (#2946) * Ensure the arguments in a multiline application are always indented further than the function expr. * Add additional test case. * Update changelog. --- CHANGELOG.md | 3 +- src/Fantomas.Core.Tests/AppTests.fs | 77 +++++++++++++++++++ .../MultiLineLambdaClosingNewlineTests.fs | 12 +-- src/Fantomas.Core/CodePrinter.fs | 19 ++++- 4 files changed, 101 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2084088dd1..bfe087cd52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [Unreleased] +## 6.1.3 - 2023-08-25 ### Changed * Update FCS to 'Add some more ranges to SynMeasure for tooling support', commit 6ed38fcb360a0015828973b1f32cd2ea6b58c6ad @@ -11,6 +11,7 @@ * Block comment between measure1 and / is moved between / and measure2 in SynMeasure.Divide. [#2934](https://github.com/fsprojects/fantomas/issues/2934) * Block comment between measure1 and * is moved between * and measure2 in SynMeasure.Product. [#2935](https://github.com/fsprojects/fantomas/issues/2935) * Block comment between ^ and exponent in SynMeasure.Power is lost. [#2936](https://github.com/fsprojects/fantomas/issues/2936) +* Opening parens make unstable/incorrect indentation for an Enum wrapping a record. [#2943](https://github.com/fsprojects/fantomas/issues/2943) ## 6.1.2 - 2023-07-23 diff --git a/src/Fantomas.Core.Tests/AppTests.fs b/src/Fantomas.Core.Tests/AppTests.fs index 0d89a46c35..9f9bdc0b77 100644 --- a/src/Fantomas.Core.Tests/AppTests.fs +++ b/src/Fantomas.Core.Tests/AppTests.fs @@ -1145,3 +1145,80 @@ let ``function invocation wrapped in parentheses, 2382`` () = 42 ) """ + +[] +let ``extra indent in multiline application`` () = + formatSourceString + false + """ +((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b c)) +""" + { config with + IndentSize = 2 + MaxLineLength = 0 } + |> prepend newline + |> should + equal + """ +((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + b + c)) +""" + +[] +let ``multiline application wrapped in parentheses that equal the indent_size, 2943`` () = + formatSourceString + false + """ +((Combo { e1 = "Making this long so it goes on a new line new line new line new line making it long so it goes on a new line new line" })) +""" + { config with IndentSize = 2 } + |> prepend newline + |> should + equal + """ +((Combo + { e1 = + "Making this long so it goes on a new line new line new line new line making it long so it goes on a new line new line" })) +""" + +[] +let ``atCurrentColumn multiline application does not need addition indent`` () = + formatSourceString + false + """ +foo { + bar in ((((aaaaaaaaaaaa b c)))) +} +""" + { config with MaxLineLength = 0 } + |> prepend newline + |> should + equal + """ +foo { + bar in ((((aaaaaaaaaaaa + b + c)))) +} +""" + +[] +let ``don't indent function application arguments when function name is further indented`` () = + formatSourceString + false + """ +(((((((((((((((((((((( + f a b c +)))))))))))))))))))))) +""" + { config with MaxLineLength = 0 } + |> prepend newline + |> should + equal + """ +((((((((((((((((((((((f + a + b + c)))))))))))))))))))))) +""" diff --git a/src/Fantomas.Core.Tests/MultiLineLambdaClosingNewlineTests.fs b/src/Fantomas.Core.Tests/MultiLineLambdaClosingNewlineTests.fs index 6a0b7a6682..1a1c257ba8 100644 --- a/src/Fantomas.Core.Tests/MultiLineLambdaClosingNewlineTests.fs +++ b/src/Fantomas.Core.Tests/MultiLineLambdaClosingNewlineTests.fs @@ -1123,12 +1123,12 @@ let ``double pipe with application with two lambdas, 2682`` () = """ (someLongItemOne, someLongItemTwo) ||> Prefix.fnName - (fun delta echo -> delta, echo) - (fun (k: One * Two * Three) -> - // multiline - () - ) - lastArgument + (fun delta echo -> delta, echo) + (fun (k: One * Two * Three) -> + // multiline + () + ) + lastArgument (someLongItemOne, someLongItemTwo) |> Prefix.fnName diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index a5ff4a388c..f6d23f8d3b 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1206,9 +1206,22 @@ let genExpr (e: Expr) = genExpr node.FunctionExpr +> sep +> col sepSpace node.Arguments genExpr - let longExpression = - genExpr node.FunctionExpr - +> indentSepNlnUnindent (col sepNln node.Arguments genExpr) + let longExpression (ctx: Context) = + let startColumn = ctx.Column + + let ensureArgumentsAreNotAlignedWithFunctionName f (ctx: Context) = + let nextColumn = + Math.Max(ctx.WriterModel.AtColumn, ctx.WriterModel.Indent) + + ctx.Config.IndentSize + + if startColumn = nextColumn then + (indent +> indent +> sepNln +> f +> unindent +> unindent) ctx + else + indentSepNlnUnindent f ctx + + (genExpr node.FunctionExpr + +> ensureArgumentsAreNotAlignedWithFunctionName (col sepNln node.Arguments genExpr)) + ctx expressionFitsOnRestOfLine shortExpression longExpression ctx