Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct indentation of clause body if when condition is multiline. #1323

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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