Skip to content

Commit

Permalink
Reuse alternative expression formatting with KeepIndentInBranch. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jul 5, 2021
1 parent 524e2bf commit f20d3ff
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 64 deletions.
157 changes: 141 additions & 16 deletions src/Fantomas.Tests/KeepIndentInBranchTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,15 +1525,16 @@ let x y =
"""
let x y =
if
not (
result.HasResultsFor(
[ "label"
"ipv4"
"macAddress"
"medium"
"manufacturer" ]
not
(
result.HasResultsFor(
[ "label"
"ipv4"
"macAddress"
"medium"
"manufacturer" ]
)
)
)
then
None
else
Expand Down Expand Up @@ -1572,15 +1573,16 @@ let x =
"""
let x =
if
not (
result.HasResultsFor(
[ "label"
"ipv4"
"macAddress"
"medium"
"manufacturer" ]
not
(
result.HasResultsFor(
[ "label"
"ipv4"
"macAddress"
"medium"
"manufacturer" ]
)
)
)
then
None
else
Expand Down Expand Up @@ -1920,3 +1922,126 @@ let nextModel, objectsRemoved =
state
[]
"""

[<Test>]
let ``long multiline if expression, 1812`` () =
formatSourceString
false
"""
module Foo =
let bar =
if output.Exists && not (output.EnumerateFiles() |> Seq.isEmpty && onlyContainsFoobar output) then
Error (FooBarBazError.ErrorCase output)
else
let blah =
let x = y
None
{
Hi = blah
}
|> Ok
"""
{ config with
MaxLineLength = 100
SpaceBeforeUppercaseInvocation = true
SpaceBeforeClassConstructor = true
SpaceBeforeMember = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
IndentOnTryWith = true
MultilineBlockBracketsOnSameColumn = true
NewlineBetweenTypeDefinitionAndMembers = true
AlignFunctionSignatureToIndentation = true
AlternativeLongMemberDefinitions = true
MultiLineLambdaClosingNewline = true
DisableElmishSyntax = true
KeepIndentInBranch = true }
|> prepend newline
|> should
equal
"""
module Foo =
let bar =
if
output.Exists
&& not
(
output.EnumerateFiles () |> Seq.isEmpty
&& onlyContainsFoobar output
)
then
Error (FooBarBazError.ErrorCase output)
else
let blah =
let x = y
None
{ Hi = blah } |> Ok
"""

[<Test>]
let ``long multiline match expression`` () =
formatSourceString
false
"""
module Foo =
let bar =
match output.Exists && not (output.EnumerateFiles() |> Seq.isEmpty && onlyContainsFoobar output) with
| true ->
Error (FooBarBazError.ErrorCase output)
| false ->
let blah =
let x = y
None
{
Hi = blah
}
|> Ok
"""
{ config with
MaxLineLength = 100
SpaceBeforeUppercaseInvocation = true
SpaceBeforeClassConstructor = true
SpaceBeforeMember = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
IndentOnTryWith = true
MultilineBlockBracketsOnSameColumn = true
NewlineBetweenTypeDefinitionAndMembers = true
AlignFunctionSignatureToIndentation = true
AlternativeLongMemberDefinitions = true
MultiLineLambdaClosingNewline = true
DisableElmishSyntax = true
KeepIndentInBranch = true }
|> prepend newline
|> should
equal
"""
module Foo =
let bar =
match
output.Exists
&& not
(
output.EnumerateFiles () |> Seq.isEmpty
&& onlyContainsFoobar output
)
with
| true -> Error (FooBarBazError.ErrorCase output)
| false ->
let blah =
let x = y
None
{ Hi = blah } |> Ok
"""
25 changes: 25 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1913,3 +1913,28 @@ match
with
| _ -> ()
"""

[<Test>]
let ``multiline infix expression in match bang`` () =
formatSourceString
false
"""
match! structuralTypes |> List.tryFind (fst >> checkIfFieldTypeSupportsComparison tycon >> not) with
| _ -> ()
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
match!
structuralTypes
|> List.tryFind
(
fst
>> checkIfFieldTypeSupportsComparison tycon
>> not
)
with
| _ -> ()
"""
76 changes: 28 additions & 48 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,23 +1513,9 @@ and genExpr astContext synExpr ctx =
!- "match "
+> expressionFitsOnRestOfLine
(genExpr astContext e
+> tokN withRange WITH (!- " with"))
+> genWithAfterMatch withRange)
(genExprInIfOrMatch astContext e
+> tokN
withRange
WITH
(fun ctx ->
let hasContentOnLastLine =
List.tryHead ctx.WriterModel.Lines
|> Option.map String.isNotNullOrWhitespace
|> Option.defaultValue false

if hasContentOnLastLine then
// add a space if there is no newline right after the expression
(!- " with") ctx
else
// add the indentation in spaces if there is no content on the current line
(rep ctx.Config.IndentSize (!- " ") +> !- "with") ctx))
+> genWithAfterMatch withRange)

