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

Fix nested values inside anonymous records and types not displaying properly in stroustrup style #2687

Merged
merged 8 commits into from
Jan 6, 2023
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## Unreleased

### Fixes
* Stroustrup: Two lists given directly as parameters, break code [#2681](https://github.com/fsprojects/fantomas/issues/2681)
* Stroustrup: Two lists given directly as parameters, break code [#2681](https://github.com/fsprojects/fantomas/issues/2681)
* fsharp_experimental_stroustrup_style=true breaks on types with nested anonymous records. [#2413](https://github.com/fsprojects/fantomas/issues/2413)
* Stroustrup style breaks on nested records. [#2587](https://github.com/fsprojects/fantomas/issues/2587)

## [5.2.0-beta-001] - 2023-01-02

Expand Down
194 changes: 194 additions & 0 deletions src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,197 @@ let fooDto =
|> Option.toObj
|}
"""

[<Test>]
let ``let binding with nested anonymous records, 2413`` () =
formatSourceString
false
"""
let foo =
{| Data =
{| Name = "Isaac"
Age = 43
Day = "Monday"
Colour = "Blue" |} |}
"""
config
|> prepend newline
|> should
equal
"""
let foo = {|
Data = {|
Name = "Isaac"
Age = 43
Day = "Monday"
Colour = "Blue"
|}
|}
"""

[<Test>]
let ``list expression inside anonymous record, 2413`` () =
formatSourceString
false
"""
let foo = {|
Data =
{|
Name = "Isaac"
Age = 43
Day = "Monday"
Colours =
[
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
]
|}
|}
"""
config
|> prepend newline
|> should
equal
"""
let foo = {|
Data = {|
Name = "Isaac"
Age = 43
Day = "Monday"
Colours = [
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
]
|}
|}
"""

[<Test>]
let ``list expression inside regular record, 2413`` () =
formatSourceString
false
"""
let foo = {
Data =
{
Name = "Isaac"
Age = 43
Day = "Monday"
Colours =
[
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
]
}
}
"""
config
|> prepend newline
|> should
equal
"""
let foo = {
Data = {
Name = "Isaac"
Age = 43
Day = "Monday"
Colours = [
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
"Red"
"Blue"
"White"
"Orange"
]
}
}
"""

[<Test>]
let ``nested records, 2587`` () =
formatSourceString
false
"""
let myRecord = {
Property1 = {
Value1 = 20
Value2 = 30
Value3 = 40
}
Property2 = {
Value1 = 20
Value2 = 30
Value3 = 40
}
}
"""
config
|> prepend newline
|> should
equal
"""
let myRecord = {
Property1 = {
Value1 = 20
Value2 = 30
Value3 = 40
}
Property2 = {
Value1 = 20
Value2 = 30
Value3 = 40
}
}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,32 @@ module OutdentingProblem =

let withSetting2 value configuration = { configuration with Setting2 = value }
"""

[<Test>]
let ``nested anonymous record in type definition, 2413`` () =
formatSourceString
false
"""
type MangaDexAtHomeResponse = {
baseUrl: string
chapter: {|
hash: string
data: string[]
otherThing: int
|}
}
"""
config
|> prepend newline
|> should
equal
"""
type MangaDexAtHomeResponse = {
baseUrl: string
chapter: {|
hash: string
data: string[]
otherThing: int
|}
}
"""
64 changes: 28 additions & 36 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module internal rec Fantomas.Core.CodePrinter
module internal rec Fantomas.Core.CodePrinter

open System
open Fantomas.Core.Context
Expand Down Expand Up @@ -436,7 +436,7 @@ let genExpr (e: Expr) =
genIdentListNode node.FieldName
+> sepSpace
+> genSingleTextNode node.Equals
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr node.Expr)
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup genExpr node.Expr
|> genNode node

let fieldsExpr = col sepNln node.Fields genRecordFieldName
Expand Down Expand Up @@ -551,7 +551,7 @@ let genExpr (e: Expr) =
genSingleTextNode node.Ident
+> sepSpace
+> genSingleTextNode node.Equals
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr node.Expr)
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup genExpr node.Expr
|> genNode node

let smallExpression =
Expand Down Expand Up @@ -609,19 +609,19 @@ let genExpr (e: Expr) =
onlyIf node.IsStruct !- "struct " +> recordExpr

let genMultilineAnonRecordAlignBrackets =
let copyExpr fieldsExpr e =
atCurrentColumnIndent (genExpr e)
+> (!- " with"
+> indent
+> whenShortIndent indent
+> sepNln
+> fieldsExpr
+> whenShortIndent unindent
+> unindent)

let genAnonRecord =
match node.CopyInfo with
| Some ci ->
let copyExpr fieldsExpr e =
atCurrentColumnIndent (genExpr e)
+> (!- " with"
+> indent
+> whenShortIndent indent
+> sepNln
+> fieldsExpr
+> whenShortIndent unindent
+> unindent)

genSingleTextNodeSuffixDelimiter node.OpeningBrace
+> sepNlnWhenWriteBeforeNewlineNotEmpty // comment after curly brace
+> copyExpr fieldsExpr ci
Expand Down Expand Up @@ -3036,8 +3036,8 @@ let genType (t: Type) =
| None -> sepOpenAnonRecdFixed +> addSpaceIfSpaceAroundDelimiter
| Some n -> genSingleTextNode n +> addSpaceIfSpaceAroundDelimiter

let genAnonRecordFieldType (i, t) =
genSingleTextNode i
let genAnonRecordFieldType (identifier, t) =
genSingleTextNode identifier
+> sepColon
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genType t)

Expand All @@ -3049,27 +3049,19 @@ let genType (t: Type) =
+> genSingleTextNode node.Closing

let longExpression =
let genFields = col sepNln node.Fields genAnonRecordFieldType

let genMultilineAnonRecordTypeAlignBrackets =
let genRecord =
sepOpenAnonRecdFixed
+> indentSepNlnUnindent (atCurrentColumnIndent genFields)
+> sepNln
+> genSingleTextNode node.Closing

genStruct +> genRecord

let genMultilineAnonRecordType =
let genRecord =
genOpening
+> atCurrentColumn genFields
+> addSpaceIfSpaceAroundDelimiter
+> genSingleTextNode node.Closing

genStruct +> genRecord
let genAnonRecordFields = col sepNln node.Fields genAnonRecordFieldType

ifAlignOrStroustrupBrackets genMultilineAnonRecordTypeAlignBrackets genMultilineAnonRecordType
ifAlignOrStroustrupBrackets
(genStruct
+> sepOpenAnonRecdFixed
+> indentSepNlnUnindent (atCurrentColumnIndent genAnonRecordFields)
+> sepNln
+> genSingleTextNode node.Closing)
(genStruct
+> genOpening
+> atCurrentColumn genAnonRecordFields
+> addSpaceIfSpaceAroundDelimiter
+> genSingleTextNode node.Closing)

fun (ctx: Context) ->
let size = getRecordSize ctx node.Fields
Expand Down Expand Up @@ -3462,7 +3454,7 @@ let genField (node: FieldNode) =
| Some name ->
genSingleTextNode name
+> sepColon
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genType node.Type))
+> autoIndentAndNlnTypeUnlessStroustrup genType node.Type)
|> genNode node

let genUnionCase (hasVerticalBar: bool) (node: UnionCaseNode) =
Expand Down
12 changes: 12 additions & 0 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,18 @@ let autoIndentAndNlnExpressUnlessStroustrup (f: Expr -> Context -> Context) (e:
else
indentSepNlnUnindent (f e) ctx

let autoIndentAndNlnTypeUnlessStroustrup (f: Type -> Context -> Context) (t: Type) (ctx: Context) =
let shouldUseStroustrup =
ctx.Config.ExperimentalStroustrupStyle
&& t.IsStroustrupStyleType
&& let node = Type.Node t in
Seq.isEmpty node.ContentBefore

if shouldUseStroustrup then
f t ctx
else
autoIndentAndNlnIfExpressionExceedsPageWidth (f t) ctx

let autoIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup
(f: Expr -> Context -> Context)
(e: Expr)
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Core/Context.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ val sepSpaceUnlessWriteBeforeNewlineNotEmpty: ctx: Context -> Context
val autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty: f: (Context -> Context) -> ctx: Context -> Context
val addParenIfAutoNln: expr: Expr -> f: (Expr -> Context -> Context) -> (Context -> Context)
val autoIndentAndNlnExpressUnlessStroustrup: f: (Expr -> Context -> Context) -> e: Expr -> ctx: Context -> Context
val autoIndentAndNlnTypeUnlessStroustrup: f: (Type -> Context -> Context) -> t: Type -> ctx: Context -> Context

val autoIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup:
f: (Expr -> Context -> Context) -> e: Expr -> ctx: Context -> Context
Expand Down
5 changes: 5 additions & 0 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ type Type =
| Or n -> n
| LongIdentApp n -> n

member e.IsStroustrupStyleType: bool =
match e with
| AnonRecord _ -> true
| _ -> false

/// A pattern composed from a left hand-side pattern, a single text token/operator and a right hand-side pattern.
type PatLeftMiddleRight(lhs: Pattern, middle: Choice<SingleTextNode, string>, rhs: Pattern, range) =
inherit NodeBase(range)
Expand Down