Skip to content

Commit

Permalink
Print comments after closing record brace. Fixes #1096. (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Sep 4, 2020
1 parent 789a842 commit 7412771
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,20 @@ let config =
#endif
}
"""

[<Test>]
let ``comment after closing brace in nested record`` () =
formatSourceString false """
let person =
{ Name = "James"
Address = { Street = "Bakerstreet"; Number = 42 } // end address
} // end person
""" config
|> prepend newline
|> should equal """
let person =
{
Name = "James"
Address = { Street = "Bakerstreet" ; Number = 42 } // end address
} // end person
"""
30 changes: 30 additions & 0 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,33 @@ type XX =

static member private foo: int = 30
"""

[<Test>]
let ``keep comments after closing brace of single line records, 1096`` () =
formatSourceString false """
let formatConfig = { PageWidth = 70; Indent = 8 } // The number of spaces
type FormatConfig = { PageWidth: int; Indent: int } // The number of spaces
()
""" config
|> prepend newline
|> should equal """
let formatConfig = { PageWidth = 70; Indent = 8 } // The number of spaces
type FormatConfig = { PageWidth: int; Indent: int } // The number of spaces
()
"""

[<Test>]
let ``comment after closing brace in nested record`` () =
formatSourceString false """
let person =
{ Name = "James"
Address = { Street = "Bakerstreet"; Number = 42 } // end address
} // end person
""" config
|> prepend newline
|> should equal """
let person =
{ Name = "James"
Address = { Street = "Bakerstreet"; Number = 42 } // end address
} // end person
"""
3 changes: 3 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ and genExpr astContext synExpr =
+> optSingle (fun e -> genExpr astContext e +> !- " with ") eo
+> col sepSemi xs (genRecordFieldName astContext)
+> sepCloseS
+> leaveNodeTokenByName synExpr.Range RBRACE

let multilineRecordExpr =
ifAlignBrackets
Expand Down Expand Up @@ -2642,6 +2643,7 @@ and genMultilineRecordInstance (inheritOpt: (SynType * SynExpr) option)
else
sepNone ({ ctx with RecordBraceStart = rest })
| [] -> sepNone ctx)
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepNone
+> enterNodeTokenByName synExpr.Range RBRACE
+> ifElseCtx lastWriteEventIsNewline sepCloseSFixed sepCloseS

Expand Down Expand Up @@ -3020,6 +3022,7 @@ and genTypeDefn astContext (TypeDef (ats, px, ao, tds, tcs, tdr, ms, s, preferPo
+> leaveLeftBrace tdr.Range
+> col sepSemi fs (genField astContext "")
+> sepCloseS
+> leaveNodeTokenByName node.Range RBRACE

let multilineExpression =
ifAlignBrackets
Expand Down

0 comments on commit 7412771

Please sign in to comment.