Skip to content

Commit

Permalink
Further indent fields in update expressions. Fixes #817 (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 16, 2020
1 parent 030ec66 commit f94bbd1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,37 @@ type A =
ALongIdentifier : string
YetAnotherLongIdentifier : bool
}
"""
"""

[<Test>]
let ``indent update record fields far enough, 817`` () =
formatSourceString false "let expected = { ThisIsAThing.Empty with TheNewValue = 1 }" ({ config with IndentSpaceNum = 2 })
|> prepend newline
|> should equal """
let expected =
{ ThisIsAThing.Empty with
TheNewValue = 1
}
"""

[<Test>]
let ``indent update anonymous record fields far enough`` () =
formatSourceString false "let expected = {| ThisIsAThing.Empty with TheNewValue = 1 |}" ({ config with IndentSpaceNum = 2 })
|> prepend newline
|> should equal """
let expected =
{| ThisIsAThing.Empty with
TheNewValue = 1
|}
"""

[<Test>]
let ``update record with standard indent`` () =
formatSourceString false "let expected = { ThisIsAThing.Empty with TheNewValue = 1 }" config
|> prepend newline
|> should equal """
let expected =
{ ThisIsAThing.Empty with
TheNewValue = 1
}
"""
7 changes: 2 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ and genMultilineRecordInstanceAlignBrackets

| None, Some e ->
sepOpenS +> genExpr astContext e
+> (!- " with" +> indent +> sepNln +> fieldsExpr +> unindent +> sepNln +> sepCloseSFixed)
+> (!- " with" +> indent +> whenShortIndent indent +> sepNln +> fieldsExpr +> unindent +> whenShortIndent unindent +> sepNln +> sepCloseSFixed)

| _ ->
(sepOpenSFixed +> indent +> sepNln +> fieldsExpr +> unindent +> sepNln +> sepCloseSFixed)
Expand All @@ -1670,10 +1670,7 @@ and genMultilineAnonRecordAlignBrackets (isStruct: bool) fields copyInfo astCont
let fieldsExpr = col sepSemiNln fields (genAnonRecordFieldName astContext)

let copyExpr fieldsExpr e =
genExpr astContext e +>
ifElseCtx (futureNlnCheck fieldsExpr)
(!- " with" +> indent +> sepNln +> fieldsExpr +> unindent)
(!- " with " +> fieldsExpr)
genExpr astContext e +> (!- " with" +> indent +> whenShortIndent indent +> sepNln +> fieldsExpr +> whenShortIndent unindent +> unindent)

let genAnonRecord =
match copyInfo with
Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ let internal onlyIf cond f ctx =
let internal onlyIfNot cond f ctx =
if cond then ctx else f ctx

let internal whenShortIndent f ctx =
onlyIf (ctx.Config.IndentSpaceNum < 3) f ctx

/// Repeat application of a function n times
let internal rep n (f : Context -> Context) (ctx : Context) =
[1..n] |> List.fold (fun c _ -> f c) ctx
Expand Down

0 comments on commit f94bbd1

Please sign in to comment.