Skip to content

Commit

Permalink
Further indent fields in update expressions. Fixes fsprojects#817
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 15, 2020
1 parent 622e2b0 commit b6896b5
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 @@ -516,4 +516,37 @@ type ShortExpressionInfo =
|| (currentColumn > maxPageWidth) // expression at current position is not going over the page width
member x.Foo() = ()
"""
"""

[<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 @@ -1622,7 +1622,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 @@ -1644,10 +1644,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 id 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 b6896b5

Please sign in to comment.