Skip to content

Commit

Permalink
Also accept alignment from if keyword in genKeepIdentIfThenElse. (#2975)
Browse files Browse the repository at this point in the history
* Also accept alignment from if keyword in genKeepIdentIfThenElse.

* Add additional test
  • Loading branch information
nojaf committed Nov 3, 2023
1 parent 5a8458e commit 0aabadd
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Changed
* ExperimentalKeepIndentInBranch should fire when first branch was all on one line. [#2973](https://github.com/fsprojects/fantomas/issues/2973)

## 6.2.3 - 2023-11-2

### Fixed
Expand Down
80 changes: 80 additions & 0 deletions src/Fantomas.Core.Tests/KeepIndentInBranchTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2183,3 +2183,83 @@ module Foo =
failwith ""
"""

[<Test>]
let ``if keyword and elseBranch align, 2973`` () =
formatSourceString
false
"""
module Program =
let main _ =
if false then 1 else
printfn "hi!"
0
"""
config
|> prepend newline
|> should
equal
"""
module Program =
let main _ =
if false then
1
else
printfn "hi!"
0
"""

[<Test>]
let ``elif keyword and elseBranch align`` () =
formatSourceString
false
"""
if false then
1
elif false then 2 else
printfn "hi!"
0
"""
config
|> prepend newline
|> should
equal
"""
if false then
1
elif false then
2
else
printfn "hi!"
0
"""

[<Test>]
let ``elseBranch aligns with if keyword in computation expression`` () =
formatSourceString
false
"""
async {
if not (proc.Start ()) then return Error "failed to start" else
use stdout = proc.StandardOutput
let! ct = Async.CancellationToken
return! Async.AwaitTask (stdout.ReadToEndAsync ct)
}
"""
config
|> prepend newline
|> should
equal
"""
async {
if not (proc.Start()) then
return Error "failed to start"
else
use stdout = proc.StandardOutput
let! ct = Async.CancellationToken
return! Async.AwaitTask(stdout.ReadToEndAsync ct)
}
"""
25 changes: 21 additions & 4 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ let genExpr (e: Expr) =
indentSepNlnUnindent (genExpr node.ThenExpr)
+> sepNln
+> genSingleTextNode node.Else
+> genKeepIdent node.Else node.ElseExpr
+> genKeepIdentIfThenElse node.If.Node node.Else node.ElseExpr

// Check if the `if expr then` is already multiline or cross the max_line_length.
let isMultiline =
Expand Down Expand Up @@ -1378,7 +1378,12 @@ let genExpr (e: Expr) =
+> indentSepNlnUnindent (genExpr node.ThenExpr)
|> genNode node)
+> optSingle
(fun (elseNode, elseExpr) -> sepNln +> genSingleTextNode elseNode +> genKeepIdent elseNode elseExpr)
(fun (elseNode, elseExpr) ->
let genKeepIdent =
let branch = List.last node.Branches
genKeepIdentIfThenElse branch.If.Node elseNode elseExpr

sepNln +> genSingleTextNode elseNode +> genKeepIdent)
node.Else

ifElseCtx areAllShort shortExpr longExpr
Expand Down Expand Up @@ -2073,7 +2078,7 @@ let genClause (isLastItem: bool) (node: MatchClauseNode) =
| None -> Pattern.Node node.Pattern
| Some bar -> bar

genKeepIdent startNode node.BodyExpr
genKeepIdentMatchClause startNode node.BodyExpr

expressionFitsOnRestOfLine (sepSpace +> genExpr node.BodyExpr) long

Expand Down Expand Up @@ -2253,7 +2258,19 @@ let genExprInMultilineInfixExpr (e: Expr) =
| Expr.Record _ -> atCurrentColumnIndent (genExpr e)
| _ -> genExpr e

let genKeepIdent (startNode: Node) (e: Expr) ctx =
let genKeepIdentIfThenElse (ifKeyword: Node) (elseKeyword: Node) (e: Expr) ctx =
let exprNode = Expr.Node e

if
ctx.Config.ExperimentalKeepIndentInBranch
&& (elseKeyword.Range.StartColumn = exprNode.Range.StartColumn
|| ifKeyword.Range.StartColumn = exprNode.Range.StartColumn)
then
(sepNln +> sepNlnUnlessContentBefore exprNode +> genExpr e) ctx
else
indentSepNlnUnindent (genExpr e) ctx

let genKeepIdentMatchClause (startNode: Node) (e: Expr) ctx =
let exprNode = Expr.Node e

if
Expand Down

0 comments on commit 0aabadd

Please sign in to comment.