From f94bbd1510b956ca8e8a157ccc08472b8fef54f3 Mon Sep 17 00:00:00 2001
From: Florian Verdonck <florian.verdonck@outlook.com>
Date: Sat, 16 May 2020 15:28:41 +0200
Subject: [PATCH] Further indent fields in update expressions. Fixes #817
 (#832)

---
 ...ineBlockBracketsOnSameColumnRecordTests.fs | 35 ++++++++++++++++++-
 src/Fantomas/CodePrinter.fs                   |  7 ++--
 src/Fantomas/Context.fs                       |  3 ++
 3 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs b/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs
index 61aef4fdd8..30d56ff6a2 100644
--- a/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs
+++ b/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs
@@ -547,4 +547,37 @@ type A =
             ALongIdentifier : string
             YetAnotherLongIdentifier : bool
         }
-"""
\ No newline at end of file
+"""
+
+[<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
+    }
+"""
diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs
index 449be9cd5a..14d9f8749f 100644
--- a/src/Fantomas/CodePrinter.fs
+++ b/src/Fantomas/CodePrinter.fs
@@ -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)
@@ -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
diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs
index 34d473f3b9..4dc5147f30 100644
--- a/src/Fantomas/Context.fs
+++ b/src/Fantomas/Context.fs
@@ -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