Skip to content

Commit

Permalink
Correct indentation of clause body if when condition is multiline. (#…
Browse files Browse the repository at this point in the history
…1323)

* Put multiline when condition on the next line. Fixes #1320.

* Correct indentation of clause body if when condition is multiline.
  • Loading branch information
nojaf committed Dec 22, 2020
1 parent 735344c commit 1719253
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
37 changes: 36 additions & 1 deletion src/Fantomas.Tests/PatternMatchingTests.fs
Expand Up @@ -1089,7 +1089,42 @@ module Foo =
// Ordinary functions or values
| false, _, _, name when
not (hasAttribute<RequireQualifiedAccessAttribute> v.ApparentEnclosingEntity.Attributes) ->
name + " " + parArgs
name + " " + parArgs
// Ordinary static members or things (?) that require fully qualified access
| _, _, _, name -> name + parArgs
"""

[<Test>]
let ``maintain indent if when condition is multiline`` () =
formatSourceString
false
"""
match foo with
| headToken :: rest when (isOperatorOrKeyword headToken && List.exists (fun k -> headToken.TokenInfo.TokenName = k) keywordTrivia) ->
let range =
getRangeBetween "keyword" headToken headToken
let info =
Trivia.Create(Keyword(headToken)) range
|> List.prependItem foundTrivia
getTriviaFromTokensThemSelves allTokens rest info
"""
config
|> prepend newline
|> should
equal
"""
match foo with
| headToken :: rest when
(isOperatorOrKeyword headToken
&& List.exists (fun k -> headToken.TokenInfo.TokenName = k) keywordTrivia) ->
let range =
getRangeBetween "keyword" headToken headToken
let info =
Trivia.Create(Keyword(headToken)) range
|> List.prependItem foundTrivia
getTriviaFromTokensThemSelves allTokens rest info
"""
18 changes: 9 additions & 9 deletions src/Fantomas/CodePrinter.fs
Expand Up @@ -4229,21 +4229,21 @@ and genClause astContext hasBar (Clause (p, e, eo)) =
let arrowRange =
mkRange "arrowRange" p.Range.End e.Range.Start

let body =
optPre
let body = clauseBody e

let astCtx =
{ astContext with
IsInsideMatchClausePattern = true }

let pat =
genPat astCtx p
+> optPre
(!- " when")
sepNone
eo
(fun e -> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))
+> sepArrow
+> leaveNodeTokenByName arrowRange RARROW
+> clauseBody e

let astCtx =
{ astContext with
IsInsideMatchClausePattern = true }

let pat = genPat astCtx p

genTriviaBeforeClausePipe p.Range
+> ifElse hasBar (sepBar +> atCurrentColumnWithPrepend pat body) (pat +> body)
Expand Down

0 comments on commit 1719253

Please sign in to comment.