Skip to content

Commit

Permalink
Produce valid code for multiline array patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Feb 10, 2023
1 parent d74c345 commit e1ce2e9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,8 +1621,9 @@ match entities with
equal
"""
match entities with
| [| { Key = "0031ff53-e59b-49e3-8e0f-53f72a3890d1"
Type = Elephant }
| [|
{ Key = "0031ff53-e59b-49e3-8e0f-53f72a3890d1"
Type = Elephant }
{ Key = "0031ff53-e59b-49e3-8e0f-53f72a3890d2"
Type = Elephant }
{ Key = "0031ff53-e59b-49e3-8e0f-53f72a3890d3"
Expand Down
13 changes: 5 additions & 8 deletions src/Fantomas.Core.Tests/MultilinePatterns.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

open NUnit.Framework
open FsUnit
open Fantomas.Core
open Fantomas.Core.Tests.TestHelper

let formatSourceString isFsi input config =
let formatted = formatSourceString isFsi input config
printfn "%s" formatted
formatted

[<Test>]
let ``long ident tuple pattern`` () =
formatSourceString
Expand Down Expand Up @@ -490,7 +486,8 @@ match x with
equal
"""
match x with
| [| Y(itemOne =
| [|
Y(itemOne =
OhSomeActivePatternThing(
a, b
)
Expand Down Expand Up @@ -521,7 +518,7 @@ match x with
"""
{ config with
MaxLineLength = 30
MultilineBlockBracketsOnSameColumn = true }
MultilineBracketStyle = Aligned }
|> prepend newline
|> should
equal
Expand Down Expand Up @@ -595,7 +592,7 @@ match x with
"""
{ config with
MaxLineLength = 30
MultilineBlockBracketsOnSameColumn = true }
MultilineBracketStyle = Aligned }
|> prepend newline
|> should
equal
Expand Down
7 changes: 4 additions & 3 deletions src/Fantomas.Core.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,9 +1530,10 @@ let args =
"""
let args =
match args with
| [| LongPatIndentifierOne
| LongPatIndentifierTwo
| LongPatIndentifierThree |] -> args
| [|
LongPatIndentifierOne
| LongPatIndentifierTwo
| LongPatIndentifierThree |] -> args
| _ -> failwith "meh"
"""

Expand Down
30 changes: 22 additions & 8 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,15 +2510,29 @@ let genPat (p: Pattern) =
+> addSpaceIfSpaceAroundDelimiter
+> genSingleTextNode node.CloseToken

// Note that we deliberately are not take the setting fsharp_multiline_block_brackets_on_same_column into account.
let multilineAlignBrackets =
genSingleTextNode node.OpenToken
+> indentSepNlnUnindent genPats
+> sepNln
+> genSingleTextNode node.CloseToken
let multiline =
let cramped =
if node.OpenToken.Text.Length = 1 then
short
else
// pattern is an array, which can lead to some offset problems.
// It is better to play it safe and indent the patterns on the next line.
genSingleTextNode node.OpenToken
+> addSpaceIfSpaceAroundDelimiter
+> indentSepNlnUnindent genPats
+> addSpaceIfSpaceAroundDelimiter
+> genSingleTextNode node.CloseToken

// Note that we deliberately are not take the setting fsharp_multiline_block_brackets_on_same_column into account.
let multilineAlignBrackets =
genSingleTextNode node.OpenToken
+> indentSepNlnUnindent genPats
+> sepNln
+> genSingleTextNode node.CloseToken

ifAlignOrStroustrupBrackets multilineAlignBrackets cramped

let nonEmpty =
expressionFitsOnRestOfLine short (ifAlignBrackets multilineAlignBrackets short)
let nonEmpty = expressionFitsOnRestOfLine short multiline

ifElse node.Patterns.IsEmpty emptyPattern nonEmpty |> genNode node
| Pattern.Record node ->
Expand Down

0 comments on commit e1ce2e9

Please sign in to comment.