atCurrentColumn (genMatchExpr +> sepNln +> genClauses astContext cs)
| MatchBang (e, cs) ->
Expand All @@ -1540,31 +1526,9 @@ and genExpr astContext synExpr ctx =
!- "match! "
+> expressionFitsOnRestOfLine
(genExpr astContext e
+> tokN withRange WITH (!- " with"))
(fun ctx ->
match e with
| AppParenArg app ->
(indent
+> sepNln
+> genAlternativeAppWithParenthesis app astContext
+> sepNln
+> tokN withRange WITH (!- "with")
+> unindent)
ctx
| Match _
| MatchBang _ ->
(indent
+> sepNln
+> genExpr astContext e
+> sepNln
+> tokN withRange WITH (!- "with")
+> unindent)
ctx
| _ ->
atCurrentColumnIndent
(genExpr astContext e
+> tokN withRange WITH (!- " with"))
ctx)
+> genWithAfterMatch withRange)
(genExprInIfOrMatch astContext e
+> genWithAfterMatch withRange)

atCurrentColumn (genMatchExpr +> sepNln +> genClauses astContext cs)
| TraitCall (tps, msg, e) ->
Expand Down Expand Up @@ -3273,6 +3237,23 @@ and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context =

expressionFitsOnRestOfLine short long ctx

and genWithAfterMatch (withRange: Range) =
tokN
withRange
WITH
(fun ctx ->
let hasContentOnLastLine =
List.tryHead ctx.WriterModel.Lines
|> Option.map String.isNotNullOrWhitespace
|> Option.defaultValue false

if hasContentOnLastLine then
// add a space if there is no newline right after the expression
(!- " with") ctx
else
// add the indentation in spaces if there is no content on the current line
(rep ctx.Config.IndentSize (!- " ") +> !- "with") ctx)

and genAlternativeAppWithParenthesis app astContext =
match app with
| Choice1Of2 t -> genAlternativeAppWithTupledArgument t astContext
Expand Down Expand Up @@ -5317,11 +5298,14 @@ and genKeepIndentMatch
(matchRange: Range)
(triviaType: FsAstType)
: Context -> Context =
let withRange (ctx: Context) =
ctx.MkRange e.Range.Start (List.head clauses).Range.Start

let lastClauseIndex = clauses.Length - 1

ifElse (triviaType = SynExpr_MatchBang) !- "match! " !- "match "
+> genExpr astContext e
-- " with"
+> genExprInIfOrMatch astContext e
+> (fun ctx -> genWithAfterMatch (withRange ctx) ctx)
+> sepNln
+> coli
sepNln
Expand Down Expand Up @@ -5365,11 +5349,7 @@ and genKeepIdentIf

let long =
ifElse (idx = 0) (!- "if ") (!- "elif ")
+> indent
+> sepNln
+> genExpr astContext ifExpr
+> unindent
+> sepNln
+> genExprInIfOrMatch astContext ifExpr
+> !- "then"

expressionFitsOnRestOfLine short long
Expand Down

0 comments on commit f20d3ff

Please sign in to comment.