From 5e754beed38a8f3c8e2b25a4959dd359e407a910 Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 27 Aug 2026 16:32:47 +0200 Subject: [PATCH 1/2] Initial implementation --- .../AlignedMultilineBracketStyleTests.fs | 9 +- .../CrampedMultilineBracketStyleTests.fs | 7 +- .../Fantomas.Core.Tests.fsproj | 1 + src/Fantomas.Core.Tests/IfThenElseTests.fs | 4 +- .../NoBreakInfixOperatorTests.fs | 486 ++++++++++++++++++ src/Fantomas.Core.Tests/OperatorTests.fs | 82 +-- src/Fantomas.Core.Tests/QuotationTests.fs | 28 +- src/Fantomas.Core/CodePrinter.fs | 76 +-- src/Fantomas.Core/Context.fsi | 5 + 9 files changed, 613 insertions(+), 85 deletions(-) create mode 100644 src/Fantomas.Core.Tests/NoBreakInfixOperatorTests.fs diff --git a/src/Fantomas.Core.Tests/AlignedMultilineBracketStyleTests.fs b/src/Fantomas.Core.Tests/AlignedMultilineBracketStyleTests.fs index 16b24caeba..b404fe62a0 100644 --- a/src/Fantomas.Core.Tests/AlignedMultilineBracketStyleTests.fs +++ b/src/Fantomas.Core.Tests/AlignedMultilineBracketStyleTests.fs @@ -1499,10 +1499,11 @@ let compareThings (first: Thing) (second: Thing) = equal """ let compareThings (first : Thing) (second : Thing) = - first = { second with - Foo = first.Foo - Bar = first.Bar - } + first = + { second with + Foo = first.Foo + Bar = first.Bar + } """ // `Aligned` copy-and-update expression keeps label on first line to match G-Research style guide. diff --git a/src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs b/src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs index cc59e53ba1..ea52199f6a 100644 --- a/src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs +++ b/src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs @@ -2145,9 +2145,10 @@ let compareThings (first: Thing) (second: Thing) = equal """ let compareThings (first: Thing) (second: Thing) = - first = { second with - Foo = first.Foo - Bar = first.Bar } + first = + { second with + Foo = first.Foo + Bar = first.Bar } """ [] diff --git a/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj b/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj index 2cdef6acea..0b1d88b39c 100644 --- a/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj +++ b/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj @@ -16,6 +16,7 @@ + diff --git a/src/Fantomas.Core.Tests/IfThenElseTests.fs b/src/Fantomas.Core.Tests/IfThenElseTests.fs index 00abded5b1..a22dca02dc 100644 --- a/src/Fantomas.Core.Tests/IfThenElseTests.fs +++ b/src/Fantomas.Core.Tests/IfThenElseTests.fs @@ -2022,7 +2022,9 @@ if UriComponents.Host ||| UriComponents.Path, UriFormat.UriEscaped, StringComparison.CurrentCulture - ) = 0 + ) + = + 0 then () else diff --git a/src/Fantomas.Core.Tests/NoBreakInfixOperatorTests.fs b/src/Fantomas.Core.Tests/NoBreakInfixOperatorTests.fs new file mode 100644 index 0000000000..a97b790575 --- /dev/null +++ b/src/Fantomas.Core.Tests/NoBreakInfixOperatorTests.fs @@ -0,0 +1,486 @@ +module Fantomas.Core.Tests.NoBreakInfixOperatorTests + +open NUnit.Framework +open FsUnit +open Fantomas.Core.Tests.TestHelpers +open Fantomas.Core + +// `=`, `>`, `<`, `%` and `%%` cannot start a line at the column of the left-hand side: there the +// parser reads `=`, `>` and `<` as the `=` of a binding, and reads `%` and `%%` inside a quotation +// as a splice. So the operator either ends the line the left-hand side is on, or takes a line of +// its own one level in. Working down from the shortest case: +// +// 1. everything fits lhs op rhs +// +// 2. the rhs does not fit lhs op +// rhs +// +// 3. the lhs spans lines lhs +// op +// rhs +// +// The third is what both style guides ask for in a long function signature, where the parameters +// are indented one level and the `=` takes a line of its own before the body. Under +// `fsharp_multiline_bracket_style = stroustrup` a right-hand side that opens a bracket keeps +// hugging the operator, which outranks the third case. + +[] +let ``= keeps the whole expression on one line when it fits`` () = + formatSourceString + """ +let v = xs = ys +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = xs = ys +""" + +[] +let ``= moves the right-hand side down one level when it does not fit`` () = + formatSourceString + """ +let v = xs = [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeeeee"; "ffffffffff" ] +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs = + [ + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" + ] +""" + +[] +let ``= takes a line of its own when the left-hand side is multiline`` () = + formatSourceString + """ +let v = someFunction aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccc = 0 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + someFunction + aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccccc + = + 0 +""" + +[] +let ``> keeps the whole expression on one line when it fits`` () = + formatSourceString + """ +let v = xs > ys +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = xs > ys +""" + +[] +let ``> moves the right-hand side down one level when it does not fit`` () = + formatSourceString + """ +let v = xs > [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeeeee"; "ffffffffff" ] +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs > + [ + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" + ] +""" + +[] +let ``> takes a line of its own when the left-hand side is multiline`` () = + formatSourceString + """ +let v = someFunction aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccc > 0 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + someFunction + aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccccc + > + 0 +""" + +[] +let ``< keeps the whole expression on one line when it fits`` () = + formatSourceString + """ +let v = xs < ys +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = xs < ys +""" + +[] +let ``< moves the right-hand side down one level when it does not fit`` () = + formatSourceString + """ +let v = xs < [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeeeee"; "ffffffffff" ] +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs < + [ + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" + ] +""" + +[] +let ``< takes a line of its own when the left-hand side is multiline`` () = + formatSourceString + """ +let v = someFunction aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccc < 0 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + someFunction + aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccccc + < + 0 +""" + +[] +let ``% keeps the whole expression on one line when it fits`` () = + formatSourceString + """ +let v = xs % ys +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = xs % ys +""" + +[] +let ``% moves the right-hand side down one level when it does not fit`` () = + formatSourceString + """ +let v = xs % [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeeeee"; "ffffffffff" ] +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs % + [ + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" + ] +""" + +[] +let ``% takes a line of its own when the left-hand side is multiline`` () = + formatSourceString + """ +let v = someFunction aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccc % 0 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + someFunction + aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccccc + % + 0 +""" + +[] +let ``%% keeps the whole expression on one line when it fits`` () = + formatSourceString + """ +let v = xs %% ys +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = xs %% ys +""" + +[] +let ``%% moves the right-hand side down one level when it does not fit`` () = + formatSourceString + """ +let v = xs %% [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeeeee"; "ffffffffff" ] +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs %% + [ + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" + ] +""" + +[] +let ``%% takes a line of its own when the left-hand side is multiline`` () = + formatSourceString + """ +let v = someFunction aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccc %% 0 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + someFunction + aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccccc + %% + 0 +""" + +[] +let ``a right-hand side that opens a bracket starts at the same column as any other`` () = + formatSourceString + """ +let v = xs = { XXXX = 1; YYYY = 2; ZZZZ = 3; WWWW = 4; VVVV = 5; TTTT = "sevenseven" } +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs = + { + XXXX = 1 + YYYY = 2 + ZZZZ = 3 + WWWW = 4 + VVVV = 5 + TTTT = "sevenseven" + } +""" + +[] +let ``a match on the right-hand side does not follow the left-hand side`` () = + formatSourceString + """ +let v = xs = match yyyyyyyyyyyyyyyyy with | Aaaaaaaaaaaaaaaaaaaa -> 1 | Bbbbbbbbbb -> 2 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs = + match yyyyyyyyyyyyyyyyy with + | Aaaaaaaaaaaaaaaaaaaa -> 1 + | Bbbbbbbbbb -> 2 +""" + +[] +let ``the name on the left does not decide where the right-hand side starts`` () = + formatSourceString + """ +let v = xs = match yyyyyyyyyyyyyyyyy with | Aaaaaaaaaaaaaaaaaaaa -> 1 | Bbbbbbbbbb -> 2 +let w = xsy = match yyyyyyyyyyyyyyyyy with | Aaaaaaaaaaaaaaaaaaaa -> 1 | Bbbbbbbbbb -> 2 +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs = + match yyyyyyyyyyyyyyyyy with + | Aaaaaaaaaaaaaaaaaaaa -> 1 + | Bbbbbbbbbb -> 2 + +let w = + xsy = + match yyyyyyyyyyyyyyyyy with + | Aaaaaaaaaaaaaaaaaaaa -> 1 + | Bbbbbbbbbb -> 2 +""" + +[] +let ``both sides multiline puts the operator between them`` () = + formatSourceString + """ +let v = xs.ReplaceEverythingEverywhere(11111, 22222).ReplaceEverythingEverywhere(33333, 44444).TrimEnd() = expected.ReplaceEverythingEverywhere(55555, 66666).ReplaceEverythingEverywhere(77777, 88888).TrimEnd() +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs + .ReplaceEverythingEverywhere(11111, 22222) + .ReplaceEverythingEverywhere(33333, 44444) + .TrimEnd() + = + expected + .ReplaceEverythingEverywhere(55555, 66666) + .ReplaceEverythingEverywhere(77777, 88888) + .TrimEnd() +""" + +[] +let ``a comment in front of the right-hand side lands below the operator`` () = + formatSourceString + """ +let v = + xs = + // a comment + [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeee" ] +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let v = + xs = + // a comment + [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeee" ] +""" + +[] +let ``a bracket keeps hugging the operator under stroustrup`` () = + formatSourceString + """ +let v = xs = [ "aaaaaaaaaaaaaaaaaa"; "bbbbbbbbbbbbbbbbbb"; "cccccccccccccccccc"; "dddddddddddddddddd" ] +""" + { config with + MaxLineLength = 80 + MultilineBracketStyle = Stroustrup + } + |> prepend newline + |> should + equal + """ +let v = + xs = [ + "aaaaaaaaaaaaaaaaaa" + "bbbbbbbbbbbbbbbbbb" + "cccccccccccccccccc" + "dddddddddddddddddd" + ] +""" + +[] +let ``the stroustrup hug outranks the operator taking its own line`` () = + formatSourceString + """ +let long = xs.ReplaceEverythingEverywhere(11111, 22222).ReplaceEverythingEverywhere(33333, 44444).TrimEnd() = [ "aaaaaaaaaaaaaaaaaaaaaa"; "bbbbbbbbbbbbbbbbbbbbbb"; "cccccccccccccccccccccc"; "dddddddddddddddddddddd" ] +""" + { config with + MaxLineLength = 80 + MultilineBracketStyle = Stroustrup + } + |> prepend newline + |> should + equal + """ +let long = + xs + .ReplaceEverythingEverywhere(11111, 22222) + .ReplaceEverythingEverywhere(33333, 44444) + .TrimEnd() = [ + "aaaaaaaaaaaaaaaaaaaaaa" + "bbbbbbbbbbbbbbbbbbbbbb" + "cccccccccccccccccccccc" + "dddddddddddddddddddddd" + ] +""" + +[] +let ``an infix percent inside a quotation does not become a splice`` () = + formatSourceString + """ +let q = <@ someLongThingHereOk aaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb % otherThingLong @> +""" + { config with MaxLineLength = 80 } + |> prepend newline + |> should + equal + """ +let q = + <@ + someLongThingHereOk aaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb % + otherThingLong + @> +""" diff --git a/src/Fantomas.Core.Tests/OperatorTests.fs b/src/Fantomas.Core.Tests/OperatorTests.fs index e3060c3c09..0697a9848c 100644 --- a/src/Fantomas.Core.Tests/OperatorTests.fs +++ b/src/Fantomas.Core.Tests/OperatorTests.fs @@ -499,7 +499,11 @@ let ``modulo operator on same line, 780`` () = """ let hasUnEvenAmount regex line = (Regex.Matches(line, regex).Count - - Regex.Matches(line, "\\\\" + regex).Count) % 2 = 1 + - Regex.Matches(line, "\\\\" + regex).Count) + % + 2 + = + 1 """ [] @@ -522,7 +526,9 @@ let a = (if aVeryLongVariableNameThatForceLineBreaking = 0 then 1 else - -1) %% 4 + -1) + %% + 4 """ [] @@ -1512,12 +1518,13 @@ let v = xs = [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeee equal """ let v = - xs = [ "aaaaaaaaaa" - "bbbbbbbbbb" - "cccccccccc" - "dddddddddd" - "eeeeeeeeee" - "ffffffffff" ] + xs = + [ "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" ] """ [] @@ -1535,14 +1542,15 @@ let v = xs = [ "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeeee equal """ let v = - xs = [ - "aaaaaaaaaa" - "bbbbbbbbbb" - "cccccccccc" - "dddddddddd" - "eeeeeeeeee" - "ffffffffff" - ] + xs = + [ + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffffffff" + ] """ [] @@ -1585,12 +1593,13 @@ let v = xs = [| "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeee equal """ let v = - xs = [| "aaaaaaaaaa" - "bbbbbbbbbb" - "cccccccccc" - "dddddddddd" - "eeeeeeeeee" - "ffffff" |] + xs = + [| "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffff" |] """ [] @@ -1608,14 +1617,15 @@ let v = xs = [| "aaaaaaaaaa"; "bbbbbbbbbb"; "cccccccccc"; "dddddddddd"; "eeeeeee equal """ let v = - xs = [| - "aaaaaaaaaa" - "bbbbbbbbbb" - "cccccccccc" - "dddddddddd" - "eeeeeeeeee" - "ffffff" - |] + xs = + [| + "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "ffffff" + |] """ [] @@ -1658,10 +1668,11 @@ let a = b = [ 1; 2 ] equal """ let a = - b = [ - 1 - 2 - ] + b = + [ + 1 + 2 + ] """ [] @@ -1676,7 +1687,8 @@ let v = xs = expected.Replace(1, 2).Replace(3, 4).Replace(5, 6).Replace(7, 8).Re equal """ let v = - xs = expected + xs = + expected .Replace(1, 2) .Replace(3, 4) .Replace(5, 6) diff --git a/src/Fantomas.Core.Tests/QuotationTests.fs b/src/Fantomas.Core.Tests/QuotationTests.fs index 8392945d76..85510b1c58 100644 --- a/src/Fantomas.Core.Tests/QuotationTests.fs +++ b/src/Fantomas.Core.Tests/QuotationTests.fs @@ -122,11 +122,13 @@ test .Replace("\u00FF\u00FF", "\u00FF") .Replace("8.12", "8.13") // CRAP score rounding .Replace("4.12", "4.13") // CRAP score rounding - .Trim([| '\u00FF' |]) = expected - .Replace('\r', '\u00FF') - .Replace('\n', '\u00FF') - .Replace("\u00FF\u00FF", "\u00FF") - .Trim([| '\u00FF' |]) + .Trim([| '\u00FF' |]) + = + expected + .Replace('\r', '\u00FF') + .Replace('\n', '\u00FF') + .Replace("\u00FF\u00FF", "\u00FF") + .Trim([| '\u00FF' |]) @> """ @@ -166,13 +168,15 @@ test RecordingMethodRef = { Visit = null Push = null - Pop = null } } = { state' with - ModuleId = def.MainModule.Mvid.ToString() - RecordingMethod = visit - RecordingMethodRef = - { Visit = null - Push = null - Pop = null } } + Pop = null } } + = + { state' with + ModuleId = def.MainModule.Mvid.ToString() + RecordingMethod = visit + RecordingMethodRef = + { Visit = null + Push = null + Pop = null } } @> """ diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 0aadb1b3b3..eb8cd69abb 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1737,38 +1737,54 @@ let genExpr (e: Expr) = // always use the multiline layout to avoid any change in semantics. if isOpenEndedExpression node.LeftHandSide then genMultilineInfixExpr node |> genNode node - // No-break operators (=, >, <, %) keep the operator on the same line as the LHS. + // No-break operators (=, >, <, %, %%) cannot start a line at the column of the left-hand + // side: there the parser reads `=`, `>` and `<` as the `=` of a binding, and reads `%` and + // `%%` inside a quotation as a splice. So the operator either ends the line the left-hand + // side is on, or takes a line of its own one level in. elif isNoBreakInfixOp then - // The right-hand side is indented so that a comment between it and the operator lands - // below the operator rather than at the column of the left-hand side, and so that the - // lines under an application or a chain are not read as a continuation of the - // left-hand side. See #2944. - // A list or an array that opens on the line of the operator needs neither: the bracket - // says where the right-hand side begins and indents its items from the left-hand side - // itself, so indenting it again pushes the items and the closing bracket a level too - // far. See #3428. Other bracketed right-hand sides have the same problem and are left - // alone here on purpose: where a no-break operator should put what follows it is a - // wider question than this fix. - let genRightHandSide (ctx: Context) : Context = - let opensBracketOnThisLine: bool = - (match node.RightHandSide with - | Expr.ArrayOrList _ -> true - | _ -> false) - && not (hasWriteBeforeNewlineContent ctx) - && not (Expr.Node node.RightHandSide).HasContentBefore - - (onlyIfNot opensBracketOnThisLine indent - +> sepNlnWhenWriteBeforeNewlineNotEmpty - +> sepSpace - +> genRhsExpr node.RightHandSide - +> onlyIfNot opensBracketOnThisLine unindent) - ctx + let genLeftHandSide: Context -> Context = genExpr node.LeftHandSide + let genOperator: Context -> Context = genSingleTextNode node.Operator + + let genLongNoBreakInfixExpr (ctx: Context) : Context = + // Someone who set `stroustrup` asked for a bracket to open on the line of what + // precedes it, and that outranks the operator taking a line of its own. A comment + // in front of the right-hand side forces a break, so the bracket cannot open there + // after all. See #3428. + let hugsOperator: bool = + isStroustrupStyleExpr ctx.Config node.RightHandSide + && canSafelyUseStroustrup (Expr.Node node.RightHandSide) ctx + + // When the left-hand side spans several lines, the operator has no line to end that + // is not also the last line of the left-hand side, and the right-hand side would + // start at the column that line ends at. The operator takes a line between the two + // instead, which is what both style guides ask for in a long function signature. + let operatorTakesItsOwnLine: bool = + not hugsOperator && futureNlnCheck genLeftHandSide ctx + + let layout: Context -> Context = + if operatorTakesItsOwnLine then + genLeftHandSide + +> indent + +> sepNln + +> genOperator + +> sepNln + +> genRhsExpr node.RightHandSide + +> unindent + else + // The right-hand side is indented when it moves down, so that a comment + // between it and the operator lands below the operator rather than at the + // column of the left-hand side, and so that the lines under an application + // or a chain are not read as a continuation of the left-hand side. + // See #2944. + genLeftHandSide + +> sepSpace + +> genOperator + +> sepNlnWhenWriteBeforeNewlineNotEmpty + +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup + genRhsExpr + node.RightHandSide - let genLongNoBreakInfixExpr = - genExpr node.LeftHandSide - +> sepSpace - +> genSingleTextNode node.Operator - +> genRightHandSide + layout ctx fun ctx -> genNode diff --git a/src/Fantomas.Core/Context.fsi b/src/Fantomas.Core/Context.fsi index cc2371bd36..48397bd9f9 100644 --- a/src/Fantomas.Core/Context.fsi +++ b/src/Fantomas.Core/Context.fsi @@ -353,6 +353,11 @@ val addParenIfAutoNln: expr: Expr -> f: (Expr -> Context -> Context) -> (Context // ============================================================================= val isStroustrupStyleExpr: config: FormatConfig -> e: Expr -> bool + +/// A bracket can only open on the line of what precedes it when nothing else wants that line first, +/// which a comment in front of the node does. +val canSafelyUseStroustrup: node: Node -> ctx: Context -> bool + val ifAlignOrStroustrupBrackets: f: (Context -> Context) -> g: (Context -> Context) -> (Context -> Context) val sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup: From 608760cb8fbb9ae7c2bfc29668cd581c122ccf58 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Tue, 1 Sep 2026 13:24:25 +0200 Subject: [PATCH 2/2] Call it v8 beta 1 --- .claude/settings.json | 15 +++++++++++++++ CHANGELOG.md | 6 ++++++ src/Fantomas.Core.Tests/OperatorTests.fs | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .claude/settings.json diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 0000000000..087f7eb00e --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,15 @@ +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Edit|Write", + "hooks": [ + { + "type": "command", + "command": "file=$(jq -r '.tool_input.file_path'); case \"$file\" in *.fs|*.fsx|*.fsi) dotnet fantomas \"$file\" >/dev/null 2>&1 ;; esac; exit 0" + } + ] + } + ] + } +} diff --git a/CHANGELOG.md b/CHANGELOG.md index 30eda117ff..2645b669e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [8.0.0-beta-001] - 2026-09-01 + +### Changed + +- `=`, `>`, `<`, `%` and `%%` cannot start a line at the column of their left-hand side: there the parser reads `=`, `>` and `<` as the `=` of a binding, and reads `%` and `%%` inside a quotation as a splice. Fantomas already kept them off that column, but did so with the layout that keeps the right-hand side beside the operator whenever it is short enough to fit, which needs the page width and a column count to predict and moves a `0` around when an unrelated part of the line changes length. It now follows the rule proposed in [fsharp/fslang-design#836](https://github.com/fsharp/fslang-design/issues/836): the expression fits on one line, or it does not and the operator stays with the left-hand side while the right-hand side moves one level in, or the left-hand side itself spans several lines, in which case the operator and the right-hand side each take a line of their own. `fsharp_multiline_bracket_style = stroustrup` still overrides this for a right-hand side that opens a bracket, which keeps hugging the operator. This is the first beta of `8.0.0`: the style guide for this is not settled yet, so give it a try and report back on [the design issue](https://github.com/fsharp/fslang-design/issues/836) if something reads worse than what it replaces. [#3434](https://github.com/fsprojects/fantomas/pull/3434) + ## [8.0.0-alpha-026] - 2026-08-29 ### Changed diff --git a/src/Fantomas.Core.Tests/OperatorTests.fs b/src/Fantomas.Core.Tests/OperatorTests.fs index 0697a9848c..dcd077a7fa 100644 --- a/src/Fantomas.Core.Tests/OperatorTests.fs +++ b/src/Fantomas.Core.Tests/OperatorTests.fs @@ -486,7 +486,7 @@ module Types = " [] -let ``modulo operator on same line, 780`` () = +let ``modulo operator, multiline left-hand side, 780`` () = formatSourceString """let hasUnEvenAmount regex line = (Regex.Matches(line, regex).Count - Regex.Matches(line, "\\\\" + regex).Count) % 2 = 1 """